Skip to content

Commit

Permalink
Merge pull request #156 from CookiePLMonster/wav-player-mem-usage
Browse files Browse the repository at this point in the history
WAV Player: Reconfigure to use 8-bit memory buffer, halving memory usage
  • Loading branch information
xMasterX authored Jul 4, 2024
2 parents 39e4b93 + 5e801b5 commit 820e6e0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion base_pack/wav_player/wav_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ typedef struct {
Storage* storage;
Stream* stream;
WavParser* parser;
uint16_t* sample_buffer;
uint8_t* sample_buffer;
uint8_t* tmp_buffer;

uint32_t sample_rate;
Expand Down
12 changes: 6 additions & 6 deletions base_pack/wav_player/wav_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ static WavPlayerApp* app_alloc() {
app->storage = furi_record_open(RECORD_STORAGE);
app->stream = file_stream_alloc(app->storage);
app->parser = wav_parser_alloc();
app->sample_buffer = malloc(sizeof(uint16_t) * app->samples_count);
app->tmp_buffer = malloc(sizeof(uint8_t) * app->samples_count);
app->sample_buffer = malloc(sizeof(*app->sample_buffer) * app->samples_count);
app->tmp_buffer = malloc(sizeof(*app->tmp_buffer) * app->samples_count);
app->queue = furi_message_queue_alloc(10, sizeof(WavPlayerEvent));

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

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

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

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

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

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

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

for(size_t i = count; i < app->samples_count; i++) {
Expand Down
4 changes: 2 additions & 2 deletions base_pack/wav_player/wav_player_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void wav_player_speaker_init(uint32_t sample_rate) {
TIM_InitStruct.Prescaler = 0;
//TIM_InitStruct.Autoreload = 1451; //64 000 000 / 1451 ~= 44100 Hz

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

LL_TIM_Init(SAMPLE_RATE_TIMER, &TIM_InitStruct);

Expand Down Expand Up @@ -95,7 +95,7 @@ void wav_player_dma_init(uint32_t address, size_t size) {
LL_DMA_SetPeriphIncMode(DMA_INSTANCE, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA_INSTANCE, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA_INSTANCE, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_SetMemorySize(DMA_INSTANCE, LL_DMA_MDATAALIGN_HALFWORD);
LL_DMA_SetMemorySize(DMA_INSTANCE, LL_DMA_MDATAALIGN_BYTE);

LL_DMA_EnableIT_TC(DMA_INSTANCE);
LL_DMA_EnableIT_HT(DMA_INSTANCE);
Expand Down
2 changes: 1 addition & 1 deletion base_pack/wav_player/wav_player_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void wav_player_view_set_bits(WavPlayerView* wav_view, uint16_t bit) {
wav_view->view, WavPlayerViewModel * model, { model->bits_per_sample = bit; }, true);
}

void wav_player_view_set_data(WavPlayerView* wav_view, uint16_t* data, size_t data_count) {
void wav_player_view_set_data(WavPlayerView* wav_view, uint8_t* data, size_t data_count) {
furi_assert(wav_view);
with_view_model(
wav_view->view,
Expand Down
2 changes: 1 addition & 1 deletion base_pack/wav_player/wav_player_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void wav_player_view_set_current(WavPlayerView* wav_view, size_t current);

void wav_player_view_set_play(WavPlayerView* wav_view, bool play);

void wav_player_view_set_data(WavPlayerView* wav_view, uint16_t* data, size_t data_count);
void wav_player_view_set_data(WavPlayerView* wav_view, uint8_t* data, size_t data_count);

void wav_player_view_set_bits(WavPlayerView* wav_view, uint16_t bit);
void wav_player_view_set_chans(WavPlayerView* wav_view, uint16_t chn);
Expand Down

0 comments on commit 820e6e0

Please sign in to comment.