Wiki Page Content

Differences between revisions 15 and 16
Revision 15 as of 2010-09-13 07:05:10
Size: 4183
Editor: SheenaSmith
Comment: update content - standard return value
Revision 16 as of 2010-09-21 19:59:47
Size: 4147
Editor: SheenaSmith
Comment: update content - standard return value
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
Returns 0 if the conversion was completed successfully ,,,otherwise -1 is returned and,, ^or a negative error code on failure^; call [[SDL_GetError]]() for more information. Returns 0 if the conversion was completed successfully or a negative error code on failure; call [[SDL_GetError]]() for more information.

DRAFT

SDL_ConvertAudio

Use this function to convert audio data to a desired audio format.

Syntax

int SDL_ConvertAudio(SDL_AudioCVT* cvt)

Function Parameters

cvt

a pointer to an SDL_AudioCVT structure that was previously set up; see Remarks for details

Return Value

Returns 0 if the conversion was completed successfully or a negative error code on failure; call SDL_GetError() for more information.

Code Examples

/* 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 = malloc(sizeof(SDL_AudioSpec));
obtained = 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\n", 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\n", 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!\n");
  SDL_CloseAudio();
  free(obtained);
  SDL_FreeWAV(wav_buf);
}

/* Setup for conversion, copy original data to new buffer*/
wav_cvt.buf = 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 */
.
.
.
.

Remarks

green

Once you have initialized the cvt structure using SDL_BuildAudioCVT(), created an audio buffer cvt->buf, and filled it with cvt->len bytes of audio data in the source format, the structure can be passed to this function which will convert it in-place to the desired format.

-or-

Once you have initialized the cvt structure, created an audio buffer cvt->buf, and filled it with cvt->len bytes of audio data in the source format using SDL_BuildAudioCVT(), the structure can be passed to this function which will convert it in-place to the desired format.

green

The data conversion may expand the size of the audio data, so the buffer cvt->buf should be allocated after the cvt structure is initialized by SDL_BuildAudioCVT(), and should be cvt->len*cvt->len_mult bytes long.

If the conversion completed successfully then the converted audio data can be read from cvt->buf. The amount of valid, converted, audio data in the buffer is equal to cvt->len*cvt->len_ratio.


CategoryAPI, CategoryAudio

None: SDL_ConvertAudio (last edited 2014-01-19 12:33:59 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit