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 /*----- Revision history --------------------------------------------------*
31 * $Log: ausys-sdl.c,v $
32 * Revision 1.2 2002/02/02 22:43:17 mdw
33 * Remove bogus debugging. Reduce output buffer size for faster response.
35 * Revision 1.1 2002/02/02 19:16:28 mdw
36 * New audio subsystem.
40 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
57 #include <mLib/alloc.h>
58 #include <mLib/bits.h>
60 #include <mLib/trace.h>
67 /*----- Data structures ---------------------------------------------------*/
69 /* --- Queue of samples to play --- */
71 typedef struct qnode
{
72 struct qnode
*next
; /* Next item in the queue */
73 au_data
*a
; /* Pointer to sample data */
76 /*----- Global variables --------------------------------------------------*/
78 const char *const ausys_suffix
= "wav";
80 /*----- Static data -------------------------------------------------------*/
82 static SDL_AudioSpec auspec
; /* Driver's selected parameters */
83 static qnode
*qhead
= 0, *qtail
= 0; /* Queue of samples to play */
84 static qnode
*qfree
= 0; /* Queue of samples to free */
86 static pthread_t tid_free
; /* The sample-freeing thread */
87 static pthread_mutex_t mx_free
; /* Mutex for @qfree@ */
88 static pthread_cond_t cv_free
; /* More sample data to free */
90 static pthread_mutex_t mx_sub
; /* Protects mLib @sub@ functions */
92 /*----- Thread structure --------------------------------------------------*
94 * SDL makes a buffer-stuffing thread that @fill@ is run from. This function
95 * reads samples to play from the queue at @qhead@. Hence @qhead@ must be
96 * locked when it's modified, using @SDL_LockAudio@.
98 * In order to keep latency down when new sample data is wanted, we don't do
99 * potentially tedious things like freeing sample data blocks in the @fill@
100 * thread. Instead, there's a queue of sample blocks which need freeing, and
101 * a separate thread @dofree@ which processes the queue. The queue, @qfree@
102 * is locked by @mx_free@. When new nodes are added to @qfree@, the
103 * condition @cv_free@ is signalled.
105 * Finally, the mLib `sub' module isn't thread-safe, so it's locked
106 * separately by @mx_sub@.
108 * There's an ordering on mutexes. If you want more than one mutex at a
109 * time, you must lock the greatest first. The ordering is:
111 * SDL_Audio > free > sub
114 /*----- Main code ---------------------------------------------------------*/
118 * Arguments: @void *u@ = user data (ignored)
119 * @octet *q@ = pointer to buffer to fill in
120 * @int qsz@ = size of the buffer
124 * Use: Fills an audio buffer with precomputed stuff.
127 static void fill(void *u
, octet
*q
, int qsz
)
129 static const octet
*p
= 0; /* Current place in current sample */
130 static size_t sz
= 0; /* How far to go in current sample */
131 qnode
*qf
= 0, **qft
= &qf
; /* Samples we've finished with */
133 /* --- If @p@ is null we need to get a new sample --- */
136 T( trace(T_AUSYS
, "ausys: begin playing `%s'", SYM_NAME(qhead
->a
->s
)); )
141 /* --- Main queue-filling loop --- */
146 /* --- If the queue is empty, play silence --- */
149 memset(q
, auspec
.silence
, qsz
);
153 /* --- Fill the buffer with my sample --- */
162 /* --- If the sample is used up, throw it away --- */
169 T( trace(T_AUSYS
, "ausys: put `%s' on free list",
170 SYM_NAME(qq
->a
->s
)); )
174 T( trace(T_AUSYS
, "ausys: begin playing `%s'",
175 SYM_NAME(qhead
->a
->s
)); )
179 T( trace(T_AUSYS
, "ausys: sample queue is empty"); )
186 /* --- Finally dump freed samples, all in one go --- */
189 pthread_mutex_lock(&mx_free
);
192 pthread_mutex_unlock(&mx_free
);
193 pthread_cond_signal(&cv_free
);
197 /* --- @dofree@ --- *
199 * Arguments: @void *u@ = unused pointer
201 * Returns: An unused pointer.
203 * Use: Frees unwanted sample queue nodes.
206 static void *dofree(void *u
)
210 pthread_mutex_lock(&mx_free
);
212 T( trace(T_AUSYS
, "ausys: dofree sleeping"); )
213 pthread_cond_wait(&cv_free
, &mx_free
);
214 T( trace(T_AUSYS
, "ausys: dofree woken: work to do"); )
219 pthread_mutex_lock(&mx_sub
);
222 T( trace(T_AUSYS
, "ausys: freeing `%s'", SYM_NAME(q
->a
->s
)); )
223 au_free_unlocked(q
->a
);
227 pthread_mutex_unlock(&mx_sub
);
232 /* --- @ausys_init@ --- *
238 * Use: Does any initialization required by the system-specific audio
242 void ausys_init(void)
248 /* --- Crank up the sample-freeing thread --- */
250 if ((e
= pthread_mutex_init(&mx_free
, 0)) != 0 ||
251 (e
= pthread_mutex_init(&mx_sub
, 0)) != 0 ||
252 (e
= pthread_cond_init(&cv_free
, 0)) != 0 ||
253 (e
= pthread_attr_init(&ta
)) != 0 ||
254 (e
= pthread_attr_setdetachstate(&ta
, PTHREAD_CREATE_DETACHED
)) != 0 ||
255 (e
= pthread_create(&tid_free
, &ta
, dofree
, 0)) != 0) {
256 err_report(ERR_AUDIO
, ERRAU_INIT
, e
,
257 "couldn't create audio threads: %s", strerror(errno
));
260 pthread_attr_destroy(&ta
);
262 /* --- Crank up the SDL audio subsystem --- */
264 if (SDL_Init(SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
)) {
265 err_report(ERR_AUDIO
, ERRAU_INIT
, 0,
266 "couldn't initialize SDL audio: %s", SDL_GetError());
270 /* --- Open the audio device --- */
273 want
.format
= AUDIO_U8
;
276 want
.callback
= fill
;
279 if (SDL_OpenAudio(&want
, &auspec
)) {
280 err_report(ERR_AUDIO
, ERRAU_INIT
, 0,
281 "couldn't open audio device: %s", SDL_GetError());
285 /* --- Prepare whatever needs to be done --- */
289 T( trace(T_AUSYS
, "ausys: initalized ok"); )
292 /* --- @ausys_shutdown@ --- *
298 * Use: Does any tidying up required.
301 void ausys_shutdown(void)
303 /* --- Busy-wait until sound buffer empties --- */
306 struct timeval tv
= { 0, 100000 };
307 select(0, 0, 0, 0, &tv
);
310 /* --- Shut stuff down --- */
312 pthread_cancel(tid_free
);
316 T( trace(T_AUSYS
, "ausys: shut down ok"); )
319 /* --- @ausys_lock@, @ausys_unlock@ --- *
325 * Use: Locks or unlocks the audio subsystem. This protects the
326 * audio queue from becoming corrupted during all the tedious
327 * asynchronous stuff.
330 void ausys_lock(void)
332 pthread_mutex_lock(&mx_free
);
333 pthread_mutex_lock(&mx_sub
);
334 T( trace(T_AUSYS
, "ausys: acquired lock"); )
337 void ausys_unlock(void)
339 pthread_mutex_unlock(&mx_sub
);
340 pthread_mutex_unlock(&mx_free
);
341 T( trace(T_AUSYS
, "ausys: released lock"); )
344 /* --- @ausys_decode@ --- *
346 * Arguments: @au_sample *s@ = pointer to sample block
347 * @const void *p@ = pointer to sample file contents
348 * @size_t sz@ = size of sample file contents
350 * Returns: Pointer to a sample data structure.
352 * Use: Decodes a WAV file into something the system-specific layer
353 * actually wants to deal with.
356 au_data
*ausys_decode(au_sample
*s
, const void *p
, size_t sz
)
366 /* --- Firstly, extract the audio data from the WAV file --- */
368 rw
= SDL_RWFromMem((void *)p
, sz
);
369 if (!SDL_LoadWAV_RW(rw
, 1, &spec
, &buf
, &len
)) {
370 err_report(ERR_AUDIO
, ERRAU_OPEN
, 0,
371 "can't read WAV file for `%s': %s",
372 SYM_NAME(s
), SDL_GetError());
376 /* --- Now convert that to the driver's expected format --- */
378 if (SDL_BuildAudioCVT(&cvt
, spec
.format
, spec
.channels
, spec
.freq
,
379 auspec
.format
, auspec
.channels
, auspec
.freq
) == -1) {
380 err_report(ERR_AUDIO
, ERRAU_OPEN
, 0,
381 "can't build conversion structure for `%s': %s",
382 SYM_NAME(s
), SDL_GetError());
386 cvt
.buf
= xmalloc(len
* cvt
.len_mult
);
388 memcpy(cvt
.buf
, buf
, len
);
391 if (SDL_ConvertAudio(&cvt
)) {
392 err_report(ERR_AUDIO
, ERRAU_OPEN
, 0,
393 "can't convert `%s': %s", SYM_NAME(s
), SDL_GetError());
397 /* --- Now fiddle with buffers --- */
399 sz
= len
* cvt
.len_ratio
;
400 if (cvt
.len_ratio
>= 1)
404 memcpy(q
, cvt
.buf
, sz
);
408 /* --- Construct a new node --- */
410 pthread_mutex_lock(&mx_sub
);
412 pthread_mutex_unlock(&mx_sub
);
416 T( trace(T_AUSYS
, "ausys: decoded `%s' ok", SYM_NAME(s
)); )
419 /* --- Tidy up after errors --- */
430 /* --- @ausys_queue@ --- *
432 * Arguments: @au_data *a@ = an audio thingy to play
436 * Use: Queues an audio sample to be played. The sample should be
437 * freed (with @au_free@) when it's no longer wanted.
440 void ausys_queue(au_data
*a
)
444 pthread_mutex_lock(&mx_sub
);
446 pthread_mutex_unlock(&mx_sub
);
456 T( trace(T_AUSYS
, "ausys: queuing `%s'", SYM_NAME(a
->s
)); )
459 /* --- @ausys_free@ --- *
461 * Arguments: @au_data *a@ = an audio thingy to free
465 * Use: Frees a decoded audio sample.
468 void ausys_free(au_data
*a
)
472 T( trace(T_AUSYS
, "ausys: freeing data for `%s' ok", SYM_NAME(a
->s
)); )
475 /*----- That's all, folks -------------------------------------------------*/