Respect the 'scroll delay' setting more accurately between scrolling rounds
[maemo-rb.git] / apps / voice_thread.c
blob6683fcc0677cd4328b130335cf3317c6b54bf57c
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 <sys/types.h>
22 #include "system.h"
23 #include "thread.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" and uncomment the LOGF_ENABLE line to log
33 regular and/or timeout messages */
34 #define VOICE_LOGQUEUES 0
35 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
37 /*#define LOGF_ENABLE*/
38 #include "logf.h"
40 #if VOICE_LOGQUEUES
41 #define LOGFQUEUE logf
42 #else
43 #define LOGFQUEUE(...)
44 #endif
46 #if VOICE_LOGQUEUES_SYS_TIMEOUT
47 #define LOGFQUEUE_SYS_TIMEOUT logf
48 #else
49 #define LOGFQUEUE_SYS_TIMEOUT(...)
50 #endif
52 #ifndef IBSS_ATTR_VOICE_STACK
53 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
54 #endif
56 #define VOICE_FRAME_SIZE 320 /* Samples / frame */
57 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
58 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
60 /* Voice thread variables */
61 static unsigned int voice_thread_id = 0;
62 static long voice_stack[(DEFAULT_STACK_SIZE + 0x3C0)/sizeof(long)] IBSS_ATTR_VOICE_STACK;
63 static const char voice_thread_name[] = "voice";
65 /* Voice thread synchronization objects */
66 static struct event_queue voice_queue SHAREDBSS_ATTR;
67 static struct mutex voice_mutex SHAREDBSS_ATTR;
68 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
69 static bool voice_done SHAREDDATA_ATTR = true;
71 /* Buffer for decoded samples */
72 static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
74 enum voice_thread_states
76 TSTATE_STOPPED = 0, /* Voice thread is stopped and awaiting commands */
77 TSTATE_DECODE, /* Voice is decoding a clip */
78 TSTATE_BUFFER_INSERT, /* Voice is sending decoded audio to PCM */
81 enum voice_thread_messages
83 Q_VOICE_NULL = 0, /* A message for thread sync - no effect on state */
84 Q_VOICE_PLAY, /* Play a clip */
85 Q_VOICE_STOP, /* Stop current clip */
86 Q_VOICE_STATE, /* Query playing state */
89 /* Structure to store clip data callback info */
90 struct voice_info
92 pcm_play_callback_type get_more; /* Callback to get more clips */
93 unsigned char *start; /* Start of clip */
94 size_t size; /* Size of clip */
97 /* Private thread data for its current state that must be passed to its
98 * internal functions */
99 struct voice_thread_data
101 int state; /* Thread state (TSTATE_*) */
102 struct queue_event ev; /* Last queue event pulled from queue */
103 void *st; /* Decoder instance */
104 SpeexBits bits; /* Bit cursor */
105 struct dsp_config *dsp; /* DSP used for voice output */
106 struct voice_info vi; /* Copy of clip data */
107 const char *src[2]; /* Current output buffer pointers */
108 int lookahead; /* Number of samples to drop at start of clip */
109 int count; /* Count of samples remaining to send to PCM */
112 /* Audio playback is in a playing state? */
113 static inline bool playback_is_playing(void)
115 return (audio_status() & AUDIO_STATUS_PLAY) != 0;
118 /* Stop any current clip and start playing a new one */
119 void mp3_play_data(const unsigned char* start, int size,
120 pcm_play_callback_type get_more)
122 /* Shared struct to get data to the thread - once it replies, it has
123 * safely cached it in its own private data */
124 static struct voice_info voice_clip SHAREDBSS_ATTR;
126 if (get_more != NULL && start != NULL && (ssize_t)size > 0)
128 mutex_lock(&voice_mutex);
130 voice_clip.get_more = get_more;
131 voice_clip.start = (unsigned char *)start;
132 voice_clip.size = size;
133 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
134 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
136 mutex_unlock(&voice_mutex);
140 /* Stop current voice clip from playing */
141 void mp3_play_stop(void)
143 if(!audio_is_thread_ready())
144 return;
146 mutex_lock(&voice_mutex); /* Sync against voice_stop */
147 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
148 queue_send(&voice_queue, Q_VOICE_STOP, 1);
150 mutex_unlock(&voice_mutex);
153 void mp3_play_pause(bool play)
155 /* a dummy */
156 (void)play;
159 /* Tell is voice is still in a playing state */
160 bool mp3_is_playing(void)
162 /* TODO: Implement a timeout or state query function for event objects */
163 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
164 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
165 return state != TSTATE_STOPPED;
168 /* This function is meant to be used by the buffer request functions to
169 ensure the codec is no longer active */
170 void voice_stop(void)
172 mutex_lock(&voice_mutex);
174 /* Stop the output and current clip */
175 mp3_play_stop();
177 /* Careful if using sync objects in talk.c - make sure locking order is
178 * observed with one or the other always granted first */
180 /* Unqueue all future clips */
181 talk_force_shutup();
183 /* Wait for any final queue_post to be processed */
184 LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
185 queue_send(&voice_queue, Q_VOICE_NULL, 0);
187 mutex_unlock(&voice_mutex);
188 } /* voice_stop */
190 /* Wait for voice to finish speaking. */
191 void voice_wait(void)
193 /* NOTE: One problem here is that we can't tell if another thread started a
194 * new clip by the time we wait. This should be resolvable if conditions
195 * ever require knowing the very clip you requested has finished. */
197 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
198 while(!voice_done || (!playback_is_playing() && pcm_is_playing()))
199 sleep(1);
202 /* Initialize voice thread data that must be valid upon starting and the
203 * setup the DSP parameters */
204 static void voice_data_init(struct voice_thread_data *td)
206 td->state = TSTATE_STOPPED;
207 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
208 CODEC_IDX_VOICE);
210 dsp_configure(td->dsp, DSP_RESET, 0);
211 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
212 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
213 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
216 /* Voice thread message processing */
217 static void voice_message(struct voice_thread_data *td)
219 while (1)
221 switch (td->ev.id)
223 case Q_VOICE_PLAY:
224 LOGFQUEUE("voice < Q_VOICE_PLAY");
225 /* Put up a block for completion signal */
226 voice_done = false;
228 /* Copy the clip info */
229 td->vi = *(struct voice_info *)td->ev.data;
231 /* Be sure audio buffer is initialized */
232 audio_restore_playback(AUDIO_WANT_VOICE);
234 /* We need nothing more from the sending thread - let it run */
235 queue_reply(&voice_queue, 1);
237 if (td->state == TSTATE_STOPPED)
239 /* Boost CPU now */
240 trigger_cpu_boost();
242 else if (!playback_is_playing())
244 /* Just voice, stop any clip still playing */
245 pcmbuf_play_stop();
248 /* Clean-start the decoder */
249 td->st = speex_decoder_init(&speex_wb_mode);
251 /* Make bit buffer use our own buffer */
252 speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
253 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
255 td->state = TSTATE_DECODE;
256 return;
258 case Q_VOICE_STOP:
259 LOGFQUEUE("voice < Q_VOICE_STOP: %ld", (long)td->ev.data);
261 if (td->ev.data != 0 && !playback_is_playing())
263 /* If not playing, it's just voice so stop pcm playback */
264 pcmbuf_play_stop();
267 /* Cancel boost */
268 cancel_cpu_boost();
270 td->state = TSTATE_STOPPED;
271 voice_done = true;
272 break;
274 case Q_VOICE_STATE:
275 LOGFQUEUE("voice < Q_VOICE_STATE");
276 queue_reply(&voice_queue, td->state);
278 if (td->state == TSTATE_STOPPED)
279 break; /* Not in a playback state */
281 return;
283 default:
284 /* Default messages get a reply and thread continues with no
285 * state transition */
286 LOGFQUEUE("voice < default");
288 if (td->state == TSTATE_STOPPED)
289 break; /* Not in playback state */
291 queue_reply(&voice_queue, 0);
292 return;
295 queue_wait(&voice_queue, &td->ev);
299 /* Voice thread entrypoint */
300 static void voice_thread(void)
302 struct voice_thread_data td;
304 voice_data_init(&td);
306 /* audio thread will only set this once after it finished the final
307 * audio hardware init so this little construct is safe - even
308 * cross-core. */
309 while (!audio_is_thread_ready())
310 sleep(0);
312 goto message_wait;
314 while (1)
316 td.state = TSTATE_DECODE;
318 if (!queue_empty(&voice_queue))
320 message_wait:
321 queue_wait(&voice_queue, &td.ev);
323 message_process:
324 voice_message(&td);
326 /* Branch to initial start point or branch back to previous
327 * operation if interrupted by a message */
328 switch (td.state)
330 case TSTATE_DECODE: goto voice_decode;
331 case TSTATE_BUFFER_INSERT: goto buffer_insert;
332 default: goto message_wait;
336 voice_decode:
337 /* Decode the data */
338 if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
340 /* End of stream or error - get next clip */
341 td.vi.size = 0;
343 if (td.vi.get_more != NULL)
344 td.vi.get_more(&td.vi.start, &td.vi.size);
346 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
348 /* Make bit buffer use our own buffer */
349 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
350 /* Don't skip any samples when we're stringing clips together */
351 td.lookahead = 0;
353 /* Paranoid check - be sure never to somehow get stuck in a
354 * loop without listening to the queue */
355 yield();
357 if (!queue_empty(&voice_queue))
358 goto message_wait;
359 else
360 goto voice_decode;
363 /* If all clips are done and not playing, force pcm playback. */
364 if (!pcm_is_playing())
365 pcmbuf_play_start();
367 /* Synthesize a stop request */
368 /* NOTE: We have no way to know when the pcm data placed in the
369 * buffer is actually consumed and playback has reached the end
370 * so until the info is available or inferred somehow, this will
371 * not be accurate and the stopped signal will come too soon.
372 * ie. You may not hear the "Shutting Down" splash even though
373 * it waits for voice to stop. */
374 td.ev.id = Q_VOICE_STOP;
375 td.ev.data = 0; /* Let PCM drain by itself */
376 yield();
377 goto message_process;
380 yield();
382 /* Output the decoded frame */
383 td.count = VOICE_FRAME_SIZE - td.lookahead;
384 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
385 td.src[1] = NULL;
386 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
388 buffer_insert:
389 /* Process the PCM samples in the DSP and send out for mixing */
390 td.state = TSTATE_BUFFER_INSERT;
392 while (td.count > 0)
394 int out_count = dsp_output_count(td.dsp, td.count);
395 int inp_count;
396 char *dest;
398 while (1)
400 if (!queue_empty(&voice_queue))
401 goto message_wait;
403 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
404 break;
406 yield();
409 /* Get the real input_size for output_size bytes, guarding
410 * against resampling buffer overflows. */
411 inp_count = dsp_input_count(td.dsp, out_count);
413 if (inp_count <= 0)
414 break;
416 /* Input size has grown, no error, just don't write more than
417 * length */
418 if (inp_count > td.count)
419 inp_count = td.count;
421 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
423 if (out_count <= 0)
424 break;
426 pcmbuf_write_voice_complete(out_count);
427 td.count -= inp_count;
430 yield();
431 } /* end while */
432 } /* voice_thread */
434 /* Initialize all synchronization objects create the thread */
435 void voice_thread_init(void)
437 logf("Starting voice thread");
438 queue_init(&voice_queue, false);
439 mutex_init(&voice_mutex);
441 voice_thread_id = create_thread(voice_thread, voice_stack,
442 sizeof(voice_stack), CREATE_THREAD_FROZEN,
443 voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
445 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
446 voice_thread_id);
447 } /* voice_thread_init */
449 /* Unfreeze the voice thread */
450 void voice_thread_resume(void)
452 logf("Thawing voice thread");
453 thread_thaw(voice_thread_id);
456 #ifdef HAVE_PRIORITY_SCHEDULING
457 /* Set the voice thread priority */
458 void voice_thread_set_priority(int priority)
460 thread_set_priority(voice_thread_id, priority);
462 #endif