| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: au.h,v 1.2 2002/02/02 22:43:50 mdw Exp $ |
| 4 | * |
| 5 | * 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 | |
| 29 | #ifndef AU_H |
| 30 | #define AU_H |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /*----- Header files ------------------------------------------------------*/ |
| 37 | |
| 38 | #include <stddef.h> |
| 39 | |
| 40 | #include <mLib/bits.h> |
| 41 | #include <mLib/sym.h> |
| 42 | |
| 43 | /*----- Data structures ---------------------------------------------------*/ |
| 44 | |
| 45 | /* --- An audio sample --- * |
| 46 | * |
| 47 | * We maintain a cache of sample data, keyed by textual tags. An entry in |
| 48 | * the cache may indicate one of three states: |
| 49 | * |
| 50 | * * a tag for a sample which doesn't exist (negative answer); |
| 51 | * |
| 52 | * * a sample whose data is currently resident; or |
| 53 | * |
| 54 | * * sample with nonresident data. |
| 55 | * |
| 56 | * A negative answer is indicated by the @AUF_NOMATCH@ flag. If an entry has |
| 57 | * resident data, the @a@ pointer is non-null. |
| 58 | */ |
| 59 | |
| 60 | typedef struct au_sample { |
| 61 | sym_base s; /* Base structure for name lookup */ |
| 62 | unsigned f; /* Flags for this entry */ |
| 63 | struct au_data *a; /* Sample data, if present */ |
| 64 | } au_sample; |
| 65 | |
| 66 | #define AUF_NOMATCH 1u /* Cached non-match for this tag */ |
| 67 | |
| 68 | /* --- Audio data --- * |
| 69 | * |
| 70 | * The actual sample data, in an internal representation chosen by the |
| 71 | * system-specific layer. |
| 72 | * |
| 73 | * The sample data can be in one of two states: in-use or spare. Sample data |
| 74 | * is considered to be in-use if the @ref@ field is nonzero. Spare sample |
| 75 | * data is left in-memory for efficiency's sake, but will be discarded |
| 76 | * (least-recently-used first) when the total size of sample data (as |
| 77 | * measured by the @sz@ fields) exceeds @AU_CACHEMAX@. |
| 78 | */ |
| 79 | |
| 80 | typedef struct au_data { |
| 81 | struct au_data *next, *prev; /* Other samples in this list */ |
| 82 | unsigned ref; /* Reference count for sample data */ |
| 83 | struct au_sample *s; /* Parent sample node */ |
| 84 | octet *p; /* Pointer to sample file data */ |
| 85 | size_t sz; /* Size of sample file data */ |
| 86 | } au_data; |
| 87 | |
| 88 | /* --- Audio cache information --- */ |
| 89 | |
| 90 | typedef struct au_cacheinfo { |
| 91 | size_t sz_max; /* Maximum allowed cache size */ |
| 92 | size_t sz_total; /* Total size used by samples */ |
| 93 | size_t sz_spare; /* Size used by `spare' samples */ |
| 94 | size_t sz_queue; /* Size used by queued samples */ |
| 95 | unsigned n_total; /* Total number of cached samples */ |
| 96 | unsigned n_spare; /* Number of `spare' samples */ |
| 97 | unsigned n_queue; /* Number of queued samples */ |
| 98 | unsigned long hits; /* Number of cache hits */ |
| 99 | unsigned long misses; /* Number of cache misses */ |
| 100 | } au_cacheinfo; |
| 101 | |
| 102 | #define AU_CACHEMAX 0x01000000 /* Default cache maximum size */ |
| 103 | |
| 104 | /*----- Functions provided ------------------------------------------------*/ |
| 105 | |
| 106 | /* --- @au_init@ --- * |
| 107 | * |
| 108 | * Arguments: @const char *dir@ = samples directory, or null |
| 109 | * @size_t max@ = maximum cache size |
| 110 | * |
| 111 | * Returns: --- |
| 112 | * |
| 113 | * Use: Initializes the audio subsystem. |
| 114 | */ |
| 115 | |
| 116 | extern void au_init(const char */*dir*/, size_t /*max*/); |
| 117 | |
| 118 | /* --- @au_shutdown@ --- * |
| 119 | * |
| 120 | * Arguments: --- |
| 121 | * |
| 122 | * Returns: --- |
| 123 | * |
| 124 | * Use: Shuts down the audio subsystem. |
| 125 | */ |
| 126 | |
| 127 | extern void au_shutdown(void); |
| 128 | |
| 129 | /* --- @au_getcacheinfo@ --- * |
| 130 | * |
| 131 | * Arguments: @au_cacheinfo *c@ = where to put the information |
| 132 | * |
| 133 | * Returns: --- |
| 134 | * |
| 135 | * Use: Extracts audio cache information. |
| 136 | */ |
| 137 | |
| 138 | extern void au_getcacheinfo(au_cacheinfo */*c*/); |
| 139 | |
| 140 | /* --- @au_setcachelimit@ --- * |
| 141 | * |
| 142 | * Arguments: @size_t max@ = new cache limit |
| 143 | * |
| 144 | * Returns: --- |
| 145 | * |
| 146 | * Use: Reconfigures the maximum cache size. This probably isn't |
| 147 | * very useful, but it was easy... |
| 148 | */ |
| 149 | |
| 150 | extern void au_setcachelimit(size_t /*max*/); |
| 151 | |
| 152 | /* --- @au_find@ --- * |
| 153 | * |
| 154 | * Arguments: @const char *tag@ = sample tag string |
| 155 | * |
| 156 | * Returns: A pointer to the sample corresponding to the tag, or null if |
| 157 | * the sample wasn't found. |
| 158 | * |
| 159 | * Use: Looks up a sample by its name. |
| 160 | */ |
| 161 | |
| 162 | extern au_sample *au_find(const char */*tag*/); |
| 163 | |
| 164 | /* --- @au_fetch@ --- * |
| 165 | * |
| 166 | * Arguments: @au_sample *s@ = sample pointer |
| 167 | * |
| 168 | * Returns: A pointer to the audio data for the sample. |
| 169 | * |
| 170 | * Use: Fetches a sample, and decodes it, if necessary. The decoded |
| 171 | * sample data is left with an outstanding reference to it, so |
| 172 | * it won't be freed. This is used internally by @au_queue@, |
| 173 | * before passing the fetched sample data to the system-specific |
| 174 | * layer for playback. It can also be used to lock sample data |
| 175 | * in memory (e.g., for the `abort' error message), or (by |
| 176 | * freeing it immediately afterwards) to prefetch a sample which |
| 177 | * will be used soon, to reduce the latency before the sample is |
| 178 | * heard. |
| 179 | */ |
| 180 | |
| 181 | extern au_data *au_fetch(au_sample */*s*/); |
| 182 | |
| 183 | /* --- @au_queue@ --- * |
| 184 | * |
| 185 | * Arguments: @au_sample *s@ = sample pointer |
| 186 | * |
| 187 | * Returns: Zero on success, nonzero on failure. |
| 188 | * |
| 189 | * Use: Queues a sample to be played. |
| 190 | */ |
| 191 | |
| 192 | extern int au_queue(au_sample */*s*/); |
| 193 | |
| 194 | /* --- @au_free@, @au_free_unlocked@ --- * |
| 195 | * |
| 196 | * Arguments: @au_data *a@ = pointer to audio data block |
| 197 | * |
| 198 | * Returns: --- |
| 199 | * |
| 200 | * Use: Frees a sample data block when it's no longer required. |
| 201 | */ |
| 202 | |
| 203 | extern void au_free(au_data */*a*/); |
| 204 | extern void au_free_unlocked(au_data */*a*/); |
| 205 | |
| 206 | /* --- @au_play@, @au_tryplay@ --- * |
| 207 | * |
| 208 | * Arguments: @const char *tag@ = sample tag string |
| 209 | * |
| 210 | * Returns: Zero on success, nonzero on failure. |
| 211 | * |
| 212 | * Use: Convenience functions for queueing samples by tag. |
| 213 | * If @au_tryplay@ cannot find the requested sample, it returns |
| 214 | * a zero value; if @au_play@ cannot find the sample, it aborts |
| 215 | * the program. |
| 216 | */ |
| 217 | |
| 218 | extern int au_play(const char */*tag*/); |
| 219 | extern int au_tryplay(const char */*tag*/); |
| 220 | |
| 221 | /*----- That's all, folks -------------------------------------------------*/ |
| 222 | |
| 223 | #ifdef __cplusplus |
| 224 | } |
| 225 | #endif |
| 226 | |
| 227 | #endif |