Convert internet audio data stream to char array

Hi!
I would like to convert an online audio stream (eg: https://oxygenmusic.hu:8000/oxygenmusic_128) to char array continuously, then play the audio with i2s.
Someone can provide me some examples to this? I can't find anything useful so far.

Here is my code so far:

#include <driver/i2s.h>

unsigned char DataChar[];

// WT32-SC01 PLUS i2s
#define CONFIG_I2S_BCK_PIN 36
#define CONFIG_I2S_LRCK_PIN 35
#define CONFIG_I2S_DATA_PIN 37

#define Speak_I2S_NUMBER I2S_NUM_0

bool InitI2SSpeak()
{
  esp_err_t err = ESP_OK;
  i2s_driver_uninstall(Speak_I2S_NUMBER);
  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER),
    .sample_rate = 44100,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 1, 0)
    .communication_format =
        I2S_COMM_FORMAT_STAND_I2S,
#else
    .communication_format = I2S_COMM_FORMAT_I2S,
#endif
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 2,
    .dma_buf_len = 128,
  };
  i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
  i2s_config.use_apll = false;          // I2S clock setup.
  i2s_config.tx_desc_auto_clear = true; // Enables auto-cleanup descriptors for understreams.

  err += i2s_driver_install(Speak_I2S_NUMBER, &i2s_config, 0, NULL);
  i2s_pin_config_t tx_pin_config;
#if (ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0))
  tx_pin_config.mck_io_num = I2S_PIN_NO_CHANGE;
#endif

  tx_pin_config.bck_io_num = CONFIG_I2S_BCK_PIN;
  tx_pin_config.ws_io_num = CONFIG_I2S_LRCK_PIN;
  tx_pin_config.data_out_num = CONFIG_I2S_DATA_PIN;
  tx_pin_config.data_in_num = I2S_PIN_NO_CHANGE;
  err += i2s_set_pin(Speak_I2S_NUMBER, &tx_pin_config);
  err += i2s_set_clk(Speak_I2S_NUMBER, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
  return true;
}

void Setup()
{
InitI2SSpeak();
}
void Loop()
{
  size_t bytes_written = 0;
  i2s_write(Speak_I2S_NUMBER, DataChar, sizeof(DataChar), &bytes_written,
            portMAX_DELAY);
}

There are many examples on youtube, here is one of them using ESP 32

Welcome to the forum

unsigned char DataChar[];

How many characters can this array hold ?
How many characters do you expect to receive ?

void Setup()

Wrong

void Loop()

Wrong

Thank you! That's helps a lot! :slight_smile:

I've used the Audio library as shown in the example video, but the ESP32 crashes when i run audio.loop();

Crash dump:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core  1 register dump:
PC      : 0x4003f3c8  PS      : 0x00060830  A0      : 0x820b778c  A1      : 0x3fcebac0
A2      : 0x3de4d328  A3      : 0x00000000  A4      : 0x00060820  A5      : 0x00060c23
A6      : 0x02cf7708  A7      : 0x00ffffff  A8      : 0x00000007  A9      : 0x3fceba80  
A10     : 0x3de4d328  A11     : 0x3fcb7078  A12     : 0x00000004  A13     : 0x00060823  
A14     : 0x00060823  A15     : 0x00000001  SAR     : 0x00000010  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000130  LBEG    : 0x40056fc5  LEND    : 0x40056fe7  LCOUNT  : 0x00000000  
Backtrace: 0x4003f3c5:0x3fcebac0 |<-CORRUPTED
ELF file SHA256: d687c75e29312faa

I got it working!
The problem was a 5ms delay in the loop, for LVGL library.
I guess i need to run this in another task. (xTaskCreatePinnedToCore)

@speedysss I can't help much with the methods shown in the video but I did recently make something similar using micropython and a micropython library by Peter Hinch, the library was for a module called VS1053.

This is the actual module that I am using

https://www.amazon.com/DAOKI-VS1053-VS1053B-Development-Arduino/dp/B091GHF5JB/ref=pd_ci_mcx_mh_mcx_views_0?pd_rd_w=vgaGc&content-id=amzn1.sym.225b4624-972d-4629-9040-f1bf9923dd95%3Aamzn1.symc.40e6a10e-cbc4-4fa5-81e3-4435ff64d03b&pf_rd_p=225b4624-972d-4629-9040-f1bf9923dd95&pf_rd_r=3P72Z8FMZVJEXB3EZ9QC&pd_rd_wg=fkOzA&pd_rd_r=ffa7d694-036d-4c1b-b0ca-667b8e5a17a0&pd_rd_i=B091GHF5JB

I decided to use a bluetooth transmitter as output and that way I could connect to a bluetooth speaker or earbuds etc. The sound quality is outstanding.

I am sure the I2S will provide a reasonable sound, I have a couple of MAX98357 I keep meaning to try out.

Good luck with your project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.