Fix red (#ifdef hell).
[kugel-rb.git] / apps / voice_thread.c
blobc59089d672c7e02f1b303e2df199f3f4e18f387d
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 "playback.h"
27 #include "pcmbuf.h"
28 #include "codecs/libspeex/speex/speex.h"
30 /* Define any of these as "1" to log regular and/or timeout messages */
31 #define VOICE_LOGQUEUES 0
32 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
34 #if VOICE_LOGQUEUES
35 #define LOGFQUEUE logf
36 #else
37 #define LOGFQUEUE(...)
38 #endif
40 #if VOICE_LOGQUEUES_SYS_TIMEOUT
41 #define LOGFQUEUE_SYS_TIMEOUT logf
42 #else
43 #define LOGFQUEUE_SYS_TIMEOUT(...)
44 #endif
46 #ifndef IBSS_ATTR_VOICE_STACK
47 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
48 #endif
50 #define VOICE_FRAME_SIZE 320 /* Samples / frame */
51 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
52 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
54 /* Voice thread variables */
55 static struct thread_entry *voice_thread_p = NULL;
56 static long voice_stack[0x7c0/sizeof(long)] IBSS_ATTR_VOICE_STACK;
57 static const char voice_thread_name[] = "voice";
59 /* Voice thread synchronization objects */
60 static struct event_queue voice_queue SHAREDBSS_ATTR;
61 static struct mutex voice_mutex SHAREDBSS_ATTR;
62 static struct event voice_event SHAREDBSS_ATTR;
63 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
65 /* Buffer for decoded samples */
66 static spx_int16_t voice_output_buf[VOICE_FRAME_SIZE] CACHEALIGN_ATTR;
68 enum voice_thread_states
70 TSTATE_STOPPED = 0, /* Voice thread is stopped and awaiting commands */
71 TSTATE_DECODE, /* Voice is decoding a clip */
72 TSTATE_BUFFER_INSERT, /* Voice is sending decoded audio to PCM */
75 enum voice_thread_messages
77 Q_VOICE_NULL = 0, /* A message for thread sync - no effect on state */
78 Q_VOICE_PLAY, /* Play a clip */
79 Q_VOICE_STOP, /* Stop current clip */
80 Q_VOICE_STATE, /* Query playing state */
83 /* Structure to store clip data callback info */
84 struct voice_info
86 pcm_more_callback_type get_more; /* Callback to get more clips */
87 unsigned char *start; /* Start of clip */
88 size_t size; /* Size of clip */
91 /* Private thread data for its current state that must be passed to its
92 * internal functions */
93 struct voice_thread_data
95 int state; /* Thread state (TSTATE_*) */
96 struct queue_event ev; /* Last queue event pulled from queue */
97 void *st; /* Decoder instance */
98 SpeexBits bits; /* Bit cursor */
99 struct dsp_config *dsp; /* DSP used for voice output */
100 struct voice_info vi; /* Copy of clip data */
101 const char *src[2]; /* Current output buffer pointers */
102 int lookahead; /* Number of samples to drop at start of clip */
103 int count; /* Count of samples remaining to send to PCM */
106 /* Audio playback is in a playing state? */
107 static inline bool playback_is_playing(void)
109 return (audio_status() & AUDIO_STATUS_PLAY) != 0;
112 /* Stop any current clip and start playing a new one */
113 void mp3_play_data(const unsigned char* start, int size,
114 pcm_more_callback_type get_more)
116 /* Shared struct to get data to the thread - once it replies, it has
117 * safely cached it in its own private data */
118 static struct voice_info voice_clip SHAREDBSS_ATTR;
120 if (get_more != NULL && start != NULL && (ssize_t)size > 0)
122 mutex_lock(&voice_mutex);
124 voice_clip.get_more = get_more;
125 voice_clip.start = (unsigned char *)start;
126 voice_clip.size = size;
127 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
128 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
130 mutex_unlock(&voice_mutex);
134 /* Stop current voice clip from playing */
135 void mp3_play_stop(void)
137 mutex_lock(&voice_mutex); /* Sync against voice_stop */
139 LOGFQUEUE("mp3 > voice Q_VOICE_STOP: 1");
140 queue_remove_from_head(&voice_queue, Q_VOICE_STOP);
141 queue_post(&voice_queue, Q_VOICE_STOP, 1);
143 mutex_unlock(&voice_mutex);
146 void mp3_play_pause(bool play)
148 /* a dummy */
149 (void)play;
152 /* Tell is voice is still in a playing state */
153 bool mp3_is_playing(void)
155 /* TODO: Implement a timeout or state query function for event objects */
156 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
157 int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
158 return state != TSTATE_STOPPED;
161 /* This function is meant to be used by the buffer request functions to
162 ensure the codec is no longer active */
163 void voice_stop(void)
165 mutex_lock(&voice_mutex);
167 /* Stop the output and current clip */
168 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
169 queue_send(&voice_queue, Q_VOICE_STOP, 1);
171 /* Careful if using sync objects in talk.c - make sure locking order is
172 * observed with one or the other always granted first */
174 /* Unqueue all future clips */
175 talk_force_shutup();
177 /* Wait for any final queue_post to be processed */
178 LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
179 queue_send(&voice_queue, Q_VOICE_NULL, 0);
181 mutex_unlock(&voice_mutex);
182 } /* voice_stop */
184 /* Wait for voice to finish speaking. */
185 void voice_wait(void)
187 /* NOTE: One problem here is that we can't tell if another thread started a
188 * new clip by the time we wait. This should be resolvable if conditions
189 * ever require knowing the very clip you requested has finished. */
190 event_wait(&voice_event, STATE_SIGNALED);
191 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
192 while(!playback_is_playing() && pcm_is_playing())
193 sleep(1);
196 /* Initialize voice thread data that must be valid upon starting and the
197 * setup the DSP parameters */
198 static void voice_data_init(struct voice_thread_data *td)
200 td->state = TSTATE_STOPPED;
201 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
202 CODEC_IDX_VOICE);
204 dsp_configure(td->dsp, DSP_RESET, 0);
205 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
206 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
207 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
210 /* Voice thread message processing */
211 static void voice_message(struct voice_thread_data *td)
213 while (1)
215 switch (td->ev.id)
217 case Q_VOICE_PLAY:
218 LOGFQUEUE("voice < Q_VOICE_PLAY");
219 /* Put up a block for completion signal */
220 event_set_state(&voice_event, STATE_NONSIGNALED);
222 /* Copy the clip info */
223 td->vi = *(struct voice_info *)td->ev.data;
225 /* Be sure audio buffer is initialized */
226 audio_restore_playback(AUDIO_WANT_VOICE);
228 /* We need nothing more from the sending thread - let it run */
229 queue_reply(&voice_queue, 1);
231 if (td->state == TSTATE_STOPPED)
233 /* Boost CPU now */
234 trigger_cpu_boost();
236 else if (!playback_is_playing())
238 /* Just voice, stop any clip still playing */
239 pcmbuf_play_stop();
242 /* Clean-start the decoder */
243 td->st = speex_decoder_init(&speex_wb_mode);
245 /* Make bit buffer use our own buffer */
246 speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
247 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
249 td->state = TSTATE_DECODE;
250 return;
252 case Q_VOICE_STOP:
253 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev.data);
255 if (td->ev.data != 0 && !playback_is_playing())
257 /* If not playing, it's just voice so stop pcm playback */
258 pcmbuf_play_stop();
261 /* Cancel boost */
262 cancel_cpu_boost();
264 td->state = TSTATE_STOPPED;
265 event_set_state(&voice_event, STATE_SIGNALED);
266 break;
268 case Q_VOICE_STATE:
269 LOGFQUEUE("voice < Q_VOICE_STATE");
270 queue_reply(&voice_queue, td->state);
272 if (td->state == TSTATE_STOPPED)
273 break; /* Not in a playback state */
275 return;
277 default:
278 /* Default messages get a reply and thread continues with no
279 * state transition */
280 LOGFQUEUE("voice < default");
282 if (td->state == TSTATE_STOPPED)
283 break; /* Not in playback state */
285 queue_reply(&voice_queue, 0);
286 return;
289 queue_wait(&voice_queue, &td->ev);
293 /* Voice thread entrypoint */
294 static void voice_thread(void)
296 struct voice_thread_data td;
298 voice_data_init(&td);
299 audio_wait_for_init();
301 goto message_wait;
303 while (1)
305 td.state = TSTATE_DECODE;
307 if (!queue_empty(&voice_queue))
309 message_wait:
310 queue_wait(&voice_queue, &td.ev);
312 message_process:
313 voice_message(&td);
315 /* Branch to initial start point or branch back to previous
316 * operation if interrupted by a message */
317 switch (td.state)
319 case TSTATE_DECODE: goto voice_decode;
320 case TSTATE_BUFFER_INSERT: goto buffer_insert;
321 default: goto message_wait;
325 voice_decode:
326 /* Decode the data */
327 if (speex_decode_int(td.st, &td.bits, voice_output_buf) < 0)
329 /* End of stream or error - get next clip */
330 td.vi.size = 0;
332 if (td.vi.get_more != NULL)
333 td.vi.get_more(&td.vi.start, &td.vi.size);
335 if (td.vi.start != NULL && (ssize_t)td.vi.size > 0)
337 /* Make bit buffer use our own buffer */
338 speex_bits_set_bit_buffer(&td.bits, td.vi.start, td.vi.size);
339 /* Don't skip any samples when we're stringing clips together */
340 td.lookahead = 0;
342 /* Paranoid check - be sure never to somehow get stuck in a
343 * loop without listening to the queue */
344 yield();
346 if (!queue_empty(&voice_queue))
347 goto message_wait;
348 else
349 goto voice_decode;
352 /* If all clips are done and not playing, force pcm playback. */
353 if (!pcm_is_playing())
354 pcmbuf_play_start();
356 /* Synthesize a stop request */
357 /* NOTE: We have no way to know when the pcm data placed in the
358 * buffer is actually consumed and playback has reached the end
359 * so until the info is available or inferred somehow, this will
360 * not be accurate and the stopped signal will come too soon.
361 * ie. You may not hear the "Shutting Down" splash even though
362 * it waits for voice to stop. */
363 td.ev.id = Q_VOICE_STOP;
364 td.ev.data = 0; /* Let PCM drain by itself */
365 yield();
366 goto message_process;
369 yield();
371 /* Output the decoded frame */
372 td.count = VOICE_FRAME_SIZE - td.lookahead;
373 td.src[0] = (const char *)&voice_output_buf[td.lookahead];
374 td.src[1] = NULL;
375 td.lookahead -= MIN(VOICE_FRAME_SIZE, td.lookahead);
377 buffer_insert:
378 /* Process the PCM samples in the DSP and send out for mixing */
379 td.state = TSTATE_BUFFER_INSERT;
381 while (td.count > 0)
383 int out_count = dsp_output_count(td.dsp, td.count);
384 int inp_count;
385 char *dest;
387 while (1)
389 if (!queue_empty(&voice_queue))
390 goto message_wait;
392 if ((dest = pcmbuf_request_voice_buffer(&out_count)) != NULL)
393 break;
395 yield();
398 /* Get the real input_size for output_size bytes, guarding
399 * against resampling buffer overflows. */
400 inp_count = dsp_input_count(td.dsp, out_count);
402 if (inp_count <= 0)
403 break;
405 /* Input size has grown, no error, just don't write more than
406 * length */
407 if (inp_count > td.count)
408 inp_count = td.count;
410 out_count = dsp_process(td.dsp, dest, td.src, inp_count);
412 if (out_count <= 0)
413 break;
415 pcmbuf_write_voice_complete(out_count);
416 td.count -= inp_count;
419 yield();
420 } /* end while */
421 } /* voice_thread */
423 /* Initialize all synchronization objects create the thread */
424 void voice_thread_init(void)
426 logf("Starting voice thread");
427 queue_init(&voice_queue, false);
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));
434 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
435 voice_thread_p);
436 } /* voice_thread_init */
438 /* Unfreeze the voice thread */
439 void voice_thread_resume(void)
441 logf("Thawing voice thread");
442 thread_thaw(voice_thread_p);
445 #ifdef HAVE_PRIORITY_SCHEDULING
446 /* Set the voice thread priority */
447 void voice_thread_set_priority(int priority)
449 thread_set_priority(voice_thread_p, priority);
451 #endif