3 * $Id: au.c,v 1.1 2002/02/02 19:16:28 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.1 2002/02/02 19:16:28 mdw
33 * New audio subsystem.
37 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
54 #include <mLib/alloc.h>
55 #include <mLib/dstr.h>
58 #include <mLib/trace.h>
65 /*----- Static variables --------------------------------------------------*/
67 static unsigned au_flags
= 0;
68 static sym_table au_tab
; /* Sample cache, by name */
69 static const char *au_dir
= AUDIODIR
; /* Directory containing samples */
70 static struct { au_data
*next
, *prev
; } au_spare
; /* Lists for sample data */
71 static size_t au_sz
, au_max
; /* Size of cached samples */
73 #define AU_SPARE ((au_data*)&au_spare)
76 /*----- Utility functions -------------------------------------------------*/
78 /* --- @filename@ --- *
80 * Arguments: @const char *tag@ = sample tag string
82 * Returns: A pointer to a filename for the sample.
84 * Use: Converts tag strings to filenames.
87 static const char *filename(const char *tag
)
89 static dstr d
= DSTR_INIT
;
92 dstr_putf(&d
, "%s/%s.%s", au_dir
, tag
, ausys_suffix
);
93 T( trace(T_AU
, "au: map tag `%s' -> `%s'", tag
, d
.buf
); )
103 * Use: Prunes the cache of old sample data.
106 static void prune(void)
108 T( trace(T_AU
, "au: pruning cache (%lu/%lu)",
109 (unsigned long)au_sz
, (unsigned long)au_max
); )
110 while (au_sz
> au_max
&& AU_SPARE
->next
!= AU_SPARE
) {
111 au_data
*a
= AU_SPARE
->next
;
114 AU_SPARE
->next
= a
->next
;
115 a
->next
->prev
= AU_SPARE
;
119 T( trace(T_AU
, "au: ... discarded `%s' (%lu/%lu)", SYM_NAME(s
),
120 (unsigned long)au_sz
, (unsigned long)au_max
); )
124 /*----- Main code ---------------------------------------------------------*/
126 /* --- @au_init@ --- *
128 * Arguments: @const char *dir@ = samples directory, or null
129 * @size_t max@ = maximum cache size
133 * Use: Initializes the audio subsystem.
136 void au_init(const char *dir
, size_t max
)
138 if (au_flags
& f_init
)
141 /* --- Set up the sound directory --- */
144 dir
= getenv("JOG_AUDIR");
148 /* --- Initialize the sample cache --- */
151 AU_SPARE
->next
= AU_SPARE
->prev
= AU_SPARE
;
154 /* --- Initialize the system-specific subsystem --- */
157 T( trace(T_AU
, "au: initialized ok (dir = `%s')", au_dir
); )
164 /* --- @au_shutdown@ --- *
170 * Use: Shuts down the audio subsystem.
173 void au_shutdown(void)
175 if (!(au_flags
& f_init
))
178 T( trace(T_AU
, "au: shutdown ok"); )
181 /* --- @au_find@ --- *
183 * Arguments: @const char *tag@ = sample tag string
185 * Returns: A pointer to the sample corresponding to the tag, or null if
186 * the sample wasn't found.
188 * Use: Looks up a sample by its name.
191 au_sample
*au_find(const char *tag
)
194 au_sample
*s
= sym_find(&au_tab
, tag
, -1, sizeof(*s
), &f
);
201 if (stat(filename(tag
), &st
))
204 if (s
->f
& AUF_NOMATCH
) {
205 T( trace(T_AU
, "au: sample `%s' not found%s", tag
, f ?
" (hit)": ""); )
208 T( trace(T_AU
, "au: sample `%s' found%s", tag
, f ?
" (hit)" : ""); )
212 /* --- @au_fetch@ --- *
214 * Arguments: @au_sample *s@ = sample pointer
216 * Returns: A pointer to the audio data for the sample.
218 * Use: Fetches a sample, and decodes it, if necessary. The decoded
219 * sample data is left with an outstanding reference to it, so
220 * it won't be freed. This is used internally by @au_queue@,
221 * before passing the fetched sample data to the system-specific
222 * layer for playback. It can also be used to lock sample data
223 * in memory (e.g., for the `abort' error message), or (by
224 * freeing it immediately afterwards) to prefetch a sample which
225 * will be used soon, to reduce the latency before the sample is
229 au_data
*au_fetch(au_sample
*s
)
239 /* --- If we already have the sample data --- *
241 * If it's currently languishing in the spare bin, rescue it. If this
242 * doesn't work, we can release the audio lock, because nothing else messes
243 * with the spare list.
251 a
->prev
->next
= a
->next
;
252 a
->next
->prev
= a
->prev
;
253 a
->next
= a
->prev
= 0;
256 T( trace(T_AU
, "au: reusing sample `%s'", SYM_NAME(s
)); )
262 /* --- Read the file --- *
264 * Buffered I/O will just involve more copying.
267 T( trace(T_AU
, "au: fetching sample `%s'", SYM_NAME(s
)); )
268 fn
= filename(SYM_NAME(s
));
269 if ((fd
= open(fn
, O_RDONLY
)) < 0) {
270 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
271 "couldn't open sample `%s': %s", fn
, strerror(errno
));
274 if (fstat(fd
, &st
)) {
275 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
276 "couldn't fstat `%s': %s", fn
, strerror(errno
));
285 if ((r
= read(fd
, d
.buf
+ d
.len
, n
)) < 0) {
286 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
|| errno
== EINTR
)
288 err_report(ERR_AUDIO
, ERRAU_OPEN
, errno
,
289 "couldn't read `%s': %s", fn
, strerror(errno
));
301 /* --- Convert it into internal form --- */
303 if ((a
= ausys_decode(s
, d
.buf
, d
.len
)) == 0)
307 a
->next
= a
->prev
= 0;
321 /* --- Tidy up after botched file I/O --- */
330 /* --- @au_queue@ --- *
332 * Arguments: @au_sample *s@ = sample pointer
334 * Returns: Zero on success, nonzero on failure.
336 * Use: Queues a sample to be played.
339 int au_queue(au_sample
*s
)
344 if ((a
= au_fetch(s
)) == 0)
346 T( trace(T_AU
, "au: queuing sample `%s'", SYM_NAME(s
)); )
351 /* --- @au_free@, @au_free_unlocked@ --- *
353 * Arguments: @au_data *a@ = pointer to audio data block
357 * Use: Frees a sample data block when it's no longer required.
360 void au_free(au_data
*a
)
367 void au_free_unlocked(au_data
*a
)
369 /* --- If the sample is unreferenced, throw it in the spare bin --- *
371 * This can be called from a background audio processing thread, so we need
372 * to acquire the lock.
380 T( trace(T_AU
, "au: drop ref to `%s' (other refs remain)",
385 a
->prev
= AU_SPARE
->prev
;
386 AU_SPARE
->prev
->next
= a
;
388 T( trace(T_AU
, "au: drop last ref to `%s'", SYM_NAME(a
->s
)); )
393 /* --- @au_play@, @au_tryplay@ --- *
395 * Arguments: @const char *tag@ = sample tag string
397 * Returns: Zero on success, nonzero on failure.
399 * Use: Convenience functions for queueing samples by tag.
400 * If @au_tryplay@ cannot find the requested sample, it returns
401 * a zero value; if @au_play@ cannot find the sample, it reports
405 int au_tryplay(const char *tag
)
409 if (!(au_flags
& f_init
))
411 if ((s
= au_find(tag
)) == 0 || au_queue(s
))
416 int au_play(const char *tag
)
420 if ((rc
= au_tryplay(tag
)) != 0)
421 err_report(ERR_AUDIO
, ERRAU_NOTFOUND
, 0, "sample `%s' not found", tag
);
425 /*----- That's all, folks -------------------------------------------------*/