Hello
I have downloaded the ArduinoSound.h library and am looking to base a program off the WavePlayer example
The WavePlayer example uses an I2S amp breakout board, but I would think this would be possible without that breakout board using the DAC on the MKRZERO? I can't tell from the ArduinoAudio.h documentation if this is possible.
WavePlayback here:
/*
This reads a wave file from an SD card and plays it using the I2S interface to
a MAX08357 I2S Amp Breakout board.
Circuit:
* Arduino/Genuino Zero, MKRZero or MKR1000 board
* SD breakout or shield connected
* MAX08357:
* GND connected GND
* VIN connected 5V
* LRC connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
* BCLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
* DIN connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
created 15 November 2016
by Sandeep Mistry
*/
#include <SD.h>
#include <ArduinoSound.h>
// filename of wave file to play
const char filename[] = "MUSIC.WAV";
// variable representing the Wave File
SDWaveFile waveFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// setup the SD card, depending on your shield of breakout board
// you may need to pass a pin number in begin for SS
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// create a SDWaveFile
waveFile = SDWaveFile(filename);
// check if the WaveFile is valid
if (!waveFile) {
Serial.println("wave file is invalid!");
while (1); // do nothing
}
// print out some info. about the wave file
Serial.print("Bits per sample = ");
Serial.println(waveFile.bitsPerSample());
long channels = waveFile.channels();
Serial.print("Channels = ");
Serial.println(channels);
long sampleRate = waveFile.sampleRate();
Serial.print("Sample rate = ");
Serial.print(sampleRate);
Serial.println(" Hz");
long duration = waveFile.duration();
Serial.print("Duration = ");
Serial.print(duration);
Serial.println(" seconds");
// adjust the playback volume
AudioOutI2S.volume(5);
// check if the I2S output can play the wave file
if (!AudioOutI2S.canPlay(waveFile)) {
Serial.println("unable to play wave file using I2S!");
while (1); // do nothing
}
// start playback
Serial.println("starting playback");
AudioOutI2S.play(waveFile);
}
void loop() {
// check if playback is still going on
if (!AudioOutI2S.isPlaying()) {
// playback has stopped
Serial.println("playback stopped");
while (1); // do nothing
}
}
The reason I am trying to use the ArduinoSound.h library with MKRZero instead of AudioZero.h is that I can't seem to stop or skip files. I saw other code examples that are able to compare if audio is playing and then either stop playback or do another function, but I am finding that the only way after starting playback of a file with AudioZero.h to either skip or stop playback is to wait for that file to finish playing.
I'll include that code as well:
/*
Simple Audio Player for Arduino Zero
Demonstrates the use of the Audio library for the Arduino Zero
Hardware required :
* Arduino shield with a SD card on CS4
* A sound file named "test.wav" in the root directory of the SD card
* An audio amplifier to connect to the DAC0 and ground
* A speaker to connect to the audio amplifier
Arturo Guadalupi <a.guadalupi@arduino.cc>
Angelo Scialabba <a.scialabba@arduino.cc>
Claudio Indellicati <c.indellicati@arduino.cc>
This example code is in the public domain
https://www.arduino.cc/en/Tutorial/SimpleAudioPlayerZero
*/
#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>
//global variables, define input pins
#define Play 7
#define Stop 8
#define Next 6
#define Previous 9
volatile bool playStatus = 0;
volatile bool stopStatus = 0;
volatile bool nextStatus = 0;
volatile bool prevStatus = 0;
File wav;
File root;
int count = 0;
void setup()
{
// debug output at 115200 baud
Serial.begin(115200);
SD.begin(SS_PIN);
// setup SD-card
Serial.print("Initializing SD card...");
if(!SD.begin(28)) {
Serial.println(" failed!!");
while(true);
}
Serial.println(" done.");
//set input pins, assign interrupts and ISRs
//when buttons go high, play, stop, prev, or next will happen
pinMode(Play, INPUT);
attachInterrupt(digitalPinToInterrupt(Play), playStateRead, HIGH);
pinMode(Stop, INPUT);
attachInterrupt(digitalPinToInterrupt(Stop), stopStateRead, HIGH);
pinMode(Next, INPUT);
pinMode(Previous, INPUT);
}
void loop()
{
// open wave file from sdcard
Serial.print("play status is ");
Serial.println(playStatus);
//File root = SD.open("/");
// 44100kHz stereo => 88200 sample rate
if (playStatus == 1){
File root = SD.open("/");
AudioZero.begin(44100);
if (!root) {
// if the file didn't open, print an error and stop
Serial.println("error opening ");
Serial.print(root.name());
while (true);
}
playWavs(root, count);
}
// Serial.println(count);
//else{
//AudioZero.end();
//}
}
int playWavs(File dir, int place){
while (playStatus == 1){
wav = dir.openNextFile();
if (!wav || stopStatus == 1){
wav.close();
break;
}
if (wav.isDirectory()){
Serial.println(wav.name());
}
else{
Serial.print("Playing ");
Serial.println(wav.name());
AudioZero.play(wav);
}
wav.close();
/*place++;
Serial.print("the place is ");
Serial.println(place);
return place;*/
}
return count;
}
void playStateRead(){
playStatus = 1;
stopStatus = 0;
Serial.println("play button pressed");
return;
}
void stopStateRead(){
stopStatus = 1;
playStatus = 0;
Serial.println("stop button pressed");
return;
}