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 /*----- Header files ------------------------------------------------------*/
41 #include <sys/types.h>
46 #include <mLib/alloc.h>
47 #include <mLib/dstr.h>
50 #include <mLib/trace.h>
57 /*----- Static variables --------------------------------------------------*/
59 static unsigned au_flags = 0;
60 static sym_table au_tab; /* Sample cache, by name */
61 static const char *au_dir = AUDIODIR; /* Directory containing samples */
62 static struct { au_data *next, *prev; } au_spare; /* Lists for sample data */
63 static au_cacheinfo cache; /* Cache usage information */
65 #define AU_SPARE ((au_data*)&au_spare)
68 /*----- Utility functions -------------------------------------------------*/
70 /* --- @filename@ --- *
72 * Arguments: @const char *tag@ = sample tag string
74 * Returns: A pointer to a filename for the sample.
76 * Use: Converts tag strings to filenames.
79 static const char *filename(const char *tag)
81 static dstr d = DSTR_INIT;
84 dstr_putf(&d, "%s/%s.%s", au_dir, tag, ausys_suffix);
85 T( trace(T_AU, "au: map tag `%s' -> `%s'", tag, d.buf); )
95 * Use: Prunes the cache of old sample data.
98 static void prune(void)
100 T( trace(T_AU, "au: pruning cache (%lu/%lu)",
101 (unsigned long)cache.sz_total, (unsigned long)cache.sz_max); )
102 while (cache.sz_total > cache.sz_max && AU_SPARE->next != AU_SPARE) {
103 au_data *a = AU_SPARE->next;
106 AU_SPARE->next = a->next;
107 a->next->prev = AU_SPARE;
109 cache.sz_spare -= a->sz;
110 cache.sz_total -= a->sz;
114 T( trace(T_AU, "au: ... discarded `%s' (%lu/%lu)", SYM_NAME(s),
115 (unsigned long)cache.sz_total, (unsigned long)cache.sz_max); )
119 /*----- Main code ---------------------------------------------------------*/
121 /* --- @au_init@ --- *
123 * Arguments: @const char *dir@ = samples directory, or null
124 * @size_t max@ = maximum cache size
128 * Use: Initializes the audio subsystem.
131 void au_init(const char *dir, size_t max)
133 if (au_flags & f_init)
136 /* --- Set up the sound directory --- */
139 dir = getenv("JOG_AUDIR");
143 /* --- Initialize the sample cache --- */
146 AU_SPARE->next = AU_SPARE->prev = AU_SPARE;
149 /* --- Initialize the system-specific subsystem --- */
152 T( trace(T_AU, "au: initialized ok (dir = `%s')", au_dir); )
159 /* --- @au_shutdown@ --- *
165 * Use: Shuts down the audio subsystem.
168 void au_shutdown(void)
170 if (!(au_flags & f_init))
173 T( trace(T_AU, "au: shutdown ok"); )
176 /* --- @au_getcacheinfo@ --- *
178 * Arguments: @au_cacheinfo *c@ = where to put the information
182 * Use: Extracts audio cache information.
185 void au_getcacheinfo(au_cacheinfo *c)
190 assert(c->sz_spare + c->sz_queue == c->sz_total);
191 assert(c->n_spare + c->n_queue == c->n_total);
194 /* --- @au_setcachelimit@ --- *
196 * Arguments: @size_t max@ = new cache limit
200 * Use: Reconfigures the maximum cache size. This probably isn't
201 * very useful, but it was easy...
204 void au_setcachelimit(size_t max)
212 /* --- @au_find@ --- *
214 * Arguments: @const char *tag@ = sample tag string
216 * Returns: A pointer to the sample corresponding to the tag, or null if
217 * the sample wasn't found.
219 * Use: Looks up a sample by its name.
222 au_sample *au_find(const char *tag)
225 au_sample *s = sym_find(&au_tab, tag, -1, sizeof(*s), &f);
232 if (stat(filename(tag), &st))
235 if (s->f & AUF_NOMATCH) {
236 T( trace(T_AU, "au: sample `%s' not found%s", tag, f ? " (hit)": ""); )
239 T( trace(T_AU, "au: sample `%s' found%s", tag, f ? " (hit)" : ""); )
243 /* --- @au_fetch@ --- *
245 * Arguments: @au_sample *s@ = sample pointer
247 * Returns: A pointer to the audio data for the sample.
249 * Use: Fetches a sample, and decodes it, if necessary. The decoded
250 * sample data is left with an outstanding reference to it, so
251 * it won't be freed. This is used internally by @au_queue@,
252 * before passing the fetched sample data to the system-specific
253 * layer for playback. It can also be used to lock sample data
254 * in memory (e.g., for the `abort' error message), or (by
255 * freeing it immediately afterwards) to prefetch a sample which
256 * will be used soon, to reduce the latency before the sample is
260 au_data *au_fetch(au_sample *s)
270 /* --- If we already have the sample data --- *
272 * If it's currently languishing in the spare bin, rescue it. If this
273 * doesn't work, we can release the audio lock, because nothing else messes
274 * with the spare list.
282 a->prev->next = a->next;
283 a->next->prev = a->prev;
284 a->next = a->prev = 0;
285 cache.sz_spare -= a->sz;
286 cache.sz_queue += a->sz;
292 T( trace(T_AU, "au: reusing sample `%s'", SYM_NAME(s)); )
298 /* --- Read the file --- *
300 * Buffered I/O will just involve more copying.
303 T( trace(T_AU, "au: fetching sample `%s'", SYM_NAME(s)); )
304 fn = filename(SYM_NAME(s));
305 if ((fd = open(fn, O_RDONLY)) < 0) {
306 err_report(ERR_AUDIO, ERRAU_OPEN, errno,
307 "couldn't open sample `%s': %s", fn, strerror(errno));
310 if (fstat(fd, &st)) {
311 err_report(ERR_AUDIO, ERRAU_OPEN, errno,
312 "couldn't fstat `%s': %s", fn, strerror(errno));
321 if ((r = read(fd, d.buf + d.len, n)) < 0) {
322 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR)
324 err_report(ERR_AUDIO, ERRAU_OPEN, errno,
325 "couldn't read `%s': %s", fn, strerror(errno));
337 /* --- Convert it into internal form --- */
339 if ((a = ausys_decode(s, d.buf, d.len)) == 0)
342 a->next = a->prev = 0;
349 cache.sz_queue += a->sz;
350 cache.sz_total += a->sz;
361 /* --- Tidy up after botched file I/O --- */
370 /* --- @au_queue@ --- *
372 * Arguments: @au_sample *s@ = sample pointer
374 * Returns: Zero on success, nonzero on failure.
376 * Use: Queues a sample to be played.
379 int au_queue(au_sample *s)
384 if ((a = au_fetch(s)) == 0)
386 T( trace(T_AU, "au: queuing sample `%s'", SYM_NAME(s)); )
391 /* --- @au_free@, @au_free_unlocked@ --- *
393 * Arguments: @au_data *a@ = pointer to audio data block
397 * Use: Frees a sample data block when it's no longer required.
400 void au_free(au_data *a)
407 void au_free_unlocked(au_data *a)
409 /* --- If the sample is unreferenced, throw it in the spare bin --- *
411 * This can be called from a background audio processing thread, so we need
412 * to acquire the lock.
420 T( trace(T_AU, "au: drop ref to `%s' (other refs remain)",
425 a->prev = AU_SPARE->prev;
426 AU_SPARE->prev->next = a;
428 cache.sz_queue -= a->sz;
429 cache.sz_spare += a->sz;
432 T( trace(T_AU, "au: drop last ref to `%s'", SYM_NAME(a->s)); )
437 /* --- @au_play@, @au_tryplay@ --- *
439 * Arguments: @const char *tag@ = sample tag string
441 * Returns: Zero on success, nonzero on failure.
443 * Use: Convenience functions for queueing samples by tag.
444 * If @au_tryplay@ cannot find the requested sample, it returns
445 * a zero value; if @au_play@ cannot find the sample, it reports
449 int au_tryplay(const char *tag)
453 if (!(au_flags & f_init))
455 if ((s = au_find(tag)) == 0 || au_queue(s))
460 int au_play(const char *tag)
464 if ((rc = au_tryplay(tag)) != 0)
465 err_report(ERR_AUDIO, ERRAU_NOTFOUND, 0, "sample `%s' not found", tag);
469 /*----- That's all, folks -------------------------------------------------*/