blob: 8d08e7744b291ad7318fe2fb147d68a2e46aa22a [file] [log] [blame]
Michael Sevakis99617d72007-11-18 17:12:19 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 Michael Sevakis
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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 Sevakis99617d72007-11-18 17:12:19 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include "system.h"
22#include "thread.h"
23#include "logf.h"
24#include "voice_thread.h"
25#include "talk.h"
26#include "dsp.h"
Nicolas Pennequin4fd27742008-03-29 14:09:14 +000027#include "audio.h"
Steve Bavinc9df8fd2008-03-28 11:24:24 +000028#include "playback.h"
Michael Sevakis99617d72007-11-18 17:12:19 +000029#include "pcmbuf.h"
30#include "codecs/libspeex/speex/speex.h"
31
32/* Define any of these as "1" to log regular and/or timeout messages */
33#define VOICE_LOGQUEUES 0
34#define VOICE_LOGQUEUES_SYS_TIMEOUT 0
35
36#if VOICE_LOGQUEUES
37#define LOGFQUEUE logf
38#else
39#define LOGFQUEUE(...)
40#endif
41
42#if VOICE_LOGQUEUES_SYS_TIMEOUT
43#define LOGFQUEUE_SYS_TIMEOUT logf
44#else
45#define LOGFQUEUE_SYS_TIMEOUT(...)
46#endif
47
48#ifndef IBSS_ATTR_VOICE_STACK
49#define IBSS_ATTR_VOICE_STACK IBSS_ATTR
50#endif
51
52#define VOICE_FRAME_SIZE 320 /* Samples / frame */
53#define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
54#define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
55
56/* Voice thread variables */
57static struct thread_entry *voice_thread_p = NULL;
Jens Arnold34e7fdb2007-11-18 20:55:43 +000058static long voice_stack[0x7c0/sizeof(long)] IBSS_ATTR_VOICE_STACK;
Michael Sevakis99617d72007-11-18 17:12:19 +000059static const char voice_thread_name[] = "voice";
60
61/* Voice thread synchronization objects */
Michael Sevakis05099142008-04-06 04:34:57 +000062static struct event_queue voice_queue SHAREDBSS_ATTR;
63static struct mutex voice_mutex SHAREDBSS_ATTR;
64static struct event voice_event SHAREDBSS_ATTR;
65static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
Michael Sevakis99617d72007-11-18 17:12:19 +000066
67/* Buffer for decoded samples */
68static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
69
70enum voice_thread_states
71{
72 TSTATE_STOPPED = 0, /* Voice thread is stopped and awaiting commands */
73 TSTATE_DECODE, /* Voice is decoding a clip */
74 TSTATE_BUFFER_INSERT, /* Voice is sending decoded audio to PCM */
75};
76
77enum voice_thread_messages
78{
79 Q_VOICE_NULL = 0, /* A message for thread sync - no effect on state */
80 Q_VOICE_PLAY, /* Play a clip */
81 Q_VOICE_STOP, /* Stop current clip */
82 Q_VOICE_STATE, /* Query playing state */
83};
84
85/* Structure to store clip data callback info */
86struct voice_info
87{
88 pcm_more_callback_type get_more; /* Callback to get more clips */
89 unsigned char *start; /* Start of clip */
Michael Sevakise8d81f92007-11-18 17:34:33 +000090 size_t size; /* Size of clip */
Michael Sevakis99617d72007-11-18 17:12:19 +000091};
92
93/* Private thread data for its current state that must be passed to its
94 * internal functions */
95struct voice_thread_data
96{
97 int state; /* Thread state (TSTATE_*) */
98 struct queue_event ev; /* Last queue event pulled from queue */
99 void *st; /* Decoder instance */
100 SpeexBits bits; /* Bit cursor */
101 struct dsp_config *dsp; /* DSP used for voice output */
102 struct voice_info vi; /* Copy of clip data */
103 const char *src[2]; /* Current output buffer pointers */
104 int lookahead; /* Number of samples to drop at start of clip */
105 int count; /* Count of samples remaining to send to PCM */
106};
107
108/* Audio playback is in a playing state? */
109static inline bool playback_is_playing(void)
110{
111 return (audio_status() & AUDIO_STATUS_PLAY) != 0;
112}
113
114/* Stop any current clip and start playing a new one */
115void mp3_play_data(const unsigned char* start, int size,
116 pcm_more_callback_type get_more)
117{
118 /* Shared struct to get data to the thread - once it replies, it has
119 * safely cached it in its own private data */
Michael Sevakis05099142008-04-06 04:34:57 +0000120 static struct voice_info voice_clip SHAREDBSS_ATTR;
Michael Sevakis99617d72007-11-18 17:12:19 +0000121
Michael Sevakise8d81f92007-11-18 17:34:33 +0000122 if (get_more != NULL && start != NULL && (ssize_t)size > 0)
Michael Sevakis99617d72007-11-18 17:12:19 +0000123 {
124 mutex_lock(&voice_mutex);
125
126 voice_clip.get_more = get_more;
127 voice_clip.start = (unsigned char *)start;
128 voice_clip.size = size;
129 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
130 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
131
132 mutex_unlock(&voice_mutex);
133 }
134}
135
136/* Stop current voice clip from playing */
137void mp3_play_stop(void)
138{
139 mutex_lock(&voice_mutex); /* Sync against voice_stop */
140
Thom Johansen91c35ff2007-12-04 15:11:45 +0000141 LOGFQUEUE("mp3 > voice Q_VOICE_STOP: 1");
142 queue_remove_from_head(&voice_queue, Q_VOICE_STOP);
143 queue_post(&voice_queue, Q_VOICE_STOP, 1);
Michael Sevakis99617d72007-11-18 17:12:19 +0000144
145 mutex_unlock(&voice_mutex);
146}
147
148void mp3_play_pause(bool play)
149{
150 /* a dummy */
151 (void)play;
152}
153
154/* Tell is voice is still in a playing state */
155bool mp3_is_playing(void)
156{
157 /* TODO: Implement a timeout or state query function for event objects */
158 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
159 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
160 return state != TSTATE_STOPPED;
161}
162
163/* This function is meant to be used by the buffer request functions to
164 ensure the codec is no longer active */
165void voice_stop(void)
166{
167 mutex_lock(&voice_mutex);
168
169 /* Stop the output and current clip */
Thom Johansen91c35ff2007-12-04 15:11:45 +0000170 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
171 queue_send(&voice_queue, Q_VOICE_STOP, 1);
Michael Sevakis99617d72007-11-18 17:12:19 +0000172
173 /* Careful if using sync objects in talk.c - make sure locking order is
174 * observed with one or the other always granted first */
175
176 /* Unqueue all future clips */
177 talk_force_shutup();
178
Michael Sevakisa9d73e52007-12-11 14:04:03 +0000179 /* Wait for any final queue_post to be processed */
180 LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
181 queue_send(&voice_queue, Q_VOICE_NULL, 0);
182
Michael Sevakis99617d72007-11-18 17:12:19 +0000183 mutex_unlock(&voice_mutex);
184} /* voice_stop */
185
186/* Wait for voice to finish speaking. */
187void voice_wait(void)
188{
189 /* NOTE: One problem here is that we can't tell if another thread started a
190 * new clip by the time we wait. This should be resolvable if conditions
191 * ever require knowing the very clip you requested has finished. */
192 event_wait(&voice_event, STATE_SIGNALED);
Stéphane Doyon686b1142007-11-24 14:21:04 +0000193 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
194 while(!playback_is_playing() && pcm_is_playing())
195 sleep(1);
Michael Sevakis99617d72007-11-18 17:12:19 +0000196}
197
198/* Initialize voice thread data that must be valid upon starting and the
199 * setup the DSP parameters */
200static void voice_data_init(struct voice_thread_data *td)
201{
202 td->state = TSTATE_STOPPED;
203 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
204 CODEC_IDX_VOICE);
205
206 dsp_configure(td->dsp, DSP_RESET, 0);
207 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
208 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
209 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
210}
211
212/* Voice thread message processing */
213static void voice_message(struct voice_thread_data *td)
214{
215 while (1)
216 {
217 switch (td->ev.id)
218 {
219 case Q_VOICE_PLAY:
220 LOGFQUEUE("voice < Q_VOICE_PLAY");
221 /* Put up a block for completion signal */
222 event_set_state(&voice_event, STATE_NONSIGNALED);
223
224 /* Copy the clip info */
225 td->vi = *(struct voice_info *)td->ev.data;
226
227 /* Be sure audio buffer is initialized */
228 audio_restore_playback(AUDIO_WANT_VOICE);
229
230 /* We need nothing more from the sending thread - let it run */
231 queue_reply(&voice_queue, 1);
232
233 if (td->state == TSTATE_STOPPED)
234 {
235 /* Boost CPU now */
236 trigger_cpu_boost();
237 }
238 else if (!playback_is_playing())
239 {
240 /* Just voice, stop any clip still playing */
241 pcmbuf_play_stop();
242 }
243
244 /* Clean-start the decoder */
245 td->st = speex_decoder_init(&speex_wb_mode);
246
247 /* Make bit buffer use our own buffer */
248 speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
249 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
250
251 td->state = TSTATE_DECODE;
252 return;
253
254 case Q_VOICE_STOP:
255 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev.data);
256
257 if (td->ev.data != 0 && !playback_is_playing())
258 {
259 /* If not playing, it's just voice so stop pcm playback */
260 pcmbuf_play_stop();
261 }
262
263 /* Cancel boost */
Michael Sevakisfadbf0a2007-11-20 03:44:25 +0000264 cancel_cpu_boost();
Michael Sevakis99617d72007-11-18 17:12:19 +0000265
266 td->state = TSTATE_STOPPED;
267 event_set_state(&voice_event, STATE_SIGNALED);
268 break;
269
270 case Q_VOICE_STATE:
271 LOGFQUEUE("voice < Q_VOICE_STATE");
272 queue_reply(&voice_queue, td->state);
273
274 if (td->state == TSTATE_STOPPED)
275 break; /* Not in a playback state */
276
277 return;
278
279 default:
280 /* Default messages get a reply and thread continues with no
281 * state transition */
282 LOGFQUEUE("voice < default");
283
284 if (td->state == TSTATE_STOPPED)
285 break; /* Not in playback state */
286
287 queue_reply(&voice_queue, 0);
288 return;
289 }
290
291 queue_wait(&voice_queue, &td->ev);
292 }
293}
294
295/* Voice thread entrypoint */
296static void voice_thread(void)
297{
298 struct voice_thread_data td;
299
300 voice_data_init(&td);
Michael Sevakisd6f2a542007-11-19 11:59:52 +0000301 audio_wait_for_init();
Michael Sevakis99617d72007-11-18 17:12:19 +0000302
303 goto message_wait;
304
305 while (1)
306 {
307 td.state = TSTATE_DECODE;
308
309 if (!queue_empty(&voice_queue))
310 {
311 message_wait:
312 queue_wait(&voice_queue, &td.ev);
313
314 message_process:
315 voice_message(&td);
316
317 /* Branch to initial start point or branch back to previous
318 * operation if interrupted by a message */
319 switch (td.state)
320 {
321 case TSTATE_DECODE: goto voice_decode;
322 case TSTATE_BUFFER_INSERT: goto buffer_insert;
323 default: goto message_wait;
324 }
325 }
326
327 voice_decode:
Michael Sevakis72df71a2007-11-21 03:09:54 +0000328 /* Decode the data */
329 if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
Michael Sevakis99617d72007-11-18 17:12:19 +0000330 {
Michael Sevakis72df71a2007-11-21 03:09:54 +0000331 /* End of stream or error - get next clip */
Michael Sevakis99617d72007-11-18 17:12:19 +0000332 td.vi.size = 0;
333
334 if (td.vi.get_more != NULL)
335 td.vi.get_more(&td.vi.start, &td.vi.size);
336
Michael Sevakise8d81f92007-11-18 17:34:33 +0000337 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
Michael Sevakis99617d72007-11-18 17:12:19 +0000338 {
339 /* Make bit buffer use our own buffer */
340 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
Thom Johansen50767232007-11-21 17:21:38 +0000341 /* Don't skip any samples when we're stringing clips together */
342 td.lookahead = 0;
Michael Sevakis99617d72007-11-18 17:12:19 +0000343
Michael Sevakis72df71a2007-11-21 03:09:54 +0000344 /* Paranoid check - be sure never to somehow get stuck in a
345 * loop without listening to the queue */
Michael Sevakis99617d72007-11-18 17:12:19 +0000346 yield();
347
348 if (!queue_empty(&voice_queue))
349 goto message_wait;
350 else
351 goto voice_decode;
352 }
353
354 /* If all clips are done and not playing, force pcm playback. */
355 if (!pcm_is_playing())
356 pcmbuf_play_start();
357
358 /* Synthesize a stop request */
359 /* NOTE: We have no way to know when the pcm data placed in the
360 * buffer is actually consumed and playback has reached the end
361 * so until the info is available or inferred somehow, this will
362 * not be accurate and the stopped signal will come too soon.
363 * ie. You may not hear the "Shutting Down" splash even though
364 * it waits for voice to stop. */
365 td.ev.id = Q_VOICE_STOP;
366 td.ev.data = 0; /* Let PCM drain by itself */
367 yield();
368 goto message_process;
369 }
370
Michael Sevakis99617d72007-11-18 17:12:19 +0000371 yield();
372
Michael Sevakis99617d72007-11-18 17:12:19 +0000373 /* Output the decoded frame */
374 td.count = VOICE_FRAME_SIZE - td.lookahead;
375 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
376 td.src[1] = NULL;
377 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
378
379 buffer_insert:
380 /* Process the PCM samples in the DSP and send out for mixing */
381 td.state = TSTATE_BUFFER_INSERT;
382
383 while (td.count > 0)
384 {
385 int out_count = dsp_output_count(td.dsp, td.count);
386 int inp_count;
387 char *dest;
388
389 while (1)
390 {
391 if (!queue_empty(&voice_queue))
392 goto message_wait;
393
394 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
395 break;
396
397 yield();
398 }
399
400 /* Get the real input_size for output_size bytes, guarding
401 * against resampling buffer overflows. */
402 inp_count = dsp_input_count(td.dsp, out_count);
403
404 if (inp_count <= 0)
405 break;
406
407 /* Input size has grown, no error, just don't write more than
408 * length */
409 if (inp_count > td.count)
410 inp_count = td.count;
411
412 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
413
414 if (out_count <= 0)
415 break;
416
417 pcmbuf_write_voice_complete(out_count);
418 td.count -= inp_count;
419 }
420
421 yield();
422 } /* end while */
423} /* voice_thread */
424
425/* Initialize all synchronization objects create the thread */
426void voice_thread_init(void)
427{
428 logf("Starting voice thread");
429 queue_init(&voice_queue, false);
Michael Sevakis99617d72007-11-18 17:12:19 +0000430 mutex_init(&voice_mutex);
431 event_init(&voice_event, STATE_SIGNALED | EVENT_MANUAL);
432 voice_thread_p = create_thread(voice_thread, voice_stack,
433 sizeof(voice_stack), CREATE_THREAD_FROZEN,
434 voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
Michael Sevakis27cf6772008-03-25 02:34:12 +0000435
436 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
437 voice_thread_p);
Michael Sevakis99617d72007-11-18 17:12:19 +0000438} /* voice_thread_init */
439
440/* Unfreeze the voice thread */
441void voice_thread_resume(void)
442{
443 logf("Thawing voice thread");
444 thread_thaw(voice_thread_p);
Michael Sevakis99617d72007-11-18 17:12:19 +0000445}
446
447#ifdef HAVE_PRIORITY_SCHEDULING
448/* Set the voice thread priority */
449void voice_thread_set_priority(int priority)
450{
451 thread_set_priority(voice_thread_p, priority);
452}
453#endif