Fix libmad compilation on MIPS with newer gcc. FS#12013
[maemo-rb.git] / apps / voice_thread.c
blob87e4eee9cbc207b61992553cc7058e6516c6e52c
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;
340 /* Voice thread message processing */
341 static enum voice_state voice_message(struct voice_thread_data *td)
343 if (quiet_counter > 0)
344 queue_wait_w_tmo(&voice_queue, &td->ev, HZ/10);
345 else
346 queue_wait(&voice_queue, &td->ev);
348 switch (td->ev.id)
350 case Q_VOICE_PLAY:
351 LOGFQUEUE("voice < Q_VOICE_PLAY");
352 if (quiet_counter == 0)
354 /* Boost CPU now */
355 trigger_cpu_boost();
357 else
359 /* Stop any clip still playing */
360 voice_stop_playback();
363 quiet_counter = QUIET_COUNT;
365 /* Copy the clip info */
366 td->vi = *(struct voice_info *)td->ev.data;
368 /* We need nothing more from the sending thread - let it run */
369 queue_reply(&voice_queue, 1);
371 /* Make audio play more softly and set delay to return to normal
372 playback level */
373 pcmbuf_soft_mode(true);
375 /* Clean-start the decoder */
376 td->st = speex_decoder_init(&speex_wb_mode);
378 /* Make bit buffer use our own buffer */
379 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
380 td->vi.size);
381 speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);
383 return VOICE_STATE_DECODE;
385 case SYS_TIMEOUT:
386 if (voice_unplayed_frames())
388 /* Waiting for PCM to finish */
389 break;
392 /* Drop through and stop the first time after clip runs out */
393 if (quiet_counter-- != QUIET_COUNT)
395 if (quiet_counter <= 0)
396 pcmbuf_soft_mode(false);
398 break;
401 /* Fall-through */
402 case Q_VOICE_STOP:
403 LOGFQUEUE("voice < Q_VOICE_STOP");
404 cancel_cpu_boost();
405 voice_stop_playback();
406 break;
408 /* No default: no other message ids are sent */
411 return VOICE_STATE_MESSAGE;
414 /* Decode frames or stop if all have completed */
415 static enum voice_state voice_decode(struct voice_thread_data *td)
417 if (!queue_empty(&voice_queue))
418 return VOICE_STATE_MESSAGE;
420 /* Decode the data */
421 if (speex_decode_int(td->st, &td->bits, voice_buf->spx_outbuf) < 0)
423 /* End of stream or error - get next clip */
424 td->vi.size = 0;
426 if (td->vi.get_more != NULL)
427 td->vi.get_more(&td->vi.start, &td->vi.size);
429 if (td->vi.start != NULL && td->vi.size > 0)
431 /* Make bit buffer use our own buffer */
432 speex_bits_set_bit_buffer(&td->bits, (void *)td->vi.start,
433 td->vi.size);
434 /* Don't skip any samples when we're stringing clips together */
435 td->lookahead = 0;
437 else
439 /* If all clips are done and not playing, force pcm playback. */
440 if (voice_unplayed_frames() > 0)
441 voice_start_playback();
442 return VOICE_STATE_MESSAGE;
445 else
447 yield();
449 /* Output the decoded frame */
450 td->src.remcount = VOICE_FRAME_COUNT - td->lookahead;
451 td->src.pin[0] = &voice_buf->spx_outbuf[td->lookahead];
452 td->src.pin[1] = NULL;
453 td->src.proc_mask = 0;
455 td->lookahead -= MIN(VOICE_FRAME_COUNT, td->lookahead);
457 if (td->src.remcount > 0)
458 return VOICE_STATE_BUFFER_INSERT;
461 return VOICE_STATE_DECODE;
464 /* Process the PCM samples in the DSP and send out for mixing */
465 static enum voice_state voice_buffer_insert(struct voice_thread_data *td)
467 if (!queue_empty(&voice_queue))
468 return VOICE_STATE_MESSAGE;
470 struct dsp_buffer dst;
472 if ((dst.p16out = voice_buf_get()) != NULL)
474 dst.remcount = 0;
475 dst.bufcount = VOICE_PCM_FRAME_COUNT;
477 td->dst = &dst;
478 dsp_process(td->dsp, &td->src, &dst);
479 td->dst = NULL;
481 voice_buf_commit(dst.remcount);
483 /* Unless other effects are introduced to voice that have delays,
484 all output should have been purged to dst in one call */
485 return td->src.remcount > 0 ?
486 VOICE_STATE_BUFFER_INSERT : VOICE_STATE_DECODE;
489 sleep(0);
490 return VOICE_STATE_BUFFER_INSERT;
493 /* Voice thread entrypoint */
494 static void NORETURN_ATTR voice_thread(void)
496 struct voice_thread_data td;
497 enum voice_state state = VOICE_STATE_MESSAGE;
499 voice_data_init(&td);
501 while (1)
503 switch (state)
505 case VOICE_STATE_MESSAGE:
506 state = voice_message(&td);
507 break;
508 case VOICE_STATE_DECODE:
509 state = voice_decode(&td);
510 break;
511 case VOICE_STATE_BUFFER_INSERT:
512 state = voice_buffer_insert(&td);
513 break;
518 /* Initialize buffers, all synchronization objects and create the thread */
519 void voice_thread_init(void)
521 if (voice_thread_id != 0)
522 return; /* Already did an init and succeeded at it */
524 if (!talk_voice_required())
526 logf("No voice required");
527 return;
530 voice_buf_hid = core_alloc_ex("voice buf", sizeof (*voice_buf), &ops);
532 if (voice_buf_hid <= 0)
534 logf("voice: core_alloc_ex failed");
535 return;
538 voice_buf = core_get_data(voice_buf_hid);
540 if (voice_buf == NULL)
542 logf("voice: core_get_data failed");
543 core_free(voice_buf_hid);
544 voice_buf_hid = 0;
545 return;
548 memset(voice_buf, 0, sizeof (*voice_buf));
550 logf("Starting voice thread");
551 queue_init(&voice_queue, false);
553 voice_thread_id = create_thread(voice_thread, voice_stack,
554 sizeof(voice_stack), 0, voice_thread_name
555 IF_PRIO(, PRIORITY_VOICE) IF_COP(, CPU));
557 queue_enable_queue_send(&voice_queue, &voice_queue_sender_list,
558 voice_thread_id);
561 #ifdef HAVE_PRIORITY_SCHEDULING
562 /* Set the voice thread priority */
563 void voice_thread_set_priority(int priority)
565 if (voice_thread_id == 0)
566 return;
568 if (priority > PRIORITY_VOICE)
569 priority = PRIORITY_VOICE;
571 thread_set_priority(voice_thread_id, priority);
573 #endif