WavePlayback.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. This reads a wave file from an SD card and plays it using the I2S interface to
  3. a MAX08357 I2S Amp Breakout board.
  4. Circuit:
  5. * Arduino/Genuino Zero, MKRZero or MKR1000 board
  6. * SD breakout or shield connected
  7. * MAX08357:
  8. * GND connected GND
  9. * VIN connected 5V
  10. * LRC connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
  11. * BCLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
  12. * DIN connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
  13. created 15 November 2016
  14. by Sandeep Mistry
  15. */
  16. #include <SD.h>
  17. #include <ArduinoSound.h>
  18. // filename of wave file to play
  19. const char filename[] = "MUSIC.WAV";
  20. // variable representing the Wave File
  21. SDWaveFile waveFile;
  22. void setup() {
  23. // Open serial communications and wait for port to open:
  24. Serial.begin(9600);
  25. while (!Serial) {
  26. ; // wait for serial port to connect. Needed for native USB port only
  27. }
  28. // setup the SD card, depending on your shield of breakout board
  29. // you may need to pass a pin number in begin for SS
  30. Serial.print("Initializing SD card...");
  31. if (!SD.begin()) {
  32. Serial.println("initialization failed!");
  33. return;
  34. }
  35. Serial.println("initialization done.");
  36. // create a SDWaveFile
  37. waveFile = SDWaveFile(filename);
  38. // check if the WaveFile is valid
  39. if (!waveFile) {
  40. Serial.println("wave file is invalid!");
  41. while (1); // do nothing
  42. }
  43. // print out some info. about the wave file
  44. Serial.print("Bits per sample = ");
  45. Serial.println(waveFile.bitsPerSample());
  46. long channels = waveFile.channels();
  47. Serial.print("Channels = ");
  48. Serial.println(channels);
  49. long sampleRate = waveFile.sampleRate();
  50. Serial.print("Sample rate = ");
  51. Serial.print(sampleRate);
  52. Serial.println(" Hz");
  53. long duration = waveFile.duration();
  54. Serial.print("Duration = ");
  55. Serial.print(duration);
  56. Serial.println(" seconds");
  57. // adjust the playback volume
  58. AudioOutI2S.volume(5);
  59. // check if the I2S output can play the wave file
  60. if (!AudioOutI2S.canPlay(waveFile)) {
  61. Serial.println("unable to play wave file using I2S!");
  62. while (1); // do nothing
  63. }
  64. // start playback
  65. Serial.println("starting playback");
  66. AudioOutI2S.play(waveFile);
  67. }
  68. void loop() {
  69. // check if playback is still going on
  70. if (!AudioOutI2S.isPlaying()) {
  71. // playback has stopped
  72. Serial.println("playback stopped");
  73. while (1); // do nothing
  74. }
  75. }