3 * $Id: ausys-win32.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 /*----- 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 qnode *qhead = 0, *qtail = 0; /* Queue of samples to play */
72 static qnode *qfree = 0; /* Queue of samples to free */
74 static pthread_t tid_play; /* The sample-playing thread */
75 static pthread_mutex_t mx_queue; /* Mutex for @qhead@ */
76 static pthread_cond_t cv_play; /* More samples to play */
78 static pthread_t tid_free; /* The sample-freeing thread */
79 static pthread_mutex_t mx_free; /* Mutex for @qfree@ */
80 static pthread_cond_t cv_free; /* More sample data to free */
82 static pthread_mutex_t mx_sub; /* Protects mLib @sub@ functions */
84 /*----- Thread structure --------------------------------------------------*
86 * In order to ensure that samples don't overlap each other, we play them
87 * synchronously in a separate thread. The @play@ function reads samples to
88 * play from the queue at @qhead@. Hence @qhead@ must be locked when it's
89 * modified, using @mx_queue@.
91 * In order to keep latency down when new sample data is wanted, we don't do
92 * potentially tedious things like freeing sample data blocks in the @play@
93 * thread. Instead, there's a queue of sample blocks which need freeing, and
94 * a separate thread @dofree@ which processes the queue. The queue, @qfree@
95 * is locked by @mx_free@. When new nodes are added to @qfree@, the
96 * condition @cv_free@ is signalled.
98 * Finally, the mLib `sub' module isn't thread-safe, so it's locked
99 * separately by @mx_sub@.
101 * There's an ordering on mutexes. If you want more than one mutex at a
102 * time, you must lock the greatest first. The ordering is:
107 /*----- Main code ---------------------------------------------------------*/
109 /* --- @dofree@ --- *
111 * Arguments: @void *u@ = unused pointer
113 * Returns: An unused pointer.
115 * Use: Frees unwanted sample queue nodes.
118 static void *play(void *u)
124 /* --- If we have nothing to do locally, fetch the external queue --- */
127 pthread_mutex_lock(&mx_queue);
129 T( trace(T_AUSYS, "ausys: waiting for samples to play"); )
130 pthread_cond_wait(&cv_play, &mx_queue);
134 pthread_mutex_unlock(&mx_queue);
137 /* --- Play the next sample --- */
140 T( trace(T_AUSYS, "ausys: playing `%s'", SYM_NAME(q->a->s)); )
141 PlaySound((const void *)q->a->p, 0, SND_SYNC | SND_MEMORY);
143 /* --- Put it on the free list --- */
145 pthread_mutex_lock(&mx_free);
148 pthread_mutex_unlock(&mx_free);
149 pthread_cond_signal(&cv_free);
153 /* --- @dofree@ --- *
155 * Arguments: @void *u@ = unused pointer
157 * Returns: An unused pointer.
159 * Use: Frees unwanted sample queue nodes.
162 static void *dofree(void *u)
166 pthread_mutex_lock(&mx_free);
168 T( trace(T_AUSYS, "ausys: dofree sleeping"); )
169 pthread_cond_wait(&cv_free, &mx_free);
170 T( trace(T_AUSYS, "ausys: dofree woken: work to do"); )
175 pthread_mutex_lock(&mx_sub);
178 T( trace(T_AUSYS, "ausys: freeing `%s'", SYM_NAME(q->a->s)); )
179 au_free_unlocked(q->a);
183 pthread_mutex_unlock(&mx_sub);
188 /* --- @ausys_init@ --- *
194 * Use: Does any initialization required by the system-specific audio
198 void ausys_init(void)
203 if ((e = pthread_mutex_init(&mx_free, 0)) != 0 ||
204 (e = pthread_mutex_init(&mx_sub, 0)) != 0 ||
205 (e = pthread_mutex_init(&mx_queue, 0)) != 0 ||
206 (e = pthread_cond_init(&cv_play, 0)) != 0 ||
207 (e = pthread_cond_init(&cv_free, 0)) != 0 ||
208 (e = pthread_attr_init(&ta)) != 0 ||
209 (e = pthread_attr_setdetachstate(&ta, PTHREAD_CREATE_DETACHED)) != 0 ||
210 (e = pthread_create(&tid_free, &ta, play, 0)) != 0 ||
211 (e = pthread_create(&tid_free, &ta, dofree, 0)) != 0) {
212 err_report(ERR_AUDIO, ERRAU_INIT, e,
213 "couldn't create audio threads: %s", strerror(errno));
216 pthread_attr_destroy(&ta);
218 T( trace(T_AUSYS, "ausys: initalized ok"); )
221 /* --- @ausys_shutdown@ --- *
227 * Use: Does any tidying up required.
230 void ausys_shutdown(void)
232 pthread_cancel(tid_free);
233 pthread_cancel(tid_play);
234 T( trace(T_AUSYS, "ausys: shut down ok"); )
237 /* --- @ausys_lock@, @ausys_unlock@ --- *
243 * Use: Locks or unlocks the audio subsystem. This protects the
244 * audio queue from becoming corrupted during all the tedious
245 * asynchronous stuff.
248 void ausys_lock(void)
250 pthread_mutex_lock(&mx_free);
251 pthread_mutex_lock(&mx_sub);
252 T( trace(T_AUSYS, "ausys: acquired lock"); )
255 void ausys_unlock(void)
257 pthread_mutex_unlock(&mx_sub);
258 pthread_mutex_unlock(&mx_free);
259 T( trace(T_AUSYS, "ausys: released lock"); )
262 /* --- @ausys_decode@ --- *
264 * Arguments: @au_sample *s@ = pointer to sample block
265 * @const void *p@ = pointer to sample file contents
266 * @size_t sz@ = size of sample file contents
268 * Returns: Pointer to a sample data structure.
270 * Use: Decodes a WAV file into something the system-specific layer
271 * actually wants to deal with.
274 au_data *ausys_decode(au_sample *s, const void *p, size_t sz)
278 pthread_mutex_lock(&mx_sub);
280 pthread_mutex_unlock(&mx_sub);
285 T( trace(T_AUSYS, "ausys: decoded `%s' ok", SYM_NAME(s)); )
289 /* --- @ausys_queue@ --- *
291 * Arguments: @au_data *a@ = an audio thingy to play
295 * Use: Queues an audio sample to be played. The sample should be
296 * freed (with @au_free@) when it's no longer wanted.
299 void ausys_queue(au_data *a)
303 pthread_mutex_lock(&mx_sub);
305 pthread_mutex_unlock(&mx_sub);
308 pthread_mutex_lock(&mx_queue);
314 pthread_mutex_unlock(&mx_queue);
315 pthread_cond_signal(&cv_play);
316 T( trace(T_AUSYS, "ausys: queuing `%s'", SYM_NAME(a->s)); )
319 /* --- @ausys_free@ --- *
321 * Arguments: @au_data *a@ = an audio thingy to free
325 * Use: Frees a decoded audio sample.
328 void ausys_free(au_data *a)
332 T( trace(T_AUSYS, "ausys: freeing data for `%s' ok", SYM_NAME(a->s)); )
335 /*----- That's all, folks -------------------------------------------------*/