Fix manual building for iPod 1G/2G/3G - scrollwheel is not defined for these in addit...
[maemo-rb.git] / apps / voice_thread.c
blob86e80cece3797ccb35e8789b410f0afd7fb646b8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Michael Sevakis
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
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"
27 #include "audio.h"
28 #include "playback.h"
29 #include "pcmbuf.h"
30 #include "codecs/libspeex/speex/speex.h"
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
36 #if VOICE_LOGQUEUES
37 #define LOGFQUEUE logf
38 #else
39 #define LOGFQUEUE(...)
40 #endif
42 #if VOICE_LOGQUEUES_SYS_TIMEOUT
43 #define LOGFQUEUE_SYS_TIMEOUT logf
44 #else
45 #define LOGFQUEUE_SYS_TIMEOUT(...)
46 #endif
48 #ifndef IBSS_ATTR_VOICE_STACK
49 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
50 #endif
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 */
56 /* Voice thread variables */
57 static unsigned int voice_thread_id = 0;
58 static long voice_stack[0x7c0/sizeof(long)] IBSS_ATTR_VOICE_STACK;
59 static const char voice_thread_name[] = "voice";
61 /* Voice thread synchronization objects */
62 static struct event_queue voice_queue SHAREDBSS_ATTR;
63 static struct mutex voice_mutex SHAREDBSS_ATTR;
64 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
65 static bool voice_done SHAREDDATA_ATTR = true;
67 /* Buffer for decoded samples */
68 static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
70 enum voice_thread_states
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 */
77 enum voice_thread_messages
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 */
85 /* Structure to store clip data callback info */
86 struct voice_info
88 pcm_more_callback_type get_more; /* Callback to get more clips */
89 unsigned char *start; /* Start of clip */
90 size_t size; /* Size of clip */
93 /* Private thread data for its current state that must be passed to its
94 * internal functions */
95 struct voice_thread_data
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 */
108 /* Audio playback is in a playing state? */
109 static inline bool playback_is_playing(void)
111 return (audio_status() & AUDIO_STATUS_PLAY) != 0;
114 /* Stop any current clip and start playing a new one */
115 void mp3_play_data(const unsigned char* start, int size,
116 pcm_more_callback_type get_more)
118 /* Shared struct to get data to the thread - once it replies, it has
119 * safely cached it in its own private data */
120 static struct voice_info voice_clip SHAREDBSS_ATTR;
122 if (get_more != NULL && start != NULL && (ssize_t)size > 0)
124 mutex_lock(&voice_mutex);
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);
132 mutex_unlock(&voice_mutex);
136 /* Stop current voice clip from playing */
137 void mp3_play_stop(void)
139 if(!audio_is_thread_ready())
140 return;
142 mutex_lock(&voice_mutex); /* Sync against voice_stop */
143 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
144 queue_send(&voice_queue, Q_VOICE_STOP, 1);
146 mutex_unlock(&voice_mutex);
149 void mp3_play_pause(bool play)
151 /* a dummy */
152 (void)play;
155 /* Tell is voice is still in a playing state */
156 bool mp3_is_playing(void)
158 /* TODO: Implement a timeout or state query function for event objects */
159 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
160 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
161 return state != TSTATE_STOPPED;
164 /* This function is meant to be used by the buffer request functions to
165 ensure the codec is no longer active */
166 void voice_stop(void)
168 mutex_lock(&voice_mutex);
170 /* Stop the output and current clip */
171 mp3_play_stop();
173 /* Careful if using sync objects in talk.c - make sure locking order is
174 * observed with one or the other always granted first */
176 /* Unqueue all future clips */
177 talk_force_shutup();
179 /* 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);
183 mutex_unlock(&voice_mutex);
184 } /* voice_stop */
186 /* Wait for voice to finish speaking. */
187 void voice_wait(void)
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. */
193 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
194 while(!voice_done || (!playback_is_playing() && pcm_is_playing()))
195 sleep(1);
198 /* Initialize voice thread data that must be valid upon starting and the
199 * setup the DSP parameters */
200 static void voice_data_init(struct voice_thread_data *td)
202 td->state = TSTATE_STOPPED;
203 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
204 CODEC_IDX_VOICE);
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);
212 /* Voice thread message processing */
213 static void voice_message(struct voice_thread_data *td)
215 while (1)
217 switch (td->ev.id)
219 case Q_VOICE_PLAY:
220 LOGFQUEUE("voice < Q_VOICE_PLAY");
221 /* Put up a block for completion signal */
222 voice_done = false;
224 /* Copy the clip info */
225 td->vi = *(struct voice_info *)td->ev.data;
227 /* Be sure audio buffer is initialized */
228 audio_restore_playback(AUDIO_WANT_VOICE);
230 /* We need nothing more from the sending thread - let it run */
231 queue_reply(&voice_queue, 1);
233 if (td->state == TSTATE_STOPPED)
235 /* Boost CPU now */
236 trigger_cpu_boost();
238 else if (!playback_is_playing())
240 /* Just voice, stop any clip still playing */
241 pcmbuf_play_stop();
244 /* Clean-start the decoder */
245 td->st = speex_decoder_init(&speex_wb_mode);
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);
251 td->state = TSTATE_DECODE;
252 return;
254 case Q_VOICE_STOP:
255 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev.data);
257 if (td->ev.data != 0 && !playback_is_playing())
259 /* If not playing, it's just voice so stop pcm playback */
260 pcmbuf_play_stop();
263 /* Cancel boost */
264 cancel_cpu_boost();
266 td->state = TSTATE_STOPPED;
267 voice_done = true;
268 break;
270 case Q_VOICE_STATE:
271 LOGFQUEUE("voice < Q_VOICE_STATE");
272 queue_reply(&voice_queue, td->state);
274 if (td->state == TSTATE_STOPPED)
275 break; /* Not in a playback state */
277 return;
279 default:
280 /* Default messages get a reply and thread continues with no
281 * state transition */
282 LOGFQUEUE("voice < default");
284 if (td->state == TSTATE_STOPPED)
285 break; /* Not in playback state */
287 queue_reply(&voice_queue, 0);
288 return;
291 queue_wait(&voice_queue, &td->ev);
295 /* Voice thread entrypoint */
296 static void voice_thread(void)
298 struct voice_thread_data td;
300 voice_data_init(&td);
302 /* audio thread will only set this once after it finished the final
303 * audio hardware init so this little construct is safe - even
304 * cross-core. */
305 while (!audio_is_thread_ready())
306 sleep(0);
308 goto message_wait;
310 while (1)
312 td.state = TSTATE_DECODE;
314 if (!queue_empty(&voice_queue))
316 message_wait:
317 queue_wait(&voice_queue, &td.ev);
319 message_process:
320 voice_message(&td);
322 /* Branch to initial start point or branch back to previous
323 * operation if interrupted by a message */
324 switch (td.state)
326 case TSTATE_DECODE: goto voice_decode;
327 case TSTATE_BUFFER_INSERT: goto buffer_insert;
328 default: goto message_wait;
332 voice_decode:
333 /* Decode the data */
334 if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
336 /* End of stream or error - get next clip */
337 td.vi.size = 0;
339 if (td.vi.get_more != NULL)
340 td.vi.get_more(&td.vi.start, &td.vi.size);
342 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
344 /* Make bit buffer use our own buffer */
345 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
346 /* Don't skip any samples when we're stringing clips together */
347 td.lookahead = 0;
349 /* Paranoid check - be sure never to somehow get stuck in a
350 * loop without listening to the queue */
351 yield();
353 if (!queue_empty(&voice_queue))
354 goto message_wait;
355 else
356 goto voice_decode;
359 /* If all clips are done and not playing, force pcm playback. */
360 if (!pcm_is_playing())
361 pcmbuf_play_start();
363 /* Synthesize a stop request */
364 /* NOTE: We have no way to know when the pcm data placed in the
365 * buffer is actually consumed and playback has reached the end
366 * so until the info is available or inferred somehow, this will
367 * not be accurate and the stopped signal will come too soon.
368 * ie. You may not hear the "Shutting Down" splash even though
369 * it waits for voice to stop. */
370 td.ev.id = Q_VOICE_STOP;
371 td.ev.data = 0; /* Let PCM drain by itself */
372 yield();
373 goto message_process;
376 yield();
378 /* Output the decoded frame */
379 td.count = VOICE_FRAME_SIZE - td.lookahead;
380 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
381 td.src[1] = NULL;
382 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
384 buffer_insert:
385 /* Process the PCM samples in the DSP and send out for mixing */
386 td.state = TSTATE_BUFFER_INSERT;
388 while (td.count > 0)
390 int out_count = dsp_output_count(td.dsp, td.count);
391 int inp_count;
392 char *dest;
394 while (1)
396 if (!queue_empty(&voice_queue))
397 goto message_wait;
399 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
400 break;
402 yield();
405 /* Get the real input_size for output_size bytes, guarding
406 * against resampling buffer overflows. */
407 inp_count = dsp_input_count(td.dsp, out_count);
409 if (inp_count <= 0)
410 break;
412 /* Input size has grown, no error, just don't write more than
413 * length */
414 if (inp_count > td.count)
415 inp_count = td.count;
417 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
419 if (out_count <= 0)
420 break;
422 pcmbuf_write_voice_complete(out_count);
423 td.count -= inp_count;
426 yield();
427 } /* end while */
428 } /* voice_thread */
430 /* Initialize all synchronization objects create the thread */
431 void voice_thread_init(void)
433 logf("Starting voice thread");
434 queue_init(&voice_queue, false);
435 mutex_init(&voice_mutex);
437 voice_thread_id = create_thread(voice_thread, voice_stack,
438 sizeof(voice_stack), CREATE_THREAD_FROZEN,
439 voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
441 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
442 voice_thread_id);
443 } /* voice_thread_init */
445 /* Unfreeze the voice thread */
446 void voice_thread_resume(void)
448 logf("Thawing voice thread");
449 thread_thaw(voice_thread_id);
452 #ifdef HAVE_PRIORITY_SCHEDULING
453 /* Set the voice thread priority */
454 void voice_thread_set_priority(int priority)
456 thread_set_priority(voice_thread_id, priority);
458 #endif