Backport d8a3871 - Update French by Amaury Pouly
[maemo-rb.git] / apps / voice_thread.c
blobe8c7fcce559b34aed8f1c75959ae386c3aefb73b
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 "core_alloc.h"
24 #include "thread.h"
25 #include "voice_thread.h"
26 #include "talk.h"
27 #include "dsp_core.h"
28 #include "audio.h"
29 #include "playback.h"
30 #include "pcmbuf.h"
31 #include "pcm.h"
32 #include "pcm_mixer.h"
33 #include "codecs/libspeex/speex/speex.h"
35 /* Default number of native-frequency PCM frames to queue - adjust as
36 necessary per-target */
37 #define VOICE_FRAMES 4
39 /* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
40 regular and/or timeout messages */
41 #define VOICE_LOGQUEUES 0
42 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
44 /*#define LOGF_ENABLE*/
45 #include "logf.h"
47 #if VOICE_LOGQUEUES
48 #define LOGFQUEUE logf
49 #else
50 #define LOGFQUEUE(...)
51 #endif
53 #if VOICE_LOGQUEUES_SYS_TIMEOUT
54 #define LOGFQUEUE_SYS_TIMEOUT logf
55 #else
56 #define LOGFQUEUE_SYS_TIMEOUT(...)
57 #endif
59 #ifndef IBSS_ATTR_VOICE_STACK
60 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
61 #endif
63 /* Minimum priority needs to be a bit elevated since voice has fairly low
64 latency */
65 #define PRIORITY_VOICE (PRIORITY_PLAYBACK-4)
67 #define VOICE_FRAME_COUNT 320 /* Samples / frame */
68 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
69 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
71 /* Voice thread variables */
72 static unsigned int voice_thread_id = 0;
73 #ifdef CPU_COLDFIRE
74 /* ISR uses any available stack - need a bit more room */
75 #define VOICE_STACK_EXTRA 0x400
76 #else
77 #define VOICE_STACK_EXTRA 0x3c0
78 #endif
79 static long voice_stack[(DEFAULT_STACK_SIZE + VOICE_STACK_EXTRA)/sizeof(long)]
80 IBSS_ATTR_VOICE_STACK;
81 static const char voice_thread_name[] = "voice";
83 /* Voice thread synchronization objects */
84 static struct event_queue voice_queue SHAREDBSS_ATTR;
85 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR;
86 static int quiet_counter SHAREDDATA_ATTR = 0;
88 #define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_COUNT + \
89 VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
90 #define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*2*sizeof (int16_t))
92 /* Voice processing states */
93 enum voice_state
95 VOICE_STATE_MESSAGE = 0,
96 VOICE_STATE_DECODE,
97 VOICE_STATE_BUFFER_INSERT,
100 /* A delay to not bring audio back to normal level too soon */
101 #define QUIET_COUNT 3
103 enum voice_thread_messages
105 Q_VOICE_PLAY = 0, /* Play a clip */
106 Q_VOICE_STOP, /* Stop current clip */
109 /* Structure to store clip data callback info */
110 struct voice_info
112 /* Callback to get more clips */
113 mp3_play_callback_t get_more;
114 /* Start of clip */
115 const void *start;
116 /* Size of clip */
117 size_t size;
120 /* Private thread data for its current state that must be passed to its
121 * internal functions */
122 struct voice_thread_data
124 struct queue_event ev; /* Last queue event pulled from queue */
125 void *st; /* Decoder instance */
126 SpeexBits bits; /* Bit cursor */
127 struct dsp_config *dsp; /* DSP used for voice output */
128 struct voice_info vi; /* Copy of clip data */
129 int lookahead; /* Number of samples to drop at start of clip */
130 struct dsp_buffer src; /* Speex output buffer/input to DSP */
131 struct dsp_buffer *dst; /* Pointer to DSP output buffer for PCM */
134 /* Functions called in their repective state that return the next state to
135 state machine loop - compiler may inline them at its discretion */
136 static enum voice_state voice_message(struct voice_thread_data *td);
137 static enum voice_state voice_decode(struct voice_thread_data *td);
138 static enum voice_state voice_buffer_insert(struct voice_thread_data *td);
140 /* Might have lookahead and be skipping samples, so size is needed */
141 static struct voice_buf
143 /* Buffer for decoded samples */
144 spx_int16_t spx_outbuf[VOICE_FRAME_COUNT];
145 /* Queue frame indexes */
146 unsigned int volatile frame_in;
147 unsigned int volatile frame_out;
148 /* For PCM pointer adjustment */
149 struct voice_thread_data *td;
150 /* Buffers for mixing voice */
151 struct voice_pcm_frame
153 size_t size;
154 int16_t pcm[2*VOICE_PCM_FRAME_COUNT];
155 } frames[VOICE_FRAMES];
156 } *voice_buf = NULL;
158 static int voice_buf_hid = 0;
160 static int move_callback(int handle, void *current, void *new)
162 /* Have to adjust the pointers that point into things in voice_buf */
163 off_t diff = new - current;
164 struct voice_thread_data *td = voice_buf->td;
166 if (td != NULL)
168 td->src.p32[0] = SKIPBYTES(td->src.p32[0], diff);
169 td->src.p32[1] = SKIPBYTES(td->src.p32[1], diff);
171 if (td->dst != NULL) /* Only when calling dsp_process */
172 td->dst->p16out = SKIPBYTES(td->dst->p16out, diff);
174 mixer_adjust_channel_address(PCM_MIXER_CHAN_VOICE, diff);
177 voice_buf = new;
179 return BUFLIB_CB_OK;
180 (void)handle;
183 static void sync_callback(int handle, bool sync_on)
185 /* A move must not allow PCM to access the channel */
186 if (sync_on)
187 pcm_play_lock();
188 else
189 pcm_play_unlock();
191 (void)handle;
194 static struct buflib_callbacks ops =
196 .move_callback = move_callback,
197 .sync_callback = sync_callback,
200 /* Number of frames in queue */
201 static unsigned int voice_unplayed_frames(void)
203 return voice_buf->frame_in - voice_buf->frame_out;
206 /* Mixer channel callback */
207 static void voice_pcm_callback(const void **start, size_t *size)
209 unsigned int frame_out = ++voice_buf->frame_out;
211 if (voice_unplayed_frames() == 0)
212 return; /* Done! */
214 struct voice_pcm_frame *frame =
215 &voice_buf->frames[frame_out % VOICE_FRAMES];
217 *start = frame->pcm;
218 *size = frame->size;
221 /* Start playback of voice channel if not already playing */
222 static void voice_start_playback(void)
224 if (mixer_channel_status(PCM_MIXER_CHAN_VOICE) != CHANNEL_STOPPED ||
225 voice_unplayed_frames() == 0)
226 return;
228 struct voice_pcm_frame *frame =
229 &voice_buf->frames[voice_buf->frame_out % VOICE_FRAMES];
231 mixer_channel_play_data(PCM_MIXER_CHAN_VOICE, voice_pcm_callback,
232 frame->pcm, frame->size);
235 /* Stop the voice channel */
236 static void voice_stop_playback(void)
238 mixer_channel_stop(PCM_MIXER_CHAN_VOICE);
239 voice_buf->frame_in = voice_buf->frame_out = 0;
242 /* Grab a free PCM frame */
243 static int16_t * voice_buf_get(void)
245 if (voice_unplayed_frames() >= VOICE_FRAMES)
247 /* Full */
248 voice_start_playback();
249 return NULL;
252 return voice_buf->frames[voice_buf->frame_in % VOICE_FRAMES].pcm;
255 /* Commit a frame returned by voice_buf_get and set the actual size */
256 static void voice_buf_commit(int count)
258 if (count > 0)
260 unsigned int frame_in = voice_buf->frame_in;
261 voice_buf->frames[frame_in % VOICE_FRAMES].size =
262 count * 2 * sizeof (int16_t);
263 voice_buf->frame_in = frame_in + 1;
267 /* Stop any current clip and start playing a new one */
268 void mp3_play_data(const void *start, size_t size,
269 mp3_play_callback_t get_more)
271 if (voice_thread_id && start && size && get_more)
273 struct voice_info voice_clip =
275 .get_more = get_more,
276 .start = start,
277 .size = size,
280 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
281 queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
285 /* Stop current voice clip from playing */
286 void mp3_play_stop(void)
288 if (voice_thread_id != 0)
290 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
291 queue_send(&voice_queue, Q_VOICE_STOP, 0);
295 void mp3_play_pause(bool play)
297 /* a dummy */
298 (void)play;
301 /* Tell if voice is still in a playing state */
302 bool mp3_is_playing(void)
304 return quiet_counter != 0;
307 /* This function is meant to be used by the buffer request functions to
308 ensure the codec is no longer active */
309 void voice_stop(void)
311 /* Unqueue all future clips */
312 talk_force_shutup();
315 /* Wait for voice to finish speaking. */
316 void voice_wait(void)
318 /* NOTE: One problem here is that we can't tell if another thread started a
319 * new clip by the time we wait. This should be resolvable if conditions
320 * ever require knowing the very clip you requested has finished. */
322 while (quiet_counter != 0)
323 sleep(1);
326 /* Initialize voice thread data that must be valid upon starting and the
327 * setup the DSP parameters */
328 static void voice_data_init(struct voice_thread_data *td)
330 td->dsp = dsp_get_config(CODEC_IDX_VOICE);
331 dsp_configure(td->dsp, DSP_RESET, 0);
332 dsp_configure(td->dsp, DSP_SET_FREQUENCY, VOICE_SAMPLE_RATE);
333 dsp_configure(td->dsp, DSP_SET_SAMPLE_DEPTH, VOICE_SAMPLE_DEPTH);
334 dsp_configure(td->dsp, DSP_SET_STEREO_MODE, STEREO_MONO);
336 mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE, MIX_AMP_UNITY);
337 voice_buf->td = td;
338 td->dst = NULL;
341 /* Voice thread message processing */
342 static enum voice_state voice_message(struct voice_thread_data *td)
344 if (quiet_counter > 0)
345 queue_wait_w_tmo(&voice_queue, &td->ev, HZ/10);
346 else
347 queue_wait(&voice_queue, &td->ev);
349 switch (td->ev.id)
351 case Q_VOICE_PLAY:
352 LOGFQUEUE("voice < Q_VOICE_PLAY");
353 if (quiet_counter == 0)
355 /* Boost CPU now */
356 trigger_cpu_boost();
358 else
360 /* Stop any clip still playing */
361 voice_stop_playback();
364 quiet_counter = QUIET_COUNT;
366 /* Copy the clip info */
367 td->vi = *(struct voice_info *)td->ev.data;
369 /* We need nothing more from the sending thread - let it run */
370 queue_reply(&voice_queue, 1);
372 /* Make audio play more softly and set delay to return to normal
373 playback level */
374 pcmbuf_soft_mode(true);
376 /* Clean-start the decoder */
377 td->st = speex_decoder_init(&speex_wb_mode);
379 /* Make bit buffer use our own buffer */
380 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
381 td->vi.size);
382 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
384 return VOICE_STATE_DECODE;
386 case SYS_TIMEOUT:
387 if (voice_unplayed_frames())
389 /* Waiting for PCM to finish */
390 break;
393 /* Drop through and stop the first time after clip runs out */
394 if (quiet_counter-- != QUIET_COUNT)
396 if (quiet_counter <= 0)
397 pcmbuf_soft_mode(false);
399 break;
402 /* Fall-through */
403 case Q_VOICE_STOP:
404 LOGFQUEUE("voice < Q_VOICE_STOP");
405 cancel_cpu_boost();
406 voice_stop_playback();
407 break;
409 /* No default: no other message ids are sent */
412 return VOICE_STATE_MESSAGE;
415 /* Decode frames or stop if all have completed */
416 static enum voice_state voice_decode(struct voice_thread_data *td)
418 if (!queue_empty(&voice_queue))
419 return VOICE_STATE_MESSAGE;
421 /* Decode the data */
422 if (speex_decode_int(td->st, &td->bits, voice_buf->spx_outbuf) < 0)
424 /* End of stream or error - get next clip */
425 td->vi.size = 0;
427 if (td->vi.get_more != NULL)
428 td->vi.get_more(&td->vi.start, &td->vi.size);
430 if (td->vi.start != NULL && td->vi.size > 0)
432 /* Make bit buffer use our own buffer */
433 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
434 td->vi.size);
435 /* Don't skip any samples when we're stringing clips together */
436 td->lookahead = 0;
438 else
440 /* If all clips are done and not playing, force pcm playback. */
441 if (voice_unplayed_frames() > 0)
442 voice_start_playback();
443 return VOICE_STATE_MESSAGE;
446 else
448 yield();
450 /* Output the decoded frame */
451 td->src.remcount = VOICE_FRAME_COUNT - td->lookahead;
452 td->src.pin[0] = &voice_buf->spx_outbuf[td->lookahead];
453 td->src.pin[1] = NULL;
454 td->src.proc_mask = 0;
456 td->lookahead -= MIN(VOICE_FRAME_COUNT, td->lookahead);
458 if (td->src.remcount > 0)
459 return VOICE_STATE_BUFFER_INSERT;
462 return VOICE_STATE_DECODE;
465 /* Process the PCM samples in the DSP and send out for mixing */
466 static enum voice_state voice_buffer_insert(struct voice_thread_data *td)
468 if (!queue_empty(&voice_queue))
469 return VOICE_STATE_MESSAGE;
471 struct dsp_buffer dst;
473 if ((dst.p16out = voice_buf_get()) != NULL)
475 dst.remcount = 0;
476 dst.bufcount = VOICE_PCM_FRAME_COUNT;
478 td->dst = &dst;
479 dsp_process(td->dsp, &td->src, &dst);
480 td->dst = NULL;
482 voice_buf_commit(dst.remcount);
484 /* Unless other effects are introduced to voice that have delays,
485 all output should have been purged to dst in one call */
486 return td->src.remcount > 0 ?
487 VOICE_STATE_BUFFER_INSERT : VOICE_STATE_DECODE;
490 sleep(0);
491 return VOICE_STATE_BUFFER_INSERT;
494 /* Voice thread entrypoint */
495 static void NORETURN_ATTR voice_thread(void)
497 struct voice_thread_data td;
498 enum voice_state state = VOICE_STATE_MESSAGE;
500 voice_data_init(&td);
502 while (1)
504 switch (state)
506 case VOICE_STATE_MESSAGE:
507 state = voice_message(&td);
508 break;
509 case VOICE_STATE_DECODE:
510 state = voice_decode(&td);
511 break;
512 case VOICE_STATE_BUFFER_INSERT:
513 state = voice_buffer_insert(&td);
514 break;
519 /* Initialize buffers, all synchronization objects and create the thread */
520 void voice_thread_init(void)
522 if (voice_thread_id != 0)
523 return; /* Already did an init and succeeded at it */
525 if (!talk_voice_required())
527 logf("No voice required");
528 return;
531 voice_buf_hid = core_alloc_ex("voice buf", sizeof (*voice_buf), &ops);
533 if (voice_buf_hid <= 0)
535 logf("voice: core_alloc_ex failed");
536 return;
539 voice_buf = core_get_data(voice_buf_hid);
541 if (voice_buf == NULL)
543 logf("voice: core_get_data failed");
544 core_free(voice_buf_hid);
545 voice_buf_hid = 0;
546 return;
549 memset(voice_buf, 0, sizeof (*voice_buf));
551 logf("Starting voice thread");
552 queue_init(&voice_queue, false);
554 voice_thread_id = create_thread(voice_thread, voice_stack,
555 sizeof(voice_stack), 0, voice_thread_name
556 IF_PRIO(, PRIORITY_VOICE) IF_COP(, CPU));
558 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
559 voice_thread_id);
562 #ifdef HAVE_PRIORITY_SCHEDULING
563 /* Set the voice thread priority */
564 void voice_thread_set_priority(int priority)
566 if (voice_thread_id == 0)
567 return;
569 if (priority > PRIORITY_VOICE)
570 priority = PRIORITY_VOICE;
572 thread_set_priority(voice_thread_id, priority);
574 #endif