From f52b5d4a80efb487d4ce258df5c02e8609f56609 Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Tue, 2 Aug 2011 13:03:08 +0200 Subject: [PATCH] * Fixes in playback.c * Adapt mpeg.c to use buflib * Show the buflib debug menu on charcell --- apps/debug_menu.c | 2 +- apps/mpeg.c | 43 +++++++++++++++++++++++++++---------------- apps/playback.c | 19 +++++++++++-------- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/apps/debug_menu.c b/apps/debug_menu.c index 62e70a859..ffa8232d9 100644 --- a/apps/debug_menu.c +++ b/apps/debug_menu.c @@ -2071,11 +2071,11 @@ static const struct the_menu_item menuitems[] = { #elif !defined(SIMULATOR) { "View audio thread", dbg_audio_thread }, #endif - { "View buflib allocs", dbg_buflib_allocs }, #ifdef PM_DEBUG { "pm histogram", peak_meter_histogram}, #endif /* PM_DEBUG */ #endif /* HAVE_LCD_BITMAP */ + { "View buflib allocs", dbg_buflib_allocs }, #ifndef SIMULATOR #if CONFIG_TUNER { "FM Radio", dbg_fm_radio }, diff --git a/apps/mpeg.c b/apps/mpeg.c index a0182ad8d..79a52f3e7 100644 --- a/apps/mpeg.c +++ b/apps/mpeg.c @@ -35,7 +35,7 @@ #include "thread.h" #include "errno.h" #include "mp3data.h" -#include "buffer.h" +#include "core_alloc.h" #include "mp3_playback.h" #include "talk.h" #include "sound.h" @@ -145,7 +145,8 @@ static unsigned int mpeg_errno; static bool playing = false; /* We are playing an MP3 stream */ static bool is_playing = false; /* We are (attempting to) playing MP3 files */ static bool paused; /* playback is paused */ -static char* mpeg_audiobuf; /* the audio buffer */ +static int audiobuf_handle; /* handle to the audio buffer */ +static char* mpeg_audiobuf; /* poiunter to the audio buffer */ static long audiobuflen; /* length of the audio buffer */ #ifdef SIMULATOR @@ -491,14 +492,20 @@ unsigned long mpeg_get_last_header(void) #endif /* !SIMULATOR */ } - +/* Buffer must not move. And not shrink for now */ +static struct buflib_callbacks ops = { NULL, NULL }; +static int auidobuf_handle; unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) { (void)talk_buf; /* always grab the voice buffer for now */ audio_hard_stop(); if (buffer_size) /* special case for talk_init() */ - return buffer_get_buffer(buffer_size); + { /* audio_hard_stop() frees audiobuf, so re-aquire */ + audiobuf_handle = core_alloc_maximum("audiobuf", buffer_size, &ops); + audiobuflen = *buffer_size; + return core_get_data(auidobuf_handle); + } return NULL; } @@ -2663,15 +2670,16 @@ void audio_set_recording_options(struct audio_recording_options *options) static void audio_reset_buffer(void) { - size_t bufsize; /* dont break strict-aliasing */ talk_buffer_steal(); /* will use the mp3 buffer */ - /* release buffer on behalf of any audio_get_buffer() caller, - * non-fatal if there was none */ - buffer_release_buffer(0); - /* re-aquire */ - mpeg_audiobuf = buffer_get_buffer(&bufsize); - audiobuflen = bufsize; + /* alloc buffer if it's was never allocated or freed by audio_hard_stop() */ + if (!audiobuf_handle) + { + size_t bufsize; /* dont break strict-aliasing */ + audiobuf_handle = core_alloc_maximum("audiobuf", &bufsize, &ops); + mpeg_audiobuf = core_get_data(audiobuf_handle); + audiobuflen = bufsize; + } } void audio_play(long offset) @@ -2744,7 +2752,8 @@ void audio_hard_stop(void) audio_stop(); /* tell voice we obtain the buffer before freeing */ talk_buffer_steal(); - buffer_release_buffer(0); + core_free(audiobuf_handle); + audiobuf_handle = 0; } void audio_pause(void) @@ -2901,11 +2910,13 @@ void audio_init(void) mpeg_errno = 0; /* cuesheet support */ if (global_settings.cuesheet) - curr_cuesheet = (struct cuesheet*)buffer_alloc(sizeof(struct cuesheet)); + { + int handle = core_alloc("cuesheet", sizeof(struct cuesheet)); + if (handle > 0) + curr_cuesheet = core_get_data(handle); + } - size_t bufsize; /* don't break strict-aliasing */ - mpeg_audiobuf = buffer_get_buffer(&bufsize); - audiobuflen = bufsize; + audio_reset_buffer(); /* give voice buffer until we start to play */ talkbuf_init(mpeg_audiobuf); #ifndef SIMULATOR diff --git a/apps/playback.c b/apps/playback.c index 98db45986..2050f76bd 100644 --- a/apps/playback.c +++ b/apps/playback.c @@ -733,6 +733,8 @@ static void scratch_mem_init(void *mem) } } +/* Buffer must not move. And not shrink for now */ +static struct buflib_callbacks ops = { NULL, NULL }; static int audiobuf_handle; /* Set up the audio buffer for playback */ static void audio_reset_buffer(void) @@ -756,7 +758,7 @@ static void audio_reset_buffer(void) core_free(audiobuf_handle); audiobuf_handle = 0; } - audiobuf_handle = core_alloc_maximum("audiobuf", &filebuflen, NULL); + audiobuf_handle = core_alloc_maximum("audiobuf", &filebuflen, &ops); unsigned char *filebuf = core_get_data(audiobuf_handle); /* Subtract whatever voice needs */ @@ -3475,12 +3477,17 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) return NULL; } + /* make sure buffer is freed and re-allocated to simplify code below + * (audio_hard_stop() likely has done that already) */ if (audiobuf_handle > 0) { core_free(audiobuf_handle); audiobuf_handle = 0; } + audiobuf_handle = core_alloc_maximum("audiobuf", buffer_size, &ops); + buf = core_get_data(audiobuf_handle); + if (talk_buf || buffer_state == AUDIOBUF_STATE_TRASHED || !talk_voice_required()) { @@ -3498,21 +3505,17 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) talk_buffer_steal(); buffer_state = AUDIOBUF_STATE_TRASHED; } - audiobuf_handle = core_alloc_maximum("audiobuf #1", buffer_size, NULL); - buf = core_get_data(audiobuf_handle); } else { + logf("get buffer: audio"); /* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or still AUDIOBUF_STATE_INITIALIZED */ /* Skip talk buffer and move pcm buffer to end to maximize available contiguous memory - no audio running means voice will not need the swap space */ - size_t siz, talkbuf_size; - logf("get buffer: audio"); - /* call buffer_get_buffer() to make use of the locking mechanism */ - audiobuf_handle = core_alloc_maximum("audiobuf #2", &siz, NULL); - buf = core_get_data(audiobuf_handle); + size_t talkbuf_size; + size_t siz = *buffer_size; buf += talkbuf_size = talkbuf_init(buf); siz -= talkbuf_size; siz -= voicebuf_init(buf + siz); -- 2.11.4.GIT