1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
22 #include "voice_thread.h"
27 #include "codecs/libspeex/speex/speex.h"
29 /* Define any of these as "1" to log regular and/or timeout messages */
30 #define VOICE_LOGQUEUES 0
31 #define VOICE_LOGQUEUES_SYS_TIMEOUT 0
34 #define LOGFQUEUE logf
36 #define LOGFQUEUE(...)
39 #if VOICE_LOGQUEUES_SYS_TIMEOUT
40 #define LOGFQUEUE_SYS_TIMEOUT logf
42 #define LOGFQUEUE_SYS_TIMEOUT(...)
45 #ifndef IBSS_ATTR_VOICE_STACK
46 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
49 #define VOICE_FRAME_SIZE 320 /* Samples / frame */
50 #define VOICE_SAMPLE_RATE 16000 /* Sample rate in HZ */
51 #define VOICE_SAMPLE_DEPTH 16 /* Sample depth in bits */
53 /* Voice thread variables */
54 static struct thread_entry
*voice_thread_p
= NULL
;
55 static long voice_stack
[0x740/sizeof(long)] IBSS_ATTR_VOICE_STACK
;
56 static const char voice_thread_name
[] = "voice";
58 /* Voice thread synchronization objects */
59 static struct event_queue voice_queue NOCACHEBSS_ATTR
;
60 static struct mutex voice_mutex NOCACHEBSS_ATTR
;
61 static struct event voice_event NOCACHEBSS_ATTR
;
62 static struct queue_sender_list voice_queue_sender_list NOCACHEBSS_ATTR
;
64 /* Buffer for decoded samples */
65 static spx_int16_t voice_output_buf
[VOICE_FRAME_SIZE
] CACHEALIGN_ATTR
;
67 enum voice_thread_states
69 TSTATE_STOPPED
= 0, /* Voice thread is stopped and awaiting commands */
70 TSTATE_DECODE
, /* Voice is decoding a clip */
71 TSTATE_BUFFER_INSERT
, /* Voice is sending decoded audio to PCM */
74 enum voice_thread_messages
76 Q_VOICE_NULL
= 0, /* A message for thread sync - no effect on state */
77 Q_VOICE_PLAY
, /* Play a clip */
78 Q_VOICE_STOP
, /* Stop current clip */
79 Q_VOICE_STATE
, /* Query playing state */
82 /* Structure to store clip data callback info */
85 pcm_more_callback_type get_more
; /* Callback to get more clips */
86 unsigned char *start
; /* Start of clip */
87 size_t size
; /* Size of clip */
90 /* Private thread data for its current state that must be passed to its
91 * internal functions */
92 struct voice_thread_data
94 int state
; /* Thread state (TSTATE_*) */
95 struct queue_event ev
; /* Last queue event pulled from queue */
96 void *st
; /* Decoder instance */
97 SpeexBits bits
; /* Bit cursor */
98 struct dsp_config
*dsp
; /* DSP used for voice output */
99 struct voice_info vi
; /* Copy of clip data */
100 const char *src
[2]; /* Current output buffer pointers */
101 int lookahead
; /* Number of samples to drop at start of clip */
102 int count
; /* Count of samples remaining to send to PCM */
105 /* Audio playback is in a playing state? */
106 static inline bool playback_is_playing(void)
108 return (audio_status() & AUDIO_STATUS_PLAY
) != 0;
111 /* Stop any current clip and start playing a new one */
112 void mp3_play_data(const unsigned char* start
, int size
,
113 pcm_more_callback_type get_more
)
115 /* Shared struct to get data to the thread - once it replies, it has
116 * safely cached it in its own private data */
117 static struct voice_info voice_clip NOCACHEBSS_ATTR
;
119 if (get_more
!= NULL
&& start
!= NULL
&& (ssize_t
)size
> 0)
121 mutex_lock(&voice_mutex
);
123 voice_clip
.get_more
= get_more
;
124 voice_clip
.start
= (unsigned char *)start
;
125 voice_clip
.size
= size
;
126 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
127 queue_send(&voice_queue
, Q_VOICE_PLAY
, (intptr_t)&voice_clip
);
129 mutex_unlock(&voice_mutex
);
133 /* Stop current voice clip from playing */
134 void mp3_play_stop(void)
136 mutex_lock(&voice_mutex
); /* Sync against voice_stop */
138 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
139 queue_send(&voice_queue
, Q_VOICE_STOP
, 1);
141 mutex_unlock(&voice_mutex
);
144 void mp3_play_pause(bool play
)
150 /* Tell is voice is still in a playing state */
151 bool mp3_is_playing(void)
153 /* TODO: Implement a timeout or state query function for event objects */
154 LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
155 int state
= queue_send(&voice_queue
, Q_VOICE_STATE
, 0);
156 return state
!= TSTATE_STOPPED
;
159 /* This function is meant to be used by the buffer request functions to
160 ensure the codec is no longer active */
161 void voice_stop(void)
163 mutex_lock(&voice_mutex
);
165 /* Stop the output and current clip */
168 /* Careful if using sync objects in talk.c - make sure locking order is
169 * observed with one or the other always granted first */
171 /* Unqueue all future clips */
174 mutex_unlock(&voice_mutex
);
177 /* Wait for voice to finish speaking. */
178 void voice_wait(void)
180 /* NOTE: One problem here is that we can't tell if another thread started a
181 * new clip by the time we wait. This should be resolvable if conditions
182 * ever require knowing the very clip you requested has finished. */
183 event_wait(&voice_event
, STATE_SIGNALED
);
186 /* Initialize voice thread data that must be valid upon starting and the
187 * setup the DSP parameters */
188 static void voice_data_init(struct voice_thread_data
*td
)
190 td
->state
= TSTATE_STOPPED
;
191 td
->dsp
= (struct dsp_config
*)dsp_configure(NULL
, DSP_MYDSP
,
194 dsp_configure(td
->dsp
, DSP_RESET
, 0);
195 dsp_configure(td
->dsp
, DSP_SET_FREQUENCY
, VOICE_SAMPLE_RATE
);
196 dsp_configure(td
->dsp
, DSP_SET_SAMPLE_DEPTH
, VOICE_SAMPLE_DEPTH
);
197 dsp_configure(td
->dsp
, DSP_SET_STEREO_MODE
, STEREO_MONO
);
200 /* Voice thread message processing */
201 static void voice_message(struct voice_thread_data
*td
)
208 LOGFQUEUE("voice < Q_VOICE_PLAY");
209 /* Put up a block for completion signal */
210 event_set_state(&voice_event
, STATE_NONSIGNALED
);
212 /* Copy the clip info */
213 td
->vi
= *(struct voice_info
*)td
->ev
.data
;
215 /* Be sure audio buffer is initialized */
216 audio_restore_playback(AUDIO_WANT_VOICE
);
218 /* We need nothing more from the sending thread - let it run */
219 queue_reply(&voice_queue
, 1);
221 if (td
->state
== TSTATE_STOPPED
)
226 else if (!playback_is_playing())
228 /* Just voice, stop any clip still playing */
232 /* Clean-start the decoder */
233 td
->st
= speex_decoder_init(&speex_wb_mode
);
235 /* Make bit buffer use our own buffer */
236 speex_bits_set_bit_buffer(&td
->bits
, td
->vi
.start
, td
->vi
.size
);
237 speex_decoder_ctl(td
->st
, SPEEX_GET_LOOKAHEAD
, &td
->lookahead
);
239 td
->state
= TSTATE_DECODE
;
243 LOGFQUEUE("voice < Q_VOICE_STOP: %d", ev
.data
);
245 if (td
->ev
.data
!= 0 && !playback_is_playing())
247 /* If not playing, it's just voice so stop pcm playback */
254 td
->state
= TSTATE_STOPPED
;
255 event_set_state(&voice_event
, STATE_SIGNALED
);
259 LOGFQUEUE("voice < Q_VOICE_STATE");
260 queue_reply(&voice_queue
, td
->state
);
262 if (td
->state
== TSTATE_STOPPED
)
263 break; /* Not in a playback state */
268 /* Default messages get a reply and thread continues with no
269 * state transition */
270 LOGFQUEUE("voice < default");
272 if (td
->state
== TSTATE_STOPPED
)
273 break; /* Not in playback state */
275 queue_reply(&voice_queue
, 0);
279 queue_wait(&voice_queue
, &td
->ev
);
283 /* Voice thread entrypoint */
284 static void voice_thread(void)
286 struct voice_thread_data td
;
288 voice_data_init(&td
);
294 td
.state
= TSTATE_DECODE
;
296 if (!queue_empty(&voice_queue
))
299 queue_wait(&voice_queue
, &td
.ev
);
304 /* Branch to initial start point or branch back to previous
305 * operation if interrupted by a message */
308 case TSTATE_DECODE
: goto voice_decode
;
309 case TSTATE_BUFFER_INSERT
: goto buffer_insert
;
310 default: goto message_wait
;
315 /* Check if all data was exhausted for this clip */
316 if (speex_bits_remaining(&td
.bits
) < 8)
322 if (td
.vi
.get_more
!= NULL
)
323 td
.vi
.get_more(&td
.vi
.start
, &td
.vi
.size
);
325 if (td
.vi
.start
!= NULL
&& (ssize_t
)td
.vi
.size
> 0)
327 /* Make bit buffer use our own buffer */
328 speex_bits_set_bit_buffer(&td
.bits
, td
.vi
.start
, td
.vi
.size
);
329 speex_decoder_ctl(td
.st
, SPEEX_GET_LOOKAHEAD
, &td
.lookahead
);
333 if (!queue_empty(&voice_queue
))
339 /* If all clips are done and not playing, force pcm playback. */
340 if (!pcm_is_playing())
343 /* Synthesize a stop request */
344 /* NOTE: We have no way to know when the pcm data placed in the
345 * buffer is actually consumed and playback has reached the end
346 * so until the info is available or inferred somehow, this will
347 * not be accurate and the stopped signal will come too soon.
348 * ie. You may not hear the "Shutting Down" splash even though
349 * it waits for voice to stop. */
350 td
.ev
.id
= Q_VOICE_STOP
;
351 td
.ev
.data
= 0; /* Let PCM drain by itself */
353 goto message_process
;
356 /* Decode the data */
357 int status
= speex_decode_int(td
.st
, &td
.bits
, voice_output_buf
);
361 goto voice_error
; /* error - try some more */
363 /* Output the decoded frame */
364 td
.count
= VOICE_FRAME_SIZE
- td
.lookahead
;
365 td
.src
[0] = (const char *)&voice_output_buf
[td
.lookahead
];
367 td
.lookahead
-= MIN(VOICE_FRAME_SIZE
, td
.lookahead
);
370 /* Process the PCM samples in the DSP and send out for mixing */
371 td
.state
= TSTATE_BUFFER_INSERT
;
375 int out_count
= dsp_output_count(td
.dsp
, td
.count
);
381 if (!queue_empty(&voice_queue
))
384 if ((dest
= pcmbuf_request_voice_buffer(&out_count
)) != NULL
)
390 /* Get the real input_size for output_size bytes, guarding
391 * against resampling buffer overflows. */
392 inp_count
= dsp_input_count(td
.dsp
, out_count
);
397 /* Input size has grown, no error, just don't write more than
399 if (inp_count
> td
.count
)
400 inp_count
= td
.count
;
402 out_count
= dsp_process(td
.dsp
, dest
, td
.src
, inp_count
);
407 pcmbuf_write_voice_complete(out_count
);
408 td
.count
-= inp_count
;
415 /* Initialize all synchronization objects create the thread */
416 void voice_thread_init(void)
418 logf("Starting voice thread");
419 queue_init(&voice_queue
, false);
420 queue_enable_queue_send(&voice_queue
, &voice_queue_sender_list
);
421 mutex_init(&voice_mutex
);
422 event_init(&voice_event
, STATE_SIGNALED
| EVENT_MANUAL
);
423 voice_thread_p
= create_thread(voice_thread
, voice_stack
,
424 sizeof(voice_stack
), CREATE_THREAD_FROZEN
,
425 voice_thread_name
IF_PRIO(, PRIORITY_PLAYBACK
) IF_COP(, CPU
));
426 } /* voice_thread_init */
428 /* Unfreeze the voice thread */
429 void voice_thread_resume(void)
431 logf("Thawing voice thread");
432 thread_thaw(voice_thread_p
);
433 /* Wait for initialization to complete (a very short wait until the
434 * voice thread is available to process messages) */
435 queue_send(&voice_queue
, Q_VOICE_NULL
, 0);
438 #ifdef HAVE_PRIORITY_SCHEDULING
439 /* Set the voice thread priority */
440 void voice_thread_set_priority(int priority
)
442 thread_set_priority(voice_thread_p
, priority
);