earlephilhower / ESP8266Audio

Arduino library to play MOD, WAV, FLAC, MIDI, RTTTL, MP3, and AAC files on I2S DACs or with a software emulated delta-sigma DAC on the ESP8266 and ESP32
GNU General Public License v3.0
2.01k stars 432 forks source link

MP3 not playing from SD #570

Closed PaoloRondot closed 1 year ago

PaoloRondot commented 1 year ago

Dear ESP8266Audio community,

I am trying to play MP3 sounds from an SD card and outputing it to an I2S DAC UDA1334. I was able to play from SPIFFS but as I need to store more sounds, I want to switch to SD.

When I run my program, I get nothing. No sound and it justs keeps looping through my titles without playing them.

Here is my code, which comes from the example with little modifications:

#include <Arduino.h>
#ifdef ARDUINO_ARCH_RP2040
void setup() {}
void loop() {}
#else
#include "AudioFileSourceSD.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"

// You may need a fast SD card. Set this as high as it will work (40MHz max).
#define SPI_SPEED SD_SCK_MHZ(4)
#define CS_PIN D8

// On ESP32 you can adjust the SPDIF_OUT_PIN (GPIO number). 
// On ESP8266 it is fixed to GPIO3/RX0 and this setting has no effect

File dir;
AudioFileSourceSD *source = NULL;
AudioOutputI2S *output = NULL;
AudioGeneratorMP3 *decoder = NULL;
bool first = true;

void setup() {
  Serial.begin(115200);
  Serial.println();
  delay(1000);

  audioLogger = &Serial;  
  source = new AudioFileSourceSD();
  output = new AudioOutputI2S();
  decoder = new AudioGeneratorMP3();

  // NOTE: SD.begin(...) should be called AFTER AudioOutputSPDIF() 
  //       to takover the the SPI pins if they share some with I2S
  //       (i.e. D8 on Wemos D1 mini is both I2S BCK and SPI SS)
  if (!SD.begin(CS_PIN, SPI_SPEED)) {
    Serial.println("Probleme carte SD");
    return;
  }
  Serial.println("SD initialisee.");
  dir = SD.open("/"); 
}

void loop() {
  if ((decoder) && (decoder->isRunning())) {
    if (!decoder->loop()) decoder->stop();
  } else {
    File file = dir.openNextFile();
    if (file) { 
      if (!first) {
        source->close();
        if (source->open(file.name())) { 
          Serial.printf_P(PSTR("Playing '%s' from SD card...\n"), file.name());
          decoder->begin(source, output);
        } else {
          Serial.printf_P(PSTR("Error opening '%s'\n"), file.name());
        }
      }else first = false;
    } else {
      Serial.println(F("Playback from SD card done\n"));
      delay(1000);
    }       
  }
}
#endif

And I get the following from the serial monitor:

