ESP8266 SinglePinI2SDriver, SPDIF output improvements, FLAC memory optimization (#237)

* Added S/PDIF output using I2S peripheral

* Added AudioOutputSPDIF to README and PROGMEM changes

* SPDIF/I2S sample rate fixes

* Fix SPDIF example file name

* Minimal (single pin) I2S driver

* AudioOutputSPDIF changes for minimal I2S driver

* Documentation changes

* FLAC decoding memory optimization
This commit is contained in:
Ivan Kostoski authored and GitHub committed 2020-02-24 09:55:30 -08:00
1 parent a7a8b917d2
commit 377b271edd
13 files changed
+525 -49

No files matched your search

@@ -0,0 +1,68 @@
#include <Arduino.h>
#include "AudioFileSourceSD.h"
#include "AudioOutputSPDIF.h"
#include "AudioGeneratorFLAC.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 reencode 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(40)
// 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;
AudioOutputSPDIF *output = NULL;
AudioGeneratorFLAC *decoder = NULL;
void setup() {
Serial.begin(115200);
Serial.println();
delay(1000);
audioLogger = &Serial;
source = new AudioFileSourceSD();
output = new AudioOutputSPDIF(SPDIF_OUT_PIN);
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 form SD card done\n"));
delay(1000);
}
}
}
+6 -5
View File
@@ -44,13 +44,14 @@ void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println();
audioLogger = &Serial;
SPIFFS.begin();
file = new AudioFileSourceSPIFFS();
id3 = NULL;
out = new AudioOutputSPDIF();
mp3 = new AudioGeneratorMP3();
const char* fileName = NULL;
String fileName = "";
// Find first MP3 file in SPIFF and play it
@@ -59,7 +60,7 @@ void setup()
while ((dir = root.openNextFile())) {
if (String(dir.name()).endsWith(".mp3")) {
if (file->open(dir.name())) {
fileName = dir.name();
fileName = String(dir.name());
break;
}
}
@@ -70,18 +71,18 @@ void setup()
while (dir.next()) {
if (dir.fileName().endsWith(".mp3")) {
if (file->open(dir.fileName().c_str())) {
fileName = dir.fileName().c_str();
fileName = dir.fileName();
break;
}
}
}
#endif
if (fileName != NULL) {
if (fileName.length() > 0) {
id3 = new AudioFileSourceID3(file);
id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
mp3->begin(id3, out);
Serial.printf("Playback of '%s' begins...\n", fileName);
Serial.printf("Playback of '%s' begins...\n", fileName.c_str());
} else {
Serial.println("Can't find .mp3 file in SPIFFS");
}