Skip to content

Commit 2fb0054

Browse files
committed
add badge.test_audio()
1 parent aa23cc3 commit 2fb0054

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

esp32/modbadge.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,131 @@ STATIC mp_obj_t badge_nvs_set_u16_(mp_obj_t _namespace, mp_obj_t _key, mp_obj_t
214214
STATIC MP_DEFINE_CONST_FUN_OBJ_3(badge_nvs_set_u16_obj, badge_nvs_set_u16_);
215215

216216

217+
// audio
218+
#define IIS_SCLK 33
219+
#define IIS_LCLK 12
220+
#define IIS_DSIN 4
221+
#define IIS_DOUT -1
222+
223+
#include "esp_log.h"
224+
#include "audio_element.h"
225+
#include "audio_pipeline.h"
226+
#include "audio_event_iface.h"
227+
#include "audio_common.h"
228+
#include "http_stream.h"
229+
#include "i2s_stream.h"
230+
#include "mp3_decoder.h"
231+
#include "esp_peripherals.h"
232+
#include "periph_wifi.h"
233+
#include "badge_power.h"
234+
void
235+
do_audio(void) {
236+
tcpip_adapter_init();
237+
238+
audio_pipeline_handle_t pipeline;
239+
audio_element_handle_t http_stream_reader, i2s_stream_writer, mp3_decoder;
240+
241+
// esp_log_level_set("*", ESP_LOG_WARN);
242+
// esp_log_level_set(TAG, ESP_LOG_DEBUG);
243+
244+
ESP_LOGI(TAG, "[ 1 ] Start audio codec chip");
245+
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+
250+
ESP_LOGI(TAG, "[2.0] Create audio pipeline for playback");
251+
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
252+
pipeline = audio_pipeline_init(&pipeline_cfg);
253+
mem_assert(pipeline);
254+
255+
ESP_LOGI(TAG, "[2.1] Create http stream to read data");
256+
http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
257+
http_stream_reader = http_stream_init(&http_cfg);
258+
259+
ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
260+
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
261+
i2s_cfg.type = AUDIO_STREAM_WRITER;
262+
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
263+
264+
ESP_LOGI(TAG, "[2.3] Create mp3 decoder to decode mp3 file");
265+
mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
266+
mp3_decoder = mp3_decoder_init(&mp3_cfg);
267+
268+
ESP_LOGI(TAG, "[2.4] Register all elements to audio pipeline");
269+
audio_pipeline_register(pipeline, http_stream_reader, "http");
270+
audio_pipeline_register(pipeline, mp3_decoder, "mp3");
271+
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");
272+
273+
ESP_LOGI(TAG, "[2.5] Link it together http_stream-->mp3_decoder-->i2s_stream-->[codec_chip]");
274+
audio_pipeline_link(pipeline, (const char *[]) {"http", "mp3", "i2s"}, 3);
275+
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);
289+
290+
ESP_LOGI(TAG, "[ 4 ] Setup event listener");
291+
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
292+
audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
293+
294+
ESP_LOGI(TAG, "[4.1] Listening event from all elements of pipeline");
295+
audio_pipeline_set_listener(pipeline, evt);
296+
297+
ESP_LOGI(TAG, "[4.2] Listening event from peripherals");
298+
audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);
299+
300+
ESP_LOGI(TAG, "[ 5 ] Start audio_pipeline");
301+
audio_pipeline_run(pipeline);
302+
303+
while (1) {
304+
audio_event_iface_msg_t msg;
305+
esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
306+
if (ret != ESP_OK) {
307+
ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
308+
continue;
309+
}
310+
311+
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT
312+
&& msg.source == (void *) mp3_decoder
313+
&& msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
314+
audio_element_info_t music_info = {0};
315+
audio_element_getinfo(mp3_decoder, &music_info);
316+
317+
ESP_LOGI(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
318+
music_info.sample_rates, music_info.bits, music_info.channels);
319+
320+
audio_element_setinfo(i2s_stream_writer, &music_info);
321+
i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
322+
continue;
323+
}
324+
325+
/* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
326+
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
327+
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
328+
ESP_LOGW(TAG, "[ * ] Stop event received");
329+
break;
330+
}
331+
}
332+
333+
ESP_LOGI(TAG, "[ 6 ] Stop audio_pipeline");
334+
audio_pipeline_terminate(pipeline);
335+
}
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);
341+
217342
// I2C (badge_i2c.h)
218343
STATIC mp_obj_t badge_i2c_read_reg_(mp_obj_t _addr, mp_obj_t _reg, mp_obj_t _len) {
219344
int addr = mp_obj_get_int(_addr);
@@ -765,6 +890,9 @@ STATIC const mp_rom_map_elem_t badge_module_globals_table[] = {
765890
{MP_ROM_QSTR(MP_QSTR_i2c_read_reg), MP_ROM_PTR(&badge_i2c_read_reg_obj)},
766891
{MP_ROM_QSTR(MP_QSTR_i2c_write_reg), MP_ROM_PTR(&badge_i2c_write_reg_obj)},
767892

893+
// audio
894+
{MP_OBJ_NEW_QSTR(MP_QSTR_test_audio), (mp_obj_t)&badge_test_audio_obj},
895+
768896
// Mpr121
769897
#ifdef I2C_MPR121_ADDR
770898
{MP_ROM_QSTR(MP_QSTR_mpr121_get_touch_info), MP_ROM_PTR(&badge_mpr121_get_touch_info_obj)},

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