SD initialisee.
'System Volume Information' from SD card...
'System Volume Information' from SD card...
'3_histoire_32k' from SD card...
'3_histoire_32k' from SD card...
Playing '3_histoire_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'5_lescreations_32k' from SD card...
'5_lescreations_32k' from SD card...
Playing '5_lescreations_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'7_notrecoeurfaitboum_32k' from SD card...
'7_notrecoeurfaitboum_32k' from SD card...
Playing '7_notrecoeurfaitboum_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'9_durable_32k' from SD card...
'9_durable_32k' from SD card...
Playing '9_durable_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'11_innovant_32k' from SD card...
'11_innovant_32k' from SD card...
Playing '11_innovant_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'13_boum_32k' from SD card...
'13_boum_32k' from SD card...
Playing '13_boum_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'15_leopard_32k' from SD card...
'15_leopard_32k' from SD card...
Playing '15_leopard_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'17_sens_32k' from SD card...
'17_sens_32k' from SD card...
Playing '17_sens_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'19_sens_32k' from SD card...
'19_sens_32k' from SD card...
Playing '19_sens_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'21_odorat_32k' from SD card...
'21_odorat_32k' from SD card...
Playing '21_odorat_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'23_legout_32k' from SD card...
'23_legout_32k' from SD card...
Playing '23_legout_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'1_histoire_32k' from SD card...
'1_histoire_32k' from SD card...
Playing '1_histoire_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'25_presentation_32k' from SD card...
'25_presentation_32k' from SD card...
Playing '25_presentation_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'27_lesclientes_32k' from SD card...
'27_lesclientes_32k' from SD card...
Playing '27_lesclientes_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'29_vousconnaissiez_32k' from SD card...
'29_vousconnaissiez_32k' from SD card...
Playing '29_vousconnaissiez_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'31_insta_32k' from SD card...
'31_insta_32k' from SD card...
Playing '31_insta_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'33_Cesar_32k' from SD card...
'33_Cesar_32k' from SD card...
Playing '33_Cesar_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2
'37_Cesar_32k' from SD card...
'37_Cesar_32k' from SD card...
Playing '37_Cesar_32k' from SD card...
MP3:desync
MP3:ERROR_BUFLEN 0
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 1
MP3:desync
MP3:desync
MP3:ERROR_BUFLEN 2

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (28):
epc1=0x4021e6d1 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffb30 end: 3fffffc0 offset: 0190
3ffffcc0:  40265cbf 00000000 3ffffd20 4021c490  
3ffffcd0:  00000000 00000000 3ffffd30 4021c451  
3ffffce0:  00000001 00000000 3ffffdd0 3ffe83c4  
3ffffcf0:  00000001 3ffe83c4 402211d0 40221294  
3ffffd00:  00000000 40265c6c 00000001 00000000  
3ffffd10:  00000000 00000000 401003c5 40265c6f  
3ffffd20:  40265c6e 00000000 3ffffde0 402216e5  
3ffffd30:  00000000 ffffffff 00000000 00000000  
3ffffd40:  0000012c 00000001 3f302073 4020c8d3  
3ffffd50:  0000012c 00000001 0000000f 40101050  
3ffffd60:  0000000c 00000000 3ffef8dc 40100450  
3ffffd70:  00fef6b4 00000204 3ffef8dc 4020c90b  
3ffffd80:  00000000 00000200 00000000 4020c0ac  
3ffffd90:  3fffff30 3fffff00 0000000c 4020c297  
3ffffda0:  3fffff30 3fffff00 00000008 3ffe83c4  
3ffffdb0:  3ffef6b4 00000000 3ffef8dc 4020c759  
3ffffdc0:  3ffffef0 3ffe83c4 00000040 3ffee844  
3ffffdd0:  3fffff00 3ffe83c4 00000040 4021e605  
3ffffde0:  3ffffeb1 00000001 0000003e ffff0208  
3ffffdf0:  3ffffeb0 0000003f 0000000c 00000001  
3ffffe00:  00000000 00000000 4bc6a7f0 00000000  
3ffffe10:  3ffffe94 00000000 3ffefc34 40209f65  
3ffffe20:  00000000 00000000 00000001 401002b0  
3ffffe30:  7fffffff 3ffefc34 00000000 4020a279  
3ffffe40:  00000600 00000001 00000000 00000001  
3ffffe50:  ffffff1b 00000008 3ffee844 4021e641  
3ffffe60:  3fffff30 3fffff00 00000008 00000000  
3ffffe70:  3ffefc6c 00000000 00000000 40208ccd  
3ffffe80:  40219178 00000000 000003e8 4021e6c4  
3ffffe90:  00000000 65000000 66666f20 3ffee990  
3ffffea0:  00000000 00000020 3ffefe2c 4020da06  
3ffffeb0:  00000127 3ffefb04 3ffefe2c 3ffee990  
3ffffec0:  3fffdad0 3ffefd28 3fffff50 4020d501  
3ffffed0:  40219380 00000000 000003e8 402087d0  
3ffffee0:  00000000 00000000 00000000 00000000  
3ffffef0:  3fffff30 3fffff00 00000008 00000000  
3fffff00:  00000000 00000001 00000000 00000000  
3fffff10:  00000000 00000001 3ffee75c 4020d5f2  
3fffff20:  00000000 40265c6c 3fffff30 402078a0 <
3fffff30:  00000000 3ffee750 3ffefaf4 3ffee990  
3fffff40:  3fffdad0 3ffee750 3ffee844 4020761f  
3fffff50:  40219380 00000000 000003e8 402087d0  
3fffff60:  00000000 00000000 00000000 00000000  
3fffff70:  00000000 00000000 3ffee7ac 40208ba0  
3fffff80:  00000000 00000000 00000001 401002b0  
3fffff90:  3fffdad0 00000000 3ffee97c 3ffee990  
3fffffa0:  3fffdad0 00000000 3ffee97c 4020ed68  
3fffffb0:  feefeffe feefeffe 3ffe864c 40100dcd  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v0006e060
~ld

I browsed through the different issue, but didn't find any solution. Any idea?

Cheers, Dorton.

PaoloRondot commented 1 year ago

It does the same until 20Mhz #define SPI_SPEED SD_SCK_MHZ(20), but above, the SD card just doesn't work: SD.begin(CS_PIN, SPI_SPEED) = false

