优化MP3播放内存占用

This commit is contained in:
wangyz1997 2024-04-10 01:11:53 +08:00
parent e0178ed5ad
commit 79366d99db
3 changed files with 12 additions and 20 deletions

View File

@ -365,7 +365,7 @@ typedef struct
drmp3_uint32 mp3FrameSampleRate; /* The sample rate of the currently loaded MP3 frame. Internal use only. */
drmp3_uint32 pcmFramesConsumedInMP3Frame;
drmp3_uint32 pcmFramesRemainingInMP3Frame;
drmp3_uint8 pcmFrames[sizeof(float)*DRMP3_MAX_SAMPLES_PER_FRAME]; /* <-- Multipled by sizeof(float) to ensure there's enough room for DR_MP3_FLOAT_OUTPUT. */
drmp3_uint8 pcmFrames[sizeof(drmp3_int16)*DRMP3_MAX_SAMPLES_PER_FRAME]; /* <-- Multipled by sizeof(float) to ensure there's enough room for DR_MP3_FLOAT_OUTPUT. */
drmp3_uint64 currentPCMFrame; /* The current PCM frame, globally, based on the output sample rate. Mainly used for seeking. */
drmp3_uint64 streamCursor; /* The current byte the decoder is sitting on in the raw stream. */
drmp3_seek_point* pSeekPoints; /* NULL by default. Set with drmp3_bind_seek_table(). Memory is owned by the client. dr_mp3 will never attempt to free this pointer. */
@ -2441,13 +2441,12 @@ DRMP3_API void drmp3dec_f32_to_s16(const float *in, drmp3_int16 *out, size_t num
#define DRMP3_SEEK_LEADING_MP3_FRAMES 2
#endif
#define DRMP3_MIN_DATA_CHUNK_SIZE 16384
/* The size in bytes of each chunk of data to read from the MP3 stream. minimp3 recommends at least 16K, but in an attempt to reduce data movement I'm making this slightly larger. */
#ifndef DRMP3_DATA_CHUNK_SIZE
#define DRMP3_DATA_CHUNK_SIZE (DRMP3_MIN_DATA_CHUNK_SIZE*4)
#endif
#define DRMP3_MIN_DATA_CHUNK_SIZE (DRMP3_DATA_CHUNK_SIZE/2)
#define DRMP3_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
#define DRMP3_CLAMP(x, lo, hi) (DRMP3_MAX(lo, DRMP3_MIN(x, hi)))
@ -3564,7 +3563,7 @@ DRMP3_API void drmp3_uninit(drmp3* pMP3)
if (pMP3 == NULL) {
return;
}
#ifndef DR_MP3_NO_STDIO
if (pMP3->onRead == drmp3__on_read_stdio) {
FILE* pFile = (FILE*)pMP3->pUserData;
@ -3952,7 +3951,7 @@ DRMP3_API drmp3_bool32 drmp3_get_mp3_and_pcm_frame_count(drmp3* pMP3, drmp3_uint
/* We'll need to seek back to where we were, so grab the PCM frame we're currently sitting on so we can restore later. */
currentPCMFrame = pMP3->currentPCMFrame;
if (!drmp3_seek_to_start_of_stream(pMP3)) {
return DRMP3_FALSE;
}
@ -4050,7 +4049,7 @@ DRMP3_API drmp3_bool32 drmp3_calculate_seek_points(drmp3* pMP3, drmp3_uint32* pS
/* We'll need to seek back to the current sample after calculating the seekpoints so we need to go ahead and grab the current location at the top. */
currentPCMFrame = pMP3->currentPCMFrame;
/* We never do more than the total number of MP3 frames and we limit it to 32-bits. */
if (!drmp3_get_mp3_and_pcm_frame_count(pMP3, &totalMP3FrameCount, &totalPCMFrameCount)) {
return DRMP3_FALSE;

View File

@ -1,4 +1,5 @@
#define DR_MP3_IMPLEMENTATION
#define DR_MP3_NO_STDIO
#define DRMP3_DATA_CHUNK_SIZE 16384
#include "dr_mp3.h"

View File

@ -132,18 +132,14 @@ void music_mp3_play(const char *file_name)
return;
}
drmp3 *mp3 = malloc_dtcm(sizeof(drmp3)); //为MP3解码器分配内存
if (mp3 == NULL) {
elog_error(TAG, "error while creating mp3 decoder structure");
goto exit;
}
drmp3_init(mp3, mp3_read, mp3_seek, file, &mp3_allocation_cbs); //初始化MP3解码器
drmp3 mp3;
drmp3_init(&mp3, mp3_read, mp3_seek, file, &mp3_allocation_cbs); //创建并初始化MP3解码器
elog_info(TAG, "music_mp3_play: channels=%d, sampleRate=%d", mp3->channels, mp3->sampleRate);
elog_info(TAG, "music_mp3_play: channels=%d, sampleRate=%d", mp3.channels, mp3.sampleRate);
/* TODO: 判断mp3文件参数并返回错误 */
bsp_aic3204_set_fs(&hi2c1, mp3->sampleRate); //设置采样率
bsp_aic3204_set_fs(&hi2c1, mp3.sampleRate); //设置采样率
audio_hal_start(DRMP3_MAX_SAMPLES_PER_FRAME, sizeof(drmp3_int16)); //初始化SAI
@ -155,7 +151,7 @@ void music_mp3_play(const char *file_name)
}
/* 此处的framesToRead指的是PCM块中采样的数量(可以理解为时间层面的采样数) 不考虑声道数量与每个采样的字节数 */
uint32_t sample_count = drmp3_read_pcm_frames_s16(mp3, DRMP3_MAX_PCM_FRAMES_PER_MP3_FRAME, pcm_sample); //解码新一帧MP3数据
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
@ -174,7 +170,6 @@ void music_mp3_play(const char *file_name)
}
}
exit:
audio_hal_stop(); //停止SAI播放
if (file != NULL) {
@ -182,8 +177,5 @@ exit:
free(file);
}
if (mp3 != NULL) { //释放为解码器分配的内存
drmp3_uninit(mp3);
free_dtcm(mp3);
}
drmp3_uninit(&mp3); //释放解码器内部申请的文件缓冲区
}