Hello,
how can i play a wave file from a other directory then the root directory? In example the file sound.wav is in the directory dir1.
-root dir
> dir1/sound.wav
Thx for help
Andy
Play wave from a directory
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- fat16lib
- Posts: 595
- Joined: Wed Dec 24, 2008 1:54 pm
Re: Play wave from a directory
This should play /DIR1/SOUND.WAV. It shows how to use a sub directory.
Code: Select all
#include "WaveHC.h"
#include "WaveUtil.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the volumes root directory
FatReader dir; // directory file
FatReader file; // This object represent the WAV file for a pi digit or period
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))
//////////////////////////////////// SETUP
void setup() {
// set up Serial library at 9600 bps
Serial.begin(9600);
if (!card.init()) {
error("Card init. failed!");
}
if (!vol.init(card)) {
error("No partition!");
}
if (!root.openRoot(vol)) {
error("Couldn't open root");
}
if (!dir.open(root, "DIR1")) {
error("DIR1 failed");
}
if (!file.open(dir, "SOUND.WAV")) {
error("SOUND.WAV failed");
}
if (!wave.create(file)) {
PgmPrintln("Not a valid WAV");
return;
}
// ok time to play!
wave.play();
}
/////////////////////////////////// LOOP
void loop() { }
/////////////////////////////////// HELPERS
/*
* print error message and halt
*/
void error_P(const char *str)
{
PgmPrint("Error: ");
SerialPrint_P(str);
sdErrorCheck();
while(1);
}
/*
* print error message and halt if SD I/O error
*/
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: ");
Serial.print(card.errorCode(), HEX);
PgmPrint(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
-
- Posts: 5
- Joined: Fri Jul 15, 2011 9:04 am
Re: Play wave from a directory
Thanks a lot, i tried it and it works fine.
Andy
Andy
-
- Posts: 5
- Joined: Fri Jul 15, 2011 9:04 am
Re: Play wave from a directory
Sorry, but now i have the next problem.
How can i switch with a button thru the directorys on my SD-Card.
I have 9 Directorys, from DIR1 to DIR9, in void setup Directory DIR1 ist preselected. If i press Button once, the next directory is selected. A Wave file give me response in which Directory i am, like "you select Dir1". Another six Button plays the files in the selected directory. In every Directory are the sama files called file1.wav to file6.wav.
Regards
Andy
How can i switch with a button thru the directorys on my SD-Card.
I have 9 Directorys, from DIR1 to DIR9, in void setup Directory DIR1 ist preselected. If i press Button once, the next directory is selected. A Wave file give me response in which Directory i am, like "you select Dir1". Another six Button plays the files in the selected directory. In every Directory are the sama files called file1.wav to file6.wav.
Regards
Andy
- fat16lib
- Posts: 595
- Joined: Wed Dec 24, 2008 1:54 pm
Re: Play wave from a directory
Look at the function nextDir() in this program. You would call nextDir() in setup and each time the next directory button is pushed.
This program will play SOUND.WAV in each directory, DIR1 - DIR6. It calls nextDir() in setup and at the end of loop().
This program will play SOUND.WAV in each directory, DIR1 - DIR6. It calls nextDir() in setup and at the end of loop().
Code: Select all
#include "WaveHC.h"
#include "WaveUtil.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the volumes root directory
FatReader dir; // directory file
FatReader file; // This object represent the WAV file for a pi digit or period
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
/*
* Define macro to put error messages in flash memory
*/
#define error(msg) error_P(PSTR(msg))
//------------------------------------------------------------------------------
// open next directory first call will open DIR1
char dirFileName[] = "DIR6";
//------------------------------------------------------------------------------
void nextDir() {
if (dirFileName[3] < '6') {
dirFileName[3]++;
} else {
dirFileName[3] = '1';
}
dir.close();
Serial.print("opening: ");
Serial.println(dirFileName);
if (!dir.open(root, dirFileName)) {
error("open dir failed");
}
}
//------------------------------------------------------------------------------
void setup() {
// set up Serial library at 9600 bps
Serial.begin(9600);
if (!card.init()) {
error("Card init. failed!");
}
if (!vol.init(card)) {
error("No partition!");
}
if (!root.openRoot(vol)) {
error("Couldn't open root");
}
// first time opens DIR1
nextDir();
}
//------------------------------------------------------------------------------
void loop() {
if (!file.open(dir, "SOUND.WAV")) {
error("SOUND.WAV failed");
}
if (!wave.create(file)) {
PgmPrintln("Not a valid WAV");
return;
}
// ok time to play!
wave.play();
while(wave.isplaying);
// open next directory
nextDir();
}
/////////////////////////////////// HELPERS
/*
* print error message and halt
*/
void error_P(const char *str)
{
PgmPrint("Error: ");
SerialPrint_P(str);
sdErrorCheck();
while(1);
}
/*
* print error message and halt if SD I/O error
*/
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: ");
Serial.print(card.errorCode(), HEX);
PgmPrint(", ");
Serial.println(card.errorData(), HEX);
while(1);
}
-
- Posts: 5
- Joined: Fri Jul 15, 2011 9:04 am
Re: Play wave from a directory
Great, thanks a lot fat16lib.
i am just a beginner in arduino programming. I came from flash programming.
Where can i get the reference about the Commands that we used in this sketch and the ojects (SdReader, FatVolume and Fat Reader).
Are the command "!dir.open" from lib FatReader? Can you tell me what the "!" means in this Command?
Many thanks for your help.
Andy
i am just a beginner in arduino programming. I came from flash programming.
Where can i get the reference about the Commands that we used in this sketch and the ojects (SdReader, FatVolume and Fat Reader).
Are the command "!dir.open" from lib FatReader? Can you tell me what the "!" means in this Command?
Many thanks for your help.
Andy
- fat16lib
- Posts: 595
- Joined: Wed Dec 24, 2008 1:54 pm
Re: Play wave from a directory
In the C++ language, ! means not. You need to learn more about C++ from tutorials like these:
http://www.cplusplus.com/doc/tutorial/
http://www.cprogramming.com/tutorial.html
http://www.learncpp.com/
Documentation for the WaveHC library is in the html directory. Open html/index.html in a browser.
Explore the classes and files tabs.
http://www.cplusplus.com/doc/tutorial/
http://www.cprogramming.com/tutorial.html
http://www.learncpp.com/
Documentation for the WaveHC library is in the html directory. Open html/index.html in a browser.
Explore the classes and files tabs.
-
- Posts: 5
- Joined: Fri Jul 15, 2011 9:04 am
Re: Play wave from a directory
Ok, i will try to learn more about C++.
Thanks for your help and links.
Andy
Thanks for your help and links.
Andy
Please be positive and constructive with your questions and comments.