Új hozzászólás Aktív témák

  • bagarol

    junior tag

    Egyszerű webrádió esp32-vel, nem kell hozzá külső alkatrész. Mono, belső DAC, kijelző nélkül.
    Soros konzolon lehet beírni a rádió címét, egyből elkezdi lejátszani, stop-ra leáll, lehet új címet megadni. DAC2-őn (gpio26) adja ki a jelet.Nekem konyhába így is megfelel, persze kijelzővel + egyebek, úgy értem, hogy mono, belső dac. Az esp8266audio[link] library kell hozzá.
    Ellenőrizni nem tudtam,nincs most esp32-m, remélem működik. :)

    #include <WiFi.h>
    #include <AudioFileSource.h>
    #include <AudioFileSourceBuffer.h>
    #include <AudioFileSourceICYStream.h>
    #include <AudioGeneratorMP3.h>
    #include <AudioOutputI2S.h>

    const char* ssid = "";
    const char* password = "";
    String s;
    char URL[96];
    const int preallocateBufferSize = 32*1024;
    const int preallocateCodecSize = 29192;
    void *preallocateBuffer = NULL;
    void *preallocateCodec = NULL;
    AudioGeneratorMP3 *mp3;
    AudioFileSourceICYStream *file;
    AudioFileSourceBuffer *buff;
    AudioOutputI2S *out;

    void setup() {
    preallocateBuffer = malloc(preallocateBufferSize);
    if (!preallocateBuffer) {
    Serial.begin(115200);
    Serial.printf_P(PSTR("FATAL ERROR: Unable to preallocate %d bytes for app\n"), preallocateBufferSize);
    while (1) delay(1000);
    }
    Serial.begin(115200);
    initwifi();
    Serial.printf("STATUS(System) Ready \n\n");
    out = new AudioOutputI2S(0, 1);
    out->SetOutputModeMono(true);
    out->SetGain(0.5);
    }

    void loop() {
    if(Serial.available()){
    s = Serial.read();
    if(s == "stop") StopPlaying();
    else if(s.startsWith("http://")) {
    s.trim();
    s.toCharArray(URL,s.length()+1);
    StartPlaying();
    }
    }
    }

    void StartPlaying() {
    file = new AudioFileSourceICYStream(URL);
    file->RegisterMetadataCB(MDCallback, (void*)"ICY");
    buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
    buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
    out = new AudioOutputI2S(0, 1);
    out->SetOutputModeMono(true);
    out->SetGain(0.5);
    mp3 = new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize);
    mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
    mp3->begin(buff, out);
    Serial.printf("STATUS(URL) %s \n", URL);
    Serial.flush();
    }

    void StopPlaying() {
    if (mp3) {
    mp3->stop();
    delete mp3;
    mp3 = NULL;
    }
    if (buff) {
    buff->close();
    delete buff;
    buff = NULL;
    }
    if (file) {
    file->close();
    delete file;
    file = NULL;
    }
    Serial.printf("STATUS(Stopped)\n");
    Serial.flush();
    }

    void initwifi() {
    WiFi.disconnect();
    WiFi.softAPdisconnect(true);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi");
    delay(1000);
    }
    Serial.println("Connected to WiFi");
    }

    void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string) {
    const char *ptr = reinterpret_cast<const char *>(cbData);
    (void) isUnicode;
    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();
    }

    void StatusCallback(void *cbData, int code, const char *string) {
    const char *ptr = reinterpret_cast<const char *>(cbData);
    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();
    }

    [ Szerkesztve ]

Új hozzászólás Aktív témák