|
Size: 6029
Comment: update content - pointers, structs
|
← Revision 40 as of 2019-05-15 14:14:25 ⇥
Size: 4605
Comment: Removed misleading "clarity" note and ancient sampling rates games haven't used for 20 years.
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 2: | Line 2: |
| #pragma disable-camelcase ||<tablewidth="100%" style="color: #FF0000;" :> DRAFT|| |
#pragma camelcase off |
| Line 7: | Line 5: |
| A structure that contains the audio output format. It ^also^ contains a callback that is called when the audio device needs more data. | A structure that contains the audio output format. It also contains a callback that is called when the audio device needs more data. |
| Line 12: | Line 10: |
| <<Color2(green,Can '''channels''' only be 1 or 2 or can it handle more? Old wiki lists 4 for surround and 6 for surround w/ center and lfe (sub?). Should it be 1=mono instead of just 1 mono etc.?)>> | |
| Line 15: | Line 12: |
| ||Uint8||'''channels'''||number of channels: 1 mono, 2 stereo; see [[#Remarks|Remarks]] for details|| | ||Uint8||'''channels'''||number of separate sound channels: see [[#Remarks|Remarks]] for details|| |
| Line 18: | Line 15: |
| ||Uint16||'''padding'''||necessary for some compile environments|| | |
| Line 20: | Line 16: |
| ||SDL_!AudioCallback||'''callback'''||a function that is called when the audio device needs more data; see [[#callback|Remarks]] for details|| ||void*||'''userdata'''||a pointer that is passed as the first parameter to '''callback'''|| <<Color2(green,Seems like '''userdata''' should be an exception to the 'pointer rule'.)>> |
||SDL_!AudioCallback||'''callback'''||the function to call when the audio device needs more data; see [[#callback|Remarks]] for details|| ||void*||'''userdata'''||a pointer that is passed to '''callback''' (otherwise ignored by SDL)|| |
| Line 27: | Line 21: |
| You can add your code example here | SDL_AudioSpec want, have; SDL_AudioDeviceID dev; SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */ want.freq = 48000; want.format = AUDIO_F32; want.channels = 2; want.samples = 4096; want.callback = MyAudioCallback; // you wrote this function elsewhere. dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE); |
| Line 31: | Line 34: |
| <<Color2(green,Should the functions [[SDL_LoadWAV_RW]] and [[SDL_OpenAudioDevice]] be added into this remark? Do they behave like their similarly named counterparts with respect to this struct?)>> | This structure is used by [[SDL_OpenAudioDevice]]() and [[SDL_LoadWAV]](). While all fields are used by [[SDL_OpenAudioDevice]](), only '''freq''', '''format''', '''channels''', and '''samples''' are used by [[SDL_LoadWAV]](). |
| Line 33: | Line 36: |
| *<<BR>>The [[SDL_AudioSpec]] structure is used to describe the format of some audio data. This structure is used by [[SDL_OpenAudio]]() and [[SDL_LoadWAV]](). While all fields are used by [[SDL_OpenAudio]](), only '''freq''', '''format''', '''channels''', and '''samples''' are used by [[SDL_LoadWAV]](). ,,We will detail these common members here:,, | '''freq''' specifies the number of sample frames sent to the sound device per second. The Nyquist Theorem states that the audio sampling frequency must be exactly twice the highest frequency represented in the audio. Humans can hear up to slightly under 20kHz, declining to 16kHz or lower as we age. Standard CD quality audio uses 44100. DVDs and the [[https://opus-codec.org/|Opus audio codec]] use 48000. Values higher than 48000 generally should not be used for playback purposes because they use more memory, use more CPU, and can cause other problems as explained in [[https://people.xiph.org/~xiphmont/demo/neil-young.html|this blog post by Chris Montgomery of Xiph]]. |
| Line 35: | Line 38: |
| '''freq''' ^specifies^ the number of samples sent to the sound device ,,every,, ^per^ second. Common values are 11025, 22050 and 44100. The higher the better. <<Color2(green,I think this info could be moved to Params above and this paragraph removed.)>><<Color2(green,I think "The higher the better." implies that the programmer has a choice and should choose the higher number. I suspect that the value is actually dictated by the audio data. Perhaps rewording to say "Higher values produce higher quality sounds/audio output" might be preferable?)>> | '''format''' specifies the size and type of each sample element and may be one of the following: <<Include(SDL_AudioFormat, , , from="=== Audio Format Values ===", to="== Code Examples ==")>> |
| Line 37: | Line 41: |
| '''format''' specifies the size and type of each sample element. ,,Values it can take are:,, ^Audio format flags may be one of the following values:^ ||||''Default,,s,, to LSB byte order'' -or- ''Basic'' ???|| ||AUDIO_U8||unsigned 8-bit samples.|| ||AUDIO_S8||signed 8-bit samples.|| ||AUDIO_U16 or AUDIO_U16LSB||not supported by all hardware (unsigned 16-bit little-endian)|| ||AUDIO_S16 or AUDIO_S16LSB||not supported by all hardware (signed 16-bit little-endian)|| ||AUDIO_U16MSB||not supported by all hardware (unsigned 16-bit big-endian)|| ||AUDIO_S16MSB||not supported by all hardware (signed 16-bit big-endian)|| ||||''int32 support''|| ||AUDIO_S32 or AUDIO_S32LSB||32-bit integer samples|| ||AUDIO_S32MSB||as above, but big-endian byte order|| ||||''float32 support''|| ||AUDIO_F32 or AUDIO_F32LSB||32-bit floating point samples|| ||AUDIO_F32MSB||as above, but big-endian byte order|| ||||''Native audio byte ordering''|| ||AUDIO_U16SYS||either AUDIO_U16LSB or AUDIO_U16MSB depending on hardware CPU endianness|| ||AUDIO_S16SYS||either AUDIO_S16LSB or AUDIO_S16MSB depending on hardware CPU endianness|| ||AUDIO_S32SYS||either AUDIO_S32LSB or AUDIO_S32MSB depending on hardware CPU endianness|| ||AUDIO_F32SYS||either AUDIO_F32LSB or AUDIO_F32MSB depending on hardware CPU endianness|| |
See [[SDL_AudioFormat]] for more info. |
| Line 57: | Line 43: |
| '''channels''' specifies the number of output channels. As of SDL 2.0, supported values are 1 (mono), 2 (stereo), 4 (quad), and 6 (5.1). | |
| Line 58: | Line 45: |
| <<Color2(green,This list was taken from the header. A portion was listed in the old wiki as well. Is it correct that audio change flags and audio flags (as opposed to the above audio format flags) should not be included here?)>> | '''samples''' specifies a unit of audio data. When used with [[SDL_OpenAudioDevice]]() this refers to the size of the audio buffer in sample frames. A sample frame is a chunk of audio data of the size specified in '''format''' multiplied by the number of channels. When the [[SDL_AudioSpec]] is used with [[SDL_LoadWAV]]() '''samples''' is set to 4096. This field's value must be a power of two. |
| Line 60: | Line 47: |
| '''channels''' ^specifies^ the number of separate sound channels. 1 is mono (single channel), 2 is stereo (dual channel). <<Color2(green,I think this paragraph is redundant with Params above and can be removed.)>> | The values '''silence''' and '''size''' are calculated by [[SDL_OpenAudioDevice]](). |
| Line 62: | Line 49: |
| '''samples''' ^specifies a unit of audio data.^ When used with [[SDL_OpenAudio]]() this refers to the size of the audio buffer in samples. A sample is a chunk of audio data of the size specified in '''format''' multiplied by the number of channels. When the [[SDL_AudioSpec]] is used with [[SDL_LoadWAV]]() '''samples''' is set to 4096. <<Color2(green,Params above specifies that this is a "power of 2". Would it be appropriate to show how to determine/calculate this value here?)>> <<BR>>* |
Channel data is interleaved. Stereo samples are stored in left/right ordering. Quad is stored in front-left/front-right/rear-left/rear-right order. 5.1 is stored in front-left/front-right/center/low-freq/rear-left/rear-right ordering ("low-freq" is the ".1" speaker). |
| Line 65: | Line 51: |
| The ,,calculated,, values '''silence''' and '''size''' are calculated by [[SDL_OpenAudio]](). | <<Anchor(callback)>> The function prototype for '''callback''' is: {{{#!highlight cpp void SDL_AudioCallback(void* userdata, Uint8* stream, int len) }}} . where its parameters are: ||`userdata`||an application-specific parameter saved in the [[SDL_AudioSpec]] structure's '''userdata''' field|| ||`stream`||a pointer to the audio data buffer filled in by SDL_!AudioCallback()|| ||`len`||the length of that buffer in bytes|| |
| Line 67: | Line 63: |
| Once the callback returns, the buffer will no longer be valid. | |
| Line 69: | Line 66: |
| <<Color2(green,No page for SDL_Audio``Callback right? Is this sufficient?)>> <<Anchor(callback)>> '''callback''' is of the form: {{{#!highlight cpp ##alt 1st line from header rev 5387? typedef void (SDLCALL * SDL_AudioCallback)(void* userdata, ##w/ wiki formatting adjustments? void* SDL_AudioCallback(void* userdata, ##original content below void callback(void* userdata, Uint8* stream, int len); }}} ^with the following parameters:^ ||'''userdata'''||an application-specific parameter saved in the [[SDL_AudioSpec]] structure|| ||'''stream'''||a pointer to the audio data buffer to be filled in by the callback|| ||'''len'''||the length of that buffer in bytes|| |
The callback ''must'' completely initialize the buffer; as of SDL 2.0, this buffer is not initialized before the callback is called. If there is nothing to play, the callback should fill the buffer with silence. |
| Line 86: | Line 68: |
| Once the callback returns, the buffer will no longer be valid. | With SDL >= 2.0.4 you can choose to avoid callbacks and use [[SDL_QueueAudio]]() instead, if you like. Just open your audio device with a NULL callback. |
| Line 89: | Line 71: |
| .[[SDL_LoadWAV]] * | .[[SDL_LoadWAV]] |
| Line 91: | Line 73: |
| .[[SDL_OpenAudio]] |
SDL_AudioSpec
A structure that contains the audio output format. It also contains a callback that is called when the audio device needs more data.
Data Fields
int |
freq |
DSP frequency (samples per second); see Remarks for details |
format |
audio data format; see Remarks for details |
|
Uint8 |
channels |
number of separate sound channels: see Remarks for details |
Uint8 |
silence |
audio buffer silence value (calculated) |
Uint16 |
samples |
audio buffer size in samples (power of 2); see Remarks for details |
Uint32 |
size |
audio buffer size in bytes (calculated) |
SDL_AudioCallback |
callback |
the function to call when the audio device needs more data; see Remarks for details |
void* |
userdata |
a pointer that is passed to callback (otherwise ignored by SDL) |
Code Examples
SDL_AudioSpec want, have;
SDL_AudioDeviceID dev;
SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
want.freq = 48000;
want.format = AUDIO_F32;
want.channels = 2;
want.samples = 4096;
want.callback = MyAudioCallback; // you wrote this function elsewhere.
dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
Remarks
This structure is used by SDL_OpenAudioDevice() and SDL_LoadWAV(). While all fields are used by SDL_OpenAudioDevice(), only freq, format, channels, and samples are used by SDL_LoadWAV().
freq specifies the number of sample frames sent to the sound device per second. The Nyquist Theorem states that the audio sampling frequency must be exactly twice the highest frequency represented in the audio. Humans can hear up to slightly under 20kHz, declining to 16kHz or lower as we age. Standard CD quality audio uses 44100. DVDs and the Opus audio codec use 48000. Values higher than 48000 generally should not be used for playback purposes because they use more memory, use more CPU, and can cause other problems as explained in this blog post by Chris Montgomery of Xiph.
format specifies the size and type of each sample element and may be one of the following:
8-bit support |
|
AUDIO_S8 |
signed 8-bit samples |
AUDIO_U8 |
unsigned 8-bit samples |
16-bit support |
|
AUDIO_S16LSB |
signed 16-bit samples in little-endian byte order |
AUDIO_S16MSB |
signed 16-bit samples in big-endian byte order |
AUDIO_S16SYS |
signed 16-bit samples in native byte order |
AUDIO_S16 |
AUDIO_S16LSB |
AUDIO_U16LSB |
unsigned 16-bit samples in little-endian byte order |
AUDIO_U16MSB |
unsigned 16-bit samples in big-endian byte order |
AUDIO_U16SYS |
unsigned 16-bit samples in native byte order |
AUDIO_U16 |
AUDIO_U16LSB |
32-bit support (new to SDL 2.0) |
|
AUDIO_S32LSB |
32-bit integer samples in little-endian byte order |
AUDIO_S32MSB |
32-bit integer samples in big-endian byte order |
AUDIO_S32SYS |
32-bit integer samples in native byte order |
AUDIO_S32 |
AUDIO_S32LSB |
float support (new to SDL 2.0) |
|
AUDIO_F32LSB |
32-bit floating point samples in little-endian byte order |
AUDIO_F32MSB |
32-bit floating point samples in big-endian byte order |
AUDIO_F32SYS |
32-bit floating point samples in native byte order |
AUDIO_F32 |
AUDIO_F32LSB |
See SDL_AudioFormat for more info.
channels specifies the number of output channels. As of SDL 2.0, supported values are 1 (mono), 2 (stereo), 4 (quad), and 6 (5.1).
samples specifies a unit of audio data. When used with SDL_OpenAudioDevice() this refers to the size of the audio buffer in sample frames. A sample frame is a chunk of audio data of the size specified in format multiplied by the number of channels. When the SDL_AudioSpec is used with SDL_LoadWAV() samples is set to 4096. This field's value must be a power of two.
The values silence and size are calculated by SDL_OpenAudioDevice().
Channel data is interleaved. Stereo samples are stored in left/right ordering. Quad is stored in front-left/front-right/rear-left/rear-right order. 5.1 is stored in front-left/front-right/center/low-freq/rear-left/rear-right ordering ("low-freq" is the ".1" speaker).
The function prototype for callback is:
void SDL_AudioCallback(void* userdata,
Uint8* stream,
int len)
- where its parameters are:
userdata
an application-specific parameter saved in the SDL_AudioSpec structure's userdata field
stream
a pointer to the audio data buffer filled in by SDL_AudioCallback()
len
the length of that buffer in bytes
Once the callback returns, the buffer will no longer be valid. Stereo samples are stored in a LRLRLR ordering.
The callback must completely initialize the buffer; as of SDL 2.0, this buffer is not initialized before the callback is called. If there is nothing to play, the callback should fill the buffer with silence.
With SDL >= 2.0.4 you can choose to avoid callbacks and use SDL_QueueAudio() instead, if you like. Just open your audio device with a NULL callback.
