1
0
Fork 0
forked from len0rd/rockbox

Use buflib for the allocation of voice PCM resources.

Buffers are not allocated and thread is not created until the first
call where voice is required.

Adds a different callback (sync_callback) to buflib so that other
sorts of synchonization are possible, such as briefly locking-out the
PCM callback for a buffer move. It's sort of a messy addition but it
is needed so voice decoding won't have to be stopped when its buffer
is moved.

Change-Id: I4d4d8c35eed5dd15fb7ee7df9323af3d036e92b3
This commit is contained in:
Michael Sevakis 2012-05-02 17:22:28 -04:00
parent 3d3a144cf6
commit da6cebb6b0
13 changed files with 244 additions and 132 deletions

View file

@ -175,7 +175,7 @@ static int buflib_move_callback(int handle, void* current, void* new)
} }
return BUFLIB_CB_OK; return BUFLIB_CB_OK;
} }
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL}; static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
static void load_icons(const char* filename, enum Iconset iconset, static void load_icons(const char* filename, enum Iconset iconset,
enum screen_type screen) enum screen_type screen)

View file

@ -60,7 +60,7 @@ static int buflib_move_callback(int handle, void* current, void* new)
skin_backdrop_show(current_lcd_backdrop[i]); skin_backdrop_show(current_lcd_backdrop[i]);
return BUFLIB_CB_OK; return BUFLIB_CB_OK;
} }
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL}; static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
static bool first_go = true; static bool first_go = true;
void skin_backdrop_init(void) void skin_backdrop_init(void)
{ {

View file

@ -1655,7 +1655,7 @@ static int buflib_move_callback(int handle, void* current, void* new)
return BUFLIB_CB_CANNOT_MOVE; return BUFLIB_CB_CANNOT_MOVE;
return BUFLIB_CB_OK; return BUFLIB_CB_OK;
} }
static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL}; static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL, NULL};
static void lock_handle(int handle) static void lock_handle(int handle)
{ {
currently_loading_handle = handle; currently_loading_handle = handle;

View file

@ -753,11 +753,12 @@ size_t audio_buffer_available(void)
/* Set up the audio buffer for playback /* Set up the audio buffer for playback
* filebuflen must be pre-initialized with the maximum size */ * filebuflen must be pre-initialized with the maximum size */
static void audio_reset_buffer_noalloc(void* filebuf) static void audio_reset_buffer_noalloc(
void *filebuf, enum audio_buffer_state state)
{ {
/* /*
* Layout audio buffer as follows: * Layout audio buffer as follows:
* [[|TALK]|SCRATCH|BUFFERING|PCM|[VOICE|]] * [[|TALK]|SCRATCH|BUFFERING|PCM]
*/ */
/* see audio_get_recording_buffer if this is modified */ /* see audio_get_recording_buffer if this is modified */
@ -770,7 +771,8 @@ static void audio_reset_buffer_noalloc(void* filebuf)
/* Initially set up file buffer as all space available */ /* Initially set up file buffer as all space available */
size_t allocsize; size_t allocsize;
/* Subtract whatever voice needs */ /* Subtract whatever voice needs (we're called when promoting
the state only) */
allocsize = talkbuf_init(filebuf); allocsize = talkbuf_init(filebuf);
allocsize = ALIGN_UP(allocsize, sizeof (intptr_t)); allocsize = ALIGN_UP(allocsize, sizeof (intptr_t));
if (allocsize > filebuflen) if (allocsize > filebuflen)
@ -779,22 +781,14 @@ static void audio_reset_buffer_noalloc(void* filebuf)
filebuf += allocsize; filebuf += allocsize;
filebuflen -= allocsize; filebuflen -= allocsize;
if (talk_voice_required()) if (state == AUDIOBUF_STATE_INITIALIZED)
{ {
/* Need a space for voice PCM output */ /* Subtract whatever the pcm buffer says it used plus the guard
allocsize = voicebuf_init(filebuf + filebuflen); buffer */
allocsize = ALIGN_UP(allocsize, sizeof (intptr_t));
if (allocsize > filebuflen)
goto bufpanic;
filebuflen -= allocsize;
}
/* Subtract whatever the pcm buffer says it used plus the guard buffer */
allocsize = pcmbuf_init(filebuf + filebuflen); allocsize = pcmbuf_init(filebuf + filebuflen);
/* Make sure filebuflen is a pointer sized multiple after adjustment */ /* Make sure filebuflen is a pointer sized multiple after
adjustment */
allocsize = ALIGN_UP(allocsize, sizeof (intptr_t)); allocsize = ALIGN_UP(allocsize, sizeof (intptr_t));
if (allocsize > filebuflen) if (allocsize > filebuflen)
goto bufpanic; goto bufpanic;
@ -811,9 +805,9 @@ static void audio_reset_buffer_noalloc(void* filebuf)
filebuflen -= allocsize; filebuflen -= allocsize;
buffering_reset(filebuf, filebuflen); buffering_reset(filebuf, filebuflen);
}
/* Clear any references to the file buffer */ buffer_state = state;
buffer_state = AUDIOBUF_STATE_INITIALIZED;
#if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE) #if defined(ROCKBOX_HAS_LOGF) && defined(LOGF_ENABLE)
/* Make sure everything adds up - yes, some info is a bit redundant but /* Make sure everything adds up - yes, some info is a bit redundant but
@ -891,11 +885,12 @@ static int shrink_callback(int handle, unsigned hints, void* start, size_t old_s
{ {
case BUFLIB_SHRINK_POS_BACK: case BUFLIB_SHRINK_POS_BACK:
core_shrink(handle, start, size); core_shrink(handle, start, size);
audio_reset_buffer_noalloc(start); audio_reset_buffer_noalloc(start, buffer_state);
break; break;
case BUFLIB_SHRINK_POS_FRONT: case BUFLIB_SHRINK_POS_FRONT:
core_shrink(handle, start + wanted_size, size); core_shrink(handle, start + wanted_size, size);
audio_reset_buffer_noalloc(start + wanted_size); audio_reset_buffer_noalloc(start + wanted_size,
buffer_state);
break; break;
} }
if (playing || play_queued) if (playing || play_queued)
@ -912,7 +907,7 @@ static struct buflib_callbacks ops = {
.shrink_callback = shrink_callback, .shrink_callback = shrink_callback,
}; };
static void audio_reset_buffer(void) static void audio_reset_buffer(enum audio_buffer_state state)
{ {
if (audiobuf_handle > 0) if (audiobuf_handle > 0)
{ {
@ -922,7 +917,7 @@ static void audio_reset_buffer(void)
audiobuf_handle = core_alloc_maximum("audiobuf", &filebuflen, &ops); audiobuf_handle = core_alloc_maximum("audiobuf", &filebuflen, &ops);
unsigned char *filebuf = core_get_data(audiobuf_handle); unsigned char *filebuf = core_get_data(audiobuf_handle);
audio_reset_buffer_noalloc(filebuf); audio_reset_buffer_noalloc(filebuf, state);
} }
/* Set the buffer margin to begin rebuffering when 'seconds' from empty */ /* Set the buffer margin to begin rebuffering when 'seconds' from empty */
@ -2051,7 +2046,7 @@ static int audio_fill_file_buffer(void)
file size shouldn't have changed so we can go straight from file size shouldn't have changed so we can go straight from
AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */ AUDIOBUF_STATE_VOICED_ONLY to AUDIOBUF_STATE_INITIALIZED */
if (buffer_state != AUDIOBUF_STATE_INITIALIZED) if (buffer_state != AUDIOBUF_STATE_INITIALIZED)
audio_reset_buffer(); audio_reset_buffer(AUDIOBUF_STATE_INITIALIZED);
logf("Starting buffer fill"); logf("Starting buffer fill");
@ -3649,14 +3644,9 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size)
logf("get buffer: audio"); logf("get buffer: audio");
/* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or /* Safe to just return this if already AUDIOBUF_STATE_VOICED_ONLY or
still AUDIOBUF_STATE_INITIALIZED */ still AUDIOBUF_STATE_INITIALIZED */
/* Skip talk buffer and move pcm buffer to end to maximize available size_t talkbuf_size = talkbuf_init(buf);
contiguous memory - no audio running means voice will not need the buf += talkbuf_size; /* Skip talk buffer */
swap space */
size_t talkbuf_size;
buf += talkbuf_size = talkbuf_init(buf);
filebuflen -= talkbuf_size; filebuflen -= talkbuf_size;
filebuflen -= voicebuf_init(buf + filebuflen);
buffer_state = AUDIOBUF_STATE_VOICED_ONLY; buffer_state = AUDIOBUF_STATE_VOICED_ONLY;
} }
@ -3673,19 +3663,18 @@ unsigned char * audio_get_recording_buffer(size_t *buffer_size)
} }
#endif /* HAVE_RECORDING */ #endif /* HAVE_RECORDING */
/* Restore audio buffer to a particular state (one more valid than the current /* Restore audio buffer to a particular state (promoting status) */
state) */
bool audio_restore_playback(int type) bool audio_restore_playback(int type)
{ {
switch (type) switch (type)
{ {
case AUDIO_WANT_PLAYBACK: case AUDIO_WANT_PLAYBACK:
if (buffer_state != AUDIOBUF_STATE_INITIALIZED) if (buffer_state != AUDIOBUF_STATE_INITIALIZED)
audio_reset_buffer(); audio_reset_buffer(AUDIOBUF_STATE_INITIALIZED);
return true; return true;
case AUDIO_WANT_VOICE: case AUDIO_WANT_VOICE:
if (buffer_state == AUDIOBUF_STATE_TRASHED) if (buffer_state == AUDIOBUF_STATE_TRASHED)
audio_reset_buffer(); audio_reset_buffer(AUDIOBUF_STATE_VOICED_ONLY);
return true; return true;
default: default:
return false; return false;
@ -3910,24 +3899,18 @@ void audio_init(void)
queue_enable_queue_send(&audio_queue, &audio_queue_sender_list, queue_enable_queue_send(&audio_queue, &audio_queue_sender_list,
audio_thread_id); audio_thread_id);
#ifdef PLAYBACK_VOICE /* Initialize the track buffering system */
voice_thread_init(); track_list_init();
#endif buffering_init();
/* audio_reset_buffer must know the size of voice buffer so init
talk first */
talk_init();
#ifdef HAVE_CROSSFADE #ifdef HAVE_CROSSFADE
/* Set crossfade setting for next buffer init which should be about... */ /* Set crossfade setting for next buffer init which should be about... */
pcmbuf_request_crossfade_enable(global_settings.crossfade); pcmbuf_request_crossfade_enable(global_settings.crossfade);
#endif #endif
/* Initialize the buffering system */ /* ...now...audio_reset_buffer must know the size of voicefile buffer so
track_list_init(); init talk first which will init the buffers */
buffering_init(); talk_init();
/* ...now! Set up the buffers */
audio_reset_buffer();
/* Probably safe to say */ /* Probably safe to say */
audio_is_initialized = true; audio_is_initialized = true;

View file

@ -31,7 +31,11 @@
#include "kernel.h" #include "kernel.h"
#include "settings.h" #include "settings.h"
#include "settings_list.h" #include "settings_list.h"
#if CONFIG_CODEC == SWCODEC
#include "voice_thread.h"
#else
#include "mp3_playback.h" #include "mp3_playback.h"
#endif
#include "audio.h" #include "audio.h"
#include "lang.h" #include "lang.h"
#include "talk.h" #include "talk.h"
@ -43,7 +47,6 @@
#include "plugin.h" /* plugin_get_buffer() */ #include "plugin.h" /* plugin_get_buffer() */
#include "debug.h" #include "debug.h"
/* Memory layout varies between targets because the /* Memory layout varies between targets because the
Archos (MASCODEC) devices cannot mix voice and audio playback Archos (MASCODEC) devices cannot mix voice and audio playback
@ -589,7 +592,6 @@ static void queue_clip(unsigned char* buf, long size, bool enqueue)
return; return;
} }
static void alloc_thumbnail_buf(void) static void alloc_thumbnail_buf(void)
{ {
/* use the audio buffer now, need to release before loading a voice */ /* use the audio buffer now, need to release before loading a voice */
@ -614,9 +616,23 @@ static void reset_state(void)
#endif #endif
p_silence = NULL; /* pause clip not accessible */ p_silence = NULL; /* pause clip not accessible */
voicebuf = NULL; voicebuf = NULL; /* voice buffer is gone */
} }
#if CONFIG_CODEC == SWCODEC
static bool restore_state(void)
{
if (!voicebuf)
{
size_t size;
audio_restore_playback(AUDIO_WANT_VOICE);
voicebuf = audio_get_buffer(false, &size);
}
return !!voicebuf;
}
#endif /* CONFIG_CODEC == SWCODEC */
/***************** Public implementation *****************/ /***************** Public implementation *****************/
@ -693,9 +709,14 @@ void talk_init(void)
voicefile_size = 0; voicefile_size = 0;
} }
alloc_thumbnail_buf();
close(filehandle); /* close again, this was just to detect presence */ close(filehandle); /* close again, this was just to detect presence */
filehandle = -1; filehandle = -1;
#if CONFIG_CODEC == SWCODEC
/* Safe to init voice playback engine now since we now know if talk is
required or not */
voice_thread_init();
#endif
} }
#if CONFIG_CODEC == SWCODEC #if CONFIG_CODEC == SWCODEC
@ -709,7 +730,7 @@ bool talk_voice_required(void)
#endif #endif
/* return size of voice file */ /* return size of voice file */
static int talk_get_buffer(void) static size_t talk_get_buffer_size(void)
{ {
#if CONFIG_CODEC == SWCODEC #if CONFIG_CODEC == SWCODEC
return voicefile_size + MAX_THUMBNAIL_BUFSIZE; return voicefile_size + MAX_THUMBNAIL_BUFSIZE;
@ -726,10 +747,11 @@ size_t talkbuf_init(char *bufstart)
if (changed) /* must reload voice file */ if (changed) /* must reload voice file */
reset_state(); reset_state();
if (bufstart) if (bufstart)
voicebuf = bufstart; voicebuf = bufstart;
return talk_get_buffer(); return talk_get_buffer_size();
} }
/* somebody else claims the mp3 buffer, e.g. for regular play/record */ /* somebody else claims the mp3 buffer, e.g. for regular play/record */
@ -748,29 +770,27 @@ void talk_buffer_steal(void)
reset_state(); reset_state();
} }
/* play a voice ID from voicefile */ /* play a voice ID from voicefile */
int talk_id(int32_t id, bool enqueue) int talk_id(int32_t id, bool enqueue)
{ {
long clipsize; long clipsize;
int temp = talk_get_buffer();
unsigned char* clipbuf; unsigned char* clipbuf;
int32_t unit; int32_t unit;
int decimals; int decimals;
if (talk_temp_disable_count > 0) if (talk_temp_disable_count > 0)
return -1; /* talking has been disabled */ return -1; /* talking has been disabled */
#if CONFIG_CODEC != SWCODEC #if CONFIG_CODEC == SWCODEC
/* If talk buffer was stolen, it must be restored for voicefile's sake */
if (!restore_state())
return -1; /* cannot get any space */
#else
if (audio_status()) /* busy, buffer in use */ if (audio_status()) /* busy, buffer in use */
return -1; return -1;
#endif #endif
/* try to get audio buffer until talkbuf_init() is called */ if (p_voicefile == NULL && has_voicefile) /* reload needed? */
if (!voicebuf) load_voicefile(false, voicebuf, talk_get_buffer_size());
voicebuf = audio_get_buffer(true, (size_t*)&temp);
if (p_voicefile == NULL && has_voicefile)
load_voicefile(false, voicebuf, MIN(talk_get_buffer(),temp)); /* reload needed */
if (p_voicefile == NULL) /* still no voices? */ if (p_voicefile == NULL) /* still no voices? */
return -1; return -1;
@ -842,7 +862,11 @@ static int _talk_file(const char* filename,
if (talk_temp_disable_count > 0) if (talk_temp_disable_count > 0)
return -1; /* talking has been disabled */ return -1; /* talking has been disabled */
#if CONFIG_CODEC != SWCODEC #if CONFIG_CODEC == SWCODEC
/* If talk buffer was stolen, it must be restored for thumbnail's sake */
if (!restore_state())
return -1; /* cannot get any space */
#else
if (audio_status()) /* busy, buffer in use */ if (audio_status()) /* busy, buffer in use */
return -1; return -1;
#endif #endif

View file

@ -20,6 +20,7 @@
****************************************************************************/ ****************************************************************************/
#include <sys/types.h> #include <sys/types.h>
#include "system.h" #include "system.h"
#include "core_alloc.h"
#include "thread.h" #include "thread.h"
#include "voice_thread.h" #include "voice_thread.h"
#include "talk.h" #include "talk.h"
@ -31,6 +32,10 @@
#include "pcm_mixer.h" #include "pcm_mixer.h"
#include "codecs/libspeex/speex/speex.h" #include "codecs/libspeex/speex/speex.h"
/* Default number of native-frequency PCM frames to queue - adjust as
necessary per-target */
#define VOICE_FRAMES 4
/* Define any of these as "1" and uncomment the LOGF_ENABLE line to log /* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
regular and/or timeout messages */ regular and/or timeout messages */
#define VOICE_LOGQUEUES 0 #define VOICE_LOGQUEUES 0
@ -80,22 +85,10 @@ static struct event_queue voice_queue SHAREDBSS_ATTR;
static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR; static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
static int quiet_counter SHAREDDATA_ATTR = 0; static int quiet_counter SHAREDDATA_ATTR = 0;
/* Buffer for decoded samples */
static spx_int16_t voice_output_buf[VOICE_FRAME_COUNT] MEM_ALIGN_ATTR;
#define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_COUNT + \ #define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_COUNT + \
VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE) VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
#define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t)) #define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t))
/* Default number of native-frequency PCM frames to queue - adjust as
necessary per-target */
#define VOICE_FRAMES 3
/* Might have lookahead and be skipping samples, so size is needed */
static size_t voicebuf_sizes[VOICE_FRAMES];
static int16_t (* voicebuf)[2*VOICE_PCM_FRAME_COUNT];
static unsigned int cur_buf_in, cur_buf_out;
/* Voice processing states */ /* Voice processing states */
enum voice_state enum voice_state
{ {
@ -135,6 +128,7 @@ struct voice_thread_data
struct voice_info vi; /* Copy of clip data */ struct voice_info vi; /* Copy of clip data */
int lookahead; /* Number of samples to drop at start of clip */ int lookahead; /* Number of samples to drop at start of clip */
struct dsp_buffer src; /* Speex output buffer/input to DSP */ struct dsp_buffer src; /* Speex output buffer/input to DSP */
struct dsp_buffer *dst; /* Pointer to DSP output buffer for PCM */
}; };
/* Functions called in their repective state that return the next state to /* Functions called in their repective state that return the next state to
@ -143,10 +137,62 @@ static enum voice_state voice_message(struct voice_thread_data *td);
static enum voice_state voice_decode(struct voice_thread_data *td); static enum voice_state voice_decode(struct voice_thread_data *td);
static enum voice_state voice_buffer_insert(struct voice_thread_data *td); static enum voice_state voice_buffer_insert(struct voice_thread_data *td);
/* Might have lookahead and be skipping samples, so size is needed */
static struct voice_buf
{
/* Buffer for decoded samples */
spx_int16_t spx_outbuf[VOICE_FRAME_COUNT];
/* Queue frame indexes */
unsigned int frame_in, frame_out;
/* For PCM pointer adjustment */
struct voice_thread_data *td;
/* Buffers for mixing voice */
struct voice_pcm_frame
{
size_t size;
int16_t pcm[2*VOICE_PCM_FRAME_COUNT];
} frames[VOICE_FRAMES];
} *voice_buf = NULL;
static int voice_buf_hid = 0;
static int move_callback(int handle, void *current, void *new)
{
/* Have to adjust the pointers that point into things in voice_buf */
off_t diff = new - current;
struct voice_thread_data *td = voice_buf->td;
td->src.p32[0] = SKIPBYTES(td->src.p32[0], diff);
td->src.p32[1] = SKIPBYTES(td->src.p32[1], diff);
if (td->dst != NULL) /* Only when calling dsp_process */
td->dst->p16out = SKIPBYTES(td->dst->p16out, diff);
mixer_adjust_channel_address(PCM_MIXER_CHAN_VOICE, diff);
voice_buf = new;
return BUFLIB_CB_OK;
(void)handle;
};
static void sync_callback(int handle, bool sync_on)
{
/* A move must not allow PCM to access the channel */
if (sync_on)
pcm_play_lock();
else
pcm_play_unlock();
(void)handle;
}
static struct buflib_callbacks ops =
{
.move_callback = move_callback,
.sync_callback = sync_callback,
};
/* Number of frames in queue */ /* Number of frames in queue */
static inline int voice_unplayed_frames(void) static inline int voice_unplayed_frames(void)
{ {
return cur_buf_in - cur_buf_out; return voice_buf->frame_in - voice_buf->frame_out;
} }
/* Mixer channel callback */ /* Mixer channel callback */
@ -155,10 +201,11 @@ static void voice_pcm_callback(const void **start, size_t *size)
if (voice_unplayed_frames() == 0) if (voice_unplayed_frames() == 0)
return; /* Done! */ return; /* Done! */
unsigned int i = ++cur_buf_out % VOICE_FRAMES; struct voice_pcm_frame *frame =
&voice_buf->frames[++voice_buf->frame_out % VOICE_FRAMES];
*start = voicebuf[i]; *start = frame->pcm;
*size = voicebuf_sizes[i]; *size = frame->size;
} }
/* Start playback of voice channel if not already playing */ /* Start playback of voice channel if not already playing */
@ -168,16 +215,18 @@ static void voice_start_playback(void)
voice_unplayed_frames() <= 0) voice_unplayed_frames() <= 0)
return; return;
unsigned int i = cur_buf_out % VOICE_FRAMES; struct voice_pcm_frame *frame =
&voice_buf->frames[voice_buf->frame_out % VOICE_FRAMES];
mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback, mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback,
voicebuf[i], voicebuf_sizes[i]); frame->pcm, frame->size);
} }
/* Stop the voice channel */ /* Stop the voice channel */
static void voice_stop_playback(void) static void voice_stop_playback(void)
{ {
mixer_channel_stop(PCM_MIXER_CHAN_VOICE); mixer_channel_stop(PCM_MIXER_CHAN_VOICE);
cur_buf_in = cur_buf_out = 0; voice_buf->frame_in = voice_buf->frame_out = 0;
} }
/* Grab a free PCM frame */ /* Grab a free PCM frame */
@ -190,7 +239,7 @@ static int16_t * voice_buf_get(void)
return NULL; return NULL;
} }
return voicebuf[cur_buf_in % VOICE_FRAMES]; return voice_buf->frames[voice_buf->frame_in % VOICE_FRAMES].pcm;
} }
/* Commit a frame returned by voice_buf_get and set the actual size */ /* Commit a frame returned by voice_buf_get and set the actual size */
@ -198,7 +247,7 @@ static void voice_buf_commit(int count)
{ {
if (count > 0) if (count > 0)
{ {
voicebuf_sizes[cur_buf_in++ % VOICE_FRAMES] = voice_buf->frames[voice_buf->frame_in++ % VOICE_FRAMES].size =
count * 2 * sizeof (int16_t); count * 2 * sizeof (int16_t);
} }
} }
@ -207,7 +256,7 @@ static void voice_buf_commit(int count)
void mp3_play_data(const void *start, size_t size, void mp3_play_data(const void *start, size_t size,
mp3_play_callback_t get_more) mp3_play_callback_t get_more)
{ {
if (get_more != NULL && start != NULL && size > 0) if (voice_thread_id && start && size && get_more)
{ {
struct voice_info voice_clip = struct voice_info voice_clip =
{ {
@ -223,10 +272,13 @@ void mp3_play_data(const void *start, size_t size,
/* Stop current voice clip from playing */ /* Stop current voice clip from playing */
void mp3_play_stop(void) void mp3_play_stop(void)
{
if (voice_thread_id != 0)
{ {
LOGFQUEUE("mp3 >| voice Q_VOICE_STOP"); LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
queue_send(&voice_queue, Q_VOICE_STOP, 0); queue_send(&voice_queue, Q_VOICE_STOP, 0);
} }
}
void mp3_play_pause(bool play) void mp3_play_pause(bool play)
{ {
@ -270,6 +322,9 @@ static void voice_data_init(struct voice_thread_data *td)
dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO); dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY); mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY);
voice_buf->td = td;
td->dst = NULL;
} }
/* Voice thread message processing */ /* Voice thread message processing */
@ -300,9 +355,6 @@ static enum voice_state voice_message(struct voice_thread_data *td)
/* Copy the clip info */ /* Copy the clip info */
td->vi = *(struct voice_info *)td->ev.data; td->vi = *(struct voice_info *)td->ev.data;
/* Be sure audio buffer is initialized */
audio_restore_playback(AUDIO_WANT_VOICE);
/* We need nothing more from the sending thread - let it run */ /* We need nothing more from the sending thread - let it run */
queue_reply(&voice_queue, 1); queue_reply(&voice_queue, 1);
@ -356,7 +408,7 @@ static enum voice_state voice_decode(struct voice_thread_data *td)
return VOICE_STATE_MESSAGE; return VOICE_STATE_MESSAGE;
/* Decode the data */ /* Decode the data */
if (speex_decode_int(td->st, &td->bits, voice_output_buf) < 0) if (speex_decode_int(td->st, &td->bits, voice_buf->spx_outbuf) < 0)
{ {
/* End of stream or error - get next clip */ /* End of stream or error - get next clip */
td->vi.size = 0; td->vi.size = 0;
@ -386,7 +438,7 @@ static enum voice_state voice_decode(struct voice_thread_data *td)
/* Output the decoded frame */ /* Output the decoded frame */
td->src.remcount = VOICE_FRAME_COUNT - td->lookahead; td->src.remcount = VOICE_FRAME_COUNT - td->lookahead;
td->src.pin[0] = &voice_output_buf[td->lookahead]; td->src.pin[0] = &voice_buf->spx_outbuf[td->lookahead];
td->src.pin[1] = NULL; td->src.pin[1] = NULL;
td->src.proc_mask = 0; td->src.proc_mask = 0;
@ -412,7 +464,9 @@ static enum voice_state voice_buffer_insert(struct voice_thread_data *td)
dst.remcount = 0; dst.remcount = 0;
dst.bufcount = VOICE_PCM_FRAME_COUNT; dst.bufcount = VOICE_PCM_FRAME_COUNT;
td->dst = &dst;
dsp_process(td->dsp, &td->src, &dst); dsp_process(td->dsp, &td->src, &dst);
td->dst = NULL;
voice_buf_commit(dst.remcount); voice_buf_commit(dst.remcount);
@ -451,9 +505,36 @@ static void NORETURN_ATTR voice_thread(void)
} }
} }
/* Initialize all synchronization objects create the thread */ /* Initialize buffers, all synchronization objects and create the thread */
void voice_thread_init(void) void voice_thread_init(void)
{ {
if (voice_thread_id != 0)
return; /* Already did an init and succeeded at it */
if (!talk_voice_required())
{
logf("No voice required");
return;
}
voice_buf_hid = core_alloc_ex("voice buf", sizeof (*voice_buf), &ops);
if (voice_buf_hid <= 0)
{
logf("voice: core_alloc_ex failed");
return;
}
voice_buf = core_get_data(voice_buf_hid);
if (voice_buf == NULL)
{
logf("voice: core_get_data failed");
core_free(voice_buf_hid);
voice_buf_hid = 0;
return;
}
logf("Starting voice thread"); logf("Starting voice thread");
queue_init(&voice_queue, false); queue_init(&voice_queue, false);
@ -469,18 +550,12 @@ void voice_thread_init(void)
/* Set the voice thread priority */ /* Set the voice thread priority */
void voice_thread_set_priority(int priority) void voice_thread_set_priority(int priority)
{ {
if (voice_thread_id == 0)
return;
if (priority > PRIORITY_VOICE) if (priority > PRIORITY_VOICE)
priority = PRIORITY_VOICE; priority = PRIORITY_VOICE;
thread_set_priority(voice_thread_id, priority); thread_set_priority(voice_thread_id, priority);
} }
#endif #endif
/* Initialize voice PCM buffer and return size, allocated from the end */
size_t voicebuf_init(void *bufend)
{
size_t size = VOICE_FRAMES * sizeof (voicebuf[0]);
cur_buf_out = cur_buf_in = 0;
voicebuf = bufend - size;
return size;
}

View file

@ -23,7 +23,10 @@
#include "config.h" #include "config.h"
#ifndef MP3_PLAY_CALLBACK_DEFINED
#define MP3_PLAY_CALLBACK_DEFINED
typedef void (*mp3_play_callback_t)(const void **start, size_t *size); typedef void (*mp3_play_callback_t)(const void **start, size_t *size);
#endif
void mp3_play_data(const void *start, size_t size, void mp3_play_data(const void *start, size_t size,
mp3_play_callback_t get_more); mp3_play_callback_t get_more);
@ -39,6 +42,4 @@ void voice_thread_init(void) INIT_ATTR;
void voice_thread_set_priority(int priority); void voice_thread_set_priority(int priority);
#endif #endif
size_t voicebuf_init(void *bufend);
#endif /* VOICE_THREAD_H */ #endif /* VOICE_THREAD_H */

View file

@ -210,23 +210,27 @@ move_block(struct buflib_context* ctx, union buflib_data* block, int shift)
/* disable IRQs to make accessing the buffer from interrupt context safe. */ /* disable IRQs to make accessing the buffer from interrupt context safe. */
/* protect the move callback, as a cached global pointer might be updated /* protect the move callback, as a cached global pointer might be updated
* in it. and protect "tmp->alloc = new_start" for buflib_get_data() */ * in it. and protect "tmp->alloc = new_start" for buflib_get_data() */
disable_irq();
/* call the callback before moving */ /* call the callback before moving */
if (ops) if (ops && ops->sync_callback)
{ ops->sync_callback(handle, true);
if (ops->move_callback(handle, tmp->alloc, new_start) else
== BUFLIB_CB_CANNOT_MOVE) disable_irq();
{
enable_irq();
return false;
}
}
bool retval = false;
if (!ops || ops->move_callback(handle, tmp->alloc, new_start)
!= BUFLIB_CB_CANNOT_MOVE)
{
tmp->alloc = new_start; /* update handle table */ tmp->alloc = new_start; /* update handle table */
memmove(new_block, block, block->val * sizeof(union buflib_data)); memmove(new_block, block, block->val * sizeof(union buflib_data));
retval = true;
}
if (ops && ops->sync_callback)
ops->sync_callback(handle, false);
else
enable_irq(); enable_irq();
return true;
return retval;
} }
/* Compact allocations and handle table, adjusting handle pointers as needed. /* Compact allocations and handle table, adjusting handle pointers as needed.

View file

@ -27,7 +27,10 @@
#include <stdbool.h> #include <stdbool.h>
/* callback fn */ /* callback fn */
#ifndef MP3_PLAY_CALLBACK_DEFINED
#define MP3_PLAY_CALLBACK_DEFINED
typedef void (*mp3_play_callback_t)(const void **start, size_t* size); typedef void (*mp3_play_callback_t)(const void **start, size_t* size);
#endif
/* functions formerly in mpeg.c */ /* functions formerly in mpeg.c */
void mp3_init(int volume, int bass, int treble, int balance, int loudness, void mp3_init(int volume, int bass, int treble, int balance, int loudness,

View file

@ -22,6 +22,8 @@
#ifndef PCM_MIXER_H #ifndef PCM_MIXER_H
#define PCM_MIXER_H #define PCM_MIXER_H
#include <sys/types.h>
/** Simple config **/ /** Simple config **/
/* Length of PCM frames (always) */ /* Length of PCM frames (always) */
@ -111,6 +113,10 @@ const void * mixer_channel_get_buffer(enum pcm_mixer_channel channel, int *count
void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel, void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
int *left, int *right); int *left, int *right);
/* Adjust channel pointer by a given offset to support movable buffers */
void mixer_adjust_channel_address(enum pcm_mixer_channel channel,
off_t offset);
/* Stop ALL channels and PCM and reset state */ /* Stop ALL channels and PCM and reset state */
void mixer_reset(void); void mixer_reset(void);

View file

@ -141,7 +141,7 @@ void font_lock(int font_id, bool lock)
lock_font_handle(buflib_allocations[font_id], lock); lock_font_handle(buflib_allocations[font_id], lock);
} }
static struct buflib_callbacks buflibops = {buflibmove_callback, NULL }; static struct buflib_callbacks buflibops = {buflibmove_callback, NULL, NULL };
static inline struct font *pf_from_handle(int handle) static inline struct font *pf_from_handle(int handle)
{ {

View file

@ -103,6 +103,12 @@ struct buflib_callbacks {
* at least shrinkable * at least shrinkable
*/ */
int (*shrink_callback)(int handle, unsigned hints, void* start, size_t old_size); int (*shrink_callback)(int handle, unsigned hints, void* start, size_t old_size);
/**
* This is called when special steps must be taken for synchronization
* both before the move_callback is called and after the data has been
* moved.
*/
void (*sync_callback)(int handle, bool sync_on);
}; };
#define BUFLIB_SHRINK_POS_MASK ((1<<0|1<<1)<<30) #define BUFLIB_SHRINK_POS_MASK ((1<<0|1<<1)<<30)

View file

@ -406,6 +406,16 @@ void mixer_channel_calculate_peaks(enum pcm_mixer_channel channel,
*right = peaks->val[1]; *right = peaks->val[1];
} }
/* Adjust channel pointer by a given offset to support movable buffers */
void mixer_adjust_channel_address(enum pcm_mixer_channel channel,
off_t offset)
{
pcm_play_lock();
/* Makes no difference if it's stopped */
channels[channel].start += offset;
pcm_play_unlock();
}
/* Stop ALL channels and PCM and reset state */ /* Stop ALL channels and PCM and reset state */
void mixer_reset(void) void mixer_reset(void)
{ {