Page 1 of 1

Play MP3 files in FreeRTOS tasks

Posted: Wed Jan 16, 2019 9:25 am
by vgonet
Hello,

I'm trying to play mp3 files based on the example https://github.com/espressif/esp-adf/tr ... ay_mp3_dac. I've only 1 DAC channel available (GPIO25). So I've created a custom DAC configuration.

#define I2S_STREAM_INTERNAL_DAC_CFG_CUSTOM() { \
.type = AUDIO_STREAM_WRITER, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.i2s_config = { \
.mode = I2S_MODE_MASTER | I2S_MODE_DAC_BUILT_IN | I2S_MODE_TX, \
.sample_rate = 44100, \
.bits_per_sample = 16, \
.channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, \
.communication_format = I2S_COMM_FORMAT_I2S_MSB, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2, \
}, \
.i2s_port = 0, \
}

My firmware has several Freertos tasks and I would like to play mp3 files inside these tasks.

I've 2 issues:
--------------------------------------------------------------------------------------
1. If I run the example in my main() function, the mp3 is played correctly but at the end of the music, the following code is not executed.

I (207) cpu_start: Pro cpu start user code
I (2) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (4) gpio: GPIO[27]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (12) uart: UART is initialized
I (15) i2c: I2C is initialized
I (18) mp3: [ 1 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event
I (28) mp3: [1.1] Create mp3 decoder to decode mp3 file and set custom read callback
I (37) mp3: [1.2] Create i2s stream to write data to ESP32 internal DAC
I (45) mp3: [1.3] Register all elements to audio pipeline
I (50) mp3: [1.4] Link it together [mp3_music_read_cb]-->mp3_decoder-->i2s_stream-->[ESP32 DAC]
I (59) mp3: [ 2 ] Setup event listener
I (64) mp3: [2.1] Listening event from all elements of pipeline
I (70) mp3: [ 3 ] Start audio_pipeline
I (88) mp3: [ * ] Receive music info from mp3 decoder, sample_rates=44100, bits=16, ch=2
I (6837) mp3: [ 4 ] Stop audio_pipeline
W (6838) AUDIO_PIPELINE: There are no listener registered

--------------------------------------------------------------------------------------
2. If I run inside a task, I've the following error and the mp3 is not played.

I (176) main: Start Sensor Task
I (10404) mp3: [ 1 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event
I (10404) mp3: [1.1] Create mp3 decoder to decode mp3 file and set custom read callback
I (10412) mp3: [1.2] Create i2s stream to write data to ESP32 internal DAC
E (10420) I2S: Register I2S Interrupt error

--------------------------------------------------------------------------------------

Thank you for your help and best regards.

Re: Play MP3 files in FreeRTOS tasks

Posted: Fri Feb 01, 2019 2:45 am
by jason.mao
Hi vgonet
1. If I run the example in my main() function, the mp3 is played correctly but at the end of the music, the following code is not executed.
I have found any problems on your logs. Could you give more messages?
2. If I run inside a task, I've the following error and the mp3 is not played.

I (176) main: Start Sensor Task
I (10404) mp3: [ 1 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event
I (10404) mp3: [1.1] Create mp3 decoder to decode mp3 file and set custom read callback
I (10412) mp3: [1.2] Create i2s stream to write data to ESP32 internal DAC
E (10420) I2S: Register I2S Interrupt error
Could you help to provide your codes on here?

Thanks.

Re: Play MP3 files in FreeRTOS tasks

Posted: Wed Feb 06, 2019 7:46 am
by vgonet
Hello,

In attachment, you can see a part of my code.

the task "sensor" is called every 125us through a notification given by a timer interrupt (see in timer.c, static void IRAM_ATTR ISR_TimerGroup0(void* para)).

- When the line 130 of main.c is commented and the task "sound" (lines 145 to 154 of main.c) is disabled, the task "sensor" is running.

I (424) sensors: Sensors are initialized
I (424) mode: Mode is initialized
I (434) mode: VM Mode is initialized
I (434) timer: Group 0 Timer 0 is initialized
I (444) timer: Group 0 Timer 1 is initialized
I (444) behavior: Start Behavior Task
I (444) sensors: Start Comm Task
I (454) sensors: Start Sensor Task
I (454) sensors: Run Sensor Task
I (464) sensors: Run Sensor Task
I (464) sensors: Run Sensor Task
I (464) sensors: Run Sensor Task

- When the line 130 of main.c is uncommented and the task "sound" (lines 145 to 154 of main.c) is enabled, the task "sensor" is NOT running but there are no errors or warning in the logs. The sound is played at startup.

- When the line 130 of main.c is uncommented and the task "sound" (lines 145 to 154 of main.c) is disabled, the task "sensor" is NOT running but there are no errors or warning in the logs. So, there is perhaps something in the function i2s_stream_init() which stops the timer interrupt or the notification.

I hope this code will help us to find this issue.
Thank you