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
1.99k stars 433 forks source link

Error opening #621

Open PMKrol opened 1 year ago

PMKrol commented 1 year ago

Hi!

I'm getting error opening (flac in one case, aac in other), based on examples:

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

// For this sketch, you need connected SD card with '.flac' music files in the root
// directory. Some samples with various sampling rates are available from i.e. 
// Espressif Audio Development Framework at:
// https://docs.espressif.com/projects/esp-adf/en/latest/design-guide/audio-samples.html
//
// On ESP8266 you might need to re-encode FLAC files with max '-2' compression level 
// (i.e. 1152 maximum block size) or you will run out of memory. FLAC files will be 
// slightly bigger but you don't loose audio quality with reencoding (lossles codec).

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

// 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
#define SPDIF_OUT_PIN 27

File dir;
AudioFileSourceSD *source = NULL;
AudioOutputI2S *output = NULL;
AudioGeneratorFLAC *decoder = NULL;

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

  audioLogger = &Serial;  
  source = new AudioFileSourceSD();

  output = new AudioOutputI2S();
  output->SetPinout(17, 22, 16);

  decoder = new AudioGeneratorFLAC();

  // 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 defined(ESP8266)
    SD.begin(SS, SPI_SPEED);
  #else
    SD.begin();
  #endif
  dir = SD.open("/"); 
}

void loop() {
  if ((decoder) && (decoder->isRunning())) {
    if (!decoder->loop()) decoder->stop();
  } else {
    File file = dir.openNextFile();
    if (file) {      
      if (String(file.name()).endsWith(".flac")) {
        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 {
      Serial.println(F("Playback from SD card done\n"));
      delay(1000);
    }       
  }
}
#endif
#include "AudioFileSourceSD.h"
#include "AudioGeneratorAAC.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(40)

AudioGeneratorAAC *aac = NULL;
AudioOutputI2S *out = NULL;
AudioFileSourceSD *source = NULL;

File dir;

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
  Serial.println();
  delay(1000);

  audioLogger = &Serial;  
  source = new AudioFileSourceSD();
  out = new AudioOutputI2S();
  aac = new AudioGeneratorAAC();

  //out->SetGain(0.4); 

  //bool SetPinout(int bclkPin, int wclkPin, int doutPin);
  //bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin);
  out->SetPinout(17, 22, 16);

  // 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)
  SD.begin();

  dir = SD.open("/"); 
}

void loop() {
  // put your main code here, to run repeatedly:
  if ((aac) && (aac->isRunning())) {
    if (!aac->loop()) aac->stop();
  } else {
    File file = dir.openNextFile();
    if (file) {      
      if (String(file.name()).endsWith(".m4a")) {
        source->close();
        if (source->open(file.name())) { 
          Serial.printf_P(PSTR("Playing '%s' from SD card...\n"), file.name());
          aac->begin(source, out);
        } else {
          //aac->begin(source, out);
          Serial.printf_P(PSTR("Error opening '%s'\n"), file.name());
        }
      } 
    } else {
      Serial.println(F("Playback from SD card done\n"));
      delay(1000);
    }       
  }
}