Skip to content

Commit 820e6e0

Browse files
authored
Merge pull request #156 from CookiePLMonster/wav-player-mem-usage
WAV Player: Reconfigure to use 8-bit memory buffer, halving memory usage
2 parents 39e4b93 + 5e801b5 commit 820e6e0

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

base_pack/wav_player/wav_parser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct {
5050
Storage* storage;
5151
Stream* stream;
5252
WavParser* parser;
53-
uint16_t* sample_buffer;
53+
uint8_t* sample_buffer;
5454
uint8_t* tmp_buffer;
5555

5656
uint32_t sample_rate;

base_pack/wav_player/wav_player.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ static WavPlayerApp* app_alloc() {
8686
app->storage = furi_record_open(RECORD_STORAGE);
8787
app->stream = file_stream_alloc(app->storage);
8888
app->parser = wav_parser_alloc();
89-
app->sample_buffer = malloc(sizeof(uint16_t) * app->samples_count);
90-
app->tmp_buffer = malloc(sizeof(uint8_t) * app->samples_count);
89+
app->sample_buffer = malloc(sizeof(*app->sample_buffer) * app->samples_count);
90+
app->tmp_buffer = malloc(sizeof(*app->tmp_buffer) * app->samples_count);
9191
app->queue = furi_message_queue_alloc(10, sizeof(WavPlayerEvent));
9292

9393
app->volume = 10.0f;
@@ -128,7 +128,7 @@ static void app_free(WavPlayerApp* app) {
128128
// TODO: that works only with 8-bit 2ch audio
129129
static bool fill_data(WavPlayerApp* app, size_t index) {
130130
if(app->num_channels == 1 && app->bits_per_sample == 8) {
131-
uint16_t* sample_buffer_start = &app->sample_buffer[index];
131+
uint8_t* sample_buffer_start = &app->sample_buffer[index];
132132
size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count_half);
133133

134134
for(size_t i = count; i < app->samples_count_half; i++) {
@@ -167,7 +167,7 @@ static bool fill_data(WavPlayerApp* app, size_t index) {
167167
}
168168

169169
if(app->num_channels == 1 && app->bits_per_sample == 16) {
170-
uint16_t* sample_buffer_start = &app->sample_buffer[index];
170+
uint8_t* sample_buffer_start = &app->sample_buffer[index];
171171
size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
172172

173173
for(size_t i = count; i < app->samples_count; i++) {
@@ -205,7 +205,7 @@ static bool fill_data(WavPlayerApp* app, size_t index) {
205205
}
206206

207207
if(app->num_channels == 2 && app->bits_per_sample == 16) {
208-
uint16_t* sample_buffer_start = &app->sample_buffer[index];
208+
uint8_t* sample_buffer_start = &app->sample_buffer[index];
209209
size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
210210

211211
for(size_t i = 0; i < app->samples_count; i += 4) {
@@ -268,7 +268,7 @@ static bool fill_data(WavPlayerApp* app, size_t index) {
268268
}
269269

270270
if(app->num_channels == 2 && app->bits_per_sample == 8) {
271-
uint16_t* sample_buffer_start = &app->sample_buffer[index];
271+
uint8_t* sample_buffer_start = &app->sample_buffer[index];
272272
size_t count = stream_read(app->stream, app->tmp_buffer, app->samples_count);
273273

274274
for(size_t i = count; i < app->samples_count; i++) {

base_pack/wav_player/wav_player_hal.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void wav_player_speaker_init(uint32_t sample_rate) {
3838
TIM_InitStruct.Prescaler = 0;
3939
//TIM_InitStruct.Autoreload = 1451; //64 000 000 / 1451 ~= 44100 Hz
4040

41-
TIM_InitStruct.Autoreload = 64000000 / sample_rate - 1; //to support various sample rates
41+
TIM_InitStruct.Autoreload = SystemCoreClock / sample_rate - 1; //to support various sample rates
4242

4343
LL_TIM_Init(SAMPLE_RATE_TIMER, &TIM_InitStruct);
4444

@@ -95,7 +95,7 @@ void wav_player_dma_init(uint32_t address, size_t size) {
9595
LL_DMA_SetPeriphIncMode(DMA_INSTANCE, LL_DMA_PERIPH_NOINCREMENT);
9696
LL_DMA_SetMemoryIncMode(DMA_INSTANCE, LL_DMA_MEMORY_INCREMENT);
9797
LL_DMA_SetPeriphSize(DMA_INSTANCE, LL_DMA_PDATAALIGN_HALFWORD);
98-
LL_DMA_SetMemorySize(DMA_INSTANCE, LL_DMA_MDATAALIGN_HALFWORD);
98+
LL_DMA_SetMemorySize(DMA_INSTANCE, LL_DMA_MDATAALIGN_BYTE);
9999

100100
LL_DMA_EnableIT_TC(DMA_INSTANCE);
101101
LL_DMA_EnableIT_HT(DMA_INSTANCE);

base_pack/wav_player/wav_player_view.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void wav_player_view_set_bits(WavPlayerView* wav_view, uint16_t bit) {
174174
wav_view->view, WavPlayerViewModel * model, { model->bits_per_sample = bit; }, true);
175175
}
176176

177-
void wav_player_view_set_data(WavPlayerView* wav_view, uint16_t* data, size_t data_count) {
177+
void wav_player_view_set_data(WavPlayerView* wav_view, uint8_t* data, size_t data_count) {
178178
furi_assert(wav_view);
179179
with_view_model(
180180
wav_view->view,

base_pack/wav_player/wav_player_view.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void wav_player_view_set_current(WavPlayerView* wav_view, size_t current);
6464

6565
void wav_player_view_set_play(WavPlayerView* wav_view, bool play);
6666

67-
void wav_player_view_set_data(WavPlayerView* wav_view, uint16_t* data, size_t data_count);
67+
void wav_player_view_set_data(WavPlayerView* wav_view, uint8_t* data, size_t data_count);
6868

6969
void wav_player_view_set_bits(WavPlayerView* wav_view, uint16_t bit);
7070
void wav_player_view_set_chans(WavPlayerView* wav_view, uint16_t chn);

0 commit comments

Comments
 (0)