Go Down

Topic: unable to play wave file using I2S! (Read 175 times) previous topic - next topic

pwhitrow

Sep 04, 2017, 12:01 pm Last Edit: Sep 04, 2017, 12:17 pm by pwhitrow
Trying to use the example wav player sketch to get a sound file to play. Have followed all of the instructions as laid out here: https://www.arduino.cc/en/Guide/ArduinoMKRZero but cannot get the thing to play a wav file! It just tells me "unable to play wave file using I2S!". The wav file has been converted to the correct format using Audacity and it does work on other Arduino/SD card players so I know it's good.

For the sake of my sanity would someone please be able to help?

I'm quite possibly missing something obvious but after nearly two days of searching for a solution I'm close to crushing this board under my boot!



Code: [Select]


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

// filename of wave file to play
const char filename[] = "start.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(SDCARD_SS_PIN)) {
    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
  }
}

sandeepmistry

Hi @pwhitrow,

What bits per sample, sample rate and number of channels does the sketch output for the wave file?

pwhitrow

Audio file is saved as 16 bit PCM, 44100Hz

sandeepmistry

Stereo or mono?

Maybe you can attach the wave file here?

pwhitrow

OK, managed to get some time to get back to this.

As requested the file is attached. The serial monitor shows the output as:

Initializing SD card...initialization done.
Bits per sample = 8
Channels = 1
Sample rate = 88200 Hz
Duration = 0 seconds
unable to play wave file using I2S!
starting playback
playback stopped

Although the duration is actually 2 seconds.

File in archive is called test.wav not start.wav as it was in the original code above, simply because I used a new file to be sure.

Any further help on this would be greatly appreciated.

sandeepmistry

ArduinoSound can currently only play stereo (2 channels) wave files using I2S. Also, I suggest you make your wave file have a lower sample rate (44.1 kHz or lower) as reading from the SD card is not fast enough for 88.2 kHz.

pwhitrow

Hmmm, OK I'll give that a try...

As I said at the top I was following the instructions on the "official" page here : https://www.arduino.cc/en/Tutorial/SimpleAudioPlayerZero

Where it quotes:

Audio File

The Audio file to store on the SD card must be in the .wav format with 88200 Hz, 8-bit unsigned PCM mono quality. This type of file can be easily obtained using audio programs like audacity.


Is this information not correct?

pwhitrow

I think I answered my own question, down to the libraries. :(

pwhitrow

Created a new stereo file (attached) as advised in Audacity, but still no output.

In fact the serial monitor is telling me:

Initializing SD card...initialization done.
Bits per sample = 8
Channels = 1
Sample rate = 88200 Hz
Duration = 0 seconds
unable to play wave file using I2S!
starting playback
playback stopped


Am I missing something? Audacity has never let me down before and It's always resampled/created files in the format/configuration I need, so I can only assume I'm doing something wrong here. Do you have a correct file I can try, one that does work on the Zero? If I can see it working with a file at least I have a glimmer of hope before giving this up as a bad job.

sandeepmistry

The start.wav file your attached is working for me, here's the output of the sketch:

Code: [Select]

Initializing SD card...initialization done.
Bits per sample = 16
Channels = 2
Sample rate = 44100 Hz
Duration = 1 seconds
starting playback
playback stopped


Maybe you have the wrong file name in the sketch? I changed the following line:

Code: [Select]

// filename of wave file to play
const char filename[] = "START.WAV";

Go Up