3 * $Id: ausys-sdl.c,v 1.1 2002/02/02 19:16:28 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 /*----- Revision history --------------------------------------------------*
31 * $Log: ausys-sdl.c,v $
32 * Revision 1.1 2002/02/02 19:16:28 mdw
33 * New audio subsystem.
37 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
54 #include <mLib/alloc.h>
55 #include <mLib/bits.h>
57 #include <mLib/trace.h>
64 /*----- Data structures ---------------------------------------------------*/
66 /* --- Queue of samples to play --- */
68 typedef struct qnode {
69 struct qnode *next; /* Next item in the queue */
70 au_data *a; /* Pointer to sample data */
73 /*----- Global variables --------------------------------------------------*/
75 const char *const ausys_suffix = "wav";
77 /*----- Static data -------------------------------------------------------*/
79 static SDL_AudioSpec auspec; /* Driver's selected parameters */
80 static qnode *qhead = 0, *qtail = 0; /* Queue of samples to play */
81 static qnode *qfree = 0; /* Queue of samples to free */
83 static pthread_t tid_free; /* The sample-freeing thread */
84 static pthread_mutex_t mx_free; /* Mutex for @qfree@ */
85 static pthread_cond_t cv_free; /* More sample data to free */
87 static pthread_mutex_t mx_sub; /* Protects mLib @sub@ functions */
89 /*----- Thread structure --------------------------------------------------*
91 * SDL makes a buffer-stuffing thread that @fill@ is run from. This function
92 * reads samples to play from the queue at @qhead@. Hence @qhead@ must be
93 * locked when it's modified, using @SDL_LockAudio@.
95 * In order to keep latency down when new sample data is wanted, we don't do
96 * potentially tedious things like freeing sample data blocks in the @fill@
97 * thread. Instead, there's a queue of sample blocks which need freeing, and
98 * a separate thread @dofree@ which processes the queue. The queue, @qfree@
99 * is locked by @mx_free@. When new nodes are added to @qfree@, the
100 * condition @cv_free@ is signalled.
102 * Finally, the mLib `sub' module isn't thread-safe, so it's locked
103 * separately by @mx_sub@.
105 * There's an ordering on mutexes. If you want more than one mutex at a
106 * time, you must lock the greatest first. The ordering is:
108 * SDL_Audio > free > sub
111 /*----- Main code ---------------------------------------------------------*/
115 * Arguments: @void *u@ = user data (ignored)
116 * @octet *q@ = pointer to buffer to fill in
117 * @int qsz@ = size of the buffer
121 * Use: Fills an audio buffer with precomputed stuff.
124 static void fill(void *u, octet *q, int qsz)
126 static const octet *p = 0; /* Current place in current sample */
127 static size_t sz = 0; /* How far to go in current sample */
128 qnode *qf = 0, **qft = &qf; /* Samples we've finished with */
130 /* --- If @p@ is null we need to get a new sample --- */
133 T( trace(T_AUSYS, "ausys: begin playing `%s'", SYM_NAME(qhead->a->s)); )
138 /* --- Main queue-filling loop --- */
143 /* --- If the queue is empty, play silence --- */
146 memset(q, auspec.silence, qsz);
150 /* --- Fill the buffer with my sample --- */
159 /* --- If the sample is used up, throw it away --- */
166 T( trace(T_AUSYS, "ausys: put `%s' on free list",
167 SYM_NAME(qq->a->s)); )
171 T( trace(T_AUSYS, "ausys: begin playing `%s'",
172 SYM_NAME(qhead->a->s)); )
176 T( trace(T_AUSYS, "ausys: sample queue is empty"); )
183 /* --- Finally dump freed samples, all in one go --- */
186 pthread_mutex_lock(&mx_free);
189 pthread_mutex_unlock(&mx_free);
190 pthread_cond_signal(&cv_free);
194 /* --- @dofree@ --- *
196 * Arguments: @void *u@ = unused pointer
198 * Returns: An unused pointer.
200 * Use: Frees unwanted sample queue nodes.
203 static void *dofree(void *u)
207 pthread_mutex_lock(&mx_free);
209 T( trace(T_AUSYS, "ausys: dofree sleeping"); )
210 pthread_cond_wait(&cv_free, &mx_free);
211 T( trace(T_AUSYS, "ausys: dofree woken: work to do"); )
216 pthread_mutex_lock(&mx_sub);
219 T( trace(T_AUSYS, "ausys: freeing `%s'", SYM_NAME(q->a->s)); )
220 au_free_unlocked(q->a);
224 pthread_mutex_unlock(&mx_sub);
229 /* --- @ausys_init@ --- *
235 * Use: Does any initialization required by the system-specific audio
239 void ausys_init(void)
245 /* --- Crank up the sample-freeing thread --- */
247 if ((e = pthread_mutex_init(&mx_free, 0)) != 0 ||
248 (e = pthread_mutex_init(&mx_sub, 0)) != 0 ||
249 (e = pthread_cond_init(&cv_free, 0)) != 0 ||
250 (e = pthread_attr_init(&ta)) != 0 ||
251 (e = pthread_attr_setdetachstate(&ta, PTHREAD_CREATE_DETACHED)) != 0 ||
252 (e = pthread_create(&tid_free, &ta, dofree, 0)) != 0) {
253 err_report(ERR_AUDIO, ERRAU_INIT, e,
254 "couldn't create audio threads: %s", strerror(errno));
257 pthread_attr_destroy(&ta);
259 /* --- Crank up the SDL audio subsystem --- */
261 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE)) {
262 err_report(ERR_AUDIO, ERRAU_INIT, 0,
263 "couldn't initialize SDL audio: %s", SDL_GetError());
267 /* --- Open the audio device --- */
270 want.format = AUDIO_U8;
273 want.callback = fill;
276 if (SDL_OpenAudio(&want, &auspec)) {
277 err_report(ERR_AUDIO, ERRAU_INIT, 0,
278 "couldn't open audio device: %s", SDL_GetError());
282 /* --- Prepare whatever needs to be done --- */
286 T( trace(T_AUSYS, "ausys: initalized ok"); )
289 /* --- @ausys_shutdown@ --- *
295 * Use: Does any tidying up required.
298 void ausys_shutdown(void)
300 /* --- Busy-wait until sound buffer empties --- */
303 struct timeval tv = { 0, 100000 };
304 select(0, 0, 0, 0, &tv);
307 /* --- Shut stuff down --- */
309 pthread_cancel(tid_free);
313 T( trace(T_AUSYS, "ausys: shut down ok"); )
316 /* --- @ausys_lock@, @ausys_unlock@ --- *
322 * Use: Locks or unlocks the audio subsystem. This protects the
323 * audio queue from becoming corrupted during all the tedious
324 * asynchronous stuff.
327 void ausys_lock(void)
329 pthread_mutex_lock(&mx_free);
330 pthread_mutex_lock(&mx_sub);
331 T( trace(T_AUSYS, "ausys: acquired lock"); )
334 void ausys_unlock(void)
336 pthread_mutex_unlock(&mx_sub);
337 pthread_mutex_unlock(&mx_free);
338 T( trace(T_AUSYS, "ausys: released lock"); )
341 /* --- @ausys_decode@ --- *
343 * Arguments: @au_sample *s@ = pointer to sample block
344 * @const void *p@ = pointer to sample file contents
345 * @size_t sz@ = size of sample file contents
347 * Returns: Pointer to a sample data structure.
349 * Use: Decodes a WAV file into something the system-specific layer
350 * actually wants to deal with.
353 au_data *ausys_decode(au_sample *s, const void *p, size_t sz)
363 /* --- Firstly, extract the audio data from the WAV file --- */
365 rw = SDL_RWFromMem((void *)p, sz);
366 if (!SDL_LoadWAV_RW(rw, 1, &spec, &buf, &len)) {
367 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
368 "can't read WAV file for `%s': %s",
369 SYM_NAME(s), SDL_GetError());
373 /* --- Now convert that to the driver's expected format --- */
375 if (SDL_BuildAudioCVT(&cvt, spec.format, spec.channels, spec.freq,
376 auspec.format, auspec.channels, auspec.freq) == -1) {
377 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
378 "can't build conversion structure for `%s': %s",
379 SYM_NAME(s), SDL_GetError());
383 cvt.buf = xmalloc(len * cvt.len_mult);
385 memcpy(cvt.buf, buf, len);
388 if (SDL_ConvertAudio(&cvt)) {
389 err_report(ERR_AUDIO, ERRAU_OPEN, 0,
390 "can't convert `%s': %s", SYM_NAME(s), SDL_GetError());
394 /* --- Now fiddle with buffers --- */
396 sz = len * cvt.len_ratio;
397 if (cvt.len_ratio >= 1)
401 memcpy(q, cvt.buf, sz);
405 /* --- Construct a new node --- */
407 pthread_mutex_lock(&mx_sub);
409 pthread_mutex_unlock(&mx_sub);
413 T( trace(T_AUSYS, "ausys: decoded `%s' ok", SYM_NAME(s)); )
416 /* --- Tidy up after errors --- */
427 /* --- @ausys_queue@ --- *
429 * Arguments: @au_data *a@ = an audio thingy to play
433 * Use: Queues an audio sample to be played. The sample should be
434 * freed (with @au_free@) when it's no longer wanted.
437 void ausys_queue(au_data *a)
441 pthread_mutex_lock(&mx_sub);
443 pthread_mutex_unlock(&mx_sub);
453 T( trace(T_AUSYS, "ausys: queuing `%s'", SYM_NAME(a->s)); )
456 /* --- @ausys_free@ --- *
458 * Arguments: @au_data *a@ = an audio thingy to free
462 * Use: Frees a decoded audio sample.
465 void ausys_free(au_data *a)
469 T( trace(T_AUSYS, "ausys: freeing data for `%s' ok", SYM_NAME(a->s)); )
472 /*----- That's all, folks -------------------------------------------------*/