Playing a wav file from SDcard

Hello everyone,
I have an Arduino MKR zero board and I am tyring to play music from SDcard.
So far, I have achived for some of wav file formats such as 08-48KHz and 16-bit.
But some of formats likes 24-bit or 32-bit bit depth, arduino errored me : wave file is invalid!
What is the problem? Code below:

#include <SPI.h>
#include <SD.h>
#include <ArduinoSound.h>

const int chipSelect = SDCARD_SS_PIN;

SDWaveFile waveFile;

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  
  Serial.begin(9600);
  while(!Serial);

  Serial.println("I2S music playing example...");

  if (!SD.begin(chipSelect)) {
    Serial.println("SDcard not started...");
    
    while(1);
  }

  const char* file_name = "2224LSS.wav"; 

  File finfo = SD.open(file_name);
  if (!finfo) {
    Serial.println("File cannot open...");
    
    while(1);
  }
  finfo.close();
  
  
  waveFile = SDWaveFile(file_name);
  // check if the WaveFile is valid
  if (!waveFile) {
    Serial.println("wave file is invalid!"); //error is here
    while (1); // do nothing
  }
  
  Serial.print("Sample Rate : ");
  Serial.println(waveFile.sampleRate());

  Serial.print("Bit Per Sample : ");
  Serial.println(waveFile.bitsPerSample());

  Serial.print("Channels : ");
  Serial.println(waveFile.channels());

  Serial.print("Duration : ");
  Serial.println(waveFile.duration());

  AudioOutI2S.volume(100.0f);

  Serial.println("Setup completed...");
}



void loop() {
      

   Serial.println("-------------");
   Serial.println("music playing now...");

   while(AudioOutI2S.isPlaying());
   delay(2000);

   
   AudioOutI2S.play(waveFile);


  Serial.println("loop end...");

}

I've never done that and I've never uses a MKR zero...

It's probably a limitation of the ArduinoSound.h library.

So, you'll probably have to find another library or write your own.

WAV files are "simple" but there are many different variations of sample rate, bit depth, and number of channels. All of that information is in the WAV file header.

You have to scale the samples (multiply, divide, or bit-shift) to match the bit-depth of the DAC. With integer WAV files 0dB is the maximum you can "count to" with a given number of bits so you multiply or divide to change the bit depth. With floating point a value of 1.0 represents 0dB so you multiply to get integer data for the DAC. 8-bit WAV files are unsigned.

Since the DAC can't put-out negative voltages you'll have to offset-bias the data before sending it to the DAC.

And of course, you have to write-out the data at the correct sample rate.

If you have a stereo file and a mono DAC the left & right samples should be averaged.

If you need it, the Audacity website has a little introduction to how digital audio works (but nothing about WAV files or programming).

Thank you.
But I have already known wav files.
Only problem is with arduino.
"It's probably a limitation of the ArduinoSound.h library"
This is enough for me...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.