From a315492cc20329f43455e1e75bfedf69c978c6d2 Mon Sep 17 00:00:00 2001 From: wangyz1997 Date: Wed, 10 Apr 2024 16:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=89=93=E5=8D=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Middlewares/CMakeLists.txt | 10 --------- User/audio/audio_flac.c | 34 ++++++++++++++++++++--------- User/audio/audio_hal.c | 4 ++-- User/audio/audio_hal.h | 2 +- User/audio/audio_mp3.c | 44 ++++++++++++++++++++++---------------- 5 files changed, 52 insertions(+), 42 deletions(-) diff --git a/Middlewares/CMakeLists.txt b/Middlewares/CMakeLists.txt index abb46e5..8b41a05 100644 --- a/Middlewares/CMakeLists.txt +++ b/Middlewares/CMakeLists.txt @@ -9,16 +9,6 @@ add_subdirectory(Third_Party/fatfs) add_subdirectory(Third_Party/dr_libs) add_subdirectory(Third_Party/tlsf) -# 屏蔽第三方库中的警告 -# target_compile_options(FLAC PRIVATE -# -Wformat=0 -# -Wno-incompatible-pointer-types -# ) - -# target_compile_options(grabbag PRIVATE -# -Wformat=0 -# ) - # target_compile_options(lvgl_demos PRIVATE # -Wformat=0 # ) diff --git a/User/audio/audio_flac.c b/User/audio/audio_flac.c index b35161a..11db2a7 100644 --- a/User/audio/audio_flac.c +++ b/User/audio/audio_flac.c @@ -13,11 +13,11 @@ const static char *TAG = "audio_flac"; static size_t flac_read(void* pUserData, void* pBufferOut, size_t bytesToRead) { - HAL_GPIO_TogglePin(LED_Y_GPIO_Port, LED_Y_Pin); - UINT br; FRESULT f_result = f_read(pUserData, pBufferOut, bytesToRead, &br); //从文件中读取新的内容 + HAL_GPIO_TogglePin(LED_Y_GPIO_Port, LED_Y_Pin); + if (f_result != FR_OK) { elog_error(TAG, "file read error"); return 0; @@ -62,12 +62,17 @@ static void* flac_malloc(size_t sz, void* pUserData) { UNUSED(pUserData); + if (sz == 0) { + return NULL; + } + void *ptr = malloc(sz); - // tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); - // tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); - // elog_debug(TAG, "flac_malloc: allocated %d bytes memory @%p, free memory: AXI %d bytes, AHB %d bytes", - // sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "flac_malloc: allocated %d bytes memory @%p, free memory: AXI %d bytes, AHB %d bytes", + sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (ptr == NULL) { elog_error(TAG, "flac_malloc: error while allocating %d bytes of memory", sz); } @@ -81,6 +86,11 @@ static void* flac_realloc(void* p, size_t sz, void* pUserData) void *ptr = realloc(p, sz); + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "flac_realloc: allocated %d bytes new memory @%p, free memory: AXI %d bytes, AHB %d bytes", + sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (ptr == NULL) { elog_error(TAG, "flac_realloc: error while allocating %d bytes of new memory", sz); } @@ -92,10 +102,14 @@ static void flac_free(void* p, void* pUserData) { UNUSED(pUserData); - // tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); - // tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); - // elog_debug(TAG, "flac_free: memory freed @%p, free memory: AXI %d bytes, AHB %d bytes", - // p, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (p == NULL) { + return; + } + + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "flac_free: memory freed @%p, free memory: AXI %d bytes, AHB %d bytes", + p, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); free(p); } diff --git a/User/audio/audio_hal.c b/User/audio/audio_hal.c index 0b5c5c7..62a5cde 100644 --- a/User/audio/audio_hal.c +++ b/User/audio/audio_hal.c @@ -9,7 +9,7 @@ static const char *TAG = "audio_hal"; typedef struct { - void *buffer_ptr; + const void *buffer_ptr; uint32_t buffer_size; } audio_sample_t; @@ -86,7 +86,7 @@ void audio_hal_stop(void) audio_buffer = NULL; } -void audio_hal_write(void *buffer_ptr, uint32_t buffer_size) +void audio_hal_write(const void *buffer_ptr, uint32_t buffer_size) { if (buffer_size == 0) { return; diff --git a/User/audio/audio_hal.h b/User/audio/audio_hal.h index dec380b..24dab3c 100644 --- a/User/audio/audio_hal.h +++ b/User/audio/audio_hal.h @@ -7,6 +7,6 @@ void audio_hal_start(uint32_t sample_count, uint8_t sample_size); void audio_hal_stop(void); -void audio_hal_write(void *buffer, uint32_t sample_count); +void audio_hal_write(const void *buffer, uint32_t sample_count); #endif diff --git a/User/audio/audio_mp3.c b/User/audio/audio_mp3.c index 7d9b210..5d34058 100644 --- a/User/audio/audio_mp3.c +++ b/User/audio/audio_mp3.c @@ -12,11 +12,11 @@ const static char *TAG = "player_mp3"; static size_t mp3_read(void* pUserData, void* pBufferOut, size_t bytesToRead) { - HAL_GPIO_TogglePin(LED_Y_GPIO_Port, LED_Y_Pin); - UINT br; FRESULT f_result = f_read(pUserData, pBufferOut, bytesToRead, &br); //从文件中读取新的内容 + HAL_GPIO_TogglePin(LED_Y_GPIO_Port, LED_Y_Pin); + if (f_result != FR_OK) { elog_error(TAG, "file read error"); return 0; @@ -62,12 +62,17 @@ static void* mp3_malloc(size_t sz, void* pUserData) { UNUSED(pUserData); + if (sz == 0) { + return NULL; + } + void *ptr = malloc(sz); - // tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); - // tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); - // elog_debug(TAG, "mp3_malloc: allocated %d bytes memory @%p, free memory: AXI %d bytes, AHB %d bytes", - // sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "mp3_malloc: allocated %d bytes memory @%p, free memory: AXI %d bytes, AHB %d bytes", + sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (ptr == NULL) { elog_error(TAG, "mp3_malloc: error while allocating %d bytes of memory", sz); } @@ -81,6 +86,11 @@ static void* mp3_realloc(void* p, size_t sz, void* pUserData) void *ptr = realloc(p, sz); + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "mp3_realloc: allocated %d bytes new memory @%p, free memory: AXI %d bytes, AHB %d bytes", + sz, ptr, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (ptr == NULL) { elog_error(TAG, "mp3_realloc: error while allocating %d bytes of new memory", sz); } @@ -92,10 +102,14 @@ static void mp3_free(void* p, void* pUserData) { UNUSED(pUserData); - // tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); - // tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); - // elog_debug(TAG, "mp3_free: memory freed @%p, free memory: AXI %d bytes, AHB %d bytes", - // p, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); + if (p == NULL) { + return; + } + + tlsf_pool_statistics *axi_heap_statistics = get_axi_heap_statistics(); + tlsf_pool_statistics *ahb_heap_statistics = get_ahb_heap_statistics(); + elog_debug(TAG, "mp3_free: memory freed @%p, free memory: AXI %d bytes, AHB %d bytes", + p, axi_heap_statistics->free_size, ahb_heap_statistics->free_size); free(p); } @@ -107,14 +121,6 @@ static const drmp3_allocation_callbacks mp3_allocation_cbs = { .pUserData = NULL }; -/** - * @brief 播放MP3文件 - * - * @param file_name 文件路径 - * @return esp_err_t 错误代码 - * - * @note 本函数需要至少25600字节任务栈空间 - */ void music_mp3_play(const char *file_name) { FIL *file = malloc(sizeof(FIL)); @@ -154,7 +160,7 @@ void music_mp3_play(const char *file_name) uint32_t sample_count = drmp3_read_pcm_frames_s16(&mp3, DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME, pcm_sample); //解码新一帧MP3数据 if (sample_count != 0) { - audio_hal_write(pcm_sample, DRMP3_MAX_SAMPLES_PER_FRAME * sizeof(drmp3_int16)); ///将解码后的PCM数据写入SAI + audio_hal_write(pcm_sample, DRMP3_MAX_SAMPLES_PER_FRAME * sizeof(drmp3_int16)); //将解码后的PCM数据写入SAI } else { free(pcm_sample); elog_info(TAG, "play done");