SDL School

API reference / Audio

SDL_LoadWAV

Function, SDL3

Loads a WAV file into memory.

Declaration

#include <SDL3/SDL.h>

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

Parameters

pathThe path of the file.
specFilled with the format of the sound.
audio_bufThe address of a pointer. SDL writes the address of the samples there.
audio_lenThe address of a value for the size of the samples in bytes.

Return value

True on success. On failure it returns false, and SDL_GetError gives the reason.

Example

Play a sound
if (!SDL_LoadWAV(path, &spec, &sound->data, &sound->length)) {
    SDL_Log("SDL_LoadWAV failed: %s", SDL_GetError());
    return false;
}

See this code run in Play a sound. Download the full program (.zip)

Notes

In SDL2

The name is the same, but it returns a pointer, or NULL on failure. Free the samples with SDL_FreeWAV.

See also