Fix some sim warnings.
[Rockbox.git] / apps / voice_thread.c
blobc104fa5c7f7a2793ef1b05820aa7224921956dfd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Michael Sevakis
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "system.h"
20 #include "thread.h"
21 #include "logf.h"
22 #include "voice_thread.h"
23 #include "talk.h"
24 #include "dsp.h"
25 #include "audio.h"
26 #include "pcmbuf.h"
27 #include "codecs/libspeex/speex/speex.h"
29 /* Define any of these as "1" to log regular and/or timeout messages */
30 #define VOICE_LOGQUEUES 0
31 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
33 #if VOICE_LOGQUEUES
34 #define LOGFQUEUE logf
35 #else
36 #define LOGFQUEUE(...)
37 #endif
39 #if VOICE_LOGQUEUES_SYS_TIMEOUT
40 #define LOGFQUEUE_SYS_TIMEOUT logf
41 #else
42 #define LOGFQUEUE_SYS_TIMEOUT(...)
43 #endif
45 #ifndef IBSS_ATTR_VOICE_STACK
46 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
47 #endif
49 #define VOICE_FRAME_SIZE 320 /* Samples / frame */
50 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
51 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
53 /* Voice thread variables */
54 static struct thread_entry *voice_thread_p = NULL;
55 static long voice_stack[0x740/sizeof(long)] IBSS_ATTR_VOICE_STACK;
56 static const char voice_thread_name[] = "voice";
58 /* Voice thread synchronization objects */
59 static struct event_queue voice_queue NOCACHEBSS_ATTR;
60 static struct mutex voice_mutex NOCACHEBSS_ATTR;
61 static struct event voice_event NOCACHEBSS_ATTR;
62 static struct queue_sender_list voice_queue_sender_list NOCACHEBSS_ATTR;
64 /* Buffer for decoded samples */
65 static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
67 enum voice_thread_states
69 TSTATE_STOPPED = 0, /* Voice thread is stopped and awaiting commands */
70 TSTATE_DECODE, /* Voice is decoding a clip */
71 TSTATE_BUFFER_INSERT, /* Voice is sending decoded audio to PCM */
74 enum voice_thread_messages
76 Q_VOICE_NULL = 0, /* A message for thread sync - no effect on state */
77 Q_VOICE_PLAY, /* Play a clip */
78 Q_VOICE_STOP, /* Stop current clip */
79 Q_VOICE_STATE, /* Query playing state */
82 /* Structure to store clip data callback info */
83 struct voice_info
85 pcm_more_callback_type get_more; /* Callback to get more clips */
86 unsigned char *start; /* Start of clip */
87 size_t size; /* Size of clip */
90 /* Private thread data for its current state that must be passed to its
91 * internal functions */
92 struct voice_thread_data
94 int state; /* Thread state (TSTATE_*) */
95 struct queue_event ev; /* Last queue event pulled from queue */
96 void *st; /* Decoder instance */
97 SpeexBits bits; /* Bit cursor */
98 struct dsp_config *dsp; /* DSP used for voice output */
99 struct voice_info vi; /* Copy of clip data */
100 const char *src[2]; /* Current output buffer pointers */
101 int lookahead; /* Number of samples to drop at start of clip */
102 int count; /* Count of samples remaining to send to PCM */
105 /* Audio playback is in a playing state? */
106 static inline bool playback_is_playing(void)
108 return (audio_status() & AUDIO_STATUS_PLAY) != 0;
111 /* Stop any current clip and start playing a new one */
112 void mp3_play_data(const unsigned char* start, int size,
113 pcm_more_callback_type get_more)
115 /* Shared struct to get data to the thread - once it replies, it has
116 * safely cached it in its own private data */
117 static struct voice_info voice_clip NOCACHEBSS_ATTR;
119 if (get_more != NULL && start != NULL && (ssize_t)size > 0)
121 mutex_lock(&voice_mutex);
123 voice_clip.get_more = get_more;
124 voice_clip.start = (unsigned char *)start;
125 voice_clip.size = size;
126 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
127 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
129 mutex_unlock(&voice_mutex);
133 /* Stop current voice clip from playing */
134 void mp3_play_stop(void)
136 mutex_lock(&voice_mutex); /* Sync against voice_stop */
138 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
139 queue_send(&voice_queue, Q_VOICE_STOP, 1);
141 mutex_unlock(&voice_mutex);
144 void mp3_play_pause(bool play)
146 /* a dummy */
147 (void)play;
150 /* Tell is voice is still in a playing state */
151 bool mp3_is_playing(void)
153 /* TODO: Implement a timeout or state query function for event objects */
154 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
155 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
156 return state != TSTATE_STOPPED;
159 /* This function is meant to be used by the buffer request functions to
160 ensure the codec is no longer active */
161 void voice_stop(void)
163 mutex_lock(&voice_mutex);
165 /* Stop the output and current clip */
166 mp3_play_stop();
168 /* Careful if using sync objects in talk.c - make sure locking order is
169 * observed with one or the other always granted first */
171 /* Unqueue all future clips */
172 talk_force_shutup();
174 mutex_unlock(&voice_mutex);
175 } /* voice_stop */
177 /* Wait for voice to finish speaking. */
178 void voice_wait(void)
180 /* NOTE: One problem here is that we can't tell if another thread started a
181 * new clip by the time we wait. This should be resolvable if conditions
182 * ever require knowing the very clip you requested has finished. */
183 event_wait(&voice_event, STATE_SIGNALED);
186 /* Initialize voice thread data that must be valid upon starting and the
187 * setup the DSP parameters */
188 static void voice_data_init(struct voice_thread_data *td)
190 td->state = TSTATE_STOPPED;
191 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
192 CODEC_IDX_VOICE);
194 dsp_configure(td->dsp, DSP_RESET, 0);
195 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
196 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
197 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
200 /* Voice thread message processing */
201 static void voice_message(struct voice_thread_data *td)
203 while (1)
205 switch (td->ev.id)
207 case Q_VOICE_PLAY:
208 LOGFQUEUE("voice < Q_VOICE_PLAY");
209 /* Put up a block for completion signal */
210 event_set_state(&voice_event, STATE_NONSIGNALED);
212 /* Copy the clip info */
213 td->vi = *(struct voice_info *)td->ev.data;
215 /* Be sure audio buffer is initialized */
216 audio_restore_playback(AUDIO_WANT_VOICE);
218 /* We need nothing more from the sending thread - let it run */
219 queue_reply(&voice_queue, 1);
221 if (td->state == TSTATE_STOPPED)
223 /* Boost CPU now */
224 trigger_cpu_boost();
226 else if (!playback_is_playing())
228 /* Just voice, stop any clip still playing */
229 pcmbuf_play_stop();
232 /* Clean-start the decoder */
233 td->st = speex_decoder_init(&speex_wb_mode);
235 /* Make bit buffer use our own buffer */
236 speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
237 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
239 td->state = TSTATE_DECODE;
240 return;
242 case Q_VOICE_STOP:
243 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev.data);
245 if (td->ev.data != 0 && !playback_is_playing())
247 /* If not playing, it's just voice so stop pcm playback */
248 pcmbuf_play_stop();
251 /* Cancel boost */
252 sleep(0);
254 td->state = TSTATE_STOPPED;
255 event_set_state(&voice_event, STATE_SIGNALED);
256 break;
258 case Q_VOICE_STATE:
259 LOGFQUEUE("voice < Q_VOICE_STATE");
260 queue_reply(&voice_queue, td->state);
262 if (td->state == TSTATE_STOPPED)
263 break; /* Not in a playback state */
265 return;
267 default:
268 /* Default messages get a reply and thread continues with no
269 * state transition */
270 LOGFQUEUE("voice < default");
272 if (td->state == TSTATE_STOPPED)
273 break; /* Not in playback state */
275 queue_reply(&voice_queue, 0);
276 return;
279 queue_wait(&voice_queue, &td->ev);
283 /* Voice thread entrypoint */
284 static void voice_thread(void)
286 struct voice_thread_data td;
288 voice_data_init(&td);
290 goto message_wait;
292 while (1)
294 td.state = TSTATE_DECODE;
296 if (!queue_empty(&voice_queue))
298 message_wait:
299 queue_wait(&voice_queue, &td.ev);
301 message_process:
302 voice_message(&td);
304 /* Branch to initial start point or branch back to previous
305 * operation if interrupted by a message */
306 switch (td.state)
308 case TSTATE_DECODE: goto voice_decode;
309 case TSTATE_BUFFER_INSERT: goto buffer_insert;
310 default: goto message_wait;
314 voice_decode:
315 /* Check if all data was exhausted for this clip */
316 if (speex_bits_remaining(&td.bits) < 8)
318 voice_error:
319 /* Get next clip */
320 td.vi.size = 0;
322 if (td.vi.get_more != NULL)
323 td.vi.get_more(&td.vi.start, &td.vi.size);
325 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
327 /* Make bit buffer use our own buffer */
328 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
329 speex_decoder_ctl(td.st, SPEEX_GET_LOOKAHEAD, &td.lookahead);
331 yield();
333 if (!queue_empty(&voice_queue))
334 goto message_wait;
335 else
336 goto voice_decode;
339 /* If all clips are done and not playing, force pcm playback. */
340 if (!pcm_is_playing())
341 pcmbuf_play_start();
343 /* Synthesize a stop request */
344 /* NOTE: We have no way to know when the pcm data placed in the
345 * buffer is actually consumed and playback has reached the end
346 * so until the info is available or inferred somehow, this will
347 * not be accurate and the stopped signal will come too soon.
348 * ie. You may not hear the "Shutting Down" splash even though
349 * it waits for voice to stop. */
350 td.ev.id = Q_VOICE_STOP;
351 td.ev.data = 0; /* Let PCM drain by itself */
352 yield();
353 goto message_process;
356 /* Decode the data */
357 int status = speex_decode_int(td.st, &td.bits, voice_output_buf);
358 yield();
360 if (status == -2)
361 goto voice_error; /* error - try some more */
363 /* Output the decoded frame */
364 td.count = VOICE_FRAME_SIZE - td.lookahead;
365 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
366 td.src[1] = NULL;
367 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
369 buffer_insert:
370 /* Process the PCM samples in the DSP and send out for mixing */
371 td.state = TSTATE_BUFFER_INSERT;
373 while (td.count > 0)
375 int out_count = dsp_output_count(td.dsp, td.count);
376 int inp_count;
377 char *dest;
379 while (1)
381 if (!queue_empty(&voice_queue))
382 goto message_wait;
384 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
385 break;
387 yield();
390 /* Get the real input_size for output_size bytes, guarding
391 * against resampling buffer overflows. */
392 inp_count = dsp_input_count(td.dsp, out_count);
394 if (inp_count <= 0)
395 break;
397 /* Input size has grown, no error, just don't write more than
398 * length */
399 if (inp_count > td.count)
400 inp_count = td.count;
402 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
404 if (out_count <= 0)
405 break;
407 pcmbuf_write_voice_complete(out_count);
408 td.count -= inp_count;
411 yield();
412 } /* end while */
413 } /* voice_thread */
415 /* Initialize all synchronization objects create the thread */
416 void voice_thread_init(void)
418 logf("Starting voice thread");
419 queue_init(&voice_queue, false);
420 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list);
421 mutex_init(&voice_mutex);
422 event_init(&voice_event, STATE_SIGNALED | EVENT_MANUAL);
423 voice_thread_p = create_thread(voice_thread, voice_stack,
424 sizeof(voice_stack), CREATE_THREAD_FROZEN,
425 voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
426 } /* voice_thread_init */
428 /* Unfreeze the voice thread */
429 void voice_thread_resume(void)
431 logf("Thawing voice thread");
432 thread_thaw(voice_thread_p);
433 /* Wait for initialization to complete (a very short wait until the
434 * voice thread is available to process messages) */
435 queue_send(&voice_queue, Q_VOICE_NULL, 0);
438 #ifdef HAVE_PRIORITY_SCHEDULING
439 /* Set the voice thread priority */
440 void voice_thread_set_priority(int priority)
442 thread_set_priority(voice_thread_p, priority);
444 #endif