AudioSDL 3.2

SDL_LoadWAV

Loads a .wav file: fills in its format and gives you a buffer with the samples.

Signature

bool SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);

How it works

WAV is the one format SDL reads without extra libraries (for MP3 or OGG use SDL3_mixer or a decoder such as stb_vorbis). The returned spec is exactly what a stream needs as its input format.

The buffer is yours: free it with SDL_free when you no longer need the sound.

Parameters

pathconst char *

The file to read.

specSDL_AudioSpec *

Receives the file's format.

audio_bufUint8 **

Receives a pointer to the samples (free with SDL_free).

audio_lenUint32 *

Receives the size in bytes.

Return value

true on success, false on failure.

Example

example.cpp
1SDL_AudioSpec spec;
2Uint8* data; Uint32 length;
3if (!SDL_LoadWAV("assets/shoot.wav", &spec, &data, &length)) {
4    SDL_Log("LoadWAV: %s", SDL_GetError());
5}
6SDL_AudioStream* sfx = SDL_OpenAudioDeviceStream(
7    SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, nullptr, nullptr);
8SDL_ResumeAudioStreamDevice(sfx);
9SDL_PutAudioStreamData(sfx, data, int(length));
10// ... at shutdown:
11SDL_free(data);
Official SDL wiki