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 int quiet_counter SHAREDDATA_ATTR
= 0;
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 /* Voice processing states */
102 VOICE_STATE_MESSAGE
= 0,
104 VOICE_STATE_BUFFER_INSERT
,
107 /* A delay to not bring audio back to normal level too soon */
108 #define QUIET_COUNT 3
110 enum voice_thread_messages
112 Q_VOICE_PLAY
= 0, /* 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 struct queue_event ev
; /* Last queue event pulled from queue */
129 void *st
; /* Decoder instance */
130 SpeexBits bits
; /* Bit cursor */
131 struct dsp_config
*dsp
; /* DSP used for voice output */
132 struct voice_info vi
; /* Copy of clip data */
133 const char *src
[2]; /* Current output buffer pointers */
134 int lookahead
; /* Number of samples to drop at start of clip */
135 int count
; /* Count of samples remaining to send to PCM */
138 /* Functions called in their repective state that return the next state to
139 state machine loop - compiler may inline them at its discretion */
140 static enum voice_state
voice_message(struct voice_thread_data
*td
);
141 static enum voice_state
voice_decode(struct voice_thread_data
*td
);
142 static enum voice_state
voice_buffer_insert(struct voice_thread_data
*td
);
144 /* Number of frames in queue */
145 static inline int voice_unplayed_frames(void)
147 return cur_buf_in
- cur_buf_out
;
150 /* Mixer channel callback */
151 static void voice_pcm_callback(unsigned char **start
, size_t *size
)
153 if (voice_unplayed_frames() == 0)
156 unsigned int i
= ++cur_buf_out
% VOICE_FRAMES
;
158 *start
= (unsigned char *)voicebuf
[i
];
159 *size
= voicebuf_sizes
[i
];
162 /* Start playback of voice channel if not already playing */
163 static void voice_start_playback(void)
165 if (mixer_channel_status(PCM_MIXER_CHAN_VOICE
) != CHANNEL_STOPPED
)
168 unsigned int i
= cur_buf_out
% VOICE_FRAMES
;
169 mixer_channel_play_data(PCM_MIXER_CHAN_VOICE
, voice_pcm_callback
,
170 (unsigned char *)voicebuf
[i
], voicebuf_sizes
[i
]);
173 /* Stop the voice channel */
174 static void voice_stop_playback(void)
176 mixer_channel_stop(PCM_MIXER_CHAN_VOICE
);
177 cur_buf_in
= cur_buf_out
= 0;
180 /* Grab a free PCM frame */
181 static uint32_t * voice_buf_get(void)
183 if (voice_unplayed_frames() >= VOICE_FRAMES
)
186 voice_start_playback();
190 return voicebuf
[cur_buf_in
% VOICE_FRAMES
];
193 /* Commit a frame returned by voice_buf_get and set the actual size */
194 static void voice_buf_commit(size_t size
)
196 voicebuf_sizes
[cur_buf_in
++ % VOICE_FRAMES
] = size
;
199 /* Stop any current clip and start playing a new one */
200 void mp3_play_data(const unsigned char* start
, int size
,
201 pcm_play_callback_type get_more
)
203 if (get_more
!= NULL
&& start
!= NULL
&& (ssize_t
)size
> 0)
205 struct voice_info voice_clip
=
207 .get_more
= get_more
,
208 .start
= (unsigned char *)start
,
212 LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
213 queue_send(&voice_queue
, Q_VOICE_PLAY
, (intptr_t)&voice_clip
);
217 /* Stop current voice clip from playing */
218 void mp3_play_stop(void)
220 LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
221 queue_send(&voice_queue
, Q_VOICE_STOP
, 0);
224 void mp3_play_pause(bool play
)
230 /* Tell if voice is still in a playing state */
231 bool mp3_is_playing(void)
233 return quiet_counter
!= 0;
236 /* This function is meant to be used by the buffer request functions to
237 ensure the codec is no longer active */
238 void voice_stop(void)
240 /* Unqueue all future clips */
244 /* Wait for voice to finish speaking. */
245 void voice_wait(void)
247 /* NOTE: One problem here is that we can't tell if another thread started a
248 * new clip by the time we wait. This should be resolvable if conditions
249 * ever require knowing the very clip you requested has finished. */
251 while (quiet_counter
!= 0)
255 /* Initialize voice thread data that must be valid upon starting and the
256 * setup the DSP parameters */
257 static void voice_data_init(struct voice_thread_data
*td
)
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
);
270 /* Voice thread message processing */
271 static enum voice_state
voice_message(struct voice_thread_data
*td
)
273 if (quiet_counter
> 0)
274 queue_wait_w_tmo(&voice_queue
, &td
->ev
, HZ
/10);
276 queue_wait(&voice_queue
, &td
->ev
);
281 LOGFQUEUE("voice < Q_VOICE_PLAY");
282 if (quiet_counter
== 0)
289 /* Stop any clip still playing */
290 voice_stop_playback();
293 quiet_counter
= QUIET_COUNT
;
295 /* Copy the clip info */
296 td
->vi
= *(struct voice_info
*)td
->ev
.data
;
298 /* Be sure audio buffer is initialized */
299 audio_restore_playback(AUDIO_WANT_VOICE
);
301 /* We need nothing more from the sending thread - let it run */
302 queue_reply(&voice_queue
, 1);
304 /* Make audio play more softly and set delay to return to normal
306 pcmbuf_soft_mode(true);
308 /* Clean-start the decoder */
309 td
->st
= speex_decoder_init(&speex_wb_mode
);
311 /* Make bit buffer use our own buffer */
312 speex_bits_set_bit_buffer(&td
->bits
, td
->vi
.start
, td
->vi
.size
);
313 speex_decoder_ctl(td
->st
, SPEEX_GET_LOOKAHEAD
, &td
->lookahead
);
315 return VOICE_STATE_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 (quiet_counter
-- != QUIET_COUNT
)
327 if (quiet_counter
<= 0)
328 pcmbuf_soft_mode(false);
335 LOGFQUEUE("voice < Q_VOICE_STOP");
337 voice_stop_playback();
340 /* No default: no other message ids are sent */
343 return VOICE_STATE_MESSAGE
;
346 /* Decode frames or stop if all have completed */
347 static enum voice_state
voice_decode(struct voice_thread_data
*td
)
349 if (!queue_empty(&voice_queue
))
350 return VOICE_STATE_MESSAGE
;
352 /* Decode the data */
353 if (speex_decode_int(td
->st
, &td
->bits
, voice_output_buf
) < 0)
355 /* End of stream or error - get next clip */
358 if (td
->vi
.get_more
!= NULL
)
359 td
->vi
.get_more(&td
->vi
.start
, &td
->vi
.size
);
361 if (td
->vi
.start
!= NULL
&& (ssize_t
)td
->vi
.size
> 0)
363 /* Make bit buffer use our own buffer */
364 speex_bits_set_bit_buffer(&td
->bits
, td
->vi
.start
, td
->vi
.size
);
365 /* Don't skip any samples when we're stringing clips together */
370 /* If all clips are done and not playing, force pcm playback. */
371 voice_start_playback();
372 return VOICE_STATE_MESSAGE
;
379 /* Output the decoded frame */
380 td
->count
= VOICE_FRAME_SIZE
- td
->lookahead
;
381 td
->src
[0] = (const char *)&voice_output_buf
[td
->lookahead
];
383 td
->lookahead
-= MIN(VOICE_FRAME_SIZE
, td
->lookahead
);
386 return VOICE_STATE_BUFFER_INSERT
;
389 return VOICE_STATE_DECODE
;
392 /* Process the PCM samples in the DSP and send out for mixing */
393 static enum voice_state
voice_buffer_insert(struct voice_thread_data
*td
)
395 if (!queue_empty(&voice_queue
))
396 return VOICE_STATE_MESSAGE
;
398 char *dest
= (char *)voice_buf_get();
402 voice_buf_commit(dsp_process(td
->dsp
, dest
, td
->src
, td
->count
)
404 return VOICE_STATE_DECODE
;
408 return VOICE_STATE_BUFFER_INSERT
;
411 /* Voice thread entrypoint */
412 static void NORETURN_ATTR
voice_thread(void)
414 struct voice_thread_data td
;
415 enum voice_state state
= VOICE_STATE_MESSAGE
;
417 voice_data_init(&td
);
423 case VOICE_STATE_MESSAGE
:
424 state
= voice_message(&td
);
426 case VOICE_STATE_DECODE
:
427 state
= voice_decode(&td
);
429 case VOICE_STATE_BUFFER_INSERT
:
430 state
= voice_buffer_insert(&td
);
436 /* Initialize all synchronization objects create the thread */
437 void voice_thread_init(void)
439 logf("Starting voice thread");
440 queue_init(&voice_queue
, false);
442 voice_thread_id
= create_thread(voice_thread
, voice_stack
,
443 sizeof(voice_stack
), 0, voice_thread_name
444 IF_PRIO(, PRIORITY_VOICE
) IF_COP(, CPU
));
446 queue_enable_queue_send(&voice_queue
, &voice_queue_sender_list
,
450 #ifdef HAVE_PRIORITY_SCHEDULING
451 /* Set the voice thread priority */
452 void voice_thread_set_priority(int priority
)
454 if (priority
> PRIORITY_VOICE
)
455 priority
= PRIORITY_VOICE
;
457 thread_set_priority(voice_thread_id
, priority
);
461 /* Initialize voice PCM buffer and return size, allocated from the end */
462 size_t voicebuf_init(unsigned char *bufend
)
464 size_t size
= VOICE_FRAMES
* VOICE_PCM_FRAME_SIZE
;
465 cur_buf_out
= cur_buf_in
= 0;
466 voicebuf
= (uint32_t (*)[VOICE_PCM_FRAME_COUNT
])(bufend
- size
);