SDL_ConvertAudio(3)

πŸ‘‘ NAME πŸ‘‘

SDL_ConvertAudio β€” converts your audio data into the audio format you actually want. πŸ’…

okay so real quick, bestie, this page lives in the SDL API Reference, section 3, and it's literally just the glow-up step for your audio bytes. let's get into it 🫢


✨ SYNOPSIS ✨

#include "SDL.h"

int SDL_ConvertAudio(SDL_AudioCVT *cvt);

πŸ’– DESCRIPTION πŸ’–

SDL_ConvertAudio takes exactly one parameter, cvt, and that baby has to be previously initialized or we're not doing anything. no cap.

And initializing an SDL_AudioCVT is a whole two step process, so pay attention bestie:

  1. First, the structure gets passed to SDL_BuildAudioCVT along with the source and destination format parameters. that's the setup era. 🌸
  2. Then, the cvt->buf and cvt->len fields have to be set up. cvt->buf should point at the audio data, and cvt->len gets set to the length of that audio data in bytes.
🚨 okay this part matters: the length of the buffer that buf points to should be len*len_mult bytes long. do not skip this. not me forgetting len_mult and ruining my whole afternoon 😭

Once the SDL_AudioCVT structure is initialized, we can pass it into SDL_ConvertAudio, and it will convert the audio data pointed to by cvt->buf. iconic behavior tbh.

Now the receipts:

  • if SDL_ConvertAudio returned 0, the conversion was completed successfully. slay. ✨
  • otherwise, -1 is returned. πŸ™ˆ

If the conversion completed successfully, the converted audio data can be read straight from cvt->buf. and the amount of valid, converted audio data sitting in that buffer is equal to cvt->len*cvt->len_ratio. yes, you do the little multiplication. πŸ’…


πŸŽ€ EXAMPLES πŸŽ€

here's the whole vibe, a WAV file getting converted to hardware format:

/* Converting some WAV data to hardware format */
void my_audio_callback(void *userdata, Uint8 *stream, int len);

SDL_AudioSpec *desired, *obtained;
SDL_AudioSpec wav_spec;
SDL_AudioCVT  wav_cvt;
Uint32 wav_len;
Uint8 *wav_buf;
int ret;

/* Allocated audio specs */
desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));

/* Set desired format */
desired->freq=22050;
desired->format=AUDIO_S16LSB;
desired->samples=8192;
desired->callback=my_audio_callback;
desired->userdata=NULL;

/* Open the audio device */
if ( SDL_OpenAudio(desired, obtained) < 0 ){
  fprintf(stderr, "Couldn't open audio: %s
", SDL_GetError());
  exit(-1);
}

free(desired);

/* Load the test.wav */
if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf, &wav_len) == NULL ){
  fprintf(stderr, "Could not open test.wav: %s
", SDL_GetError());
  SDL_CloseAudio();
  free(obtained);
  exit(-1);
}

/* Build AudioCVT */
ret = SDL_BuildAudioCVT(&wav_cvt,
                        wav_spec.format, wav_spec.channels, wav_spec.freq,
                        obtained->format, obtained->channels, obtained->freq);

/* Check that the convert was built */
if(ret==-1){
  fprintf(stderr, "Couldn't build converter!
");
  SDL_CloseAudio();
  free(obtained);
  SDL_FreeWAV(wav_buf);
}

/* Setup for conversion */
wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
wav_cvt.len=wav_len;
memcpy(wav_cvt.buf, wav_buf, wav_len);

/* We can delete to original WAV data now */
SDL_FreeWAV(wav_buf);

/* And now we're ready to convert */
SDL_ConvertAudio(&wav_cvt);

/* do whatever */
.
.
.
.

πŸ¦‹ SEE ALSO πŸ¦‹

  • SDL_BuildAudioCVT β€” the one that builds the converter first, iykyk
  • SDL_AudioCVT β€” the structure itself, the main character

okay that's the whole page bestie, you ate it πŸ’–βœ¨

this was SDL β€” the SDL API Reference, dated Tue 11 Sep 2001, 22:58, filed under SDL_ConvertAudio(3). periodt. πŸ’…πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from