Move bin2c handling to libtools.make
[maemo-rb.git] / apps / voice_thread.c
blob07a67256c4deb155e076f28d15ef9299fd145dcd
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 "pcm.h"
31 #include "pcm_mixer.h"
32 #include "codecs/libspeex/speex/speex.h"
34 /* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
35 regular and/or timeout messages */
36 #define VOICE_LOGQUEUES 0
37 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
39 /*#define LOGF_ENABLE*/
40 #include "logf.h"
42 #if VOICE_LOGQUEUES
43 #define LOGFQUEUE logf
44 #else
45 #define LOGFQUEUE(...)
46 #endif
48 #if VOICE_LOGQUEUES_SYS_TIMEOUT
49 #define LOGFQUEUE_SYS_TIMEOUT logf
50 #else
51 #define LOGFQUEUE_SYS_TIMEOUT(...)
52 #endif
54 #ifndef IBSS_ATTR_VOICE_STACK
55 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
56 #endif
58 /* Minimum priority needs to be a bit elevated since voice has fairly low
59 latency */
60 #define PRIORITY_VOICE (PRIORITY_PLAYBACK-4)
62 #define VOICE_FRAME_COUNT 320 /* Samples / frame */
63 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
64 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
66 /* Voice thread variables */
67 static unsigned int voice_thread_id = 0;
68 #ifdef CPU_COLDFIRE
69 /* ISR uses any available stack - need a bit more room */
70 #define VOICE_STACK_EXTRA 0x400
71 #else
72 #define VOICE_STACK_EXTRA 0x3c0
73 #endif
74 static long voice_stack[(DEFAULT_STACK_SIZE + VOICE_STACK_EXTRA)/sizeof(long)]
75 IBSS_ATTR_VOICE_STACK;
76 static const char voice_thread_name[] = "voice";
78 /* Voice thread synchronization objects */
79 static struct event_queue voice_queue SHAREDBSS_ATTR;
80 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
81 static int quiet_counter SHAREDDATA_ATTR = 0;
83 /* Buffer for decoded samples */
84 static spx_int16_t voice_output_buf[VOICE_FRAME_COUNT] MEM_ALIGN_ATTR;
86 #define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_COUNT + \
87 VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
88 #define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t))
90 /* Default number of native-frequency PCM frames to queue - adjust as
91 necessary per-target */
92 #define VOICE_FRAMES 3
94 /* Might have lookahead and be skipping samples, so size is needed */
95 static size_t voicebuf_sizes[VOICE_FRAMES];
96 static int16_t (* voicebuf)[2*VOICE_PCM_FRAME_COUNT];
97 static unsigned int cur_buf_in, cur_buf_out;
99 /* Voice processing states */
100 enum voice_state
102 VOICE_STATE_MESSAGE = 0,
103 VOICE_STATE_DECODE,
104 VOICE_STATE_BUFFER_INSERT,
107 /* A delay to not bring audio back to normal level too soon */
108 #define QUIET_COUNT 3
110 enum voice_thread_messages
112 Q_VOICE_PLAY = 0, /* Play a clip */
113 Q_VOICE_STOP, /* Stop current clip */
116 /* Structure to store clip data callback info */
117 struct voice_info
119 /* Callback to get more clips */
120 mp3_play_callback_t get_more;
121 /* Start of clip */
122 const void *start;
123 /* Size of clip */
124 size_t size;
127 /* Private thread data for its current state that must be passed to its
128 * internal functions */
129 struct voice_thread_data
131 struct queue_event ev; /* Last queue event pulled from queue */
132 void *st; /* Decoder instance */
133 SpeexBits bits; /* Bit cursor */
134 struct dsp_config *dsp; /* DSP used for voice output */
135 struct voice_info vi; /* Copy of clip data */
136 const char *src[2]; /* Current output buffer pointers */
137 int lookahead; /* Number of samples to drop at start of clip */
138 int count; /* Count of samples remaining to send to PCM */
141 /* Functions called in their repective state that return the next state to
142 state machine loop - compiler may inline them at its discretion */
143 static enum voice_state voice_message(struct voice_thread_data *td);
144 static enum voice_state voice_decode(struct voice_thread_data *td);
145 static enum voice_state voice_buffer_insert(struct voice_thread_data *td);
147 /* Number of frames in queue */
148 static inline int voice_unplayed_frames(void)
150 return cur_buf_in - cur_buf_out;
153 /* Mixer channel callback */
154 static void voice_pcm_callback(const void **start, size_t *size)
156 if (voice_unplayed_frames() == 0)
157 return; /* Done! */
159 unsigned int i = ++cur_buf_out % VOICE_FRAMES;
161 *start = voicebuf[i];
162 *size = voicebuf_sizes[i];
165 /* Start playback of voice channel if not already playing */
166 static void voice_start_playback(void)
168 if (mixer_channel_status(PCM_MIXER_CHAN_VOICE) != CHANNEL_STOPPED ||
169 voice_unplayed_frames() <= 0)
170 return;
172 unsigned int i = cur_buf_out % VOICE_FRAMES;
173 mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback,
174 voicebuf[i], voicebuf_sizes[i]);
177 /* Stop the voice channel */
178 static void voice_stop_playback(void)
180 mixer_channel_stop(PCM_MIXER_CHAN_VOICE);
181 cur_buf_in = cur_buf_out = 0;
184 /* Grab a free PCM frame */
185 static int16_t * voice_buf_get(void)
187 if (voice_unplayed_frames() >= VOICE_FRAMES)
189 /* Full */
190 voice_start_playback();
191 return NULL;
194 return voicebuf[cur_buf_in % VOICE_FRAMES];
197 /* Commit a frame returned by voice_buf_get and set the actual size */
198 static void voice_buf_commit(int count)
200 if (count > 0)
202 voicebuf_sizes[cur_buf_in++ % VOICE_FRAMES] =
203 count * 2 * sizeof (int16_t);
207 /* Stop any current clip and start playing a new one */
208 void mp3_play_data(const void *start, size_t size,
209 mp3_play_callback_t get_more)
211 if (get_more != NULL && start != NULL && size > 0)
213 struct voice_info voice_clip =
215 .get_more = get_more,
216 .start = start,
217 .size = size,
220 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
221 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
225 /* Stop current voice clip from playing */
226 void mp3_play_stop(void)
228 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
229 queue_send(&voice_queue, Q_VOICE_STOP, 0);
232 void mp3_play_pause(bool play)
234 /* a dummy */
235 (void)play;
238 /* Tell if voice is still in a playing state */
239 bool mp3_is_playing(void)
241 return quiet_counter != 0;
244 /* This function is meant to be used by the buffer request functions to
245 ensure the codec is no longer active */
246 void voice_stop(void)
248 /* Unqueue all future clips */
249 talk_force_shutup();
252 /* Wait for voice to finish speaking. */
253 void voice_wait(void)
255 /* NOTE: One problem here is that we can't tell if another thread started a
256 * new clip by the time we wait. This should be resolvable if conditions
257 * ever require knowing the very clip you requested has finished. */
259 while (quiet_counter != 0)
260 sleep(1);
263 /* Initialize voice thread data that must be valid upon starting and the
264 * setup the DSP parameters */
265 static void voice_data_init(struct voice_thread_data *td)
267 td->dsp = (struct dsp_config *)dsp_configure(NULL, DSP_MYDSP,
268 CODEC_IDX_VOICE);
270 dsp_configure(td->dsp, DSP_RESET, 0);
271 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
272 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
273 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
275 mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY);
278 /* Voice thread message processing */
279 static enum voice_state voice_message(struct voice_thread_data *td)
281 if (quiet_counter > 0)
282 queue_wait_w_tmo(&voice_queue, &td->ev, HZ/10);
283 else
284 queue_wait(&voice_queue, &td->ev);
286 switch (td->ev.id)
288 case Q_VOICE_PLAY:
289 LOGFQUEUE("voice < Q_VOICE_PLAY");
290 if (quiet_counter == 0)
292 /* Boost CPU now */
293 trigger_cpu_boost();
295 else
297 /* Stop any clip still playing */
298 voice_stop_playback();
301 quiet_counter = QUIET_COUNT;
303 /* Copy the clip info */
304 td->vi = *(struct voice_info *)td->ev.data;
306 /* Be sure audio buffer is initialized */
307 audio_restore_playback(AUDIO_WANT_VOICE);
309 /* We need nothing more from the sending thread - let it run */
310 queue_reply(&voice_queue, 1);
312 /* Make audio play more softly and set delay to return to normal
313 playback level */
314 pcmbuf_soft_mode(true);
316 /* Clean-start the decoder */
317 td->st = speex_decoder_init(&speex_wb_mode);
319 /* Make bit buffer use our own buffer */
320 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
321 td->vi.size);
322 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
324 return VOICE_STATE_DECODE;
326 case SYS_TIMEOUT:
327 if (voice_unplayed_frames())
329 /* Waiting for PCM to finish */
330 break;
333 /* Drop through and stop the first time after clip runs out */
334 if (quiet_counter-- != QUIET_COUNT)
336 if (quiet_counter <= 0)
337 pcmbuf_soft_mode(false);
339 break;
342 /* Fall-through */
343 case Q_VOICE_STOP:
344 LOGFQUEUE("voice < Q_VOICE_STOP");
345 cancel_cpu_boost();
346 voice_stop_playback();
347 break;
349 /* No default: no other message ids are sent */
352 return VOICE_STATE_MESSAGE;
355 /* Decode frames or stop if all have completed */
356 static enum voice_state voice_decode(struct voice_thread_data *td)
358 if (!queue_empty(&voice_queue))
359 return VOICE_STATE_MESSAGE;
361 /* Decode the data */
362 if (speex_decode_int(td->st, &td->bits, voice_output_buf) < 0)
364 /* End of stream or error - get next clip */
365 td->vi.size = 0;
367 if (td->vi.get_more != NULL)
368 td->vi.get_more(&td->vi.start, &td->vi.size);
370 if (td->vi.start != NULL && td->vi.size > 0)
372 /* Make bit buffer use our own buffer */
373 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
374 td->vi.size);
375 /* Don't skip any samples when we're stringing clips together */
376 td->lookahead = 0;
378 else
380 /* If all clips are done and not playing, force pcm playback. */
381 voice_start_playback();
382 return VOICE_STATE_MESSAGE;
385 else
387 yield();
389 /* Output the decoded frame */
390 td->count = VOICE_FRAME_COUNT - td->lookahead;
391 td->src[0] = (const char *)&voice_output_buf[td->lookahead];
392 td->src[1] = NULL;
393 td->lookahead -= MIN(VOICE_FRAME_COUNT, td->lookahead);
395 if (td->count > 0)
396 return VOICE_STATE_BUFFER_INSERT;
399 return VOICE_STATE_DECODE;
402 /* Process the PCM samples in the DSP and send out for mixing */
403 static enum voice_state voice_buffer_insert(struct voice_thread_data *td)
405 if (!queue_empty(&voice_queue))
406 return VOICE_STATE_MESSAGE;
408 char *dest = (char *)voice_buf_get();
410 if (dest != NULL)
412 voice_buf_commit(dsp_process(td->dsp, dest, td->src, td->count));
413 return VOICE_STATE_DECODE;
416 sleep(0);
417 return VOICE_STATE_BUFFER_INSERT;
420 /* Voice thread entrypoint */
421 static void NORETURN_ATTR voice_thread(void)
423 struct voice_thread_data td;
424 enum voice_state state = VOICE_STATE_MESSAGE;
426 voice_data_init(&td);
428 while (1)
430 switch (state)
432 case VOICE_STATE_MESSAGE:
433 state = voice_message(&td);
434 break;
435 case VOICE_STATE_DECODE:
436 state = voice_decode(&td);
437 break;
438 case VOICE_STATE_BUFFER_INSERT:
439 state = voice_buffer_insert(&td);
440 break;
445 /* Initialize all synchronization objects create the thread */
446 void voice_thread_init(void)
448 logf("Starting voice thread");
449 queue_init(&voice_queue, false);
451 voice_thread_id = create_thread(voice_thread, voice_stack,
452 sizeof(voice_stack), 0, voice_thread_name
453 IF_PRIO(, PRIORITY_VOICE) IF_COP(, CPU));
455 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
456 voice_thread_id);
459 #ifdef HAVE_PRIORITY_SCHEDULING
460 /* Set the voice thread priority */
461 void voice_thread_set_priority(int priority)
463 if (priority > PRIORITY_VOICE)
464 priority = PRIORITY_VOICE;
466 thread_set_priority(voice_thread_id, priority);
468 #endif
470 /* Initialize voice PCM buffer and return size, allocated from the end */
471 size_t voicebuf_init(void *bufend)
473 size_t size = VOICE_FRAMES * sizeof (voicebuf[0]);
474 cur_buf_out = cur_buf_in = 0;
475 voicebuf = bufend - size;
476 return size;