Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 Michael Sevakis |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Thomas Martitz | 281d1fa | 2014-01-05 01:22:19 +0100 | [diff] [blame] | 21 | |
Michael Sevakis | d37bf24 | 2013-05-23 13:58:51 -0400 | [diff] [blame] | 22 | #include "config.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 23 | #include "system.h" |
Thomas Martitz | 281d1fa | 2014-01-05 01:22:19 +0100 | [diff] [blame] | 24 | #include "kernel.h" |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 25 | #include "core_alloc.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 26 | #include "thread.h" |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 27 | #include "appevents.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 28 | #include "voice_thread.h" |
| 29 | #include "talk.h" |
Michael Sevakis | 56f17c4 | 2012-04-29 17:31:30 -0400 | [diff] [blame] | 30 | #include "dsp_core.h" |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 31 | #include "pcm.h" |
| 32 | #include "pcm_mixer.h" |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 33 | #include "codecs/libspeex/speex/speex.h" |
| 34 | |
Michael Sevakis | d37bf24 | 2013-05-23 13:58:51 -0400 | [diff] [blame] | 35 | /* Default number of PCM frames to queue - adjust as necessary per-target */ |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 36 | #define VOICE_FRAMES 4 |
| 37 | |
Jeffrey Goode | 09cd188 | 2010-05-04 14:43:01 +0000 | [diff] [blame] | 38 | /* Define any of these as "1" and uncomment the LOGF_ENABLE line to log |
| 39 | regular and/or timeout messages */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 40 | #define VOICE_LOGQUEUES 0 |
| 41 | #define VOICE_LOGQUEUES_SYS_TIMEOUT 0 |
| 42 | |
Jeffrey Goode | 09cd188 | 2010-05-04 14:43:01 +0000 | [diff] [blame] | 43 | /*#define LOGF_ENABLE*/ |
| 44 | #include "logf.h" |
| 45 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 46 | #if VOICE_LOGQUEUES |
| 47 | #define LOGFQUEUE logf |
| 48 | #else |
| 49 | #define LOGFQUEUE(...) |
| 50 | #endif |
| 51 | |
| 52 | #if VOICE_LOGQUEUES_SYS_TIMEOUT |
| 53 | #define LOGFQUEUE_SYS_TIMEOUT logf |
| 54 | #else |
| 55 | #define LOGFQUEUE_SYS_TIMEOUT(...) |
| 56 | #endif |
| 57 | |
| 58 | #ifndef IBSS_ATTR_VOICE_STACK |
| 59 | #define IBSS_ATTR_VOICE_STACK IBSS_ATTR |
| 60 | #endif |
| 61 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 62 | /* Minimum priority needs to be a bit elevated since voice has fairly low |
| 63 | latency */ |
| 64 | #define PRIORITY_VOICE (PRIORITY_PLAYBACK-4) |
| 65 | |
Thomas Martitz | e5eb745 | 2014-01-21 23:22:37 +0100 | [diff] [blame] | 66 | /* A speex frame generally consists of 20ms of audio |
| 67 | * (http://www.speex.org/docs/manual/speex-manual/node10.html) |
| 68 | * for wideband mode this results in 320 samples of decoded PCM. |
| 69 | */ |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 70 | #define VOICE_FRAME_COUNT 320 /* Samples / frame */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 71 | #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */ |
| 72 | #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */ |
Thomas Martitz | e5eb745 | 2014-01-21 23:22:37 +0100 | [diff] [blame] | 73 | /* The max. wideband bitrate is 42.4 kbps |
| 74 | * (http://www.speex.org/docs/manual/speex-manual/node11.html). For 20ms |
| 75 | * this gives a maximum of 106 bytes for an encoded speex frame */ |
| 76 | #define VOICE_MAX_ENCODED_FRAME_SIZE 106 |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 77 | |
| 78 | /* Voice thread variables */ |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 79 | static unsigned int voice_thread_id = 0; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 80 | #ifdef CPU_COLDFIRE |
| 81 | /* ISR uses any available stack - need a bit more room */ |
| 82 | #define VOICE_STACK_EXTRA 0x400 |
| 83 | #else |
| 84 | #define VOICE_STACK_EXTRA 0x3c0 |
| 85 | #endif |
| 86 | static long voice_stack[(DEFAULT_STACK_SIZE + VOICE_STACK_EXTRA)/sizeof(long)] |
| 87 | IBSS_ATTR_VOICE_STACK; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 88 | static const char voice_thread_name[] = "voice"; |
| 89 | |
| 90 | /* Voice thread synchronization objects */ |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 91 | static struct event_queue voice_queue SHAREDBSS_ATTR; |
Michael Sevakis | 0509914 | 2008-04-06 04:34:57 +0000 | [diff] [blame] | 92 | static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR; |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 93 | static int quiet_counter SHAREDDATA_ATTR = 0; |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 94 | static bool voice_playing = false; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 95 | |
Michael Sevakis | d37bf24 | 2013-05-23 13:58:51 -0400 | [diff] [blame] | 96 | #define VOICE_PCM_FRAME_COUNT ((PLAY_SAMPR_MAX*VOICE_FRAME_COUNT + \ |
| 97 | VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE) |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 98 | #define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t)) |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 99 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 100 | /* Voice processing states */ |
| 101 | enum voice_state |
| 102 | { |
| 103 | VOICE_STATE_MESSAGE = 0, |
| 104 | VOICE_STATE_DECODE, |
| 105 | VOICE_STATE_BUFFER_INSERT, |
| 106 | }; |
| 107 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 108 | /* A delay to not bring audio back to normal level too soon */ |
| 109 | #define QUIET_COUNT 3 |
| 110 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 111 | enum voice_thread_messages |
| 112 | { |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 113 | Q_VOICE_PLAY = 0, /* Play a clip */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 114 | Q_VOICE_STOP, /* Stop current clip */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /* Structure to store clip data callback info */ |
| 118 | struct voice_info |
| 119 | { |
Michael Sevakis | 286a4c5 | 2012-02-23 08:14:46 -0500 | [diff] [blame] | 120 | /* Callback to get more clips */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 121 | mp3_play_callback_t get_more; |
Michael Sevakis | 286a4c5 | 2012-02-23 08:14:46 -0500 | [diff] [blame] | 122 | /* Start of clip */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 123 | const void *start; |
Michael Sevakis | 286a4c5 | 2012-02-23 08:14:46 -0500 | [diff] [blame] | 124 | /* Size of clip */ |
| 125 | size_t size; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /* Private thread data for its current state that must be passed to its |
| 129 | * internal functions */ |
| 130 | struct voice_thread_data |
| 131 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 132 | struct queue_event ev; /* Last queue event pulled from queue */ |
| 133 | void *st; /* Decoder instance */ |
| 134 | SpeexBits bits; /* Bit cursor */ |
| 135 | struct dsp_config *dsp; /* DSP used for voice output */ |
| 136 | struct voice_info vi; /* Copy of clip data */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 137 | int lookahead; /* Number of samples to drop at start of clip */ |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 138 | struct dsp_buffer src; /* Speex output buffer/input to DSP */ |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 139 | struct dsp_buffer *dst; /* Pointer to DSP output buffer for PCM */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 142 | /* Functions called in their repective state that return the next state to |
| 143 | state machine loop - compiler may inline them at its discretion */ |
| 144 | static enum voice_state voice_message(struct voice_thread_data *td); |
| 145 | static enum voice_state voice_decode(struct voice_thread_data *td); |
| 146 | static enum voice_state voice_buffer_insert(struct voice_thread_data *td); |
| 147 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 148 | /* Might have lookahead and be skipping samples, so size is needed */ |
| 149 | static struct voice_buf |
| 150 | { |
| 151 | /* Buffer for decoded samples */ |
| 152 | spx_int16_t spx_outbuf[VOICE_FRAME_COUNT]; |
| 153 | /* Queue frame indexes */ |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 154 | unsigned int volatile frame_in; |
| 155 | unsigned int volatile frame_out; |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 156 | /* For PCM pointer adjustment */ |
| 157 | struct voice_thread_data *td; |
| 158 | /* Buffers for mixing voice */ |
| 159 | struct voice_pcm_frame |
| 160 | { |
| 161 | size_t size; |
| 162 | int16_t pcm[2*VOICE_PCM_FRAME_COUNT]; |
| 163 | } frames[VOICE_FRAMES]; |
| 164 | } *voice_buf = NULL; |
| 165 | |
| 166 | static int voice_buf_hid = 0; |
| 167 | |
| 168 | static int move_callback(int handle, void *current, void *new) |
| 169 | { |
| 170 | /* Have to adjust the pointers that point into things in voice_buf */ |
| 171 | off_t diff = new - current; |
| 172 | struct voice_thread_data *td = voice_buf->td; |
Michael Sevakis | 8bbd4d9 | 2012-05-17 11:16:20 -0400 | [diff] [blame] | 173 | |
| 174 | if (td != NULL) |
| 175 | { |
| 176 | td->src.p32[0] = SKIPBYTES(td->src.p32[0], diff); |
| 177 | td->src.p32[1] = SKIPBYTES(td->src.p32[1], diff); |
| 178 | |
| 179 | if (td->dst != NULL) /* Only when calling dsp_process */ |
| 180 | td->dst->p16out = SKIPBYTES(td->dst->p16out, diff); |
| 181 | |
| 182 | mixer_adjust_channel_address(PCM_MIXER_CHAN_VOICE, diff); |
| 183 | } |
| 184 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 185 | voice_buf = new; |
| 186 | |
| 187 | return BUFLIB_CB_OK; |
| 188 | (void)handle; |
| 189 | }; |
| 190 | |
| 191 | static void sync_callback(int handle, bool sync_on) |
| 192 | { |
| 193 | /* A move must not allow PCM to access the channel */ |
| 194 | if (sync_on) |
| 195 | pcm_play_lock(); |
| 196 | else |
| 197 | pcm_play_unlock(); |
| 198 | |
| 199 | (void)handle; |
| 200 | } |
| 201 | |
| 202 | static struct buflib_callbacks ops = |
| 203 | { |
| 204 | .move_callback = move_callback, |
| 205 | .sync_callback = sync_callback, |
| 206 | }; |
| 207 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 208 | /* Number of frames in queue */ |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 209 | static unsigned int voice_unplayed_frames(void) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 210 | { |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 211 | return voice_buf->frame_in - voice_buf->frame_out; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Mixer channel callback */ |
Michael Sevakis | 286a4c5 | 2012-02-23 08:14:46 -0500 | [diff] [blame] | 215 | static void voice_pcm_callback(const void **start, size_t *size) |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 216 | { |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 217 | unsigned int frame_out = ++voice_buf->frame_out; |
| 218 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 219 | if (voice_unplayed_frames() == 0) |
| 220 | return; /* Done! */ |
| 221 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 222 | struct voice_pcm_frame *frame = |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 223 | &voice_buf->frames[frame_out % VOICE_FRAMES]; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 224 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 225 | *start = frame->pcm; |
| 226 | *size = frame->size; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Start playback of voice channel if not already playing */ |
| 230 | static void voice_start_playback(void) |
| 231 | { |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 232 | if (mixer_channel_status(PCM_MIXER_CHAN_VOICE) != CHANNEL_STOPPED || |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 233 | voice_unplayed_frames() == 0) |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 234 | return; |
| 235 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 236 | struct voice_pcm_frame *frame = |
| 237 | &voice_buf->frames[voice_buf->frame_out % VOICE_FRAMES]; |
| 238 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 239 | mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback, |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 240 | frame->pcm, frame->size); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Stop the voice channel */ |
| 244 | static void voice_stop_playback(void) |
| 245 | { |
| 246 | mixer_channel_stop(PCM_MIXER_CHAN_VOICE); |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 247 | voice_buf->frame_in = voice_buf->frame_out = 0; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* Grab a free PCM frame */ |
Michael Sevakis | 97a535d | 2012-04-26 16:24:12 -0400 | [diff] [blame] | 251 | static int16_t * voice_buf_get(void) |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 252 | { |
| 253 | if (voice_unplayed_frames() >= VOICE_FRAMES) |
| 254 | { |
| 255 | /* Full */ |
| 256 | voice_start_playback(); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 260 | return voice_buf->frames[voice_buf->frame_in % VOICE_FRAMES].pcm; |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* Commit a frame returned by voice_buf_get and set the actual size */ |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 264 | static void voice_buf_commit(int count) |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 265 | { |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 266 | if (count > 0) |
| 267 | { |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 268 | unsigned int frame_in = voice_buf->frame_in; |
| 269 | voice_buf->frames[frame_in % VOICE_FRAMES].size = |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 270 | count * 2 * sizeof (int16_t); |
Michael Sevakis | 7722014 | 2012-05-30 12:55:26 -0400 | [diff] [blame] | 271 | voice_buf->frame_in = frame_in + 1; |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 272 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Stop any current clip and start playing a new one */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 276 | void mp3_play_data(const void *start, size_t size, |
| 277 | mp3_play_callback_t get_more) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 278 | { |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 279 | if (voice_thread_id && start && size && get_more) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 280 | { |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 281 | struct voice_info voice_clip = |
| 282 | { |
| 283 | .get_more = get_more, |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 284 | .start = start, |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 285 | .size = size, |
| 286 | }; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 287 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 288 | LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY"); |
| 289 | queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /* Stop current voice clip from playing */ |
| 294 | void mp3_play_stop(void) |
| 295 | { |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 296 | if (voice_thread_id != 0) |
| 297 | { |
| 298 | LOGFQUEUE("mp3 >| voice Q_VOICE_STOP"); |
| 299 | queue_send(&voice_queue, Q_VOICE_STOP, 0); |
| 300 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void mp3_play_pause(bool play) |
| 304 | { |
| 305 | /* a dummy */ |
| 306 | (void)play; |
| 307 | } |
| 308 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 309 | /* Tell if voice is still in a playing state */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 310 | bool mp3_is_playing(void) |
| 311 | { |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 312 | return voice_playing; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* This function is meant to be used by the buffer request functions to |
| 316 | ensure the codec is no longer active */ |
| 317 | void voice_stop(void) |
| 318 | { |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 319 | /* Unqueue all future clips */ |
| 320 | talk_force_shutup(); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 321 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 322 | |
| 323 | /* Wait for voice to finish speaking. */ |
| 324 | void voice_wait(void) |
| 325 | { |
| 326 | /* NOTE: One problem here is that we can't tell if another thread started a |
| 327 | * new clip by the time we wait. This should be resolvable if conditions |
| 328 | * ever require knowing the very clip you requested has finished. */ |
Michael Sevakis | effceea | 2008-10-23 13:13:00 +0000 | [diff] [blame] | 329 | |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 330 | while (voice_playing) |
Stéphane Doyon | 686b114 | 2007-11-24 14:21:04 +0000 | [diff] [blame] | 331 | sleep(1); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* Initialize voice thread data that must be valid upon starting and the |
| 335 | * setup the DSP parameters */ |
| 336 | static void voice_data_init(struct voice_thread_data *td) |
| 337 | { |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 338 | td->dsp = dsp_get_config(CODEC_IDX_VOICE); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 339 | dsp_configure(td->dsp, DSP_RESET, 0); |
| 340 | dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE); |
| 341 | dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH); |
| 342 | dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO); |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 343 | |
| 344 | mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY); |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 345 | voice_buf->td = td; |
Thomas Martitz | b985d5c | 2012-09-11 06:39:53 +0200 | [diff] [blame] | 346 | td->dst = NULL; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* Voice thread message processing */ |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 350 | static enum voice_state voice_message(struct voice_thread_data *td) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 351 | { |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 352 | queue_wait_w_tmo(&voice_queue, &td->ev, |
| 353 | quiet_counter > 0 ? HZ/10 : TIMEOUT_BLOCK); |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 354 | |
| 355 | switch (td->ev.id) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 356 | { |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 357 | case Q_VOICE_PLAY: |
| 358 | LOGFQUEUE("voice < Q_VOICE_PLAY"); |
| 359 | if (quiet_counter == 0) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 360 | { |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 361 | /* Boost CPU now */ |
| 362 | trigger_cpu_boost(); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | /* Stop any clip still playing */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 367 | voice_stop_playback(); |
Michael Sevakis | d37bf24 | 2013-05-23 13:58:51 -0400 | [diff] [blame] | 368 | dsp_configure(td->dsp, DSP_FLUSH, 0); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 371 | if (quiet_counter <= 0) |
| 372 | { |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 373 | voice_playing = true; |
Michael Sevakis | d37bf24 | 2013-05-23 13:58:51 -0400 | [diff] [blame] | 374 | dsp_configure(td->dsp, DSP_SET_OUT_FREQUENCY, mixer_get_frequency()); |
Michael Sevakis | c8564f1 | 2017-12-12 20:14:34 -0500 | [diff] [blame] | 375 | send_event(VOICE_EVENT_IS_PLAYING, &voice_playing); |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 378 | quiet_counter = QUIET_COUNT; |
| 379 | |
| 380 | /* Copy the clip info */ |
| 381 | td->vi = *(struct voice_info *)td->ev.data; |
| 382 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 383 | /* We need nothing more from the sending thread - let it run */ |
| 384 | queue_reply(&voice_queue, 1); |
| 385 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 386 | /* Clean-start the decoder */ |
| 387 | td->st = speex_decoder_init(&speex_wb_mode); |
| 388 | |
| 389 | /* Make bit buffer use our own buffer */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 390 | speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start, |
| 391 | td->vi.size); |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 392 | speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead); |
| 393 | |
| 394 | return VOICE_STATE_DECODE; |
| 395 | |
| 396 | case SYS_TIMEOUT: |
| 397 | if (voice_unplayed_frames()) |
| 398 | { |
| 399 | /* Waiting for PCM to finish */ |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | /* Drop through and stop the first time after clip runs out */ |
| 404 | if (quiet_counter-- != QUIET_COUNT) |
| 405 | { |
| 406 | if (quiet_counter <= 0) |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 407 | { |
| 408 | voice_playing = false; |
Michael Sevakis | c8564f1 | 2017-12-12 20:14:34 -0500 | [diff] [blame] | 409 | send_event(VOICE_EVENT_IS_PLAYING, &voice_playing); |
Michael Sevakis | e62cb56 | 2013-05-31 18:45:51 -0400 | [diff] [blame] | 410 | } |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | /* Fall-through */ |
| 415 | case Q_VOICE_STOP: |
| 416 | LOGFQUEUE("voice < Q_VOICE_STOP"); |
| 417 | cancel_cpu_boost(); |
| 418 | voice_stop_playback(); |
| 419 | break; |
| 420 | |
| 421 | /* No default: no other message ids are sent */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 422 | } |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 423 | |
| 424 | return VOICE_STATE_MESSAGE; |
| 425 | } |
| 426 | |
| 427 | /* Decode frames or stop if all have completed */ |
| 428 | static enum voice_state voice_decode(struct voice_thread_data *td) |
| 429 | { |
| 430 | if (!queue_empty(&voice_queue)) |
| 431 | return VOICE_STATE_MESSAGE; |
| 432 | |
| 433 | /* Decode the data */ |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 434 | if (speex_decode_int(td->st, &td->bits, voice_buf->spx_outbuf) < 0) |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 435 | { |
| 436 | /* End of stream or error - get next clip */ |
| 437 | td->vi.size = 0; |
| 438 | |
| 439 | if (td->vi.get_more != NULL) |
| 440 | td->vi.get_more(&td->vi.start, &td->vi.size); |
| 441 | |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 442 | if (td->vi.start != NULL && td->vi.size > 0) |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 443 | { |
| 444 | /* Make bit buffer use our own buffer */ |
Michael Sevakis | d18a5ca | 2012-03-04 14:44:43 -0500 | [diff] [blame] | 445 | speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start, |
| 446 | td->vi.size); |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 447 | /* Don't skip any samples when we're stringing clips together */ |
| 448 | td->lookahead = 0; |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | /* If all clips are done and not playing, force pcm playback. */ |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 453 | if (voice_unplayed_frames() > 0) |
| 454 | voice_start_playback(); |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 455 | return VOICE_STATE_MESSAGE; |
| 456 | } |
| 457 | } |
| 458 | else |
| 459 | { |
Thomas Martitz | e5eb745 | 2014-01-21 23:22:37 +0100 | [diff] [blame] | 460 | if (td->vi.size > VOICE_MAX_ENCODED_FRAME_SIZE |
Thomas Martitz | 10b3bc7 | 2014-02-03 12:33:33 +0100 | [diff] [blame] | 461 | && td->bits.charPtr > (int)(td->vi.size - VOICE_MAX_ENCODED_FRAME_SIZE) |
Thomas Martitz | e5eb745 | 2014-01-21 23:22:37 +0100 | [diff] [blame] | 462 | && td->vi.get_more != NULL) |
| 463 | { |
| 464 | /* request more data _before_ running out of data (requesting |
| 465 | * more after the fact prevents speex from successful decoding) |
| 466 | * place a hint telling the callback how much of the |
| 467 | * previous buffer we have consumed such that it can rewind |
| 468 | * as necessary */ |
| 469 | int bitPtr = td->bits.bitPtr; |
| 470 | td->vi.size = td->bits.charPtr; |
| 471 | td->vi.get_more(&td->vi.start, &td->vi.size); |
| 472 | speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start, |
| 473 | td->vi.size); |
| 474 | td->bits.bitPtr = bitPtr; |
| 475 | } |
| 476 | |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 477 | yield(); |
| 478 | |
| 479 | /* Output the decoded frame */ |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 480 | td->src.remcount = VOICE_FRAME_COUNT - td->lookahead; |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 481 | td->src.pin[0] = &voice_buf->spx_outbuf[td->lookahead]; |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 482 | td->src.pin[1] = NULL; |
| 483 | td->src.proc_mask = 0; |
| 484 | |
Michael Sevakis | cfc32fe | 2012-04-26 15:57:07 -0400 | [diff] [blame] | 485 | td->lookahead -= MIN(VOICE_FRAME_COUNT, td->lookahead); |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 486 | |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 487 | if (td->src.remcount > 0) |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 488 | return VOICE_STATE_BUFFER_INSERT; |
| 489 | } |
| 490 | |
| 491 | return VOICE_STATE_DECODE; |
| 492 | } |
| 493 | |
| 494 | /* Process the PCM samples in the DSP and send out for mixing */ |
| 495 | static enum voice_state voice_buffer_insert(struct voice_thread_data *td) |
| 496 | { |
| 497 | if (!queue_empty(&voice_queue)) |
| 498 | return VOICE_STATE_MESSAGE; |
| 499 | |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 500 | struct dsp_buffer dst; |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 501 | |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 502 | if ((dst.p16out = voice_buf_get()) != NULL) |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 503 | { |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 504 | dst.remcount = 0; |
| 505 | dst.bufcount = VOICE_PCM_FRAME_COUNT; |
| 506 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 507 | td->dst = &dst; |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 508 | dsp_process(td->dsp, &td->src, &dst); |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 509 | td->dst = NULL; |
Michael Sevakis | c9bcbe2 | 2012-03-27 19:52:15 -0400 | [diff] [blame] | 510 | |
| 511 | voice_buf_commit(dst.remcount); |
| 512 | |
| 513 | /* Unless other effects are introduced to voice that have delays, |
| 514 | all output should have been purged to dst in one call */ |
| 515 | return td->src.remcount > 0 ? |
| 516 | VOICE_STATE_BUFFER_INSERT : VOICE_STATE_DECODE; |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | sleep(0); |
| 520 | return VOICE_STATE_BUFFER_INSERT; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* Voice thread entrypoint */ |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 524 | static void NORETURN_ATTR voice_thread(void) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 525 | { |
| 526 | struct voice_thread_data td; |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 527 | enum voice_state state = VOICE_STATE_MESSAGE; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 528 | |
| 529 | voice_data_init(&td); |
Stéphane Doyon | c893aff | 2008-07-15 14:06:11 +0000 | [diff] [blame] | 530 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 531 | while (1) |
| 532 | { |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 533 | switch (state) |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 534 | { |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 535 | case VOICE_STATE_MESSAGE: |
| 536 | state = voice_message(&td); |
| 537 | break; |
| 538 | case VOICE_STATE_DECODE: |
| 539 | state = voice_decode(&td); |
| 540 | break; |
| 541 | case VOICE_STATE_BUFFER_INSERT: |
| 542 | state = voice_buffer_insert(&td); |
| 543 | break; |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 544 | } |
Michael Sevakis | 456170a | 2011-08-30 15:35:25 +0000 | [diff] [blame] | 545 | } |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 546 | } |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 547 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 548 | /* Initialize buffers, all synchronization objects and create the thread */ |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 549 | void voice_thread_init(void) |
| 550 | { |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 551 | if (voice_thread_id != 0) |
| 552 | return; /* Already did an init and succeeded at it */ |
| 553 | |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 554 | voice_buf_hid = core_alloc_ex("voice buf", sizeof (*voice_buf), &ops); |
| 555 | |
| 556 | if (voice_buf_hid <= 0) |
| 557 | { |
| 558 | logf("voice: core_alloc_ex failed"); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | voice_buf = core_get_data(voice_buf_hid); |
| 563 | |
| 564 | if (voice_buf == NULL) |
| 565 | { |
| 566 | logf("voice: core_get_data failed"); |
| 567 | core_free(voice_buf_hid); |
| 568 | voice_buf_hid = 0; |
| 569 | return; |
| 570 | } |
| 571 | |
Michael Sevakis | 8bbd4d9 | 2012-05-17 11:16:20 -0400 | [diff] [blame] | 572 | memset(voice_buf, 0, sizeof (*voice_buf)); |
| 573 | |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 574 | logf("Starting voice thread"); |
| 575 | queue_init(&voice_queue, false); |
Michael Sevakis | effceea | 2008-10-23 13:13:00 +0000 | [diff] [blame] | 576 | |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 577 | voice_thread_id = create_thread(voice_thread, voice_stack, |
Michael Sevakis | 6d3a6f7 | 2011-09-01 07:32:07 +0000 | [diff] [blame] | 578 | sizeof(voice_stack), 0, voice_thread_name |
| 579 | IF_PRIO(, PRIORITY_VOICE) IF_COP(, CPU)); |
Michael Sevakis | 27cf677 | 2008-03-25 02:34:12 +0000 | [diff] [blame] | 580 | |
| 581 | queue_enable_queue_send(&voice_queue, &voice_queue_sender_list, |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 582 | voice_thread_id); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | #ifdef HAVE_PRIORITY_SCHEDULING |
| 586 | /* Set the voice thread priority */ |
| 587 | void voice_thread_set_priority(int priority) |
| 588 | { |
Michael Sevakis | da6cebb | 2012-05-02 17:22:28 -0400 | [diff] [blame] | 589 | if (voice_thread_id == 0) |
| 590 | return; |
| 591 | |
Michael Sevakis | a2b6703 | 2011-06-29 06:37:04 +0000 | [diff] [blame] | 592 | if (priority > PRIORITY_VOICE) |
| 593 | priority = PRIORITY_VOICE; |
| 594 | |
Michael Sevakis | 8cfbd36 | 2008-12-10 08:57:10 +0000 | [diff] [blame] | 595 | thread_set_priority(voice_thread_id, priority); |
Michael Sevakis | 99617d7 | 2007-11-18 17:12:19 +0000 | [diff] [blame] | 596 | } |
| 597 | #endif |