stb_vorbis.h 15 KB

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