2007-11-18 17:12:19 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Michael Sevakis
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-11-18 17:12:19 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2010-05-06 21:04:40 +00:00
|
|
|
#include <sys/types.h>
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "voice_thread.h"
|
|
|
|
#include "talk.h"
|
|
|
|
#include "dsp.h"
|
2008-03-29 14:09:14 +00:00
|
|
|
#include "audio.h"
|
2008-03-28 11:24:24 +00:00
|
|
|
#include "playback.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "pcmbuf.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
#include "pcm.h"
|
|
|
|
#include "pcm_mixer.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "codecs/libspeex/speex/speex.h"
|
|
|
|
|
2010-05-04 14:43:01 +00:00
|
|
|
/* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
|
|
|
|
regular and/or timeout messages */
|
2007-11-18 17:12:19 +00:00
|
|
|
#define VOICE_LOGQUEUES 0
|
|
|
|
#define VOICE_LOGQUEUES_SYS_TIMEOUT 0
|
|
|
|
|
2010-05-04 14:43:01 +00:00
|
|
|
/*#define LOGF_ENABLE*/
|
|
|
|
#include "logf.h"
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
#if VOICE_LOGQUEUES
|
|
|
|
#define LOGFQUEUE logf
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VOICE_LOGQUEUES_SYS_TIMEOUT
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT logf
|
|
|
|
#else
|
|
|
|
#define LOGFQUEUE_SYS_TIMEOUT(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IBSS_ATTR_VOICE_STACK
|
|
|
|
#define IBSS_ATTR_VOICE_STACK IBSS_ATTR
|
|
|
|
#endif
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Minimum priority needs to be a bit elevated since voice has fairly low
|
|
|
|
latency */
|
|
|
|
#define PRIORITY_VOICE (PRIORITY_PLAYBACK-4)
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
#define VOICE_FRAME_SIZE 320 /* Samples / frame */
|
|
|
|
#define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
|
|
|
|
#define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
|
|
|
|
|
|
|
|
/* Voice thread variables */
|
2008-12-10 08:57:10 +00:00
|
|
|
static unsigned int voice_thread_id = 0;
|
2011-06-29 06:37:04 +00:00
|
|
|
#ifdef CPU_COLDFIRE
|
|
|
|
/* ISR uses any available stack - need a bit more room */
|
|
|
|
#define VOICE_STACK_EXTRA 0x400
|
|
|
|
#else
|
|
|
|
#define VOICE_STACK_EXTRA 0x3c0
|
|
|
|
#endif
|
|
|
|
static long voice_stack[(DEFAULT_STACK_SIZE + VOICE_STACK_EXTRA)/sizeof(long)]
|
|
|
|
IBSS_ATTR_VOICE_STACK;
|
2007-11-18 17:12:19 +00:00
|
|
|
static const char voice_thread_name[] = "voice";
|
|
|
|
|
|
|
|
/* Voice thread synchronization objects */
|
2008-04-06 04:34:57 +00:00
|
|
|
static struct event_queue voice_queue SHAREDBSS_ATTR;
|
|
|
|
static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
|
2008-10-23 13:13:00 +00:00
|
|
|
static bool voice_done SHAREDDATA_ATTR = true;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
/* Buffer for decoded samples */
|
|
|
|
static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
#define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_SIZE + \
|
|
|
|
VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
|
|
|
|
#define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*4)
|
|
|
|
|
|
|
|
/* 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 uint32_t (* voicebuf)[VOICE_PCM_FRAME_COUNT];
|
|
|
|
static unsigned int cur_buf_in, cur_buf_out;
|
|
|
|
|
|
|
|
/* A delay to not bring audio back to normal level too soon */
|
|
|
|
#define QUIET_COUNT 3
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
enum voice_thread_states
|
|
|
|
{
|
|
|
|
TSTATE_STOPPED = 0, /* Voice thread is stopped and awaiting commands */
|
|
|
|
TSTATE_DECODE, /* Voice is decoding a clip */
|
|
|
|
TSTATE_BUFFER_INSERT, /* Voice is sending decoded audio to PCM */
|
|
|
|
};
|
|
|
|
|
|
|
|
enum voice_thread_messages
|
|
|
|
{
|
|
|
|
Q_VOICE_NULL = 0, /* A message for thread sync - no effect on state */
|
|
|
|
Q_VOICE_PLAY, /* Play a clip */
|
|
|
|
Q_VOICE_STOP, /* Stop current clip */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Structure to store clip data callback info */
|
|
|
|
struct voice_info
|
|
|
|
{
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_callback_type get_more; /* Callback to get more clips */
|
2007-11-18 17:12:19 +00:00
|
|
|
unsigned char *start; /* Start of clip */
|
2007-11-18 17:34:33 +00:00
|
|
|
size_t size; /* Size of clip */
|
2007-11-18 17:12:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Private thread data for its current state that must be passed to its
|
|
|
|
* internal functions */
|
|
|
|
struct voice_thread_data
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
volatile int state; /* Thread state (TSTATE_*) */
|
2007-11-18 17:12:19 +00:00
|
|
|
struct queue_event ev; /* Last queue event pulled from queue */
|
|
|
|
void *st; /* Decoder instance */
|
|
|
|
SpeexBits bits; /* Bit cursor */
|
|
|
|
struct dsp_config *dsp; /* DSP used for voice output */
|
|
|
|
struct voice_info vi; /* Copy of clip data */
|
|
|
|
const char *src[2]; /* Current output buffer pointers */
|
|
|
|
int lookahead; /* Number of samples to drop at start of clip */
|
|
|
|
int count; /* Count of samples remaining to send to PCM */
|
2011-06-29 06:37:04 +00:00
|
|
|
int quiet_counter; /* Countdown until audio goes back to normal */
|
2007-11-18 17:12:19 +00:00
|
|
|
};
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Number of frames in queue */
|
|
|
|
static inline int voice_unplayed_frames(void)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
return cur_buf_in - cur_buf_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mixer channel callback */
|
|
|
|
static void voice_pcm_callback(unsigned char **start, size_t *size)
|
|
|
|
{
|
|
|
|
if (voice_unplayed_frames() == 0)
|
|
|
|
return; /* Done! */
|
|
|
|
|
|
|
|
unsigned int i = ++cur_buf_out % VOICE_FRAMES;
|
|
|
|
|
|
|
|
*start = (unsigned char *)voicebuf[i];
|
|
|
|
*size = voicebuf_sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start playback of voice channel if not already playing */
|
|
|
|
static void voice_start_playback(void)
|
|
|
|
{
|
|
|
|
if (mixer_channel_status(PCM_MIXER_CHAN_VOICE) != CHANNEL_STOPPED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned int i = cur_buf_out % VOICE_FRAMES;
|
|
|
|
mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback,
|
|
|
|
(unsigned char *)voicebuf[i], voicebuf_sizes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop the voice channel */
|
|
|
|
static void voice_stop_playback(void)
|
|
|
|
{
|
|
|
|
mixer_channel_stop(PCM_MIXER_CHAN_VOICE);
|
|
|
|
cur_buf_in = cur_buf_out = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab a free PCM frame */
|
|
|
|
static uint32_t * voice_buf_get(void)
|
|
|
|
{
|
|
|
|
if (voice_unplayed_frames() >= VOICE_FRAMES)
|
|
|
|
{
|
|
|
|
/* Full */
|
|
|
|
voice_start_playback();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return voicebuf[cur_buf_in % VOICE_FRAMES];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Commit a frame returned by voice_buf_get and set the actual size */
|
|
|
|
static void voice_buf_commit(size_t size)
|
|
|
|
{
|
|
|
|
voicebuf_sizes[cur_buf_in++ % VOICE_FRAMES] = size;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop any current clip and start playing a new one */
|
|
|
|
void mp3_play_data(const unsigned char* start, int size,
|
2010-05-24 16:42:32 +00:00
|
|
|
pcm_play_callback_type get_more)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2007-11-18 17:34:33 +00:00
|
|
|
if (get_more != NULL && start != NULL && (ssize_t)size > 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
struct voice_info voice_clip =
|
|
|
|
{
|
|
|
|
.get_more = get_more,
|
|
|
|
.start = (unsigned char *)start,
|
|
|
|
.size = size,
|
|
|
|
};
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
|
|
|
|
queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop current voice clip from playing */
|
|
|
|
void mp3_play_stop(void)
|
|
|
|
{
|
2008-07-15 14:06:11 +00:00
|
|
|
if(!audio_is_thread_ready())
|
|
|
|
return;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
|
|
|
|
queue_send(&voice_queue, Q_VOICE_STOP, 0);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp3_play_pause(bool play)
|
|
|
|
{
|
|
|
|
/* a dummy */
|
|
|
|
(void)play;
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Tell if voice is still in a playing state */
|
2007-11-18 17:12:19 +00:00
|
|
|
bool mp3_is_playing(void)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
return !voice_done;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is meant to be used by the buffer request functions to
|
|
|
|
ensure the codec is no longer active */
|
|
|
|
void voice_stop(void)
|
|
|
|
{
|
|
|
|
/* Unqueue all future clips */
|
|
|
|
talk_force_shutup();
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
/* Wait for voice to finish speaking. */
|
|
|
|
void voice_wait(void)
|
|
|
|
{
|
|
|
|
/* NOTE: One problem here is that we can't tell if another thread started a
|
|
|
|
* new clip by the time we wait. This should be resolvable if conditions
|
|
|
|
* ever require knowing the very clip you requested has finished. */
|
2008-10-23 13:13:00 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
while (!voice_done)
|
2007-11-24 14:21:04 +00:00
|
|
|
sleep(1);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize voice thread data that must be valid upon starting and the
|
|
|
|
* setup the DSP parameters */
|
|
|
|
static void voice_data_init(struct voice_thread_data *td)
|
|
|
|
{
|
|
|
|
td->state = TSTATE_STOPPED;
|
|
|
|
td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
|
|
|
|
CODEC_IDX_VOICE);
|
|
|
|
|
|
|
|
dsp_configure(td->dsp, DSP_RESET, 0);
|
|
|
|
dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
|
|
|
|
dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
|
|
|
|
dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY);
|
|
|
|
td->quiet_counter = 0;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Voice thread message processing */
|
|
|
|
static void voice_message(struct voice_thread_data *td)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
switch (td->ev.id)
|
|
|
|
{
|
|
|
|
case Q_VOICE_PLAY:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_PLAY");
|
2008-10-23 13:13:00 +00:00
|
|
|
voice_done = false;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
/* Copy the clip info */
|
|
|
|
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 */
|
|
|
|
queue_reply(&voice_queue, 1);
|
|
|
|
|
|
|
|
if (td->state == TSTATE_STOPPED)
|
|
|
|
{
|
|
|
|
/* Boost CPU now */
|
|
|
|
trigger_cpu_boost();
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
else
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Stop any clip still playing */
|
|
|
|
voice_stop_playback();
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Make audio play more softly and set delay to return to normal
|
|
|
|
playback level */
|
|
|
|
pcmbuf_soft_mode(true);
|
|
|
|
td->quiet_counter = QUIET_COUNT;
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
/* Clean-start the decoder */
|
|
|
|
td->st = speex_decoder_init(&speex_wb_mode);
|
|
|
|
|
|
|
|
/* Make bit buffer use our own buffer */
|
|
|
|
speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
|
|
|
|
speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
|
|
|
|
|
|
|
|
td->state = TSTATE_DECODE;
|
|
|
|
return;
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
case SYS_TIMEOUT:
|
|
|
|
if (voice_unplayed_frames())
|
|
|
|
{
|
|
|
|
/* Waiting for PCM to finish */
|
|
|
|
break;
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Drop through and stop the first time after clip runs out */
|
|
|
|
if (td->quiet_counter-- != QUIET_COUNT)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (td->quiet_counter <= 0)
|
|
|
|
pcmbuf_soft_mode(false);
|
|
|
|
|
|
|
|
break;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Fall-through */
|
|
|
|
case Q_VOICE_STOP:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_STOP");
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
td->state = TSTATE_STOPPED;
|
2008-10-23 13:13:00 +00:00
|
|
|
voice_done = true;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
cancel_cpu_boost();
|
|
|
|
voice_stop_playback();
|
|
|
|
break;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Default messages get a reply and thread continues with no
|
|
|
|
* state transition */
|
|
|
|
LOGFQUEUE("voice < default");
|
|
|
|
|
|
|
|
if (td->state == TSTATE_STOPPED)
|
2011-06-29 06:37:04 +00:00
|
|
|
break; /* Not in (active) playback state */
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
queue_reply(&voice_queue, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (td->quiet_counter > 0)
|
|
|
|
queue_wait_w_tmo(&voice_queue, &td->ev, HZ/10);
|
|
|
|
else
|
|
|
|
queue_wait(&voice_queue, &td->ev);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Voice thread entrypoint */
|
2011-06-29 06:37:04 +00:00
|
|
|
static void NORETURN_ATTR voice_thread(void)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
|
|
|
struct voice_thread_data td;
|
2011-06-29 06:37:04 +00:00
|
|
|
char *dest;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
voice_data_init(&td);
|
2008-07-15 14:06:11 +00:00
|
|
|
|
|
|
|
/* audio thread will only set this once after it finished the final
|
|
|
|
* audio hardware init so this little construct is safe - even
|
|
|
|
* cross-core. */
|
|
|
|
while (!audio_is_thread_ready())
|
|
|
|
sleep(0);
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
goto message_wait;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
td.state = TSTATE_DECODE;
|
|
|
|
|
|
|
|
if (!queue_empty(&voice_queue))
|
|
|
|
{
|
|
|
|
message_wait:
|
|
|
|
queue_wait(&voice_queue, &td.ev);
|
|
|
|
|
|
|
|
message_process:
|
|
|
|
voice_message(&td);
|
|
|
|
|
|
|
|
/* Branch to initial start point or branch back to previous
|
|
|
|
* operation if interrupted by a message */
|
|
|
|
switch (td.state)
|
|
|
|
{
|
|
|
|
case TSTATE_DECODE: goto voice_decode;
|
|
|
|
case TSTATE_BUFFER_INSERT: goto buffer_insert;
|
|
|
|
default: goto message_wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
voice_decode:
|
2007-11-21 03:09:54 +00:00
|
|
|
/* Decode the data */
|
|
|
|
if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2007-11-21 03:09:54 +00:00
|
|
|
/* End of stream or error - get next clip */
|
2007-11-18 17:12:19 +00:00
|
|
|
td.vi.size = 0;
|
|
|
|
|
|
|
|
if (td.vi.get_more != NULL)
|
|
|
|
td.vi.get_more(&td.vi.start, &td.vi.size);
|
|
|
|
|
2007-11-18 17:34:33 +00:00
|
|
|
if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
|
|
|
/* Make bit buffer use our own buffer */
|
|
|
|
speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
|
2007-11-21 17:21:38 +00:00
|
|
|
/* Don't skip any samples when we're stringing clips together */
|
|
|
|
td.lookahead = 0;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2007-11-21 03:09:54 +00:00
|
|
|
/* Paranoid check - be sure never to somehow get stuck in a
|
|
|
|
* loop without listening to the queue */
|
2007-11-18 17:12:19 +00:00
|
|
|
yield();
|
|
|
|
|
|
|
|
if (!queue_empty(&voice_queue))
|
|
|
|
goto message_wait;
|
|
|
|
else
|
|
|
|
goto voice_decode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If all clips are done and not playing, force pcm playback. */
|
2011-06-29 06:37:04 +00:00
|
|
|
voice_start_playback();
|
|
|
|
|
|
|
|
td.state = TSTATE_STOPPED;
|
|
|
|
td.ev.id = SYS_TIMEOUT;
|
2007-11-18 17:12:19 +00:00
|
|
|
goto message_process;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield();
|
|
|
|
|
|
|
|
/* Output the decoded frame */
|
|
|
|
td.count = VOICE_FRAME_SIZE - td.lookahead;
|
|
|
|
td.src[0] = (const char *)&voice_output_buf[td.lookahead];
|
|
|
|
td.src[1] = NULL;
|
|
|
|
td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (td.count <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
td.state = TSTATE_BUFFER_INSERT;
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
buffer_insert:
|
|
|
|
/* Process the PCM samples in the DSP and send out for mixing */
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
while (1)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (!queue_empty(&voice_queue))
|
|
|
|
goto message_wait;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if ((dest = (char *)voice_buf_get()) != NULL)
|
2007-11-18 17:12:19 +00:00
|
|
|
break;
|
|
|
|
|
2011-07-08 23:39:01 +00:00
|
|
|
sleep(0);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
voice_buf_commit(dsp_process(td.dsp, dest, td.src, td.count)
|
|
|
|
* sizeof (int32_t));
|
2007-11-18 17:12:19 +00:00
|
|
|
} /* end while */
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
/* Initialize all synchronization objects create the thread */
|
|
|
|
void voice_thread_init(void)
|
|
|
|
{
|
|
|
|
logf("Starting voice thread");
|
|
|
|
queue_init(&voice_queue, false);
|
2008-10-23 13:13:00 +00:00
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
voice_thread_id = create_thread(voice_thread, voice_stack,
|
2007-11-18 17:12:19 +00:00
|
|
|
sizeof(voice_stack), CREATE_THREAD_FROZEN,
|
2011-06-29 06:37:04 +00:00
|
|
|
voice_thread_name IF_PRIO(, PRIORITY_VOICE) IF_COP(, CPU));
|
2008-03-25 02:34:12 +00:00
|
|
|
|
|
|
|
queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
|
2008-12-10 08:57:10 +00:00
|
|
|
voice_thread_id);
|
2007-11-18 17:12:19 +00:00
|
|
|
} /* voice_thread_init */
|
|
|
|
|
|
|
|
/* Unfreeze the voice thread */
|
|
|
|
void voice_thread_resume(void)
|
|
|
|
{
|
|
|
|
logf("Thawing voice thread");
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_thaw(voice_thread_id);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* Set the voice thread priority */
|
|
|
|
void voice_thread_set_priority(int priority)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (priority > PRIORITY_VOICE)
|
|
|
|
priority = PRIORITY_VOICE;
|
|
|
|
|
2008-12-10 08:57:10 +00:00
|
|
|
thread_set_priority(voice_thread_id, priority);
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
/* Initialize voice PCM buffer and return size, allocated from the end */
|
|
|
|
size_t voicebuf_init(unsigned char *bufend)
|
|
|
|
{
|
|
|
|
size_t size = VOICE_FRAMES * VOICE_PCM_FRAME_SIZE;
|
|
|
|
cur_buf_out = cur_buf_in = 0;
|
|
|
|
voicebuf = (uint32_t (*)[VOICE_PCM_FRAME_COUNT])(bufend - size);
|
|
|
|
return size;
|
|
|
|
}
|