| 1 | // Ogg Vorbis audio decoder - v1.22 - public domain, http://nothings.org/stb_vorbis/, see also README.md |
| 2 | |
| 3 | #ifndef STB_VORBIS_INCLUDE_STB_VORBIS_H |
| 4 | #define STB_VORBIS_INCLUDE_STB_VORBIS_H |
| 5 | |
| 6 | #if defined(STB_VORBIS_NO_CRT) && !defined(STB_VORBIS_NO_STDIO) |
| 7 | #define STB_VORBIS_NO_STDIO 1 |
| 8 | #endif |
| 9 | |
| 10 | #ifndef STB_VORBIS_NO_STDIO |
| 11 | #include <stdio.h> |
| 12 | #endif |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | /////////// THREAD SAFETY |
| 19 | |
| 20 | // Individual stb_vorbis* handles are not thread-safe; you cannot decode from |
| 21 | // them from multiple threads at the same time. However, you can have multiple |
| 22 | // stb_vorbis* handles and decode from them independently in multiple thrads. |
| 23 | |
| 24 | |
| 25 | /////////// MEMORY ALLOCATION |
| 26 | |
| 27 | // normally stb_vorbis uses malloc() to allocate memory at startup, |
| 28 | // and alloca() to allocate temporary memory during a frame on the |
| 29 | // stack. (Memory consumption will depend on the amount of setup |
| 30 | // data in the file and how you set the compile flags for speed |
| 31 | // vs. size. In my test files the maximal-size usage is ~150KB.) |
| 32 | // |
| 33 | // You can modify the wrapper functions in the source (setup_malloc, |
| 34 | // setup_temp_malloc, temp_malloc) to change this behavior, or you |
| 35 | // can use a simpler allocation model: you pass in a buffer from |
| 36 | // which stb_vorbis will allocate _all_ its memory (including the |
| 37 | // temp memory). "open" may fail with a VORBIS_outofmem if you |
| 38 | // do not pass in enough data; there is no way to determine how |
| 39 | // much you do need except to succeed (at which point you can |
| 40 | // query get_info to find the exact amount required. yes I know |
| 41 | // this is lame). |
| 42 | // |
| 43 | // If you pass in a non-NULL buffer of the type below, allocation |
| 44 | // will occur from it as described above. Otherwise just pass NULL |
| 45 | // to use malloc()/alloca() |
| 46 | |
| 47 | typedef struct |
| 48 | { |
| 49 | char *alloc_buffer; |
| 50 | int alloc_buffer_length_in_bytes; |
| 51 | } stb_vorbis_alloc; |
| 52 | |
| 53 | |
| 54 | /////////// FUNCTIONS USEABLE WITH ALL INPUT MODES |
| 55 | |
| 56 | typedef struct stb_vorbis stb_vorbis; |
| 57 | |
| 58 | typedef struct |
| 59 | { |
| 60 | unsigned int sample_rate; |
| 61 | int channels; |
| 62 | |
| 63 | unsigned int setup_memory_required; |
| 64 | unsigned int setup_temp_memory_required; |
| 65 | unsigned int temp_memory_required; |
| 66 | |
| 67 | int max_frame_size; |
| 68 | } stb_vorbis_info; |
| 69 | |
| 70 | typedef struct |
| 71 | { |
| 72 | char *vendor; |
| 73 | |
| 74 | int comment_list_length; |
| 75 | char **comment_list; |
| 76 | } stb_vorbis_comment; |
| 77 | |
| 78 | // get general information about the file |
| 79 | extern stb_vorbis_info stb_vorbis_get_info(stb_vorbis *f); |
| 80 | |
| 81 | // get ogg comments |
| 82 | extern stb_vorbis_comment stb_vorbis_get_comment(stb_vorbis *f); |
| 83 | |
| 84 | // get the last error detected (clears it, too) |
| 85 | extern int stb_vorbis_get_error(stb_vorbis *f); |
| 86 | |
| 87 | // close an ogg vorbis file and free all memory in use |
| 88 | extern void stb_vorbis_close(stb_vorbis *f); |
| 89 | |
| 90 | // this function returns the offset (in samples) from the beginning of the |
| 91 | // file that will be returned by the next decode, if it is known, or -1 |
| 92 | // otherwise. after a flush_pushdata() call, this may take a while before |
| 93 | // it becomes valid again. |
| 94 | // NOT WORKING YET after a seek with PULLDATA API |
| 95 | extern int stb_vorbis_get_sample_offset(stb_vorbis *f); |
| 96 | |
| 97 | // returns the current seek point within the file, or offset from the beginning |
| 98 | // of the memory buffer. In pushdata mode it returns 0. |
| 99 | extern unsigned int stb_vorbis_get_file_offset(stb_vorbis *f); |
| 100 | |
| 101 | /////////// PUSHDATA API |
| 102 | |
| 103 | #ifndef STB_VORBIS_NO_PUSHDATA_API |
| 104 | |
| 105 | // this API allows you to get blocks of data from any source and hand |
| 106 | // them to stb_vorbis. you have to buffer them; stb_vorbis will tell |
| 107 | // you how much it used, and you have to give it the rest next time; |
| 108 | // and stb_vorbis may not have enough data to work with and you will |
| 109 | // need to give it the same data again PLUS more. Note that the Vorbis |
| 110 | // specification does not bound the size of an individual frame. |
| 111 | |
| 112 | extern stb_vorbis *stb_vorbis_open_pushdata( |
| 113 | const unsigned char * datablock, int datablock_length_in_bytes, |
| 114 | int *datablock_memory_consumed_in_bytes, |
| 115 | int *error, |
| 116 | const stb_vorbis_alloc *alloc_buffer); |
| 117 | // create a vorbis decoder by passing in the initial data block containing |
| 118 | // the ogg&vorbis headers (you don't need to do parse them, just provide |
| 119 | // the first N bytes of the file--you're told if it's not enough, see below) |
| 120 | // on success, returns an stb_vorbis *, does not set error, returns the amount of |
| 121 | // data parsed/consumed on this call in *datablock_memory_consumed_in_bytes; |
| 122 | // on failure, returns NULL on error and sets *error, does not change *datablock_memory_consumed |
| 123 | // if returns NULL and *error is VORBIS_need_more_data, then the input block was |
| 124 | // incomplete and you need to pass in a larger block from the start of the file |
| 125 | |
| 126 | extern int stb_vorbis_decode_frame_pushdata( |
| 127 | stb_vorbis *f, |
| 128 | const unsigned char *datablock, int datablock_length_in_bytes, |
| 129 | int *channels, // place to write number of float * buffers |
| 130 | float ***output, // place to write float ** array of float * buffers |
| 131 | int *samples // place to write number of output samples |
| 132 | ); |
| 133 | // decode a frame of audio sample data if possible from the passed-in data block |
| 134 | // |
| 135 | // return value: number of bytes we used from datablock |
| 136 | // |
| 137 | // possible cases: |
| 138 | // 0 bytes used, 0 samples output (need more data) |
| 139 | // N bytes used, 0 samples output (resynching the stream, keep going) |
| 140 | // N bytes used, M samples output (one frame of data) |
| 141 | // note that after opening a file, you will ALWAYS get one N-bytes,0-sample |
| 142 | // frame, because Vorbis always "discards" the first frame. |
| 143 | // |
| 144 | // Note that on resynch, stb_vorbis will rarely consume all of the buffer, |
| 145 | // instead only datablock_length_in_bytes-3 or less. This is because it wants |
| 146 | // to avoid missing parts of a page header if they cross a datablock boundary, |
| 147 | // without writing state-machiney code to record a partial detection. |
| 148 | // |
| 149 | // The number of channels returned are stored in *channels (which can be |
| 150 | // NULL--it is always the same as the number of channels reported by |
| 151 | // get_info). *output will contain an array of float* buffers, one per |
| 152 | // channel. In other words, (*output)[0][0] contains the first sample from |
| 153 | // the first channel, and (*output)[1][0] contains the first sample from |
| 154 | // the second channel. |
| 155 | // |
| 156 | // *output points into stb_vorbis's internal output buffer storage; these |
| 157 | // buffers are owned by stb_vorbis and application code should not free |
| 158 | // them or modify their contents. They are transient and will be overwritten |
| 159 | // once you ask for more data to get decoded, so be sure to grab any data |
| 160 | // you need before then. |
| 161 | |
| 162 | extern void stb_vorbis_flush_pushdata(stb_vorbis *f); |
| 163 | // inform stb_vorbis that your next datablock will not be contiguous with |
| 164 | // previous ones (e.g. you've seeked in the data); future attempts to decode |
| 165 | // frames will cause stb_vorbis to resynchronize (as noted above), and |
| 166 | // once it sees a valid Ogg page (typically 4-8KB, as large as 64KB), it |
| 167 | // will begin decoding the _next_ frame. |
| 168 | // |
| 169 | // if you want to seek using pushdata, you need to seek in your file, then |
| 170 | // call stb_vorbis_flush_pushdata(), then start calling decoding, then once |
| 171 | // decoding is returning you data, call stb_vorbis_get_sample_offset, and |
| 172 | // if you don't like the result, seek your file again and repeat. |
| 173 | #endif |
| 174 | |
| 175 | |
| 176 | ////////// PULLING INPUT API |
| 177 | |
| 178 | #ifndef STB_VORBIS_NO_PULLDATA_API |
| 179 | // This API assumes stb_vorbis is allowed to pull data from a source-- |
| 180 | // either a block of memory containing the _entire_ vorbis stream, or a |
| 181 | // FILE * that you or it create, or possibly some other reading mechanism |
| 182 | // if you go modify the source to replace the FILE * case with some kind |
| 183 | // of callback to your code. (But if you don't support seeking, you may |
| 184 | // just want to go ahead and use pushdata.) |
| 185 | |
| 186 | #if !defined(STB_VORBIS_NO_STDIO) && !defined(STB_VORBIS_NO_INTEGER_CONVERSION) |
| 187 | extern int stb_vorbis_decode_filename(const char *filename, int *channels, int *sample_rate, short **output); |
| 188 | #endif |
| 189 | #if !defined(STB_VORBIS_NO_INTEGER_CONVERSION) |
| 190 | extern int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output); |
| 191 | #endif |
| 192 | // decode an entire file and output the data interleaved into a malloc()ed |
| 193 | // buffer stored in *output. The return value is the number of samples |
| 194 | // decoded, or -1 if the file could not be opened or was not an ogg vorbis file. |
| 195 | // When you're done with it, just free() the pointer returned in *output. |
| 196 | |
| 197 | extern stb_vorbis * stb_vorbis_open_memory(const unsigned char *data, int len, |
| 198 | int *error, const stb_vorbis_alloc *alloc_buffer); |
| 199 | // create an ogg vorbis decoder from an ogg vorbis stream in memory (note |
| 200 | // this must be the entire stream!). on failure, returns NULL and sets *error |
| 201 | |
| 202 | #ifndef STB_VORBIS_NO_STDIO |
| 203 | extern stb_vorbis * stb_vorbis_open_filename(const char *filename, |
| 204 | int *error, const stb_vorbis_alloc *alloc_buffer); |
| 205 | // create an ogg vorbis decoder from a filename via fopen(). on failure, |
| 206 | // returns NULL and sets *error (possibly to VORBIS_file_open_failure). |
| 207 | |
| 208 | extern stb_vorbis * stb_vorbis_open_file(FILE *f, int close_handle_on_close, |
| 209 | int *error, const stb_vorbis_alloc *alloc_buffer); |
| 210 | // create an ogg vorbis decoder from an open FILE *, looking for a stream at |
| 211 | // the _current_ seek point (ftell). on failure, returns NULL and sets *error. |
| 212 | // note that stb_vorbis must "own" this stream; if you seek it in between |
| 213 | // calls to stb_vorbis, it will become confused. Moreover, if you attempt to |
| 214 | // perform stb_vorbis_seek_*() operations on this file, it will assume it |
| 215 | // owns the _entire_ rest of the file after the start point. Use the next |
| 216 | // function, stb_vorbis_open_file_section(), to limit it. |
| 217 | |
| 218 | extern stb_vorbis * stb_vorbis_open_file_section(FILE *f, int close_handle_on_close, |
| 219 | int *error, const stb_vorbis_alloc *alloc_buffer, unsigned int len); |
| 220 | // create an ogg vorbis decoder from an open FILE *, looking for a stream at |
| 221 | // the _current_ seek point (ftell); the stream will be of length 'len' bytes. |
| 222 | // on failure, returns NULL and sets *error. note that stb_vorbis must "own" |
| 223 | // this stream; if you seek it in between calls to stb_vorbis, it will become |
| 224 | // confused. |
| 225 | #endif |
| 226 | |
| 227 | extern int stb_vorbis_seek_frame(stb_vorbis *f, unsigned int sample_number); |
| 228 | extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); |
| 229 | // these functions seek in the Vorbis file to (approximately) 'sample_number'. |
| 230 | // after calling seek_frame(), the next call to get_frame_*() will include |
| 231 | // the specified sample. after calling stb_vorbis_seek(), the next call to |
| 232 | // stb_vorbis_get_samples_* will start with the specified sample. If you |
| 233 | // do not need to seek to EXACTLY the target sample when using get_samples_*, |
| 234 | // you can also use seek_frame(). |
| 235 | |
| 236 | extern int stb_vorbis_seek_start(stb_vorbis *f); |
| 237 | // this function is equivalent to stb_vorbis_seek(f,0) |
| 238 | |
| 239 | extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); |
| 240 | extern float stb_vorbis_stream_length_in_seconds(stb_vorbis *f); |
| 241 | // these functions return the total length of the vorbis stream |
| 242 | |
| 243 | extern int stb_vorbis_get_frame_float(stb_vorbis *f, int *channels, float ***output); |
| 244 | // decode the next frame and return the number of samples. the number of |
| 245 | // channels returned are stored in *channels (which can be NULL--it is always |
| 246 | // the same as the number of channels reported by get_info). *output will |
| 247 | // contain an array of float* buffers, one per channel. These outputs will |
| 248 | // be overwritten on the next call to stb_vorbis_get_frame_*. |
| 249 | // |
| 250 | // You generally should not intermix calls to stb_vorbis_get_frame_*() |
| 251 | // and stb_vorbis_get_samples_*(), since the latter calls the former. |
| 252 | |
| 253 | #ifndef STB_VORBIS_NO_INTEGER_CONVERSION |
| 254 | extern int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buffer, int num_shorts); |
| 255 | extern int stb_vorbis_get_frame_short (stb_vorbis *f, int num_c, short **buffer, int num_samples); |
| 256 | #endif |
| 257 | // decode the next frame and return the number of *samples* per channel. |
| 258 | // Note that for interleaved data, you pass in the number of shorts (the |
| 259 | // size of your array), but the return value is the number of samples per |
| 260 | // channel, not the total number of samples. |
| 261 | // |
| 262 | // The data is coerced to the number of channels you request according to the |
| 263 | // channel coercion rules (see below). You must pass in the size of your |
| 264 | // buffer(s) so that stb_vorbis will not overwrite the end of the buffer. |
| 265 | // The maximum buffer size needed can be gotten from get_info(); however, |
| 266 | // the Vorbis I specification implies an absolute maximum of 4096 samples |
| 267 | // per channel. |
| 268 | |
| 269 | // Channel coercion rules: |
| 270 | // Let M be the number of channels requested, and N the number of channels present, |
| 271 | // and Cn be the nth channel; let stereo L be the sum of all L and center channels, |
| 272 | // and stereo R be the sum of all R and center channels (channel assignment from the |
| 273 | // vorbis spec). |
| 274 | // M N output |
| 275 | // 1 k sum(Ck) for all k |
| 276 | // 2 * stereo L, stereo R |
| 277 | // k l k > l, the first l channels, then 0s |
| 278 | // k l k <= l, the first k channels |
| 279 | // Note that this is not _good_ surround etc. mixing at all! It's just so |
| 280 | // you get something useful. |
| 281 | |
| 282 | extern int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float *buffer, int num_floats); |
| 283 | extern int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples); |
| 284 | // gets num_samples samples, not necessarily on a frame boundary--this requires |
| 285 | // buffering so you have to supply the buffers. DOES NOT APPLY THE COERCION RULES. |
| 286 | // Returns the number of samples stored per channel; it may be less than requested |
| 287 | // at the end of the file. If there are no more samples in the file, returns 0. |
| 288 | |
| 289 | #ifndef STB_VORBIS_NO_INTEGER_CONVERSION |
| 290 | extern int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts); |
| 291 | extern int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int num_samples); |
| 292 | #endif |
| 293 | // gets num_samples samples, not necessarily on a frame boundary--this requires |
| 294 | // buffering so you have to supply the buffers. Applies the coercion rules above |
| 295 | // to produce 'channels' channels. Returns the number of samples stored per channel; |
| 296 | // it may be less than requested at the end of the file. If there are no more |
| 297 | // samples in the file, returns 0. |
| 298 | |
| 299 | #endif |
| 300 | |
| 301 | //////// ERROR CODES |
| 302 | |
| 303 | enum STBVorbisError |
| 304 | { |
| 305 | VORBIS__no_error, |
| 306 | |
| 307 | VORBIS_need_more_data=1, // not a real error |
| 308 | |
| 309 | VORBIS_invalid_api_mixing, // can't mix API modes |
| 310 | VORBIS_outofmem, // not enough memory |
| 311 | VORBIS_feature_not_supported, // uses floor 0 |
| 312 | VORBIS_too_many_channels, // STB_VORBIS_MAX_CHANNELS is too small |
| 313 | VORBIS_file_open_failure, // fopen() failed |
| 314 | VORBIS_seek_without_length, // can't seek in unknown-length file |
| 315 | |
| 316 | VORBIS_unexpected_eof=10, // file is truncated? |
| 317 | VORBIS_seek_invalid, // seek past EOF |
| 318 | |
| 319 | // decoding errors (corrupt/invalid stream) -- you probably |
| 320 | // don't care about the exact details of these |
| 321 | |
| 322 | // vorbis errors: |
| 323 | VORBIS_invalid_setup=20, |
| 324 | VORBIS_invalid_stream, |
| 325 | |
| 326 | // ogg errors: |
| 327 | VORBIS_missing_capture_pattern=30, |
| 328 | VORBIS_invalid_stream_structure_version, |
| 329 | VORBIS_continued_packet_flag_invalid, |
| 330 | VORBIS_incorrect_stream_serial_number, |
| 331 | VORBIS_invalid_first_page, |
| 332 | VORBIS_bad_packet_type, |
| 333 | VORBIS_cant_find_last_page, |
| 334 | VORBIS_seek_failed, |
| 335 | VORBIS_ogg_skeleton_not_supported |
| 336 | }; |
| 337 | |
| 338 | |
| 339 | #ifdef __cplusplus |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | #endif // STB_VORBIS_INCLUDE_STB_VORBIS_H |
| 344 | |
| 345 | |