1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "voice_thread.h"
29 #include "codecs/libspeex/speex/speex.h"
31 /* Define any of these as "1" and uncomment the LOGF_ENABLE line to log
32 regular and/or timeout messages */
33 #define VOICE_LOGQUEUES 0
34 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
36 /*#define LOGF_ENABLE*/
40 #define LOGFQUEUE logf
42 #define LOGFQUEUE(...)
45 #if VOICE_LOGQUEUES_SYS_TIMEOUT
46 #define LOGFQUEUE_SYS_TIMEOUT logf
48 #define LOGFQUEUE_SYS_TIMEOUT(...)
51 #ifndef IBSS_ATTR_VOICE_STACK
52 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
55 #define VOICE_FRAME_SIZE 320 /* Samples / frame */
56 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
57 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
59 /* Voice thread variables */
60 static unsigned int voice_thread_id
= 0;
61 static long voice_stack
[0x7c0/sizeof(long)] IBSS_ATTR_VOICE_STACK
;
62 static const char voice_thread_name
[] = "voice";
64 /* Voice thread synchronization objects */
65 static struct event_queue voice_queue SHAREDBSS_ATTR
;
66 static struct mutex voice_mutex SHAREDBSS_ATTR
;
67 static struct queue_sender_list voice_queue_sender_list SHAREDBSS_ATTR
;
68 static bool voice_done SHAREDDATA_ATTR
= true;
70 /* Buffer for decoded samples */
71 static spx_int16_t voice_output_buf
[VOICE_FRAME_SIZE
] CACHEALIGN_ATTR
;
73 enum voice_thread_states
75 TSTATE_STOPPED
= 0, /* Voice thread is stopped and awaiting commands */
76 TSTATE_DECODE
, /* Voice is decoding a clip */
77 TSTATE_BUFFER_INSERT
, /* Voice is sending decoded audio to PCM */
80 enum voice_thread_messages
82 Q_VOICE_NULL
= 0, /* A message for thread sync - no effect on state */
83 Q_VOICE_PLAY
, /* Play a clip */
84 Q_VOICE_STOP
, /* Stop current clip */
85 Q_VOICE_STATE
, /* Query playing state */
88 /* Structure to store clip data callback info */
91 pcm_more_callback_type get_more
; /* Callback to get more clips */
92 unsigned char *start
; /* Start of clip */
93 size_t size
; /* Size of clip */
96 /* Private thread data for its current state that must be passed to its
97 * internal functions */
98 struct voice_thread_data
100 int state
; /* Thread state (TSTATE_*) */
101 struct queue_event ev
; /* Last queue event pulled from queue */
102 void *st
; /* Decoder instance */
103 SpeexBits bits
; /* Bit cursor */
104 struct dsp_config
*dsp
; /* DSP used for voice output */
105 struct voice_info vi
; /* Copy of clip data */
106 const char *src
[2]; /* Current output buffer pointers */
107 int lookahead
; /* Number of samples to drop at start of clip */
108 int count
; /* Count of samples remaining to send to PCM */
111 /* Audio playback is in a playing state? */
112 static inline bool playback_is_playing(void)
114 return (audio_status() & AUDIO_STATUS_PLAY
) != 0;
117 /* Stop any current clip and start playing a new one */
118 void mp3_play_data(const unsigned char* start
, int size
,
119 pcm_more_callback_type get_more
)
121 /* Shared struct to get data to the thread - once it replies, it has
122 * safely cached it in its own private data */
123 static struct voice_info voice_clip SHAREDBSS_ATTR
;
125 if (get_more
!= NULL
&& start
!= NULL
&& (ssize_t
)size
> 0)
127 mutex_lock(&voice_mutex
);
129 voice_clip
.get_more
= get_more
;
130 voice_clip
.start
= (unsigned char *)start
;
131 voice_clip
.size
= size
;
132 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
133 queue_send(&voice_queue
, Q_VOICE_PLAY
, (intptr_t)&voice_clip
);
135 mutex_unlock(&voice_mutex
);
139 /* Stop current voice clip from playing */
140 void mp3_play_stop(void)
142 if(!audio_is_thread_ready())
145 mutex_lock(&voice_mutex
); /* Sync against voice_stop */
146 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
147 queue_send(&voice_queue
, Q_VOICE_STOP
, 1);
149 mutex_unlock(&voice_mutex
);
152 void mp3_play_pause(bool play
)
158 /* Tell is voice is still in a playing state */
159 bool mp3_is_playing(void)
161 /* TODO: Implement a timeout or state query function for event objects */
162 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
163 int state
= queue_send(&voice_queue
, Q_VOICE_STATE
, 0);
164 return state
!= TSTATE_STOPPED
;
167 /* This function is meant to be used by the buffer request functions to
168 ensure the codec is no longer active */
169 void voice_stop(void)
171 mutex_lock(&voice_mutex
);
173 /* Stop the output and current clip */
176 /* Careful if using sync objects in talk.c - make sure locking order is
177 * observed with one or the other always granted first */
179 /* Unqueue all future clips */
182 /* Wait for any final queue_post to be processed */
183 LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
184 queue_send(&voice_queue
, Q_VOICE_NULL
, 0);
186 mutex_unlock(&voice_mutex
);
189 /* Wait for voice to finish speaking. */
190 void voice_wait(void)
192 /* NOTE: One problem here is that we can't tell if another thread started a
193 * new clip by the time we wait. This should be resolvable if conditions
194 * ever require knowing the very clip you requested has finished. */
196 /* Wait for PCM buffer to be exhausted. Works only if not playing. */
197 while(!voice_done
|| (!playback_is_playing() && pcm_is_playing()))
201 /* Initialize voice thread data that must be valid upon starting and the
202 * setup the DSP parameters */
203 static void voice_data_init(struct voice_thread_data
*td
)
205 td
->state
= TSTATE_STOPPED
;
206 td
->dsp
= (struct dsp_config
*)dsp_configure(NULL
, DSP_MYDSP
,
209 dsp_configure(td
->dsp
, DSP_RESET
, 0);
210 dsp_configure(td
->dsp
, DSP_SET_FREQUENCY
, VOICE_SAMPLE_RATE
);
211 dsp_configure(td
->dsp
, DSP_SET_SAMPLE_DEPTH
, VOICE_SAMPLE_DEPTH
);
212 dsp_configure(td
->dsp
, DSP_SET_STEREO_MODE
, STEREO_MONO
);
215 /* Voice thread message processing */
216 static void voice_message(struct voice_thread_data
*td
)
223 LOGFQUEUE("voice < Q_VOICE_PLAY");
224 /* Put up a block for completion signal */
227 /* Copy the clip info */
228 td
->vi
= *(struct voice_info
*)td
->ev
.data
;
230 /* Be sure audio buffer is initialized */
231 audio_restore_playback(AUDIO_WANT_VOICE
);
233 /* We need nothing more from the sending thread - let it run */
234 queue_reply(&voice_queue
, 1);
236 if (td
->state
== TSTATE_STOPPED
)
241 else if (!playback_is_playing())
243 /* Just voice, stop any clip still playing */
247 /* Clean-start the decoder */
248 td
->st
= speex_decoder_init(&speex_wb_mode
);
250 /* Make bit buffer use our own buffer */
251 speex_bits_set_bit_buffer(&td
->bits
, td
->vi
.start
, td
->vi
.size
);
252 speex_decoder_ctl(td
->st
, SPEEX_GET_LOOKAHEAD
, &td
->lookahead
);
254 td
->state
= TSTATE_DECODE
;
258 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev
.data
);
260 if (td
->ev
.data
!= 0 && !playback_is_playing())
262 /* If not playing, it's just voice so stop pcm playback */
269 td
->state
= TSTATE_STOPPED
;
274 LOGFQUEUE("voice < Q_VOICE_STATE");
275 queue_reply(&voice_queue
, td
->state
);
277 if (td
->state
== TSTATE_STOPPED
)
278 break; /* Not in a playback state */
283 /* Default messages get a reply and thread continues with no
284 * state transition */
285 LOGFQUEUE("voice < default");
287 if (td
->state
== TSTATE_STOPPED
)
288 break; /* Not in playback state */
290 queue_reply(&voice_queue
, 0);
294 queue_wait(&voice_queue
, &td
->ev
);
298 /* Voice thread entrypoint */
299 static void voice_thread(void)
301 struct voice_thread_data td
;
303 voice_data_init(&td
);
305 /* audio thread will only set this once after it finished the final
306 * audio hardware init so this little construct is safe - even
308 while (!audio_is_thread_ready())
315 td
.state
= TSTATE_DECODE
;
317 if (!queue_empty(&voice_queue
))
320 queue_wait(&voice_queue
, &td
.ev
);
325 /* Branch to initial start point or branch back to previous
326 * operation if interrupted by a message */
329 case TSTATE_DECODE
: goto voice_decode
;
330 case TSTATE_BUFFER_INSERT
: goto buffer_insert
;
331 default: goto message_wait
;
336 /* Decode the data */
337 if (speex_decode_int(td
.st
, &td
.bits
, voice_output_buf
) < 0)
339 /* End of stream or error - get next clip */
342 if (td
.vi
.get_more
!= NULL
)
343 td
.vi
.get_more(&td
.vi
.start
, &td
.vi
.size
);
345 if (td
.vi
.start
!= NULL
&& (ssize_t
)td
.vi
.size
> 0)
347 /* Make bit buffer use our own buffer */
348 speex_bits_set_bit_buffer(&td
.bits
, td
.vi
.start
, td
.vi
.size
);
349 /* Don't skip any samples when we're stringing clips together */
352 /* Paranoid check - be sure never to somehow get stuck in a
353 * loop without listening to the queue */
356 if (!queue_empty(&voice_queue
))
362 /* If all clips are done and not playing, force pcm playback. */
363 if (!pcm_is_playing())
366 /* Synthesize a stop request */
367 /* NOTE: We have no way to know when the pcm data placed in the
368 * buffer is actually consumed and playback has reached the end
369 * so until the info is available or inferred somehow, this will
370 * not be accurate and the stopped signal will come too soon.
371 * ie. You may not hear the "Shutting Down" splash even though
372 * it waits for voice to stop. */
373 td
.ev
.id
= Q_VOICE_STOP
;
374 td
.ev
.data
= 0; /* Let PCM drain by itself */
376 goto message_process
;
381 /* Output the decoded frame */
382 td
.count
= VOICE_FRAME_SIZE
- td
.lookahead
;
383 td
.src
[0] = (const char *)&voice_output_buf
[td
.lookahead
];
385 td
.lookahead
-= MIN(VOICE_FRAME_SIZE
, td
.lookahead
);
388 /* Process the PCM samples in the DSP and send out for mixing */
389 td
.state
= TSTATE_BUFFER_INSERT
;
393 int out_count
= dsp_output_count(td
.dsp
, td
.count
);
399 if (!queue_empty(&voice_queue
))
402 if ((dest
= pcmbuf_request_voice_buffer(&out_count
)) != NULL
)
408 /* Get the real input_size for output_size bytes, guarding
409 * against resampling buffer overflows. */
410 inp_count
= dsp_input_count(td
.dsp
, out_count
);
415 /* Input size has grown, no error, just don't write more than
417 if (inp_count
> td
.count
)
418 inp_count
= td
.count
;
420 out_count
= dsp_process(td
.dsp
, dest
, td
.src
, inp_count
);
425 pcmbuf_write_voice_complete(out_count
);
426 td
.count
-= inp_count
;
433 /* Initialize all synchronization objects create the thread */
434 void voice_thread_init(void)
436 logf("Starting voice thread");
437 queue_init(&voice_queue
, false);
438 mutex_init(&voice_mutex
);
440 voice_thread_id
= create_thread(voice_thread
, voice_stack
,
441 sizeof(voice_stack
), CREATE_THREAD_FROZEN
,
442 voice_thread_name
IF_PRIO(, PRIORITY_PLAYBACK
) IF_COP(, CPU
));
444 queue_enable_queue_send(&voice_queue
, &voice_queue_sender_list
,
446 } /* voice_thread_init */
448 /* Unfreeze the voice thread */
449 void voice_thread_resume(void)
451 logf("Thawing voice thread");
452 thread_thaw(voice_thread_id
);
455 #ifdef HAVE_PRIORITY_SCHEDULING
456 /* Set the voice thread priority */
457 void voice_thread_set_priority(int priority
)
459 thread_set_priority(voice_thread_id
, priority
);