wimmatthijs commented 1 year ago

@PaoloRondot I'm a bit late here, i'm doing an ESP8266 audio project myself now.

For me it helped to change the ChipSelect Pin of the SD card to D0 because i2S is already using D8

PaoloRondot commented 1 year ago

That's what I did in the end and it works now. Thanks!

CobaltEcho commented 1 year ago

I'm trying to use this example, but I keep having an issue compiling with the SPI_SPEED variable.

The example you shows calls out if (!SD.begin(CS_PIN, SPI_SPEED)) { but the Ardiono reference says SD.begin(cspin), and there is no other varable. (Ref: https://www.arduino.cc/reference/en/libraries/sd/begin/)

Also, is that #endif really suppose to be there at the end?

Thanks again for posting this, it's getting me a lot closer to doing what I need to do!

PaoloRondot commented 1 year ago

Hi @CobaltEcho, The #endif should definitely be after the last include, not where it is right now. As for the SD.begin, note that I'm not using the Arduino SD class. Actually, you don't need to add any other imports that the ones I have. This is because SD is already included in AudioFileSourceSD.h. (If you are using VS Code and PlatformIO, you can ctrl+left click to navigate in function declarations and source code. I'ld advise you look into the ESP8266Audio files.) But if you are able to compile without the SPI_SPEED and it works, I'ld just go for this.

CobaltEcho commented 1 year ago

Thanks for the response @PaoloRondot ! I moved my project over to VSCode/PlatformIO because I know it can sometimes do things Arduino cant. I included the #endif, but not sure I have it in the right spot as its throwing errors?

void loop() {
  if ((decoder) && (decoder->isRunning())) {
    if (!decoder->loop()) decoder->stop();
  } else {
    File file = dir.openNextFile();
    if (file) { 
      if (!first) {
        source->close();
        if (source->open(file.name())) { 
          Serial.printf_P(PSTR("Playing '%s' from SD card...\n"), file.name());
          decoder->begin(source, output);
        } else {
          Serial.printf_P(PSTR("Error opening '%s'\n"), file.name());
        }
      }else first = false;
    } else {
      Serial.println(F("Playback from SD card done\n"));
      delay(1000);
    }       
  } #endif;
}

I see a lot of the codes have the #define SPI_SPEED SD_SCK_MHZ(4), but cant figure out why im getting error: 'SD_SCK_MHZ' was not declared in this scope.

PaoloRondot commented 1 year ago

Well actually I don't think you need this #endif and #ifdef at all:

#include <Arduino.h>
#include "AudioFileSourceSD.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"

// You may need a fast SD card. Set this as high as it will work (40MHz max).
#define SPI_SPEED SD_SCK_MHZ(4)
#define CS_PIN D0

// On ESP32 you can adjust the SPDIF_OUT_PIN (GPIO number). 
// On ESP8266 it is fixed to GPIO3/RX0 and this setting has no effect

File dir;
AudioFileSourceSD *source = NULL;
AudioOutputI2S *output = NULL;
AudioGeneratorMP3 *decoder = NULL;
bool first = true;

void setup() {
  Serial.begin(115200);
  Serial.println();
  delay(1000);

  audioLogger = &Serial;  
  source = new AudioFileSourceSD();
  output = new AudioOutputI2S();
  decoder = new AudioGeneratorMP3();

  // NOTE: SD.begin(...) should be called AFTER AudioOutputSPDIF() 
  //       to takover the the SPI pins if they share some with I2S
  //       (i.e. D8 on Wemos D1 mini is both I2S BCK and SPI SS)
  if (!SD.begin(CS_PIN, SPI_SPEED)) {
    Serial.println("Probleme carte SD");
    return;
  }
  Serial.println("SD initialisee.");
  dir = SD.open("/"); 
}

void loop() {
  if ((decoder) && (decoder->isRunning())) {
    if (!decoder->loop()) decoder->stop();
  } else {
    File file = dir.openNextFile();
    if (file) { 
      if (!first) {
        source->close();
        if (source->open(file.name())) { 
          Serial.printf_P(PSTR("Playing '%s' from SD card...\n"), file.name());
          decoder->begin(source, output);
        } else {
          Serial.printf_P(PSTR("Error opening '%s'\n"), file.name());
        }
      }else first = false;
    } else {
      Serial.println(F("Playback from SD card done\n"));
      delay(1000);
    }       
  }
}

for the #define SPI_SPEED SD_SCK_MHZ(4): when you create your project on platformIO you can choose the board. What I did is I chose NodeMCU 1.0 (ESP-12E Module) and framework: Arduino. Let me know if this helps