e9060e7e |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: ausys-win32.c,v 1.1 2002/02/02 19:16:28 mdw Exp $ |
4 | * |
5 | * Unix-specific (SDL) audio handling |
6 | * |
7 | * (c) 2002 Mark Wooding |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Jog: Programming for a jogging machine. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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. |
27 | */ |
28 | |
e9060e7e |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #ifdef HAVE_CONFIG_H |
32 | # include "config.h" |
33 | #endif |
34 | |
35 | #include <errno.h> |
36 | #include <stdio.h> |
37 | #include <string.h> |
38 | |
39 | #include <sys/types.h> |
40 | #include <sys/time.h> |
41 | #include <unistd.h> |
42 | #include <pthread.h> |
43 | |
44 | #include <windows.h> |
45 | |
46 | #include <mLib/alloc.h> |
47 | #include <mLib/bits.h> |
48 | #include <mLib/sub.h> |
49 | #include <mLib/trace.h> |
50 | |
51 | #include "au.h" |
52 | #include "ausys.h" |
53 | #include "err.h" |
54 | #include "jog.h" |
55 | |
56 | /*----- Data structures ---------------------------------------------------*/ |
57 | |
58 | /* --- Queue of samples to play --- */ |
59 | |
60 | typedef struct qnode { |
61 | struct qnode *next; /* Next item in the queue */ |
62 | au_data *a; /* Pointer to sample data */ |
63 | } qnode; |
64 | |
65 | /*----- Global variables --------------------------------------------------*/ |
66 | |
67 | const char *const ausys_suffix = "wav"; |
68 | |
69 | /*----- Static data -------------------------------------------------------*/ |
70 | |
71 | static qnode *qhead = 0, *qtail = 0; /* Queue of samples to play */ |
72 | static qnode *qfree = 0; /* Queue of samples to free */ |
73 | |
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 */ |
77 | |
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 */ |
81 | |
82 | static pthread_mutex_t mx_sub; /* Protects mLib @sub@ functions */ |
83 | |
84 | /*----- Thread structure --------------------------------------------------* |
85 | * |
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@. |
90 | * |
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. |
97 | * |
98 | * Finally, the mLib `sub' module isn't thread-safe, so it's locked |
99 | * separately by @mx_sub@. |
100 | * |
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: |
103 | * |
104 | * play > free > sub |
105 | */ |
106 | |
107 | /*----- Main code ---------------------------------------------------------*/ |
108 | |
109 | /* --- @dofree@ --- * |
110 | * |
111 | * Arguments: @void *u@ = unused pointer |
112 | * |
113 | * Returns: An unused pointer. |
114 | * |
115 | * Use: Frees unwanted sample queue nodes. |
116 | */ |
117 | |
118 | static void *play(void *u) |
119 | { |
120 | qnode *q, *qq = 0; |
121 | |
122 | for (;;) { |
123 | |
124 | /* --- If we have nothing to do locally, fetch the external queue --- */ |
125 | |
126 | if (!qq) { |
127 | pthread_mutex_lock(&mx_queue); |
128 | while (!qhead) { |
129 | T( trace(T_AUSYS, "ausys: waiting for samples to play"); ) |
130 | pthread_cond_wait(&cv_play, &mx_queue); |
131 | } |
132 | qq = qhead; |
133 | qhead = qtail = 0; |
134 | pthread_mutex_unlock(&mx_queue); |
135 | } |
136 | |
137 | /* --- Play the next sample --- */ |
138 | q = qq; |
139 | qq = q->next; |
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); |
142 | |
143 | /* --- Put it on the free list --- */ |
144 | |
145 | pthread_mutex_lock(&mx_free); |
146 | q->next = qfree; |
147 | qfree = q; |
148 | pthread_mutex_unlock(&mx_free); |
149 | pthread_cond_signal(&cv_free); |
150 | } |
151 | } |
152 | |
153 | /* --- @dofree@ --- * |
154 | * |
155 | * Arguments: @void *u@ = unused pointer |
156 | * |
157 | * Returns: An unused pointer. |
158 | * |
159 | * Use: Frees unwanted sample queue nodes. |
160 | */ |
161 | |
162 | static void *dofree(void *u) |
163 | { |
164 | qnode *q, *qq; |
165 | |
166 | pthread_mutex_lock(&mx_free); |
167 | for (;;) { |
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"); ) |
171 | |
172 | while (qfree) { |
173 | q = qfree; |
174 | qfree = 0; |
175 | pthread_mutex_lock(&mx_sub); |
176 | while (q) { |
177 | qq = q->next; |
178 | T( trace(T_AUSYS, "ausys: freeing `%s'", SYM_NAME(q->a->s)); ) |
179 | au_free_unlocked(q->a); |
180 | DESTROY(q); |
181 | q = qq; |
182 | } |
183 | pthread_mutex_unlock(&mx_sub); |
184 | } |
185 | } |
186 | } |
187 | |
188 | /* --- @ausys_init@ --- * |
189 | * |
190 | * Arguments: --- |
191 | * |
192 | * Returns: --- |
193 | * |
194 | * Use: Does any initialization required by the system-specific audio |
195 | * handler. |
196 | */ |
197 | |
198 | void ausys_init(void) |
199 | { |
200 | pthread_attr_t ta; |
201 | int e; |
202 | |
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)); |
214 | exit(EXIT_FAILURE); |
215 | } |
216 | pthread_attr_destroy(&ta); |
217 | |
218 | T( trace(T_AUSYS, "ausys: initalized ok"); ) |
219 | } |
220 | |
221 | /* --- @ausys_shutdown@ --- * |
222 | * |
223 | * Arguments: --- |
224 | * |
225 | * Returns: --- |
226 | * |
227 | * Use: Does any tidying up required. |
228 | */ |
229 | |
230 | void ausys_shutdown(void) |
231 | { |
232 | pthread_cancel(tid_free); |
233 | pthread_cancel(tid_play); |
234 | T( trace(T_AUSYS, "ausys: shut down ok"); ) |
235 | } |
236 | |
237 | /* --- @ausys_lock@, @ausys_unlock@ --- * |
238 | * |
239 | * Arguments: --- |
240 | * |
241 | * Returns: --- |
242 | * |
243 | * Use: Locks or unlocks the audio subsystem. This protects the |
244 | * audio queue from becoming corrupted during all the tedious |
245 | * asynchronous stuff. |
246 | */ |
247 | |
248 | void ausys_lock(void) |
249 | { |
250 | pthread_mutex_lock(&mx_free); |
251 | pthread_mutex_lock(&mx_sub); |
252 | T( trace(T_AUSYS, "ausys: acquired lock"); ) |
253 | } |
254 | |
255 | void ausys_unlock(void) |
256 | { |
257 | pthread_mutex_unlock(&mx_sub); |
258 | pthread_mutex_unlock(&mx_free); |
259 | T( trace(T_AUSYS, "ausys: released lock"); ) |
260 | } |
261 | |
262 | /* --- @ausys_decode@ --- * |
263 | * |
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 |
267 | * |
268 | * Returns: Pointer to a sample data structure. |
269 | * |
270 | * Use: Decodes a WAV file into something the system-specific layer |
271 | * actually wants to deal with. |
272 | */ |
273 | |
274 | au_data *ausys_decode(au_sample *s, const void *p, size_t sz) |
275 | { |
276 | au_data *a; |
277 | |
278 | pthread_mutex_lock(&mx_sub); |
279 | a = CREATE(au_data); |
280 | pthread_mutex_unlock(&mx_sub); |
281 | a->p = xmalloc(sz); |
282 | memcpy(a->p, p, sz); |
283 | a->sz = sz; |
284 | |
285 | T( trace(T_AUSYS, "ausys: decoded `%s' ok", SYM_NAME(s)); ) |
286 | return (a); |
287 | } |
288 | |
289 | /* --- @ausys_queue@ --- * |
290 | * |
291 | * Arguments: @au_data *a@ = an audio thingy to play |
292 | * |
293 | * Returns: --- |
294 | * |
295 | * Use: Queues an audio sample to be played. The sample should be |
296 | * freed (with @au_free@) when it's no longer wanted. |
297 | */ |
298 | |
299 | void ausys_queue(au_data *a) |
300 | { |
301 | qnode *q; |
302 | |
303 | pthread_mutex_lock(&mx_sub); |
304 | q = CREATE(qnode); |
305 | pthread_mutex_unlock(&mx_sub); |
306 | q->next = 0; |
307 | q->a = a; |
308 | pthread_mutex_lock(&mx_queue); |
309 | if (qtail) |
310 | qtail->next = q; |
311 | else |
312 | qhead = q; |
313 | qtail = q; |
314 | pthread_mutex_unlock(&mx_queue); |
315 | pthread_cond_signal(&cv_play); |
316 | T( trace(T_AUSYS, "ausys: queuing `%s'", SYM_NAME(a->s)); ) |
317 | } |
318 | |
319 | /* --- @ausys_free@ --- * |
320 | * |
321 | * Arguments: @au_data *a@ = an audio thingy to free |
322 | * |
323 | * Returns: --- |
324 | * |
325 | * Use: Frees a decoded audio sample. |
326 | */ |
327 | |
328 | void ausys_free(au_data *a) |
329 | { |
330 | xfree(a->p); |
331 | DESTROY(a); |
332 | T( trace(T_AUSYS, "ausys: freeing data for `%s' ok", SYM_NAME(a->s)); ) |
333 | } |
334 | |
335 | /*----- That's all, folks -------------------------------------------------*/ |