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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-01-05 00:22:19 +00:00
|
|
|
|
2013-05-23 17:58:51 +00:00
|
|
|
#include "config.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "system.h"
|
2014-01-05 00:22:19 +00:00
|
|
|
#include "kernel.h"
|
2012-05-02 21:22:28 +00:00
|
|
|
#include "core_alloc.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "thread.h"
|
2013-05-31 22:45:51 +00:00
|
|
|
#include "appevents.h"
|
2007-11-18 17:12:19 +00:00
|
|
|
#include "voice_thread.h"
|
|
|
|
#include "talk.h"
|
2012-04-29 21:31:30 +00:00
|
|
|
#include "dsp_core.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"
|
|
|
|
|
2013-05-23 17:58:51 +00:00
|
|
|
/* Default number of PCM frames to queue - adjust as necessary per-target */
|
2012-05-02 21:22:28 +00:00
|
|
|
#define VOICE_FRAMES 4
|
|
|
|
|
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)
|
|
|
|
|
2014-01-21 22:22:37 +00:00
|
|
|
/* A speex frame generally consists of 20ms of audio
|
|
|
|
* (http://www.speex.org/docs/manual/speex-manual/node10.html)
|
|
|
|
* for wideband mode this results in 320 samples of decoded PCM.
|
|
|
|
*/
|
2012-04-26 19:57:07 +00:00
|
|
|
#define VOICE_FRAME_COUNT 320 /* Samples / frame */
|
2007-11-18 17:12:19 +00:00
|
|
|
#define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
|
|
|
|
#define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
|
2014-01-21 22:22:37 +00:00
|
|
|
/* The max. wideband bitrate is 42.4 kbps
|
|
|
|
* (http://www.speex.org/docs/manual/speex-manual/node11.html). For 20ms
|
|
|
|
* this gives a maximum of 106 bytes for an encoded speex frame */
|
|
|
|
#define VOICE_MAX_ENCODED_FRAME_SIZE 106
|
2007-11-18 17:12:19 +00:00
|
|
|
|
|
|
|
/* Voice thread variables */
|
2008-12-10 08:57:10 +00:00
|
|
|
static unsigned int voice_thread_id = 0;
|
2020-08-25 11:52:04 +00:00
|
|
|
#if defined(CPU_MIPS)
|
|
|
|
/* MIPS is stack-hungry */
|
|
|
|
#define VOICE_STACK_EXTRA 0x500
|
|
|
|
#elif defined(CPU_COLDFIRE)
|
2011-06-29 06:37:04 +00:00
|
|
|
/* ISR uses any available stack - need a bit more room */
|
|
|
|
#define VOICE_STACK_EXTRA 0x400
|
2020-12-17 18:11:43 +00:00
|
|
|
#elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
|
|
|
|
/* Needed at least on the Sony NWZ hosted targets, but probably a good idea on all of them */
|
|
|
|
#define VOICE_STACK_EXTRA 0x500
|
2011-06-29 06:37:04 +00:00
|
|
|
#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;
|
2011-08-30 15:35:25 +00:00
|
|
|
static int quiet_counter SHAREDDATA_ATTR = 0;
|
2013-05-31 22:45:51 +00:00
|
|
|
static bool voice_playing = false;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2013-05-23 17:58:51 +00:00
|
|
|
#define VOICE_PCM_FRAME_COUNT ((PLAY_SAMPR_MAX*VOICE_FRAME_COUNT + \
|
|
|
|
VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
|
2012-04-26 19:57:07 +00:00
|
|
|
#define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t))
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Voice processing states */
|
|
|
|
enum voice_state
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
VOICE_STATE_MESSAGE = 0,
|
|
|
|
VOICE_STATE_DECODE,
|
|
|
|
VOICE_STATE_BUFFER_INSERT,
|
2020-09-20 17:29:02 +00:00
|
|
|
VOICE_STATE_QUIT,
|
2007-11-18 17:12:19 +00:00
|
|
|
};
|
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* 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_messages
|
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
Q_VOICE_PLAY = 0, /* Play a clip */
|
2007-11-18 17:12:19 +00:00
|
|
|
Q_VOICE_STOP, /* Stop current clip */
|
2020-09-20 17:29:02 +00:00
|
|
|
Q_VOICE_KILL, /* Kill voice thread till restart*/
|
2007-11-18 17:12:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Structure to store clip data callback info */
|
|
|
|
struct voice_info
|
|
|
|
{
|
2012-02-23 13:14:46 +00:00
|
|
|
/* Callback to get more clips */
|
2020-09-20 14:36:25 +00:00
|
|
|
voice_play_callback_t get_more;
|
2012-02-23 13:14:46 +00:00
|
|
|
/* Start of clip */
|
2012-03-04 19:44:43 +00:00
|
|
|
const void *start;
|
2012-02-23 13:14:46 +00:00
|
|
|
/* Size of clip */
|
|
|
|
size_t size;
|
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
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
int lookahead; /* Number of samples to drop at start of clip */
|
2012-03-27 23:52:15 +00:00
|
|
|
struct dsp_buffer src; /* Speex output buffer/input to DSP */
|
2012-05-02 21:22:28 +00:00
|
|
|
struct dsp_buffer *dst; /* Pointer to DSP output buffer for PCM */
|
2007-11-18 17:12:19 +00:00
|
|
|
};
|
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Functions called in their repective state that return the next state to
|
|
|
|
state machine loop - compiler may inline them at its discretion */
|
|
|
|
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_buffer_insert(struct voice_thread_data *td);
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
/* 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 */
|
2012-05-30 16:55:26 +00:00
|
|
|
unsigned int volatile frame_in;
|
|
|
|
unsigned int volatile frame_out;
|
2012-05-02 21:22:28 +00:00
|
|
|
/* 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;
|
2012-05-17 15:16:20 +00:00
|
|
|
|
|
|
|
if (td != NULL)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/* Number of frames in queue */
|
2012-05-30 16:55:26 +00:00
|
|
|
static unsigned int voice_unplayed_frames(void)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2012-05-02 21:22:28 +00:00
|
|
|
return voice_buf->frame_in - voice_buf->frame_out;
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mixer channel callback */
|
2012-02-23 13:14:46 +00:00
|
|
|
static void voice_pcm_callback(const void **start, size_t *size)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2012-05-30 16:55:26 +00:00
|
|
|
unsigned int frame_out = ++voice_buf->frame_out;
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (voice_unplayed_frames() == 0)
|
|
|
|
return; /* Done! */
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
struct voice_pcm_frame *frame =
|
2012-05-30 16:55:26 +00:00
|
|
|
&voice_buf->frames[frame_out % VOICE_FRAMES];
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
*start = frame->pcm;
|
|
|
|
*size = frame->size;
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start playback of voice channel if not already playing */
|
|
|
|
static void voice_start_playback(void)
|
|
|
|
{
|
2012-04-26 19:57:07 +00:00
|
|
|
if (mixer_channel_status(PCM_MIXER_CHAN_VOICE) != CHANNEL_STOPPED ||
|
2012-05-30 16:55:26 +00:00
|
|
|
voice_unplayed_frames() == 0)
|
2011-06-29 06:37:04 +00:00
|
|
|
return;
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
struct voice_pcm_frame *frame =
|
|
|
|
&voice_buf->frames[voice_buf->frame_out % VOICE_FRAMES];
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback,
|
2012-05-02 21:22:28 +00:00
|
|
|
frame->pcm, frame->size);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop the voice channel */
|
|
|
|
static void voice_stop_playback(void)
|
|
|
|
{
|
|
|
|
mixer_channel_stop(PCM_MIXER_CHAN_VOICE);
|
2012-05-02 21:22:28 +00:00
|
|
|
voice_buf->frame_in = voice_buf->frame_out = 0;
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab a free PCM frame */
|
2012-04-26 20:24:12 +00:00
|
|
|
static int16_t * voice_buf_get(void)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
|
|
|
if (voice_unplayed_frames() >= VOICE_FRAMES)
|
|
|
|
{
|
|
|
|
/* Full */
|
|
|
|
voice_start_playback();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
return voice_buf->frames[voice_buf->frame_in % VOICE_FRAMES].pcm;
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Commit a frame returned by voice_buf_get and set the actual size */
|
2012-04-26 19:57:07 +00:00
|
|
|
static void voice_buf_commit(int count)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2012-04-26 19:57:07 +00:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2012-05-30 16:55:26 +00:00
|
|
|
unsigned int frame_in = voice_buf->frame_in;
|
|
|
|
voice_buf->frames[frame_in % VOICE_FRAMES].size =
|
2012-04-26 19:57:07 +00:00
|
|
|
count * 2 * sizeof (int16_t);
|
2012-05-30 16:55:26 +00:00
|
|
|
voice_buf->frame_in = frame_in + 1;
|
2012-04-26 19:57:07 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop any current clip and start playing a new one */
|
2020-09-20 14:36:25 +00:00
|
|
|
void voice_play_data(const void *start, size_t size,
|
|
|
|
voice_play_callback_t get_more)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2012-05-02 21:22:28 +00:00
|
|
|
if (voice_thread_id && start && size && get_more)
|
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,
|
2012-03-04 19:44:43 +00:00
|
|
|
.start = start,
|
2011-06-29 06:37:04 +00:00
|
|
|
.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 */
|
2020-09-20 14:36:25 +00:00
|
|
|
void voice_play_stop(void)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2012-05-02 21:22:28 +00:00
|
|
|
if (voice_thread_id != 0)
|
|
|
|
{
|
|
|
|
LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
|
|
|
|
queue_send(&voice_queue, Q_VOICE_STOP, 0);
|
|
|
|
}
|
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
|
|
|
|
2013-05-31 22:45:51 +00:00
|
|
|
while (voice_playing)
|
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)
|
|
|
|
{
|
2012-03-27 23:52:15 +00:00
|
|
|
td->dsp = dsp_get_config(CODEC_IDX_VOICE);
|
2007-11-18 17:12:19 +00:00
|
|
|
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);
|
2012-05-02 21:22:28 +00:00
|
|
|
voice_buf->td = td;
|
2012-09-11 04:39:53 +00:00
|
|
|
td->dst = NULL;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Voice thread message processing */
|
2011-08-30 15:35:25 +00:00
|
|
|
static enum voice_state voice_message(struct voice_thread_data *td)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2013-05-31 22:45:51 +00:00
|
|
|
queue_wait_w_tmo(&voice_queue, &td->ev,
|
|
|
|
quiet_counter > 0 ? HZ/10 : TIMEOUT_BLOCK);
|
2011-08-30 15:35:25 +00:00
|
|
|
|
|
|
|
switch (td->ev.id)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
case Q_VOICE_PLAY:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_PLAY");
|
|
|
|
if (quiet_counter == 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Boost CPU now */
|
|
|
|
trigger_cpu_boost();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Stop any clip still playing */
|
|
|
|
voice_stop_playback();
|
2013-05-23 17:58:51 +00:00
|
|
|
dsp_configure(td->dsp, DSP_FLUSH, 0);
|
2011-08-30 15:35:25 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2013-05-31 22:45:51 +00:00
|
|
|
if (quiet_counter <= 0)
|
|
|
|
{
|
|
|
|
voice_playing = true;
|
2013-05-23 17:58:51 +00:00
|
|
|
dsp_configure(td->dsp, DSP_SET_OUT_FREQUENCY, mixer_get_frequency());
|
2017-12-13 01:14:34 +00:00
|
|
|
send_event(VOICE_EVENT_IS_PLAYING, &voice_playing);
|
2013-05-31 22:45:51 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
quiet_counter = QUIET_COUNT;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Copy the clip info */
|
|
|
|
td->vi = *(struct voice_info *)td->ev.data;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* We need nothing more from the sending thread - let it run */
|
|
|
|
queue_reply(&voice_queue, 1);
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Clean-start the decoder */
|
|
|
|
td->st = speex_decoder_init(&speex_wb_mode);
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Make bit buffer use our own buffer */
|
2012-03-04 19:44:43 +00:00
|
|
|
speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
|
|
|
|
td->vi.size);
|
2011-08-30 15:35:25 +00:00
|
|
|
speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
return VOICE_STATE_DECODE;
|
2020-09-20 17:29:02 +00:00
|
|
|
case Q_VOICE_KILL:
|
|
|
|
queue_delete(&voice_queue);
|
|
|
|
return VOICE_STATE_QUIT;
|
2011-08-30 15:35:25 +00:00
|
|
|
case SYS_TIMEOUT:
|
|
|
|
if (voice_unplayed_frames())
|
|
|
|
{
|
|
|
|
/* Waiting for PCM to finish */
|
|
|
|
break;
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Drop through and stop the first time after clip runs out */
|
|
|
|
if (quiet_counter-- != QUIET_COUNT)
|
|
|
|
{
|
|
|
|
if (quiet_counter <= 0)
|
2013-05-31 22:45:51 +00:00
|
|
|
{
|
|
|
|
voice_playing = false;
|
2017-12-13 01:14:34 +00:00
|
|
|
send_event(VOICE_EVENT_IS_PLAYING, &voice_playing);
|
2013-05-31 22:45:51 +00:00
|
|
|
}
|
2011-08-30 15:35:25 +00:00
|
|
|
break;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Fall-through */
|
|
|
|
case Q_VOICE_STOP:
|
|
|
|
LOGFQUEUE("voice < Q_VOICE_STOP");
|
|
|
|
cancel_cpu_boost();
|
|
|
|
voice_stop_playback();
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* No default: no other message ids are sent */
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
2011-08-30 15:35:25 +00:00
|
|
|
|
|
|
|
return VOICE_STATE_MESSAGE;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Decode frames or stop if all have completed */
|
|
|
|
static enum voice_state voice_decode(struct voice_thread_data *td)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
if (!queue_empty(&voice_queue))
|
|
|
|
return VOICE_STATE_MESSAGE;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Decode the data */
|
2012-05-02 21:22:28 +00:00
|
|
|
if (speex_decode_int(td->st, &td->bits, voice_buf->spx_outbuf) < 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
/* End of stream or error - get next clip */
|
|
|
|
td->vi.size = 0;
|
|
|
|
|
|
|
|
if (td->vi.get_more != NULL)
|
|
|
|
td->vi.get_more(&td->vi.start, &td->vi.size);
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2012-03-04 19:44:43 +00:00
|
|
|
if (td->vi.start != NULL && td->vi.size > 0)
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Make bit buffer use our own buffer */
|
2012-03-04 19:44:43 +00:00
|
|
|
speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
|
|
|
|
td->vi.size);
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Don't skip any samples when we're stringing clips together */
|
|
|
|
td->lookahead = 0;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
2011-08-30 15:35:25 +00:00
|
|
|
else
|
2007-11-18 17:12:19 +00:00
|
|
|
{
|
|
|
|
/* If all clips are done and not playing, force pcm playback. */
|
2012-03-27 23:52:15 +00:00
|
|
|
if (voice_unplayed_frames() > 0)
|
|
|
|
voice_start_playback();
|
2011-08-30 15:35:25 +00:00
|
|
|
return VOICE_STATE_MESSAGE;
|
2007-11-18 17:12:19 +00:00
|
|
|
}
|
2011-08-30 15:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-21 22:22:37 +00:00
|
|
|
if (td->vi.size > VOICE_MAX_ENCODED_FRAME_SIZE
|
2014-02-03 11:33:33 +00:00
|
|
|
&& td->bits.charPtr > (int)(td->vi.size - VOICE_MAX_ENCODED_FRAME_SIZE)
|
2014-01-21 22:22:37 +00:00
|
|
|
&& td->vi.get_more != NULL)
|
|
|
|
{
|
|
|
|
/* request more data _before_ running out of data (requesting
|
|
|
|
* more after the fact prevents speex from successful decoding)
|
|
|
|
* place a hint telling the callback how much of the
|
|
|
|
* previous buffer we have consumed such that it can rewind
|
|
|
|
* as necessary */
|
|
|
|
int bitPtr = td->bits.bitPtr;
|
|
|
|
td->vi.size = td->bits.charPtr;
|
|
|
|
td->vi.get_more(&td->vi.start, &td->vi.size);
|
|
|
|
speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
|
|
|
|
td->vi.size);
|
|
|
|
td->bits.bitPtr = bitPtr;
|
|
|
|
}
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
yield();
|
|
|
|
|
|
|
|
/* Output the decoded frame */
|
2012-03-27 23:52:15 +00:00
|
|
|
td->src.remcount = VOICE_FRAME_COUNT - td->lookahead;
|
2012-05-02 21:22:28 +00:00
|
|
|
td->src.pin[0] = &voice_buf->spx_outbuf[td->lookahead];
|
2012-03-27 23:52:15 +00:00
|
|
|
td->src.pin[1] = NULL;
|
|
|
|
td->src.proc_mask = 0;
|
|
|
|
|
2012-04-26 19:57:07 +00:00
|
|
|
td->lookahead -= MIN(VOICE_FRAME_COUNT, td->lookahead);
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
if (td->src.remcount > 0)
|
2011-08-30 15:35:25 +00:00
|
|
|
return VOICE_STATE_BUFFER_INSERT;
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
return VOICE_STATE_DECODE;
|
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Process the PCM samples in the DSP and send out for mixing */
|
|
|
|
static enum voice_state voice_buffer_insert(struct voice_thread_data *td)
|
|
|
|
{
|
|
|
|
if (!queue_empty(&voice_queue))
|
|
|
|
return VOICE_STATE_MESSAGE;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
struct dsp_buffer dst;
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2012-03-27 23:52:15 +00:00
|
|
|
if ((dst.p16out = voice_buf_get()) != NULL)
|
2011-08-30 15:35:25 +00:00
|
|
|
{
|
2012-03-27 23:52:15 +00:00
|
|
|
dst.remcount = 0;
|
|
|
|
dst.bufcount = VOICE_PCM_FRAME_COUNT;
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
td->dst = &dst;
|
2012-03-27 23:52:15 +00:00
|
|
|
dsp_process(td->dsp, &td->src, &dst);
|
2012-05-02 21:22:28 +00:00
|
|
|
td->dst = NULL;
|
2012-03-27 23:52:15 +00:00
|
|
|
|
|
|
|
voice_buf_commit(dst.remcount);
|
|
|
|
|
|
|
|
/* Unless other effects are introduced to voice that have delays,
|
|
|
|
all output should have been purged to dst in one call */
|
|
|
|
return td->src.remcount > 0 ?
|
|
|
|
VOICE_STATE_BUFFER_INSERT : VOICE_STATE_DECODE;
|
2011-08-30 15:35:25 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
sleep(0);
|
|
|
|
return VOICE_STATE_BUFFER_INSERT;
|
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2011-08-30 15:35:25 +00:00
|
|
|
/* Voice thread entrypoint */
|
2020-09-20 17:29:02 +00:00
|
|
|
static void voice_thread(void)
|
2011-08-30 15:35:25 +00:00
|
|
|
{
|
|
|
|
struct voice_thread_data td;
|
|
|
|
enum voice_state state = VOICE_STATE_MESSAGE;
|
|
|
|
|
|
|
|
voice_data_init(&td);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case VOICE_STATE_MESSAGE:
|
|
|
|
state = voice_message(&td);
|
|
|
|
break;
|
|
|
|
case VOICE_STATE_DECODE:
|
|
|
|
state = voice_decode(&td);
|
|
|
|
break;
|
|
|
|
case VOICE_STATE_BUFFER_INSERT:
|
|
|
|
state = voice_buffer_insert(&td);
|
|
|
|
break;
|
2020-09-20 17:29:02 +00:00
|
|
|
case VOICE_STATE_QUIT:
|
|
|
|
logf("Exiting voice thread");
|
|
|
|
core_free(voice_buf_hid);
|
|
|
|
voice_buf_hid = 0;
|
|
|
|
return;
|
2011-08-30 15:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-20 17:29:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kill voice thread and dont allow re-init*/
|
|
|
|
void voice_thread_kill(void)
|
|
|
|
{
|
|
|
|
queue_send(&voice_queue, Q_VOICE_KILL, 0);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2007-11-18 17:12:19 +00:00
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
/* Initialize buffers, all synchronization objects and create the thread */
|
2007-11-18 17:12:19 +00:00
|
|
|
void voice_thread_init(void)
|
|
|
|
{
|
2012-05-02 21:22:28 +00:00
|
|
|
if (voice_thread_id != 0)
|
|
|
|
return; /* Already did an init and succeeded at it */
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-17 15:16:20 +00:00
|
|
|
memset(voice_buf, 0, sizeof (*voice_buf));
|
|
|
|
|
2007-11-18 17:12:19 +00:00
|
|
|
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,
|
2011-09-01 07:32:07 +00:00
|
|
|
sizeof(voice_stack), 0, 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
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
/* Set the voice thread priority */
|
|
|
|
void voice_thread_set_priority(int priority)
|
|
|
|
{
|
2012-05-02 21:22:28 +00:00
|
|
|
if (voice_thread_id == 0)
|
|
|
|
return;
|
|
|
|
|
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
|