3 * $Id: ausys-sdl.c,v 1.2 2002/02/02 22:43:17 mdw Exp $
5 * Unix-specific (SDL) audio handling
7 * (c) 2002 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
46 #include <mLib/alloc.h>
47 #include <mLib/bits.h>
49 #include <mLib/trace.h>
56 /*----- Data structures ---------------------------------------------------*/
58 /* --- Queue of samples to play --- */
60 typedef struct qnode {
61 struct qnode *next; /* Next item in the queue */
62 au_data *a; /* Pointer to sample data */
65 /*----- Global variables --------------------------------------------------*/
67 const char *const ausys_suffix = "wav";
69 /*----- Static data -------------------------------------------------------*/
71 static SDL_AudioSpec auspec; /* Driver's selected parameters */
72 static qnode *qhead = 0, *qtail = 0; /* Queue of samples to play */
73 static qnode *qfree = 0; /* Queue of samples to free */
75 static pthread_t tid_free; /* The sample-freeing thread */
76 static pthread_mutex_t mx_free; /* Mutex for @qfree@ */
77 static pthread_cond_t cv_free; /* More sample data to free */
79 static pthread_mutex_t mx_sub; /* Protects mLib @sub@ functions */
81 /*----- Thread structure --------------------------------------------------*
83 * SDL makes a buffer-stuffing thread that @fill@ is run from. This function
84 * reads samples to play from the queue at @qhead@. Hence @qhead@ must be
85 * locked when it's modified, using @SDL_LockAudio@.
87 * In order to keep latency down when new sample data is wanted, we don't do
88 * potentially tedious things like freeing sample data blocks in the @fill@
89 * thread. Instead, there's a queue of sample blocks which need freeing, and
90 * a separate thread @dofree@ which processes the queue. The queue, @qfree@
91 * is locked by @mx_free@. When new nodes are added to @qfree@, the
92 * condition @cv_free@ is signalled.
94 * Finally, the mLib `sub' module isn't thread-safe, so it's locked
95 * separately by @mx_sub@.
97 * There's an ordering on mutexes. If you want more than one mutex at a
98 * time, you must lock the greatest first. The ordering is:
100 * SDL_Audio > free > sub
103 /*----- Main code ---------------------------------------------------------*/
107 * Arguments: @void *u@ = user data (ignored)
108 * @octet *q@ = pointer to buffer to fill in
109 * @int qsz@ = size of the buffer
113 * Use: Fills an audio buffer with precomputed stuff.
116 static void fill(void *u, octet *q, int qsz)
118 static const octet *p = 0; /* Current place in current sample */
119 static size_t sz = 0; /* How far to go in current sample */
120 qnode *qf = 0, **qft = &qf; /* Samples we've finished with */
122 /* --- If @p@ is null we need to get a new sample --- */
125 T( trace(T_AUSYS, "ausys: begin playing `%s'", SYM_NAME(qhead->a->s)); )
130 /* --- Main queue-filling loop --- */
135 /* --- If the queue is empty, play silence --- */
138 memset(q, auspec.silence, qsz);
142 /* --- Fill the buffer with my sample --- */
151 /* --- If the sample is used up, throw it away --- */
158 T( trace(T_AUSYS, "ausys: put `%s' on free list",
159 SYM_NAME(qq->a->s)); )
163 T( trace(T_AUSYS, "ausys: begin playing `%s'",
164 SYM_NAME(qhead->a->s)); )
168 T( trace(T_AUSYS, "ausys: sample queue is empty"); )
175 /* --- Finally dump freed samples, all in one go --- */
178 pthread_mutex_lock(&mx_free);
181 pthread_mutex_unlock(&mx_free);
182 pthread_cond_signal(&cv_free);
186 /* --- @dofree@ --- *
188 * Arguments: @void *u@ = unused pointer
190 * Returns: An unused pointer.
192 * Use: Frees unwanted sample queue nodes.
195 static void *dofree(void *u)
199 pthread_mutex_lock(&mx_free);
201 T( trace(T_AUSYS, "ausys: dofree sleeping"); )
202 pthread_cond_wait(&cv_free, &mx_free);
203 T( trace(T_AUSYS, "ausys: dofree woken: work to do"); )
208 pthread_mutex_lock(&mx_sub);
211 T( trace(T_AUSYS, "ausys: freeing `%s'", SYM_NAME(q->a->s)); )
212 au_free_unlocked(q->a);
216 pthread_mutex_unlock(&mx_sub);
221 /* --- @ausys_init@ --- *
227 * Use: Does any initialization required by the system-specific audio
231 void ausys_init(void)
237 /* --- Crank up the sample-freeing thread --- */
239 if ((e = pthread_mutex_init(&mx_free, 0)) != 0 ||
240 (e = pthread_mutex_init(&mx_sub, 0)) != 0 ||
241 (e = pthread_cond_init(&cv_free, 0)) != 0 ||
242 (e = pthread_attr_init(&ta)) != 0 ||
243 (e = pthread_attr_setdetachstate(&ta, PTHREAD_CREATE_DETACHED)) != 0 ||
244 (e = pthread_create(&tid_free, &ta, dofree, 0)) != 0) {
245 err_report(ERR_AUDIO, ERRAU_INIT, e,
246 "couldn't create audio threads: %s", strerror(errno));
249 pthread_attr_destroy(&ta);
251 /* --- Crank up the SDL audio subsystem --- */
253 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE)) {
254 err_report(ERR_AUDIO, ERRAU_INIT, 0,
255 "couldn't initialize SDL audio: %s", SDL_GetError());
259 /* --- Open the audio device --- */
262 want.format = AUDIO_U8;
265 want.callback = fill;
268 if (SDL_OpenAudio(&want, &auspec)) {
269 err_report(ERR_AUDIO, ERRAU_INIT, 0,
270 "couldn't open audio device: %s", SDL_GetError());
274 /* --- Prepare whatever needs to be done --- */
278 T( trace(T_AUSYS, "ausys: initalized ok"); )
281 /* --- @ausys_shutdown@ --- *
287 * Use: Does any tidying up required.
290 void ausys_shutdown(void)
292 /* --- Busy-wait until sound buffer empties --- */
295 struct timeval tv = { 0, 100000 };
296 select(0, 0, 0, 0, &tv);
299 /* --- Shut stuff down --- */
301 pthread_cancel(tid_free);
305 T( trace(T_AUSYS, "ausys: shut down ok"); )
308 /* --- @ausys_lock@, @ausys_unlock@ --- *
314 * Use: Locks or unlocks the audio subsystem. This protects the
315 * audio queue from becoming corrupted during all the tedious
316 * asynchronous stuff.
319 void ausys_lock(void)
321 pthread_mutex_lock(&mx_free);
322 pthread_mutex_lock(&mx_sub);
323 T( trace(T_AUSYS, "ausys: acquired lock"); )
326 void ausys_unlock(void)
328 pthread_mutex_unlock(&mx_sub);
329 pthread_mutex_unlock(&mx_free);
330 T( trace(T_AUSYS, "ausys: released lock"); )
333 /* --- @ausys_decode@ --- *
335 * Arguments: @au_sample *s@ = pointer to sample block
336 * @const void *p@ = pointer to sample file contents
337 * @size_t sz@ = size of sample file contents
339 * Returns: Pointer to a sample data structure.
341 * Use: Decodes a WAV file into something the system-specific layer
342 * actually wants to deal with.
345 au_data *ausys_decode(au_sample *s, const void *p, size_t sz)
355 /* --- Firstly, extract the audio data from the WAV file --- */
357 rw = SDL_RWFromMem((void *)p, sz);
358 if (!SDL_LoadWAV_RW(rw, 1, &spec, &buf, &len)) {
359 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
360 "can't read WAV file for `%s': %s",
361 SYM_NAME(s), SDL_GetError());
365 /* --- Now convert that to the driver's expected format --- */
367 if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
368 auspec.format, auspec.channels, auspec.freq) == -1) {
369 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
370 "can't build conversion structure for `%s': %s",
371 SYM_NAME(s), SDL_GetError());
375 cvt.buf = xmalloc(len * cvt.len_mult);
377 memcpy(cvt.buf, buf, len);
380 if (SDL_ConvertAudio(&cvt)) {
381 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
382 "can't convert `%s': %s", SYM_NAME(s), SDL_GetError());
386 /* --- Now fiddle with buffers --- */
388 sz = len * cvt.len_ratio;
389 if (cvt.len_ratio >= 1)
393 memcpy(q, cvt.buf, sz);
397 /* --- Construct a new node --- */
399 pthread_mutex_lock(&mx_sub);
401 pthread_mutex_unlock(&mx_sub);
405 T( trace(T_AUSYS, "ausys: decoded `%s' ok", SYM_NAME(s)); )
408 /* --- Tidy up after errors --- */
419 /* --- @ausys_queue@ --- *
421 * Arguments: @au_data *a@ = an audio thingy to play
425 * Use: Queues an audio sample to be played. The sample should be
426 * freed (with @au_free@) when it's no longer wanted.
429 void ausys_queue(au_data *a)
433 pthread_mutex_lock(&mx_sub);
435 pthread_mutex_unlock(&mx_sub);
445 T( trace(T_AUSYS, "ausys: queuing `%s'", SYM_NAME(a->s)); )
448 /* --- @ausys_free@ --- *
450 * Arguments: @au_data *a@ = an audio thingy to free
454 * Use: Frees a decoded audio sample.
457 void ausys_free(au_data *a)
461 T( trace(T_AUSYS, "ausys: freeing data for `%s' ok", SYM_NAME(a->s)); )
464 /*----- That's all, folks -------------------------------------------------*/