ESP8266Audio
ESP8266Audio copied to clipboard
NodeMCU ESP8266 LoLin using AudioFileSourceHTTPStream & AudioOutputI2S with MAX98357 I2S DAC
Here my Code:
#include <Arduino.h>
#if defined(ARDUINO_ARCH_RP2040)
void setup() {}
void loop() {}
#else
#if defined(ESP32)
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourceHTTPStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2S.h"
// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
// Enter your WiFi setup here:
#ifndef STASSID
#define STASSID "SCMA-3"
#define STAPSK "XXXXXXX"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
// Randomly picked URL
const char *URL="http://192.168.2.245/tahubulat.mp3"; //LOCAL WEBSERVER, JUST LITTLE FILE 128 bit, 7 seconds
AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioFileSourceBuffer *buff;
AudioOutputI2S *out;
// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
(void) isUnicode; // Punt this ball for now
// Note that the type and string may be in PROGMEM, so copy them to RAM for printf
char s1[32], s2[64];
strncpy_P(s1, type, sizeof(s1));
s1[sizeof(s1)-1]=0;
strncpy_P(s2, string, sizeof(s2));
s2[sizeof(s2)-1]=0;
Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
Serial.flush();
}
// Called when there's a warning or error (like a buffer underflow or decode hiccup)
void StatusCallback(void *cbData, int code, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
// Note that the string may be in PROGMEM, so copy it to RAM for printf
char s1[64];
strncpy_P(s1, string, sizeof(s1));
s1[sizeof(s1)-1]=0;
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
Serial.flush();
}
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Connecting to WiFi");
WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Try forever
while (WiFi.status() != WL_CONNECTED) {
Serial.println("...Connecting to WiFi");
delay(1000);
}
Serial.println("Connected");
audioLogger = &Serial;
file = new AudioFileSourceHTTPStream(URL);
// file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, 2048);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputI2S();
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
}
void loop()
{
static int lastms = 0;
if (mp3->isRunning()) {
if (millis()-lastms > 1000) {
lastms = millis();
Serial.printf("Running for %d ms...\n", lastms);
Serial.flush();
}
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
}
}
#endif
Error Message:
14:01:48.451 -> Connecting to WiFi
14:01:48.573 -> ...Connecting to WiFi
14:01:49.574 -> ...Connecting to WiFi
14:01:50.574 -> ...Connecting to WiFi
14:01:52.406 -> ...Connecting to WiFi
14:01:53.410 -> Connected
14:01:53.442 -> Running for 6077 ms...
14:01:53.442 -> STATUS(buffer) '2' = 'Refilling buffer'
14:02:00.118 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 1069'
14:02:00.118 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 1069'
14:02:00.118 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2050'
14:02:00.118 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2051'
14:02:00.118 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2052'
14:02:00.152 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2053'
14:02:00.152 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2054'
14:02:00.152 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2055'
14:02:00.152 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2056'
14:02:00.152 -> STATUS(mp3) '257' = 'Decoding error 'lost synchronization' at byte offset 2057'
...............
14:02:00.419 ->
14:02:00.419 -> Exception (3):
14:02:00.419 -> epc1=0x40204926 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4025e5ce depc=0x00000000
14:02:00.419 ->
14:02:00.419 -> >>>stack>>>
14:02:00.419 ->
14:02:00.419 -> ctx: cont
14:02:00.419 -> sp: 3ffef800 end: 3ffefc90 offset: 0150
14:02:00.452 -> 3ffef950: 00000000 00000000 00000000 00000000
14:02:00.452 -> 3ffef960: 00000000 40000000 00009bf0 00000000
14:02:00.452 -> 3ffef970: 00000000 00000000 00000000 00000000
14:02:00.452 -> 3ffef980: 3ffe0000 3ffefad0 00000014 3ffe83c4
14:02:00.452 -> 3ffef990: 3ffefb00 00000000 00000000 00000000
14:02:00.452 -> 3ffef9a0: 00000000 00000000 3ffefa13 00000000
14:02:00.452 -> 3ffef9b0: 40100265 40100398 40100265 3ffeead0
14:02:00.484 -> 3ffef9c0: 4000050c 40100228 3fffc270 0000050c
14:02:00.484 -> 3ffef9d0: 00000000 00000000 00000000 0000ffff
14:02:00.484 -> 3ffef9e0: 40000ea3 00000023 00000001 00000001
...............
14:02:00.679 -> <<<stack<<<
14:02:00.679 ->
14:02:00.679 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
14:02:00.679 ->
14:02:00.679 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
14:02:00.679 ->
14:02:00.679 -> load 0x4010f000, len 3424, room 16
14:02:00.679 -> tail 0
14:02:00.679 -> chksum 0x2e
14:02:00.679 -> load 0x3fff20b8, len 40, room 8
14:02:00.725 -> tail 0
14:02:00.725 -> chksum 0x2b
14:02:00.725 -> csum 0x2b
14:02:00.725 -> v00072410
14:02:00.725 -> ~ld
if I change line code:
from:
buff = new AudioFileSourceBuffer(file, 2048);
to this:
buff = new AudioFileSourceBuffer(file, 2048*8);
Error Code will change like this:
14:20:55.527 -> ...Connecting to WiFi
14:20:56.528 -> ...Connecting to WiFi
14:20:57.528 -> ...Connecting to WiFi
14:20:59.377 -> ...Connecting to WiFi
14:21:00.333 -> Connected
14:21:00.378 -> OOM error in MP3
14:21:00.378 -> MP3 done
14:21:01.388 -> MP3 done
14:21:02.400 -> MP3 done
14:21:03.402 -> MP3 done <<<< forever like this
Plzzz help me solve the problem