102 lines
2.5 KiB
C++
102 lines
2.5 KiB
C++
/*
|
|
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";
|
|
|
|
#define cardSelect 4
|
|
|
|
// variable representing the Wave File
|
|
SDWaveFile waveFile;
|
|
|
|
void setup() {
|
|
// Open SerialUSB communications and wait for port to open:
|
|
SerialUSB.begin(9600);
|
|
while (!SerialUSB) {
|
|
; // wait for SerialUSB 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
|
|
SerialUSB.print("Initializing SD card...");
|
|
if (!SD.begin(cardSelect)) { // feather CS pin
|
|
SerialUSB.println("initialization failed!");
|
|
return;
|
|
}
|
|
SerialUSB.println("initialization done.");
|
|
|
|
// create a SDWaveFile
|
|
waveFile = SDWaveFile(filename);
|
|
|
|
// check if the WaveFile is valid
|
|
if (!waveFile) {
|
|
SerialUSB.println("wave file is invalid!");
|
|
while (1); // do nothing
|
|
}
|
|
|
|
// print out some info. about the wave file
|
|
SerialUSB.print("Bits per sample = ");
|
|
SerialUSB.println(waveFile.bitsPerSample());
|
|
|
|
long channels = waveFile.channels();
|
|
SerialUSB.print("Channels = ");
|
|
SerialUSB.println(channels);
|
|
|
|
long sampleRate = waveFile.sampleRate();
|
|
SerialUSB.print("Sample rate = ");
|
|
SerialUSB.print(sampleRate);
|
|
SerialUSB.println(" Hz");
|
|
|
|
long duration = waveFile.duration();
|
|
SerialUSB.print("Duration = ");
|
|
SerialUSB.print(duration);
|
|
SerialUSB.println(" seconds");
|
|
|
|
// adjust the playback volume
|
|
AudioOutI2S.volume(10);
|
|
|
|
// check if the I2S output can play the wave file
|
|
if (!AudioOutI2S.canPlay(waveFile)) {
|
|
SerialUSB.println("unable to play wave file using I2S!");
|
|
while (1); // do nothing
|
|
}
|
|
|
|
// start playback
|
|
SerialUSB.println("starting playback");
|
|
AudioOutI2S.play(waveFile);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
// check if playback is still going on
|
|
if (!AudioOutI2S.isPlaying()) {
|
|
// playback has stopped
|
|
|
|
SerialUSB.println("playback stopped");
|
|
while (1); // do nothing
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|