Code police tabs and spaces
[kugel-rb.git] / apps / voice_thread.c
blob7bf52d4d0d1623441f06cead6083dd7e6158e2e3
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[0x7c0/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_remove_from_head(&voice_queue, Q_VOICE_STOP);
140 queue_post(&voice_queue, Q_VOICE_STOP, 1);
142 mutex_unlock(&voice_mutex);
145 void mp3_play_pause(bool play)
147 /* a dummy */
148 (void)play;
151 /* Tell is voice is still in a playing state */
152 bool mp3_is_playing(void)
154 /* TODO: Implement a timeout or state query function for event objects */
155 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
156 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
157 return state != TSTATE_STOPPED;
160 /* This function is meant to be used by the buffer request functions to
161 ensure the codec is no longer active */
162 void voice_stop(void)
164 mutex_lock(&voice_mutex);
166 /* Stop the output and current clip */
167 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
168 queue_send(&voice_queue, Q_VOICE_STOP, 1);
170 /* Careful if using sync objects in talk.c - make sure locking order is
171 * observed with one or the other always granted first */
173 /* Unqueue all future clips */
174 talk_force_shutup();
176 /* Wait for any final queue_post to be processed */
177 LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
178 queue_send(&voice_queue, Q_VOICE_NULL, 0);
180 mutex_unlock(&voice_mutex);
181 } /* voice_stop */
183 /* Wait for voice to finish speaking. */
184 void voice_wait(void)
186 /* NOTE: One problem here is that we can't tell if another thread started a
187 * new clip by the time we wait. This should be resolvable if conditions
188 * ever require knowing the very clip you requested has finished. */
189 event_wait(&voice_event, STATE_SIGNALED);
190 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
191 while(!playback_is_playing() && pcm_is_playing())
192 sleep(1);
195 /* Initialize voice thread data that must be valid upon starting and the
196 * setup the DSP parameters */
197 static void voice_data_init(struct voice_thread_data *td)
199 td->state = TSTATE_STOPPED;
200 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
201 CODEC_IDX_VOICE);
203 dsp_configure(td->dsp, DSP_RESET, 0);
204 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
205 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
206 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
209 /* Voice thread message processing */
210 static void voice_message(struct voice_thread_data *td)
212 while (1)
214 switch (td->ev.id)
216 case Q_VOICE_PLAY:
217 LOGFQUEUE("voice < Q_VOICE_PLAY");
218 /* Put up a block for completion signal */
219 event_set_state(&voice_event, STATE_NONSIGNALED);
221 /* Copy the clip info */
222 td->vi = *(struct voice_info *)td->ev.data;
224 /* Be sure audio buffer is initialized */
225 audio_restore_playback(AUDIO_WANT_VOICE);
227 /* We need nothing more from the sending thread - let it run */
228 queue_reply(&voice_queue, 1);
230 if (td->state == TSTATE_STOPPED)
232 /* Boost CPU now */
233 trigger_cpu_boost();
235 else if (!playback_is_playing())
237 /* Just voice, stop any clip still playing */
238 pcmbuf_play_stop();
241 /* Clean-start the decoder */
242 td->st = speex_decoder_init(&speex_wb_mode);
244 /* Make bit buffer use our own buffer */
245 speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
246 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
248 td->state = TSTATE_DECODE;
249 return;
251 case Q_VOICE_STOP:
252 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev.data);
254 if (td->ev.data != 0 && !playback_is_playing())
256 /* If not playing, it's just voice so stop pcm playback */
257 pcmbuf_play_stop();
260 /* Cancel boost */
261 cancel_cpu_boost();
263 td->state = TSTATE_STOPPED;
264 event_set_state(&voice_event, STATE_SIGNALED);
265 break;
267 case Q_VOICE_STATE:
268 LOGFQUEUE("voice < Q_VOICE_STATE");
269 queue_reply(&voice_queue, td->state);
271 if (td->state == TSTATE_STOPPED)
272 break; /* Not in a playback state */
274 return;
276 default:
277 /* Default messages get a reply and thread continues with no
278 * state transition */
279 LOGFQUEUE("voice < default");
281 if (td->state == TSTATE_STOPPED)
282 break; /* Not in playback state */
284 queue_reply(&voice_queue, 0);
285 return;
288 queue_wait(&voice_queue, &td->ev);
292 /* Voice thread entrypoint */
293 static void voice_thread(void)
295 struct voice_thread_data td;
297 voice_data_init(&td);
298 audio_wait_for_init();
300 goto message_wait;
302 while (1)
304 td.state = TSTATE_DECODE;
306 if (!queue_empty(&voice_queue))
308 message_wait:
309 queue_wait(&voice_queue, &td.ev);
311 message_process:
312 voice_message(&td);
314 /* Branch to initial start point or branch back to previous
315 * operation if interrupted by a message */
316 switch (td.state)
318 case TSTATE_DECODE: goto voice_decode;
319 case TSTATE_BUFFER_INSERT: goto buffer_insert;
320 default: goto message_wait;
324 voice_decode:
325 /* Decode the data */
326 if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
328 /* End of stream or error - get next clip */
329 td.vi.size = 0;
331 if (td.vi.get_more != NULL)
332 td.vi.get_more(&td.vi.start, &td.vi.size);
334 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
336 /* Make bit buffer use our own buffer */
337 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
338 /* Don't skip any samples when we're stringing clips together */
339 td.lookahead = 0;
341 /* Paranoid check - be sure never to somehow get stuck in a
342 * loop without listening to the queue */
343 yield();
345 if (!queue_empty(&voice_queue))
346 goto message_wait;
347 else
348 goto voice_decode;
351 /* If all clips are done and not playing, force pcm playback. */
352 if (!pcm_is_playing())
353 pcmbuf_play_start();
355 /* Synthesize a stop request */
356 /* NOTE: We have no way to know when the pcm data placed in the
357 * buffer is actually consumed and playback has reached the end
358 * so until the info is available or inferred somehow, this will
359 * not be accurate and the stopped signal will come too soon.
360 * ie. You may not hear the "Shutting Down" splash even though
361 * it waits for voice to stop. */
362 td.ev.id = Q_VOICE_STOP;
363 td.ev.data = 0; /* Let PCM drain by itself */
364 yield();
365 goto message_process;
368 yield();
370 /* Output the decoded frame */
371 td.count = VOICE_FRAME_SIZE - td.lookahead;
372 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
373 td.src[1] = NULL;
374 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
376 buffer_insert:
377 /* Process the PCM samples in the DSP and send out for mixing */
378 td.state = TSTATE_BUFFER_INSERT;
380 while (td.count > 0)
382 int out_count = dsp_output_count(td.dsp, td.count);
383 int inp_count;
384 char *dest;
386 while (1)
388 if (!queue_empty(&voice_queue))
389 goto message_wait;
391 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
392 break;
394 yield();
397 /* Get the real input_size for output_size bytes, guarding
398 * against resampling buffer overflows. */
399 inp_count = dsp_input_count(td.dsp, out_count);
401 if (inp_count <= 0)
402 break;
404 /* Input size has grown, no error, just don't write more than
405 * length */
406 if (inp_count > td.count)
407 inp_count = td.count;
409 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
411 if (out_count <= 0)
412 break;
414 pcmbuf_write_voice_complete(out_count);
415 td.count -= inp_count;
418 yield();
419 } /* end while */
420 } /* voice_thread */
422 /* Initialize all synchronization objects create the thread */
423 void voice_thread_init(void)
425 logf("Starting voice thread");
426 queue_init(&voice_queue, false);
427 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list);
428 mutex_init(&voice_mutex);
429 event_init(&voice_event, STATE_SIGNALED | EVENT_MANUAL);
430 voice_thread_p = create_thread(voice_thread, voice_stack,
431 sizeof(voice_stack), CREATE_THREAD_FROZEN,
432 voice_thread_name IF_PRIO(, PRIORITY_PLAYBACK) IF_COP(, CPU));
433 } /* voice_thread_init */
435 /* Unfreeze the voice thread */
436 void voice_thread_resume(void)
438 logf("Thawing voice thread");
439 thread_thaw(voice_thread_p);
442 #ifdef HAVE_PRIORITY_SCHEDULING
443 /* Set the voice thread priority */
444 void voice_thread_set_priority(int priority)
446 thread_set_priority(voice_thread_p, priority);
448 #endif