comparison SDL_sound.h @ 3:fd6cd0e04e6f

First stab at implementation complete.
author Ryan C. Gordon <icculus@icculus.org>
date Mon, 17 Sep 2001 18:10:16 +0000
parents 508aac690b19
children 5ff1dce70aec
comparison
equal deleted inserted replaced
2:f58a664c1557 3:fd6cd0e04e6f
22 * into this library, and SDL_sound will take that data, in one of several 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 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 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 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 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 27 * callback (or whatever). Since it gets data from an SDL_RWops, you can get
28 * the initial sound data from any number of sources: file, memory buffer, 28 * the initial sound data from any number of sources: file, memory buffer,
29 * network connection, etc. 29 * network connection, etc.
30 * 30 *
31 * As the name implies, this library depends on SDL: Simple Directmedia Layer, 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 32 * which is a powerful, free, and cross-platform multimedia library. It can
33 * be found at http://www.libsdl.org/ 33 * be found at http://www.libsdl.org/
34 * 34 *
35 * Support is in place or planned for the following sound formats: 35 * Support is in place or planned for the following sound formats:
36 * - .WAV (Microsoft WAVfile RIFF data, internal.) 36 * - .WAV (Microsoft WAVfile RIFF data, internal.)
37 * - .VOC (Creative Labs' Voice format, internal.) 37 * - .VOC (Creative Labs' Voice format, internal.)
38 * - .MP3 (MPEG-1 layer 3 support, via the SMPEG library.) 38 * - .MP3 (MPEG-1 layer 3 support, via the SMPEG library.)
39 * - .MID (MIDI music converted to Waveform data, via Timidity.) 39 * - .MID (MIDI music converted to Waveform data, via Timidity.)
40 * - .MOD (MOD files, via MikMod.) 40 * - .MOD (MOD files, via MikMod.)
41 * - .OGG (Ogg files, via Ogg Vorbis libraries.) 41 * - .OGG (Ogg files, via Ogg Vorbis libraries.)
42 * - .RAWDATA (Raw sound data in any format, internal.) 42 * - .RAW (Raw sound data in any format, internal.)
43 * - .CDA (CD audio read into a sound buffer, internal.) 43 * - .CDA (CD audio read into a sound buffer, internal.)
44 * - .AU 44 * - .AU
45 * - .AIFF 45 * - .AIFF
46 * 46 *
47 * (...and more to come...) 47 * (...and more to come...)
48 * 48 *
62 62
63 /* 63 /*
64 * These are flags that are used in a Sound_Sample (below) to show various 64 * These are flags that are used in a Sound_Sample (below) to show various
65 * states. 65 * states.
66 * 66 *
67 * To use: "if (sample->flags & SOUND_SAMPLEFLAGS_ERROR) { dosomething(); }" 67 * To use: "if (sample->flags & SOUND_SAMPLEFLAG_ERROR) { dosomething(); }"
68 */ 68 */
69 typedef enum __SOUND_SAMPLEFLAGS__ 69 typedef enum __SOUND_SAMPLEFLAGS__
70 { 70 {
71 SOUND_SAMPLEFLAG_NONE = 0, /* Null flag. */ 71 SOUND_SAMPLEFLAG_NONE = 0, /* Null flag. */
72 72
73 /* these are set at sample creation time... */ 73 /* these are set at sample creation time... */
74 SOUND_SAMPLEFLAG_NEEDSEEK = 1, /* SDL_RWops must be able to seek. */ 74 SOUND_SAMPLEFLAG_NEEDSEEK = 1, /* SDL_RWops must be able to seek. */
75 SOUND_SAMPLEFLAG_STREAMING = 1 << 1, /* source is streaming (no EOF). */
76 75
77 /* these are set during decoding... */ 76 /* these are set during decoding... */
78 SOUND_SAMPLEFLAG_EOF = 1 << 29, /* end of input stream. */ 77 SOUND_SAMPLEFLAG_EOF = 1 << 29, /* end of input stream. */
79 SOUND_SAMPLEFLAG_ERROR = 1 << 30, /* unrecoverable error. */ 78 SOUND_SAMPLEFLAG_ERROR = 1 << 30, /* unrecoverable error. */
80 SOUND_SAMPLEFLAG_AGAIN = 1 << 31 /* couldn't read without blocking. */ 79 SOUND_SAMPLEFLAG_EAGAIN = 1 << 31 /* would block, or temp error. */
81 } Sound_SampleFlags; 80 } Sound_SampleFlags;
81
82
83 /*
84 * Basics of a decoded sample's data structure: data format (see AUDIO_U8 and
85 * friends in SDL_audio.h), number of channels, and sample rate. If you need
86 * more explanation than that, you should stop developing sound code right
87 * now.
88 */
89 typedef struct __SOUND_AUDIOINFO__
90 {
91 Uint16 format;
92 Uint8 channels;
93 Uint32 rate;
94 } Sound_AudioInfo;
95
96
97 /*
98 * Each decoder sets up one of these structs, which can be retrieved via
99 * the Sound_AvailableDecoders() function. EVERY FIELD IN THIS IS READ-ONLY.
100 */
101 typedef struct __SOUND_DECODERINFO__
102 {
103 const char *extension; /* standard file extension. "MP3", "WAV"... */
104 const char *description; /* Human readable description of decoder. */
105 const char *author; /* "Name Of Author <email@emailhost.dom>" */
106 const char *url; /* URL specific to this decoder. */
107 } Sound_DecoderInfo;
108
82 109
83 110
84 /* 111 /*
85 * The Sound_Sample structure is the heart of SDL_sound. This holds 112 * 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. 113 * information about a source of sound data as it is being decoded.
88 * change them. 115 * change them.
89 */ 116 */
90 typedef struct __SOUND_SAMPLE__ 117 typedef struct __SOUND_SAMPLE__
91 { 118 {
92 void *opaque; /* Internal use only. */ 119 void *opaque; /* Internal use only. */
93 Sound_DecoderInfo *decoder; /* Decoder used for this sample. */ 120 const Sound_DecoderInfo *decoder; /* Decoder used for this sample. */
94 SDL_AudioSpec desired; /* Desired audio format for conversion. */ 121 Sound_AudioInfo desired; /* Desired audio format for conversion. */
95 SDL_AudioSpec actual; /* Actual audio format of sample. */ 122 Sound_AudioInfo actual; /* Actual audio format of sample. */
96 void *buffer; /* Decoded sound data lands in here. */ 123 void *buffer; /* Decoded sound data lands in here. */
97 Uint32 buffer_size; /* Current size of (buffer), in bytes. */ 124 Uint32 buffer_size; /* Current size of (buffer), in bytes. */
98 Sound_SampleFlags flags; /* Flags relating to this sample. */ 125 Sound_SampleFlags flags; /* Flags relating to this sample. */
99 } Sound_Sample; 126 } 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 127
114 128
115 /* 129 /*
116 * Just what it says: a x.y.z style version number... 130 * Just what it says: a x.y.z style version number...
117 */ 131 */
177 * sound sources, and frees any resources in use by SDL_sound. 191 * sound sources, and frees any resources in use by SDL_sound.
178 * 192 *
179 * All Sound_Sample pointers you had prior to this call are INVALIDATED. 193 * All Sound_Sample pointers you had prior to this call are INVALIDATED.
180 * 194 *
181 * Once successfully deinitialized, Sound_Init() can be called again to 195 * Once successfully deinitialized, Sound_Init() can be called again to
182 * restart the subsystem. All defaults API states are restored at this 196 * restart the subsystem. All default API states are restored at this
183 * point. 197 * point.
184 * 198 *
185 * You should call this BEFORE SDL_Quit(). This will NOT call SDL_Quit() 199 * You should call this BEFORE SDL_Quit(). This will NOT call SDL_Quit()
186 * for you! 200 * for you!
187 * 201 *
230 * with that thread. It is safe to call this function at anytime, even 244 * with that thread. It is safe to call this function at anytime, even
231 * before Sound_Init(). 245 * before Sound_Init().
232 * 246 *
233 * @return READ ONLY string of last error message. 247 * @return READ ONLY string of last error message.
234 */ 248 */
235 extern DECLSPEC const char *Sound_GetLastError(void); 249 extern DECLSPEC const char *Sound_GetError(void);
250
251
252 /**
253 * Clear the current error message, so the next call to Sound_GetError() will
254 * return NULL.
255 */
256 extern DECLSPEC void Sound_ClearError(void);
236 257
237 258
238 /** 259 /**
239 * Start decoding a new sound sample. The data is read via an SDL_RWops 260 * 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 261 * 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 262 * coming from memory, disk, network stream, etc. The (ext) parameter is
242 * merely a hint to determining the correct decoder; if you specify, for 263 * 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 264 * example, "mp3" for an extension, and one of the decoders lists that
244 * (case sensitive) as a handled extension, then that decoder is given 265 * as a handled extension, then that decoder is given first shot at trying
245 * first shot at trying to claim the data for decoding. If none of the 266 * to claim the data for decoding. If none of the extensions match (or the
246 * extensions match (or the extension is NULL), then every decoder examines 267 * extension is NULL), then every decoder examines the data to determine if
247 * the data to determine if it can handle it, until one accepts it. 268 * it can handle it, until one accepts it. In such a case your SDL_RWops will
269 * need to be capable of rewinding to the start of the stream.
248 * If no decoders can handle the data, a NULL value is returned, and a human 270 * 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(). 271 * readable error message can be fetched from Sound_GetLastError().
250 * Optionally, a desired audio format can be specified. If the incoming data 272 * 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 273 * 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 274 * on the fly. Note that this can be an expensive operation, so it may be
254 * make sure your data is initially in the format that you need it in. 276 * 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 277 * 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 278 * format. The incoming format of the data, preconversion, can be found
257 * in the Sound_Sample structure. 279 * in the Sound_Sample structure.
258 * Note that the raw sound data "decoder" needs you to specify both the 280 * 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 281 * extension "RAW" and a "desired" format, or it will refuse to handle
260 * the data. 282 * the data. This is to prevent it from catching all formats unsupported
283 * by the other decoders.
261 * Finally, specify an initial buffer size; this is the number of bytes that 284 * 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 285 * 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 286 * 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 287 * 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 288 * take. Note that different data formats require more or less space to
266 * store. This buffer can be resized via Sound_SetBufferSize() ... 289 * store. This buffer can be resized via Sound_SetBufferSize() ...
267 * 290 * The buffer size specified must be a multiple of the size of a single
291 * sample (not to be confused with a single Sound_Sample). So, if you want
292 * 16-bit, stereo samples, then your sample size is (2 channels * 16 bits),
293 * or 32 bits per sample, which is four bytes. In such a case, you could
294 * specify 128 or 132 bytes for a buffer, but not 129, 130, or 131, although
295 * in reality, you'll want to specify a MUCH larger buffer.
268 * When you are done with this Sound_Sample pointer, you can dispose of it 296 * When you are done with this Sound_Sample pointer, you can dispose of it
269 * via Sound_FreeSample(). 297 * via Sound_FreeSample().
298 * You do not have to keep a reference to (rw) around. If this function
299 * suceeds, it stores (rw) internally (and disposes of it during the call
300 * to Sound_FreeSample()). If this function fails, it will dispose of the
301 * SDL_RWops for you.
270 * 302 *
271 * @param rw SDL_RWops with sound data. 303 * @param rw SDL_RWops with sound data.
272 * @param ext File extension normally associated with a data format. 304 * @param ext File extension normally associated with a data format.
273 * Can usually be NULL. 305 * Can usually be NULL.
274 * @param desired Format to convert sound data into. Can usually be NULL, 306 * @param desired Format to convert sound data into. Can usually be NULL,
276 * @return Sound_Sample pointer, which is used as a handle to several other 308 * @return Sound_Sample pointer, which is used as a handle to several other
277 * SDL_sound APIs. NULL on error. If error, use 309 * SDL_sound APIs. NULL on error. If error, use
278 * Sound_GetLastError() to see what went wrong. 310 * Sound_GetLastError() to see what went wrong.
279 */ 311 */
280 extern DECLSPEC Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext, 312 extern DECLSPEC Sound_Sample *Sound_NewSample(SDL_RWops *rw, const char *ext,
281 SDL_AudioInfo *desired, 313 Sound_AudioInfo *desired,
282 Uint32 bufferSize); 314 Uint32 bufferSize);
315
316 /**
317 * This is identical to Sound_NewSample(), but it creates an SDL_RWops for you
318 * from the file located in (filename). Note that (filename) is specified in
319 * platform-dependent notation. ("C:\\music\\mysong.mp3" on windows, and
320 * "/home/icculus/music/mysong.mp3" or whatever on Unix, etc.)
321 * Sound_NewSample()'s "ext" parameter is gleaned from the contents of
322 * (filename).
323 *
324 * @param filename file containing sound data.
325 * @param desired Format to convert sound data into. Can usually be NULL,
326 * if you don't need conversion.
327 * @param bufferSize size, in bytes, of initial read buffer.
328 * @return Sound_Sample pointer, which is used as a handle to several other
329 * SDL_sound APIs. NULL on error. If error, use
330 * Sound_GetLastError() to see what went wrong.
331 */
332 extern DECLSPEC Sound_Sample *Sound_NewSampleFromFile(const char *filename,
333 Sound_AudioInfo *desired,
334 Uint32 bufferSize);
283 335
284 /** 336 /**
285 * Dispose of a Sound_Sample pointer that was returned from Sound_NewSample(). 337 * 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 338 * 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. 339 * time, so there's no need to keep a reference to that around.
289 * certainly result in a crash if you attempt to keep using it. 341 * certainly result in a crash if you attempt to keep using it.
290 * 342 *
291 * @param sample The Sound_Sample to delete. 343 * @param sample The Sound_Sample to delete.
292 */ 344 */
293 extern DECLSPEC void Sound_FreeSample(Sound_Sample *sample); 345 extern DECLSPEC void Sound_FreeSample(Sound_Sample *sample);
346
347
348 /**
349 * Change the current buffer size for a sample. If the buffer size could
350 * be changed, then the sample->buffer and sample->buffer_size fields will
351 * reflect that. If they could not be changed, then your original sample
352 * state is preserved. If the buffer is shrinking, the data at the end of
353 * buffer is truncated. If the buffer is growing, the contents of the new
354 * space at the end is undefined until you decode more into it or initialize
355 * it yourself.
356 *
357 * The buffer size specified must be a multiple of the size of a single
358 * sample (not to be confused with a single Sound_Sample). So, if you want
359 * 16-bit, stereo samples, then your sample size is (2 channels * 16 bits),
360 * or 32 bits per sample, which is four bytes. In such a case, you could
361 * specify 128 or 132 bytes for a buffer, but not 129, 130, or 131, although
362 * in reality, you'll want to specify a MUCH larger buffer.
363 *
364 * @param sample The Sound_Sample whose buffer to modify.
365 * @param new_size The desired size, in bytes, of the new buffer.
366 * @return non-zero if buffer size changed, zero on failure.
367 */
368 extern DECLSPEC int Sound_SetBufferSize(Sound_Sample *sample, Uint32 new_size);
294 369
295 370
296 /** 371 /**
297 * Decode more of the sound data in a Sound_Sample. It will decode at most 372 * 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 373 * sample->buffer_size bytes into sample->buffer in the desired format, and