ESP32 Radio - How to improve the sound?

Hello,

I got this ESP32 Radio source code from this site.

I love it because its very simple radio and works fine on ESP32.

But the sound is very choppy, maybe because lack of proper buffer and I don’t know how to fix it.

The wiring:

The code:

/*
WITHOUT OLED, only ESP32 & VS1053

VS1053 - connections detail
XRST = EN (D3)
 MISO = D19
 MOSI = D23
 SCLK = D18
 VCC = 5V / 3.3 V
 Gnd = Gnd 
*/


#include <VS1053.h> //ESP_VS1053_Library

#include <Preferences.h>  //For reading and writing into the ROM memory
Preferences preferences;
unsigned int counter,old_counter,new_counter;

#include "helloMp3.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <esp_wifi.h>

char ssid[] = "SSID";    //  your network SSID (name) 
char pass[] = "PASSWORD";   // your network password

char *host[7] = {"184.95.47.178","us4.internet-radio.com","us3.internet-radio.com","us3.internet-radio.com","174.36.206.197","sj64.hnux.com","uk7.internet-radio.com"};
char *path[7] = {"/","/","/live.m3u","/","/","/","/"};
int   port[7] = {9328,8266,8371,8297,8000,80,8226};

char *sname[7] = {"Love-Radio-USA","Smooth-Jazz-Florida","Best-Rock-Radio","Country-Radio","Venice-Classic-Radio","Smooth-Jazz-Global","UK-Top-40"};


int change=13;
bool x=true;
int status = WL_IDLE_STATUS;
WiFiClient  client;
uint8_t mp3buff[32];   // vs1053 likes 32 bytes at a time

// Wiring of VS1053 board (SPI connected in a standard way)
#define VS1053_CS    32 //32
#define VS1053_DCS   33  //33
#define VS1053_DREQ  35 //15
#define VOLUME  100 // volume level 0-100
VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);

void setup () {
 
   pinMode(change,INPUT_PULLUP);
// initialize SPI
   Serial.begin(115200);
   delay(500);
// initialize SPI bus;
    SPI.begin();

// initialize VS1053 player
    player.begin();
//    player.switchToMp3Mode(); // optional, some boards require this
    player.setVolume(VOLUME);
   
   WiFi.begin(ssid, pass);
   while (WiFi.status() != WL_CONNECTED) {
  //  display.drawString(0,15,"   Connecting..");
    delay(500);
    Serial.print(".");
   }
  Serial.println("WiFi connected");  
  Serial.print("IP address:");  
  Serial.println(WiFi.localIP());
 
preferences.begin("my-app", false);
counter = preferences.getUInt("counter", 0);
old_counter=counter;

Serial.printf("Current counter value1: %u\n", counter);
//if(counter>0) counter=0;
//Serial.printf("Current counter value2: %u\n", counter);
delay(100);
player.playChunk(hello2, sizeof(hello2)); //VS1053 is wake up & running
station_connect(counter); 
}

void loop() {
 
      if (client.available() > 0) {
      uint8_t bytesread = client.read(mp3buff, 32);
      player.playChunk(mp3buff, bytesread);
      }
      
    if(digitalRead(change)==0 and x==true){
    x=false;
    counter = counter+1;
    if(counter>6) counter=0;
    preferences.putUInt("counter",counter);
    new_counter=counter;
    Serial.printf("Old_Counter: %u\n",old_counter);
    Serial.printf("New_Counter: %u\n",new_counter);
    Serial.printf("Set counter to new_value: %u\n", counter);
    delay(500); 

    if(old_counter != new_counter) {
  
    player.softReset(); 
    x=true;
    station_connect(new_counter); 
    preferences.putUInt("counter",new_counter); 

    }
  } 
}

void station_connect (int station_no ) {
    if (client.connect(host[station_no],port[station_no]) ) {
    Serial.println("Connected now");
     }
    Serial.print(host[station_no]);
    Serial.println(path[station_no]);
    Serial.println(sname[station_no]);
    client.print(String("GET ") + path[station_no] + " HTTP/1.1\r\n" +
               "Host: " + host[station_no] + "\r\n" + 
               "Connection: close\r\n\r\n");     
  }

My question:

  1. How to improve the sound?
  2. Trying to play this 128kbps stream URL: http://us3.internet-radio.com:8371/live.m3u but unfortunately this ESP32 Radio can’t play it. Why this ESP32 Radio can’t play M3U stream? How to fix it?

Thanks for all the helpers! :slight_smile:

esp8266internetradio.zip (282 KB)

helloMp3.h (60.8 KB)

wieb: 2. Trying to play this 128kbps stream URL: http://us3.internet-radio.com:8371/live.m3u but unfortunately this ESP32 Radio can't play it. Why this ESP32 Radio can't play M3U stream? How to fix it?

Figure out: 1. M3U is playlist, not audio stream 2. To make audio stream playing on ESP32 Radio, just change http://us3.internet-radio.com:8371/live.m3u to http://us3.internet-radio.com:8371/stream

But I still need help adding some buffer code to improve the sound quality.

Please help.

wieb: My question: 1. How to improve the sound?

Found the answer of my own question in here: What is the best way to get more RAM for a sound buffer?.