5c90186df89fdb2849dcb23bf1a3844c04c119ba
3 * $Id: au.c,v 1.2 2002/02/02 22:43:50 mdw Exp $
5 * High-level audio subsystem
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 --------------------------------------------------*
32 * Revision 1.2 2002/02/02 22:43:50 mdw
33 * Provide a decent interface for finding out about the audio cache and
34 * configuring its capacity.
36 * Revision 1.1 2002/02/02 19:16:28 mdw
37 * New audio subsystem.
41 /*----- Header files ------------------------------------------------------*/
53 #include <sys/types.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
62 #include <mLib/trace.h>
69 /*----- Static variables --------------------------------------------------*/
71 static unsigned au_flags
= 0;
72 static sym_table au_tab
; /* Sample cache, by name */
73 static const char *au_dir
= AUDIODIR
; /* Directory containing samples */
74 static struct { au_data
*next
, *prev
; } au_spare
; /* Lists for sample data */
75 static au_cacheinfo cache
; /* Cache usage information */
77 #define AU_SPARE ((au_data*)&au_spare)
80 /*----- Utility functions -------------------------------------------------*/
82 /* --- @filename@ --- *
84 * Arguments: @const char *tag@ = sample tag string
86 * Returns: A pointer to a filename for the sample.
88 * Use: Converts tag strings to filenames.
91 static const char *filename(const char *tag
)
93 static dstr d
= DSTR_INIT
;
96 dstr_putf(&d
, "%s/%s.%s", au_dir
, tag
, ausys_suffix
);
97 T( trace(T_AU
, "au: map tag `%s' -> `%s'", tag
, d
.buf
); )
107 * Use: Prunes the cache of old sample data.
110 static void prune(void)
112 T( trace(T_AU
, "au: pruning cache (%lu/%lu)",
113 (unsigned long)cache
.sz_total
, (unsigned long)cache
.sz_max
); )
114 while (cache
.sz_total
> cache
.sz_max
&& AU_SPARE
->next
!= AU_SPARE
) {
115 au_data
*a
= AU_SPARE
->next
;
118 AU_SPARE
->next
= a
->next
;
119 a
->next
->prev
= AU_SPARE
;
121 cache
.sz_spare
-= a
->sz
;
122 cache
.sz_total
-= a
->sz
;
126 T( trace(T_AU
, "au: ... discarded `%s' (%lu/%lu)", SYM_NAME(s
),
127 (unsigned long)cache
.sz_total
, (unsigned long)cache
.sz_max
); )
131 /*----- Main code ---------------------------------------------------------*/
133 /* --- @au_init@ --- *
135 * Arguments: @const char *dir@ = samples directory, or null
136 * @size_t max@ = maximum cache size
140 * Use: Initializes the audio subsystem.
143 void au_init(const char *dir
, size_t max
)
145 if (au_flags
& f_init
)
148 /* --- Set up the sound directory --- */
151 dir
= getenv("JOG_AUDIR");
155 /* --- Initialize the sample cache --- */
158 AU_SPARE
->next
= AU_SPARE
->prev
= AU_SPARE
;
161 /* --- Initialize the system-specific subsystem --- */
164 T( trace(T_AU
, "au: initialized ok (dir = `%s')", au_dir
); )
171 /* --- @au_shutdown@ --- *
177 * Use: Shuts down the audio subsystem.
180 void au_shutdown(void)
182 if (!(au_flags
& f_init
))
185 T( trace(T_AU
, "au: shutdown ok"); )
188 /* --- @au_getcacheinfo@ --- *
190 * Arguments: @au_cacheinfo *c@ = where to put the information
194 * Use: Extracts audio cache information.
197 void au_getcacheinfo(au_cacheinfo
*c
)
202 assert(c
->sz_spare
+ c
->sz_queue
== c
->sz_total
);
203 assert(c
->n_spare
+ c
->n_queue
== c
->n_total
);
206 /* --- @au_setcachelimit@ --- *
208 * Arguments: @size_t max@ = new cache limit
212 * Use: Reconfigures the maximum cache size. This probably isn't
213 * very useful, but it was easy...
216 void au_setcachelimit(size_t max
)
224 /* --- @au_find@ --- *
226 * Arguments: @const char *tag@ = sample tag string
228 * Returns: A pointer to the sample corresponding to the tag, or null if
229 * the sample wasn't found.
231 * Use: Looks up a sample by its name.
234 au_sample
*au_find(const char *tag
)
237 au_sample
*s
= sym_find(&au_tab
, tag
, -1, sizeof(*s
), &f
);
244 if (stat(filename(tag
), &st
))
247 if (s
->f
& AUF_NOMATCH
) {
248 T( trace(T_AU
, "au: sample `%s' not found%s", tag
, f ?
" (hit)": ""); )
251 T( trace(T_AU
, "au: sample `%s' found%s", tag
, f ?
" (hit)" : ""); )
255 /* --- @au_fetch@ --- *
257 * Arguments: @au_sample *s@ = sample pointer
259 * Returns: A pointer to the audio data for the sample.
261 * Use: Fetches a sample, and decodes it, if necessary. The decoded
262 * sample data is left with an outstanding reference to it, so
263 * it won't be freed. This is used internally by @au_queue@,
264 * before passing the fetched sample data to the system-specific
265 * layer for playback. It can also be used to lock sample data
266 * in memory (e.g., for the `abort' error message), or (by
267 * freeing it immediately afterwards) to prefetch a sample which
268 * will be used soon, to reduce the latency before the sample is
272 au_data
*au_fetch(au_sample
*s
)
282 /* --- If we already have the sample data --- *
284 * If it's currently languishing in the spare bin, rescue it. If this
285 * doesn't work, we can release the audio lock, because nothing else messes
286 * with the spare list.
294 a
->prev
->next
= a
->next
;
295 a
->next
->prev
= a
->prev
;
296 a
->next
= a
->prev
= 0;
297 cache
.sz_spare
-= a
->sz
;
298 cache
.sz_queue
+= a
->sz
;
304 T( trace(T_AU
, "au: reusing sample `%s'", SYM_NAME(s
)); )
310 /* --- Read the file --- *
312 * Buffered I/O will just involve more copying.
315 T( trace(T_AU
, "au: fetching sample `%s'", SYM_NAME(s
)); )
316 fn
= filename(SYM_NAME(s
));
317 if ((fd
= open(fn
, O_RDONLY
)) < 0) {
318 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
319 "couldn't open sample `%s': %s", fn
, strerror(errno
));
322 if (fstat(fd
, &st
)) {
323 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
324 "couldn't fstat `%s': %s", fn
, strerror(errno
));
333 if ((r
= read(fd
, d
.buf
+ d
.len
, n
)) < 0) {
334 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
|| errno
== EINTR
)
336 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
337 "couldn't read `%s': %s", fn
, strerror(errno
));
349 /* --- Convert it into internal form --- */
351 if ((a
= ausys_decode(s
, d
.buf
, d
.len
)) == 0)
354 a
->next
= a
->prev
= 0;
361 cache
.sz_queue
+= a
->sz
;
362 cache
.sz_total
+= a
->sz
;
373 /* --- Tidy up after botched file I/O --- */
382 /* --- @au_queue@ --- *
384 * Arguments: @au_sample *s@ = sample pointer
386 * Returns: Zero on success, nonzero on failure.
388 * Use: Queues a sample to be played.
391 int au_queue(au_sample
*s
)
396 if ((a
= au_fetch(s
)) == 0)
398 T( trace(T_AU
, "au: queuing sample `%s'", SYM_NAME(s
)); )
403 /* --- @au_free@, @au_free_unlocked@ --- *
405 * Arguments: @au_data *a@ = pointer to audio data block
409 * Use: Frees a sample data block when it's no longer required.
412 void au_free(au_data
*a
)
419 void au_free_unlocked(au_data
*a
)
421 /* --- If the sample is unreferenced, throw it in the spare bin --- *
423 * This can be called from a background audio processing thread, so we need
424 * to acquire the lock.
432 T( trace(T_AU
, "au: drop ref to `%s' (other refs remain)",
437 a
->prev
= AU_SPARE
->prev
;
438 AU_SPARE
->prev
->next
= a
;
440 cache
.sz_queue
-= a
->sz
;
441 cache
.sz_spare
+= a
->sz
;
444 T( trace(T_AU
, "au: drop last ref to `%s'", SYM_NAME(a
->s
)); )
449 /* --- @au_play@, @au_tryplay@ --- *
451 * Arguments: @const char *tag@ = sample tag string
453 * Returns: Zero on success, nonzero on failure.
455 * Use: Convenience functions for queueing samples by tag.
456 * If @au_tryplay@ cannot find the requested sample, it returns
457 * a zero value; if @au_play@ cannot find the sample, it reports
461 int au_tryplay(const char *tag
)
465 if (!(au_flags
& f_init
))
467 if ((s
= au_find(tag
)) == 0 || au_queue(s
))
472 int au_play(const char *tag
)
476 if ((rc
= au_tryplay(tag
)) != 0)
477 err_report(ERR_AUDIO
, ERRAU_NOTFOUND
, 0, "sample `%s' not found", tag
);
481 /*----- That's all, folks -------------------------------------------------*/