SDL Wiki
(This is the documentation for SDL3, which is under heavy development and the API is changing! SDL2 is the current stable version!)


Initialize an SDL_AudioCVT structure for conversion.


int SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
                      SDL_AudioFormat src_format,
                      Uint8 src_channels,
                      int src_rate,
                      SDL_AudioFormat dst_format,
                      Uint8 dst_channels,
                      int dst_rate);

Function Parameters


an SDL_AudioCVT structure filled in with audio conversion information


the source format of the audio data; for more info see SDL_AudioFormat


the number of channels in the source


the frequency (sample-frames-per-second) of the source


the destination format of the audio data; for more info see SDL_AudioFormat


the number of channels in the destination


the frequency (sample-frames-per-second) of the destination

Return Value

Returns 1 if the audio filter is prepared, 0 if no conversion is needed, or a negative error code on failure; call SDL_GetError() for more information.


Before an SDL_AudioCVT structure can be used to convert audio data it must be initialized with source and destination information.

This function will zero out every field of the SDL_AudioCVT, so it must be called before the application fills in the final buffer information.

Once this function has returned successfully, and reported that a conversion is necessary, the application fills in the rest of the fields in SDL_AudioCVT, now that it knows how large a buffer it needs to allocate, and then can call SDL_ConvertAudio() to complete the conversion.


This function is available since SDL 3.0.0.

Code Examples

// Change 1024 stereo sample frames at 48000Hz from float32 to int16.
SDL_AudioCVT cvt;
SDL_BuildAudioCVT(&cvt, AUDIO_F32, 2, 48000, AUDIO_S16, 2, 48000);
SDL_assert(cvt.needed); // obviously, this one is always needed.
cvt.len = 1024 * 2 * 4;  // 1024 stereo float32 sample frames.
cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult);
// read your float32 data into cvt.buf here.
// cvt.buf has cvt.len_cvt bytes of converted data now.

CategoryAPI, CategoryAudio

[ edit | delete | history | feedback | raw ]

[ front page | index | search | recent changes | git repo | offline html ]

All wiki content is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
Wiki powered by ghwikipp.