Mercurial > SDL_sound_CoreAudio
annotate SDL_sound.h @ 1:508aac690b19
Whoops...changed some overlooked "voice" to "sound".
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 17 Sep 2001 07:07:51 +0000 |
parents | 1078b3528e6f |
children | fd6cd0e04e6f |
rev | line source |
---|---|
0 | 1 /* |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /** | |
21 * The basic gist of SDL_sound is that you use an SDL_RWops to get sound data | |
22 * into this library, and SDL_sound will take that data, in one of several | |
23 * popular formats, and decode it into raw waveform data in the format of | |
24 * your choice. This gives you a nice abstraction for getting sound into your | |
25 * game or application; just feed it to SDL_sound, and it will handle | |
26 * decoding and converting, so you can just pass it to your SDL audio | |
27 * callback (or whatever). Since it gets data from a SDL_RWops, you can get | |
28 * the initial sound data from any number of sources: file, memory buffer, | |
29 * network connection, etc. | |
30 * | |
31 * As the name implies, this library depends on SDL: Simple Directmedia Layer, | |
32 * which is a powerful, free, and cross-platform multimedia library. It can | |
33 * be found at http://www.libsdl.org/ | |
34 * | |
35 * Support is in place or planned for the following sound formats: | |
36 * - .WAV (Microsoft WAVfile RIFF data, internal.) | |
37 * - .VOC (Creative Labs' Voice format, internal.) | |
38 * - .MP3 (MPEG-1 layer 3 support, via the SMPEG library.) | |
39 * - .MID (MIDI music converted to Waveform data, via Timidity.) | |
40 * - .MOD (MOD files, via MikMod.) | |
41 * - .OGG (Ogg files, via Ogg Vorbis libraries.) | |
42 * - .RAWDATA (Raw sound data in any format, internal.) | |
43 * - .CDA (CD audio read into a sound buffer, internal.) | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
44 * - .AU |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
45 * - .AIFF |
0 | 46 * |
47 * (...and more to come...) | |
48 * | |
49 * Please see the file LICENSE in the source's root directory. | |
50 * | |
51 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
52 */ | |
53 | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
54 #ifndef _INCLUDE_SDL_SOUND_H_ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
55 #define _INCLUDE_SDL_SOUND_H_ |
0 | 56 |
57 #include "SDL.h" | |
58 | |
59 #ifdef __cplusplus | |
60 extern "C" { | |
61 #endif | |
62 | |
63 /* | |
64 * These are flags that are used in a Sound_Sample (below) to show various | |
65 * states. | |
66 * | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
67 * To use: "if (sample->flags & SOUND_SAMPLEFLAGS_ERROR) { dosomething(); }" |
0 | 68 */ |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
69 typedef enum __SOUND_SAMPLEFLAGS__ |
0 | 70 { |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
71 SOUND_SAMPLEFLAG_NONE = 0, /* Null flag. */ |
0 | 72 |
73 /* these are set at sample creation time... */ | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
74 SOUND_SAMPLEFLAG_NEEDSEEK = 1, /* SDL_RWops must be able to seek. */ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
75 SOUND_SAMPLEFLAG_STREAMING = 1 << 1, /* source is streaming (no EOF). */ |
0 | 76 |
77 /* these are set during decoding... */ | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
78 SOUND_SAMPLEFLAG_EOF = 1 << 29, /* end of input stream. */ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
79 SOUND_SAMPLEFLAG_ERROR = 1 << 30, /* unrecoverable error. */ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
80 SOUND_SAMPLEFLAG_AGAIN = 1 << 31 /* couldn't read without blocking. */ |
0 | 81 } Sound_SampleFlags; |
82 | |
83 | |
84 /* | |
85 * The Sound_Sample structure is the heart of SDL_sound. This holds | |
86 * information about a source of sound data as it is being decoded. | |
87 * EVERY FIELD IN THIS IS READ-ONLY. Please use the API functions to | |
88 * change them. | |
89 */ | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
90 typedef struct __SOUND_SAMPLE__ |
0 | 91 { |
92 void *opaque; /* Internal use only. */ | |
93 Sound_DecoderInfo *decoder; /* Decoder used for this sample. */ | |
94 SDL_AudioSpec desired; /* Desired audio format for conversion. */ | |
95 SDL_AudioSpec actual; /* Actual audio format of sample. */ | |
96 void *buffer; /* Decoded sound data lands in here. */ | |
97 Uint32 buffer_size; /* Current size of (buffer), in bytes. */ | |
98 Sound_SampleFlags flags; /* Flags relating to this sample. */ | |
99 } Sound_Sample; | |
100 | |
101 | |
102 /* | |
103 * Each decoder sets up one of these structs, which can be retrieved via | |
104 * the Sound_AvailableDecoders() function. | |
105 */ | |
106 typedef struct __PHYSFS_ARCHIVEINFO__ | |
107 { | |
108 const char *extension; /* Case sensitive standard file extension. */ | |
109 const char *description; /* Human readable description of decoder. */ | |
110 const char *author; /* "Name Of Author (email@emailhost.dom)" */ | |
111 const char *url; /* URL specific to this decoder. */ | |
112 } PHYSFS_ArchiveInfo; | |
113 | |
114 | |
115 /* | |
116 * Just what it says: a x.y.z style version number... | |
117 */ | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
118 typedef struct __SOUND_VERSION__ |
0 | 119 { |
120 int major; | |
121 int minor; | |
122 int patch; | |
123 } Sound_Version; | |
124 | |
125 | |
126 | |
127 /* functions and macros... */ | |
128 | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
129 #define SOUND_VER_MAJOR 0 |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
130 #define SOUND_VER_MINOR 0 |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
131 #define SOUND_VER_PATCH 1 |
0 | 132 |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
133 #define SOUND_VERSION(x) { \ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
134 (x)->major = SOUND_VER_MAJOR; \ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
135 (x)->minor = SOUND_VER_MINOR; \ |
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
136 (x)->patch = SOUND_VER_PATCH; \ |
0 | 137 } |
138 | |
139 /** | |
140 * Get the version of SDL_sound that is linked against your program. If you | |
141 * are using a shared library (DLL) version of SDL_sound, then it is possible | |
142 * that it will be different than the version you compiled against. | |
143 * | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
144 * This is a real function; the macro SOUND_VERSION tells you what version |
0 | 145 * of SDL_sound you compiled against: |
146 * | |
147 * Sound_Version compiled; | |
148 * Sound_Version linked; | |
149 * | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
150 * SOUND_VERSION(&compiled); |
0 | 151 * Sound_GetLinkedVersion(&linked); |
152 * printf("We compiled against SDL_sound version %d.%d.%d ...\n", | |
153 * compiled.major, compiled.minor, compiled.patch); | |
154 * printf("But we linked against SDL_sound version %d.%d.%d.\n", | |
155 * linked.major, linked.minor, linked.patch); | |
156 * | |
157 * This function may be called safely at any time, even before Sound_Init(). | |
158 */ | |
159 extern DECLSPEC void Sound_GetLinkedVersion(Sound_Version *ver); | |
160 | |
161 | |
162 /** | |
163 * Initialize SDL_sound. This must be called before any other SDL_sound | |
164 * function (except perhaps Sound_GetLinkedVersion()). You should call | |
165 * SDL_Init() before calling this. Sound_Init() will attempt to call | |
166 * SDL_Init(SDL_INIT_AUDIO), just in case. This is a safe behaviour, but it | |
167 * may not configure SDL to your liking by itself. | |
168 * | |
169 * @return nonzero on success, zero on error. Specifics of the error can be | |
170 * gleaned from Sound_GetLastError(). | |
171 */ | |
172 extern DECLSPEC int Sound_Init(void); | |
173 | |
174 | |
175 /** | |
176 * Shutdown SDL_sound. This closes any SDL_RWops that were being used as | |
177 * sound sources, and frees any resources in use by SDL_sound. | |
178 * | |
179 * All Sound_Sample pointers you had prior to this call are INVALIDATED. | |
180 * | |
181 * Once successfully deinitialized, Sound_Init() can be called again to | |
182 * restart the subsystem. All defaults API states are restored at this | |
183 * point. | |
184 * | |
185 * You should call this BEFORE SDL_Quit(). This will NOT call SDL_Quit() | |
186 * for you! | |
187 * | |
188 * @return nonzero on success, zero on error. Specifics of the error can be | |
189 * gleaned from Sound_GetLastError(). If failure, state of SDL_sound | |
190 * is undefined, and probably badly screwed up. | |
191 */ | |
192 extern DECLSPEC int Sound_Quit(void); | |
193 | |
194 | |
195 /** | |
196 * Get a list of sound formats supported by this implementation of SDL_sound. | |
197 * This is for informational purposes only. Note that the extension listed is | |
198 * merely convention: if we list "MP3", you can open an MPEG Audio layer 3 | |
199 * file with an extension of "XYZ", if you like. The file extensions are | |
200 * informational, and only required as a hint to choosing the correct | |
201 * decoder, since the sound data may not be coming from a file at all, thanks | |
202 * to the abstraction that an SDL_RWops provides. | |
203 * | |
204 * The returned value is an array of pointers to Sound_DecoderInfo structures, | |
205 * with a NULL entry to signify the end of the list: | |
206 * | |
207 * Sound_DecoderInfo **i; | |
208 * | |
209 * for (i = Sound_AvailableDecoders(); *i != NULL; i++) | |
210 * { | |
211 * printf("Supported sound format: [%s], which is [%s].\n", | |
212 * i->extension, i->description); | |
213 * // ...and other fields... | |
214 * } | |
215 * | |
216 * The return values are pointers to static internal memory, and should | |
217 * be considered READ ONLY, and never freed. | |
218 * | |
219 * @return READ ONLY Null-terminated array of READ ONLY structures. | |
220 */ | |
221 extern DECLSPEC const Sound_DecoderInfo **Sound_AvailableDecoders(void); | |
222 | |
223 | |
224 /** | |
225 * Get the last SDL_sound error message as a null-terminated string. | |
226 * This will be NULL if there's been no error since the last call to this | |
227 * function. The pointer returned by this call points to an internal buffer. | |
228 * Each thread has a unique error state associated with it, but each time | |
229 * a new error message is set, it will overwrite the previous one associated | |
230 * with that thread. It is safe to call this function at anytime, even | |
231 * before Sound_Init(). | |
232 * | |
233 * @return READ ONLY string of last error message. | |
234 */ | |
235 extern DECLSPEC const char *Sound_GetLastError(void); | |
236 | |
237 | |
238 /** | |
239 * Start decoding a new sound sample. The data is read via an SDL_RWops | |
240 * structure (see SDL_rwops.h in the SDL include directory), so it may be | |
241 * coming from memory, disk, network stream, etc. The (ext) parameter is | |
242 * merely a hint to determining the correct decoder; if you specify, for | |
243 * example, "mp3" for an extension, and one of the decoders lists that | |
244 * (case sensitive) as a handled extension, then that decoder is given | |
245 * first shot at trying to claim the data for decoding. If none of the | |
246 * extensions match (or the extension is NULL), then every decoder examines | |
247 * the data to determine if it can handle it, until one accepts it. | |
248 * If no decoders can handle the data, a NULL value is returned, and a human | |
249 * readable error message can be fetched from Sound_GetLastError(). | |
250 * Optionally, a desired audio format can be specified. If the incoming data | |
251 * is in a different format, SDL_sound will convert it to the desired format | |
252 * on the fly. Note that this can be an expensive operation, so it may be | |
253 * wise to convert data before you need to play it back, if possible, or | |
254 * make sure your data is initially in the format that you need it in. | |
255 * If you don't want to convert the data, you can specify NULL for a desired | |
256 * format. The incoming format of the data, preconversion, can be found | |
257 * in the Sound_Sample structure. | |
258 * Note that the raw sound data "decoder" needs you to specify both the | |
259 * extension "RAWDATA" and a "desired" format, or it will refuse to handle | |
260 * the data. | |
261 * Finally, specify an initial buffer size; this is the number of bytes that | |
262 * will be allocated to store each read from the sound buffer. The more you | |
263 * can safely allocate, the more decoding can be done in one block, but the | |
264 * more resources you have to use up, and the longer each decoding call will | |
265 * take. Note that different data formats require more or less space to | |
266 * store. This buffer can be resized via Sound_SetBufferSize() ... | |
267 * | |
268 * When you are done with this Sound_Sample pointer, you can dispose of it | |
269 * via Sound_FreeSample(). | |
270 * | |
271 * @param rw SDL_RWops with sound data. | |
272 * @param ext File extension normally associated with a data format. | |
273 * Can usually be NULL. | |
274 * @param desired Format to convert sound data into. Can usually be NULL, | |
275 * if you don't need conversion. | |
276 * @return Sound_Sample pointer, which is used as a handle to several other | |
277 * SDL_sound APIs. NULL on error. If error, use | |
278 * Sound_GetLastError() to see what went wrong. | |
279 */ | |
280 extern DECLSPEC Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext, | |
281 SDL_AudioInfo *desired, | |
282 Uint32 bufferSize); | |
283 | |
284 /** | |
285 * Dispose of a Sound_Sample pointer that was returned from Sound_NewSample(). | |
286 * This will also close/dispose of the SDL_RWops that was used at creation | |
287 * time, so there's no need to keep a reference to that around. | |
288 * The Sound_Sample pointer is invalid after this call, and will almost | |
289 * certainly result in a crash if you attempt to keep using it. | |
290 * | |
291 * @param sample The Sound_Sample to delete. | |
292 */ | |
293 extern DECLSPEC void Sound_FreeSample(Sound_Sample *sample); | |
294 | |
295 | |
296 /** | |
297 * Decode more of the sound data in a Sound_Sample. It will decode at most | |
298 * sample->buffer_size bytes into sample->buffer in the desired format, and | |
299 * return the number of decoded bytes. | |
300 * If sample->buffer_size bytes could not be decoded, then please refer to | |
301 * sample->flags to determine if this was an End-of-stream or error condition. | |
302 * | |
303 * @param sample Do more decoding to this Sound_Sample. | |
304 * @return number of bytes decoded into sample->buffer. If it is less than | |
305 * sample->buffer_size, then you should check sample->flags to see | |
306 * what the current state of the sample is (EOF, error, read again). | |
307 */ | |
308 extern DECLSPEC Uint32 Sound_Decode(Sound_Sample *sample); | |
309 | |
310 | |
311 /** | |
312 * Decode the remainder of the sound data in a Sound_Sample. This will | |
313 * dynamically allocate memory for the ENTIRE remaining sample. | |
314 * sample->buffer_size and sample->buffer will be updated to reflect the | |
315 * new buffer. Please refer to sample->flags to determine if the decoding | |
316 * finished due to an End-of-stream or error condition. | |
317 * | |
318 * Be aware that sound data can take a large amount of memory, and that | |
319 * this function may block for quite awhile while processing. Also note | |
320 * that a streaming source (for example, from a SDL_RWops that is getting | |
321 * fed from an Internet radio feed that doesn't end) may fill all available | |
322 * memory before giving up...be sure to use this on finite sound sources | |
323 * only! | |
324 * | |
325 * @param sample Do all decoding for this Sound_Sample. | |
326 * @return number of bytes decoded into sample->buffer. If it is less than | |
327 * sample->buffer_size, then you should check sample->flags to see | |
328 * what the current state of the sample is (EOF, error, read again). | |
329 */ | |
330 extern DECLSPEC Uint32 Sound_DecodeAll(Sound_Sample *sample); | |
331 | |
332 #ifdef __cplusplus | |
333 } | |
334 #endif | |
335 | |
1
508aac690b19
Whoops...changed some overlooked "voice" to "sound".
Ryan C. Gordon <icculus@icculus.org>
parents:
0
diff
changeset
|
336 #endif /* !defined _INCLUDE_SDL_SOUND_H_ */ |
0 | 337 |
338 /* end of SDL_sound.h ... */ | |
339 |