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 ****************************************************************************/
21 #include <sys/types.h>
24 #include "voice_thread.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*/
43 #define LOGFQUEUE logf
45 #define LOGFQUEUE(...)
48 #if VOICE_LOGQUEUES_SYS_TIMEOUT
49 #define LOGFQUEUE_SYS_TIMEOUT logf
51 #define LOGFQUEUE_SYS_TIMEOUT(...)
54 #ifndef IBSS_ATTR_VOICE_STACK
55 #define IBSS_ATTR_VOICE_STACK IBSS_ATTR
58 /* Minimum priority needs to be a bit elevated since voice has fairly low
60 #define PRIORITY_VOICE (PRIORITY_PLAYBACK-4)
62 #define VOICE_FRAME_SIZE 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;
69 /* ISR uses any available stack - need a bit more room */
70 #define VOICE_STACK_EXTRA 0x400
72 #define VOICE_STACK_EXTRA 0x3c0
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 bool voice_done SHAREDDATA_ATTR
= true;
83 /* Buffer for decoded samples */
84 static spx_int16_t voice_output_buf
[VOICE_FRAME_SIZE
] CACHEALIGN_ATTR
;
86 #define VOICE_PCM_FRAME_COUNT ((NATIVE_FREQUENCY*VOICE_FRAME_SIZE + \
87 VOICE_SAMPLE_RATE) / VOICE_SAMPLE_RATE)
88 #define VOICE_PCM_FRAME_SIZE (VOICE_PCM_FRAME_COUNT*4)
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 uint32_t (* voicebuf
)[VOICE_PCM_FRAME_COUNT
];
97 static unsigned int cur_buf_in
, cur_buf_out
;
99 /* A delay to not bring audio back to normal level too soon */
100 #define QUIET_COUNT 3
102 enum voice_thread_states
104 TSTATE_STOPPED
= 0, /* Voice thread is stopped and awaiting commands */
105 TSTATE_DECODE
, /* Voice is decoding a clip */
106 TSTATE_BUFFER_INSERT
, /* Voice is sending decoded audio to PCM */
109 enum voice_thread_messages
111 Q_VOICE_NULL
= 0, /* A message for thread sync - no effect on state */
112 Q_VOICE_PLAY
, /* Play a clip */
113 Q_VOICE_STOP
, /* Stop current clip */
116 /* Structure to store clip data callback info */
119 pcm_play_callback_type get_more
; /* Callback to get more clips */
120 unsigned char *start
; /* Start of clip */
121 size_t size
; /* Size of clip */
124 /* Private thread data for its current state that must be passed to its
125 * internal functions */
126 struct voice_thread_data
128 volatile int state
; /* Thread state (TSTATE_*) */
129 struct queue_event ev
; /* Last queue event pulled from queue */
130 void *st
; /* Decoder instance */
131 SpeexBits bits
; /* Bit cursor */
132 struct dsp_config
*dsp
; /* DSP used for voice output */
133 struct voice_info vi
; /* Copy of clip data */
134 const char *src
[2]; /* Current output buffer pointers */
135 int lookahead
; /* Number of samples to drop at start of clip */
136 int count
; /* Count of samples remaining to send to PCM */
137 int quiet_counter
; /* Countdown until audio goes back to normal */
140 /* Number of frames in queue */
141 static inline int voice_unplayed_frames(void)
143 return cur_buf_in
- cur_buf_out
;
146 /* Mixer channel callback */
147 static void voice_pcm_callback(unsigned char **start
, size_t *size
)
149 if (voice_unplayed_frames() == 0)
152 unsigned int i
= ++cur_buf_out
% VOICE_FRAMES
;
154 *start
= (unsigned char *)voicebuf
[i
];
155 *size
= voicebuf_sizes
[i
];
158 /* Start playback of voice channel if not already playing */
159 static void voice_start_playback(void)
161 if (mixer_channel_status(PCM_MIXER_CHAN_VOICE
) != CHANNEL_STOPPED
)
164 unsigned int i
= cur_buf_out
% VOICE_FRAMES
;
165 mixer_channel_play_data(PCM_MIXER_CHAN_VOICE
, voice_pcm_callback
,
166 (unsigned char *)voicebuf
[i
], voicebuf_sizes
[i
]);
169 /* Stop the voice channel */
170 static void voice_stop_playback(void)
172 mixer_channel_stop(PCM_MIXER_CHAN_VOICE
);
173 cur_buf_in
= cur_buf_out
= 0;
176 /* Grab a free PCM frame */
177 static uint32_t * voice_buf_get(void)
179 if (voice_unplayed_frames() >= VOICE_FRAMES
)
182 voice_start_playback();
186 return voicebuf
[cur_buf_in
% VOICE_FRAMES
];
189 /* Commit a frame returned by voice_buf_get and set the actual size */
190 static void voice_buf_commit(size_t size
)
192 voicebuf_sizes
[cur_buf_in
++ % VOICE_FRAMES
] = size
;
195 /* Stop any current clip and start playing a new one */
196 void mp3_play_data(const unsigned char* start
, int size
,
197 pcm_play_callback_type get_more
)
199 if (get_more
!= NULL
&& start
!= NULL
&& (ssize_t
)size
> 0)
201 struct voice_info voice_clip
=
203 .get_more
= get_more
,
204 .start
= (unsigned char *)start
,
208 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
209 queue_send(&voice_queue
, Q_VOICE_PLAY
, (intptr_t)&voice_clip
);
213 /* Stop current voice clip from playing */
214 void mp3_play_stop(void)
216 if(!audio_is_thread_ready())
219 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
220 queue_send(&voice_queue
, Q_VOICE_STOP
, 0);
223 void mp3_play_pause(bool play
)
229 /* Tell if voice is still in a playing state */
230 bool mp3_is_playing(void)
235 /* This function is meant to be used by the buffer request functions to
236 ensure the codec is no longer active */
237 void voice_stop(void)
239 /* Unqueue all future clips */
243 /* Wait for voice to finish speaking. */
244 void voice_wait(void)
246 /* NOTE: One problem here is that we can't tell if another thread started a
247 * new clip by the time we wait. This should be resolvable if conditions
248 * ever require knowing the very clip you requested has finished. */
254 /* Initialize voice thread data that must be valid upon starting and the
255 * setup the DSP parameters */
256 static void voice_data_init(struct voice_thread_data
*td
)
258 td
->state
= TSTATE_STOPPED
;
259 td
->dsp
= (struct dsp_config
*)dsp_configure(NULL
, DSP_MYDSP
,
262 dsp_configure(td
->dsp
, DSP_RESET
, 0);
263 dsp_configure(td
->dsp
, DSP_SET_FREQUENCY
, VOICE_SAMPLE_RATE
);
264 dsp_configure(td
->dsp
, DSP_SET_SAMPLE_DEPTH
, VOICE_SAMPLE_DEPTH
);
265 dsp_configure(td
->dsp
, DSP_SET_STEREO_MODE
, STEREO_MONO
);
267 mixer_channel_set_amplitude(PCM_MIXER_CHAN_VOICE
, MIX_AMP_UNITY
);
268 td
->quiet_counter
= 0;
271 /* Voice thread message processing */
272 static void voice_message(struct voice_thread_data
*td
)
279 LOGFQUEUE("voice < Q_VOICE_PLAY");
282 /* Copy the clip info */
283 td
->vi
= *(struct voice_info
*)td
->ev
.data
;
285 /* Be sure audio buffer is initialized */
286 audio_restore_playback(AUDIO_WANT_VOICE
);
288 /* We need nothing more from the sending thread - let it run */
289 queue_reply(&voice_queue
, 1);
291 if (td
->state
== TSTATE_STOPPED
)
298 /* Stop any clip still playing */
299 voice_stop_playback();
302 /* Make audio play more softly and set delay to return to normal
304 pcmbuf_soft_mode(true);
305 td
->quiet_counter
= QUIET_COUNT
;
307 /* Clean-start the decoder */
308 td
->st
= speex_decoder_init(&speex_wb_mode
);
310 /* Make bit buffer use our own buffer */
311 speex_bits_set_bit_buffer(&td
->bits
, td
->vi
.start
, td
->vi
.size
);
312 speex_decoder_ctl(td
->st
, SPEEX_GET_LOOKAHEAD
, &td
->lookahead
);
314 td
->state
= TSTATE_DECODE
;
318 if (voice_unplayed_frames())
320 /* Waiting for PCM to finish */
324 /* Drop through and stop the first time after clip runs out */
325 if (td
->quiet_counter
-- != QUIET_COUNT
)
327 if (td
->quiet_counter
<= 0)
328 pcmbuf_soft_mode(false);
335 LOGFQUEUE("voice < Q_VOICE_STOP");
337 td
->state
= TSTATE_STOPPED
;
341 voice_stop_playback();
345 /* Default messages get a reply and thread continues with no
346 * state transition */
347 LOGFQUEUE("voice < default");
349 if (td
->state
== TSTATE_STOPPED
)
350 break; /* Not in (active) playback state */
352 queue_reply(&voice_queue
, 0);
356 if (td
->quiet_counter
> 0)
357 queue_wait_w_tmo(&voice_queue
, &td
->ev
, HZ
/10);
359 queue_wait(&voice_queue
, &td
->ev
);
363 /* Voice thread entrypoint */
364 static void NORETURN_ATTR
voice_thread(void)
366 struct voice_thread_data td
;
369 voice_data_init(&td
);
371 /* audio thread will only set this once after it finished the final
372 * audio hardware init so this little construct is safe - even
374 while (!audio_is_thread_ready())
381 td
.state
= TSTATE_DECODE
;
383 if (!queue_empty(&voice_queue
))
386 queue_wait(&voice_queue
, &td
.ev
);
391 /* Branch to initial start point or branch back to previous
392 * operation if interrupted by a message */
395 case TSTATE_DECODE
: goto voice_decode
;
396 case TSTATE_BUFFER_INSERT
: goto buffer_insert
;
397 default: goto message_wait
;
402 /* Decode the data */
403 if (speex_decode_int(td
.st
, &td
.bits
, voice_output_buf
) < 0)
405 /* End of stream or error - get next clip */
408 if (td
.vi
.get_more
!= NULL
)
409 td
.vi
.get_more(&td
.vi
.start
, &td
.vi
.size
);
411 if (td
.vi
.start
!= NULL
&& (ssize_t
)td
.vi
.size
> 0)
413 /* Make bit buffer use our own buffer */
414 speex_bits_set_bit_buffer(&td
.bits
, td
.vi
.start
, td
.vi
.size
);
415 /* Don't skip any samples when we're stringing clips together */
418 /* Paranoid check - be sure never to somehow get stuck in a
419 * loop without listening to the queue */
422 if (!queue_empty(&voice_queue
))
428 /* If all clips are done and not playing, force pcm playback. */
429 voice_start_playback();
431 td
.state
= TSTATE_STOPPED
;
432 td
.ev
.id
= SYS_TIMEOUT
;
433 goto message_process
;
438 /* Output the decoded frame */
439 td
.count
= VOICE_FRAME_SIZE
- td
.lookahead
;
440 td
.src
[0] = (const char *)&voice_output_buf
[td
.lookahead
];
442 td
.lookahead
-= MIN(VOICE_FRAME_SIZE
, td
.lookahead
);
447 td
.state
= TSTATE_BUFFER_INSERT
;
450 /* Process the PCM samples in the DSP and send out for mixing */
454 if (!queue_empty(&voice_queue
))
457 if ((dest
= (char *)voice_buf_get()) != NULL
)
463 voice_buf_commit(dsp_process(td
.dsp
, dest
, td
.src
, td
.count
)
468 /* Initialize all synchronization objects create the thread */
469 void voice_thread_init(void)
471 logf("Starting voice thread");
472 queue_init(&voice_queue
, false);
474 voice_thread_id
= create_thread(voice_thread
, voice_stack
,
475 sizeof(voice_stack
), CREATE_THREAD_FROZEN
,
476 voice_thread_name
IF_PRIO(, PRIORITY_VOICE
) IF_COP(, CPU
));
478 queue_enable_queue_send(&voice_queue
, &voice_queue_sender_list
,
480 } /* voice_thread_init */
482 /* Unfreeze the voice thread */
483 void voice_thread_resume(void)
485 logf("Thawing voice thread");
486 thread_thaw(voice_thread_id
);
489 #ifdef HAVE_PRIORITY_SCHEDULING
490 /* Set the voice thread priority */
491 void voice_thread_set_priority(int priority
)
493 if (priority
> PRIORITY_VOICE
)
494 priority
= PRIORITY_VOICE
;
496 thread_set_priority(voice_thread_id
, priority
);
500 /* Initialize voice PCM buffer and return size, allocated from the end */
501 size_t voicebuf_init(unsigned char *bufend
)
503 size_t size
= VOICE_FRAMES
* VOICE_PCM_FRAME_SIZE
;
504 cur_buf_out
= cur_buf_in
= 0;
505 voicebuf
= (uint32_t (*)[VOICE_PCM_FRAME_COUNT
])(bufend
- size
);