Skip to content

Commit 7c113ae

Browse files
committed
test_audio() -> play_mp3_stream($url); add cleanups
1 parent 43c2cf9 commit 7c113ae

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

esp32/modbadge.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(badge_nvs_set_u16_obj, badge_nvs_set_u16_);
231231
#include "esp_peripherals.h"
232232
#include "periph_wifi.h"
233233
#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);
237237

238238
audio_pipeline_handle_t pipeline;
239239
audio_element_handle_t http_stream_reader, i2s_stream_writer, mp3_decoder;
240240

241-
// esp_log_level_set("*", ESP_LOG_WARN);
242-
// esp_log_level_set(TAG, ESP_LOG_DEBUG);
243-
244241
ESP_LOGI(TAG, "[ 1 ] Start audio codec chip");
245242
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);
249243

250244
ESP_LOGI(TAG, "[2.0] Create audio pipeline for playback");
251245
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
@@ -258,6 +252,12 @@ do_audio(void) {
258252

259253
ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
260254
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+
261261
i2s_cfg.type = AUDIO_STREAM_WRITER;
262262
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
263263

@@ -274,18 +274,11 @@ do_audio(void) {
274274
audio_pipeline_link(pipeline, (const char *[]) {"http", "mp3", "i2s"}, 3);
275275

276276
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+
}
289282

290283
ESP_LOGI(TAG, "[ 4 ] Setup event listener");
291284
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
@@ -294,9 +287,6 @@ do_audio(void) {
294287
ESP_LOGI(TAG, "[4.1] Listening event from all elements of pipeline");
295288
audio_pipeline_set_listener(pipeline, evt);
296289

297-
ESP_LOGI(TAG, "[4.2] Listening event from peripherals");
298-
audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);
299-
300290
ESP_LOGI(TAG, "[ 5 ] Start audio_pipeline");
301291
audio_pipeline_run(pipeline);
302292

@@ -332,12 +322,26 @@ do_audio(void) {
332322

333323
ESP_LOGI(TAG, "[ 6 ] Stop audio_pipeline");
334324
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;
335343
}
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);
341345

342346
// I2C (badge_i2c.h)
343347
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[] = {
891895
{MP_ROM_QSTR(MP_QSTR_i2c_write_reg), MP_ROM_PTR(&badge_i2c_write_reg_obj)},
892896

893897
// 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},
895899

896900
// Mpr121
897901
#ifdef I2C_MPR121_ADDR

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy