Go Down

Topic: Simple Audio Player: error opening test.wav (Read 2342 times) previous topic - next topic

meaou

Jun 16, 2020, 03:43 am Last Edit: Jun 16, 2020, 03:44 am by meaou
I have an Arduino MKR Zero set up with an SD card in it and headphones connected to ground and DAC0 for a test (I'm making the amplifier circuit for an 8ohm speaker as a permanent solution).

I also have a "test.wav" file in the root of the SD card, which is an 8-bit mono 82000hz recording; the file is under 1mb. When I upload the code and open the Serial Monitor, the card initializes correctly, but I get "error opening test.wav" every time.

 
I have tried exporting the file from Audacity many times, renaming it, playing with the size. The current file has been created in Audacity and exported with the attached settings through File - Export - Other Audio.


I'm also perplexed on why the sample suggests AudioZero.begin(2*44100) instead of AudioZero.begin(88200), but it doesn't work in any combination I've tried.

I would really appreciate any suggestions on what to try as the next step, as I've run out of ideas on how to troubleshoot this further.

Here is my code:

Code: [Select]
// include the SD library:
#include <SPI.h>
#include <SD.h>
#include <AudioZero.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
// MKRZero SD: SDCARD_SS_PIN or 28
const int chipSelect = 28;
int track;

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
  }


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // 44100kHz stereo => 88200 sample rate
  AudioZero.begin(2*44100);
}

void loop()
{
  int count = 0;

  // open wave file from sdcard
  File myFile = SD.open("test.wav");
  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening test.wav");
    while (true);
  }

  Serial.print("Playing");
 
  // until the file is not finished 
  AudioZero.play(myFile);

  Serial.println("End of file. Thank you for listening!");
  while (true) ;
}

meaou

I double checked that the card is formatted as FAT-32 and that Arduino "sees" the file using this - https://www.arduino.cc/en/Tutorial/CardInfo. No luck getting it to work yet, though.

pert

Please put the .wav in a .zip file and then attach it to a reply here. I'll try it out on my MKR Zero to see whether I have the same problem. If you click the "Reply" button, you'll see an "Attachments and other options" link that will allow you to make the attachment.

I'm also perplexed on why the sample suggests AudioZero.begin(2*44100) instead of AudioZero.begin(88200)
The math is done during compilation, so the two are functionally equivalent. However, 2*44100 is more clear to the human reading the code because there are two channels, each with a 44100 kHz sample rate.

meaou

Thank you so much for getting back to me! Attached the test file.

The math is done during compilation, so the two are functionally equivalent. However, 2*44100 is more clear to the human reading the code because there are two channels, each with a 44100 kHz sample rate.
That makes sense now, thank you!

pert

Code: [Select]
  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

What is the reason for using this code in your sketch?

I get the same error as you when I test your sketch, but if I replace that code with the standard code from the AudioZero library's example:
Code: [Select]
  if (!SD.begin(chipSelect)) {
    Serial.println(" failed!");
    while(true);
  }
  Serial.println(" done.");

then your audio file is played, just as expected.

meaou

Thank you so much! I just tested your snippet instead and the file played perfectly well with headphones attached to ground and DAC0!


I had an initial problem with the card not being initialized and serial monitor not displaying anything (sample code from Simple Audio Player), so I altered it a bit, and then saw an old post with a similar issue where this method worked.

My bad, and a useful lesson to always start with the standard code from the libraries' examples.

Thank you again!

pert

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

Go Up