@@ -231,21 +231,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(badge_nvs_set_u16_obj, badge_nvs_set_u16_);
231
231
#include "esp_peripherals.h"
232
232
#include "periph_wifi.h"
233
233
#include "badge_power.h"
234
- void
235
- do_audio ( void ) {
236
- tcpip_adapter_init ( );
234
+
235
+ STATIC mp_obj_t badge_play_mp3_stream ( mp_obj_t _url ) {
236
+ const char * url = mp_obj_str_get_str ( _url );
237
237
238
238
audio_pipeline_handle_t pipeline ;
239
239
audio_element_handle_t http_stream_reader , i2s_stream_writer , mp3_decoder ;
240
240
241
- // esp_log_level_set("*", ESP_LOG_WARN);
242
- // esp_log_level_set(TAG, ESP_LOG_DEBUG);
243
-
244
241
ESP_LOGI (TAG , "[ 1 ] Start audio codec chip" );
245
242
badge_power_sdcard_enable ();
246
- // audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT();
247
- // audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
248
- // audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
249
243
250
244
ESP_LOGI (TAG , "[2.0] Create audio pipeline for playback" );
251
245
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG ();
@@ -258,6 +252,12 @@ do_audio(void) {
258
252
259
253
ESP_LOGI (TAG , "[2.2] Create i2s stream to write data to codec chip" );
260
254
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT ();
255
+
256
+ // fixes
257
+ i2s_cfg .i2s_config .mode = I2S_MODE_MASTER | I2S_MODE_TX ;
258
+ i2s_cfg .i2s_config .sample_rate = 48000 ;
259
+ i2s_cfg .i2s_config .use_apll = 0 ; // too far off
260
+
261
261
i2s_cfg .type = AUDIO_STREAM_WRITER ;
262
262
i2s_stream_writer = i2s_stream_init (& i2s_cfg );
263
263
@@ -274,18 +274,11 @@ do_audio(void) {
274
274
audio_pipeline_link (pipeline , (const char * []) {"http" , "mp3" , "i2s" }, 3 );
275
275
276
276
ESP_LOGI (TAG , "[2.6] Setup uri (http as http_stream, mp3 as mp3 decoder, and default output is i2s)" );
277
- audio_element_set_uri (http_stream_reader , "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3" );
278
-
279
- ESP_LOGI (TAG , "[ 3 ] Start and wait for Wi-Fi network" );
280
- esp_periph_config_t periph_cfg = { 0 };
281
- esp_periph_init (& periph_cfg );
282
- periph_wifi_cfg_t wifi_cfg = {
283
- .ssid = CONFIG_WIFI_SSID ,
284
- .password = CONFIG_WIFI_PASSWORD ,
285
- };
286
- esp_periph_handle_t wifi_handle = periph_wifi_init (& wifi_cfg );
287
- esp_periph_start (wifi_handle );
288
- periph_wifi_wait_for_connected (wifi_handle , portMAX_DELAY );
277
+ if (* url == 0 ) { // empty string; keep as hack to test audio
278
+ audio_element_set_uri (http_stream_reader , "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3" );
279
+ } else {
280
+ audio_element_set_uri (http_stream_reader , url );
281
+ }
289
282
290
283
ESP_LOGI (TAG , "[ 4 ] Setup event listener" );
291
284
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG ();
@@ -294,9 +287,6 @@ do_audio(void) {
294
287
ESP_LOGI (TAG , "[4.1] Listening event from all elements of pipeline" );
295
288
audio_pipeline_set_listener (pipeline , evt );
296
289
297
- ESP_LOGI (TAG , "[4.2] Listening event from peripherals" );
298
- audio_event_iface_set_listener (esp_periph_get_event_iface (), evt );
299
-
300
290
ESP_LOGI (TAG , "[ 5 ] Start audio_pipeline" );
301
291
audio_pipeline_run (pipeline );
302
292
@@ -332,12 +322,26 @@ do_audio(void) {
332
322
333
323
ESP_LOGI (TAG , "[ 6 ] Stop audio_pipeline" );
334
324
audio_pipeline_terminate (pipeline );
325
+
326
+ /* Terminate the pipeline before removing the listener */
327
+ audio_pipeline_unregister (pipeline , http_stream_reader );
328
+ audio_pipeline_unregister (pipeline , i2s_stream_writer );
329
+ audio_pipeline_unregister (pipeline , mp3_decoder );
330
+
331
+ audio_pipeline_remove_listener (pipeline );
332
+
333
+ /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
334
+ audio_event_iface_destroy (evt );
335
+
336
+ /* Release all resources */
337
+ audio_pipeline_deinit (pipeline );
338
+ audio_element_deinit (http_stream_reader );
339
+ audio_element_deinit (i2s_stream_writer );
340
+ audio_element_deinit (mp3_decoder );
341
+
342
+ return mp_const_none ;
335
343
}
336
- STATIC mp_obj_t badge_test_audio () {
337
- do_audio ();
338
- return mp_obj_new_int (0 );
339
- }
340
- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (badge_test_audio_obj , badge_test_audio );
344
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (badge_play_mp3_stream_obj , badge_play_mp3_stream );
341
345
342
346
// I2C (badge_i2c.h)
343
347
STATIC mp_obj_t badge_i2c_read_reg_ (mp_obj_t _addr , mp_obj_t _reg , mp_obj_t _len ) {
@@ -891,7 +895,7 @@ STATIC const mp_rom_map_elem_t badge_module_globals_table[] = {
891
895
{MP_ROM_QSTR (MP_QSTR_i2c_write_reg ), MP_ROM_PTR (& badge_i2c_write_reg_obj )},
892
896
893
897
// audio
894
- {MP_OBJ_NEW_QSTR (MP_QSTR_test_audio ), (mp_obj_t )& badge_test_audio_obj },
898
+ {MP_OBJ_NEW_QSTR (MP_QSTR_play_mp3_stream ), (mp_obj_t )& badge_play_mp3_stream_obj },
895
899
896
900
// Mpr121
897
901
#ifdef I2C_MPR121_ADDR
0 commit comments