1 .TH "SDL_ConvertAudio" "3" "Tue 11 Sep 2001, 22:58" "SDL" "SDL API Reference"
3 SDL_ConvertAudio\- Convert audio data to a desired audio format\&.
8 \fBint \fBSDL_ConvertAudio\fP\fR(\fBSDL_AudioCVT *cvt\fR);
11 \fBSDL_ConvertAudio\fP takes one parameter, \fBcvt\fR, which was previously initilized\&. Initilizing a \fI\fBSDL_AudioCVT\fR\fR is a two step process\&. First of all, the structure must be passed to \fI\fBSDL_BuildAudioCVT\fP\fR along with source and destination format parameters\&. Secondly, the \fBcvt\fR->\fBbuf\fR and \fBcvt\fR->\fBlen\fR fields must be setup\&. \fBcvt\fR->\fBbuf\fR should point to the audio data and \fBcvt\fR->\fBlen\fR should be set to the length of the audio data in bytes\&. Remember, the length of the buffer pointed to by \fBbuf\fR show be \fBlen\fR*\fBlen_mult\fR bytes in length\&.
13 Once the \fBSDL_AudioCVT\fRstructure is initilized then we can pass it to \fBSDL_ConvertAudio\fP, which will convert the audio data pointer to by \fBcvt\fR->\fBbuf\fR\&. If \fBSDL_ConvertAudio\fP returned \fB0\fR then the conversion was completed successfully, otherwise \fB-1\fR is returned\&.
15 If the conversion completed successfully then the converted audio data can be read from \fBcvt\fR->\fBbuf\fR\&. The amount of valid, converted, audio data in the buffer is equal to \fBcvt\fR->\fBlen\fR*\fBcvt\fR->\fBlen_ratio\fR\&.
19 \f(CW/* Converting some WAV data to hardware format */
20 void my_audio_callback(void *userdata, Uint8 *stream, int len);
22 SDL_AudioSpec *desired, *obtained;
23 SDL_AudioSpec wav_spec;
29 /* Allocated audio specs */
30 desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
31 obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
33 /* Set desired format */
35 desired->format=AUDIO_S16LSB;
36 desired->samples=8192;
37 desired->callback=my_audio_callback;
38 desired->userdata=NULL;
40 /* Open the audio device */
41 if ( SDL_OpenAudio(desired, obtained) < 0 ){
42 fprintf(stderr, "Couldn\&'t open audio: %s
49 /* Load the test\&.wav */
50 if( SDL_LoadWAV("test\&.wav", &wav_spec, &wav_buf, &wav_len) == NULL ){
51 fprintf(stderr, "Could not open test\&.wav: %s
59 ret = SDL_BuildAudioCVT(&wav_cvt,
60 wav_spec\&.format, wav_spec\&.channels, wav_spec\&.freq,
61 obtained->format, obtained->channels, obtained->freq);
63 /* Check that the convert was built */
65 fprintf(stderr, "Couldn\&'t build converter!
72 /* Setup for conversion */
73 wav_cvt\&.buf=(Uint8 *)malloc(wav_len*wav_cvt\&.len_mult);
74 wav_cvt\&.len=wav_len;
75 memcpy(wav_cvt\&.buf, wav_buf, wav_len);
77 /* We can delete to original WAV data now */
80 /* And now we\&'re ready to convert */
81 SDL_ConvertAudio(&wav_cvt);
94 \fI\fBSDL_BuildAudioCVT\fP\fR, \fI\fBSDL_AudioCVT\fP\fR
95 ...\" created by instant / docbook-to-man, Tue 11 Sep 2001, 22:58