1SDL_ConvertAudio(3) SDL API Reference SDL_ConvertAudio(3)
2
3
4
6 SDL_ConvertAudio - Convert audio data to a desired audio format.
7
9 #include "SDL.h"
10
11 int SDL_ConvertAudio(SDL_AudioCVT *cvt);
12
14 SDL_ConvertAudio takes one parameter, cvt, which was previously ini‐
15 tilized. Initilizing a SDL_AudioCVT is a two step process. First of
16 all, the structure must be passed to SDL_BuildAudioCVT along with
17 source and destination format parameters. Secondly, the cvt->buf and
18 cvt->len fields must be setup. cvt->buf should point to the audio data
19 and cvt->len should be set to the length of the audio data in bytes.
20 Remember, the length of the buffer pointed to by buf show be
21 len*len_mult bytes in length.
22
23 Once the SDL_AudioCVTstructure is initilized then we can pass it to
24 SDL_ConvertAudio, which will convert the audio data pointer to by
25 cvt->buf. If SDL_ConvertAudio returned 0 then the conversion was com‐
26 pleted successfully, otherwise -1 is returned.
27
28 If the conversion completed successfully then the converted audio data
29 can be read from cvt->buf. The amount of valid, converted, audio data
30 in the buffer is equal to cvt->len*cvt->len_ratio.
31
33 /* Converting some WAV data to hardware format */
34 void my_audio_callback(void *userdata, Uint8 *stream, int len);
35
36 SDL_AudioSpec *desired, *obtained;
37 SDL_AudioSpec wav_spec;
38 SDL_AudioCVT wav_cvt;
39 Uint32 wav_len;
40 Uint8 *wav_buf;
41 int ret;
42
43 /* Allocated audio specs */
44 desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
45 obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
46
47 /* Set desired format */
48 desired->freq=22050;
49 desired->format=AUDIO_S16LSB;
50 desired->samples=8192;
51 desired->callback=my_audio_callback;
52 desired->userdata=NULL;
53
54 /* Open the audio device */
55 if ( SDL_OpenAudio(desired, obtained) < 0 ){
56 fprintf(stderr, "Couldn't open audio: %s
57 ", SDL_GetError());
58 exit(-1);
59 }
60
61 free(desired);
62
63 /* Load the test.wav */
64 if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf, &wav_len) == NULL ){
65 fprintf(stderr, "Could not open test.wav: %s
66 ", SDL_GetError());
67 SDL_CloseAudio();
68 free(obtained);
69 exit(-1);
70 }
71
72 /* Build AudioCVT */
73 ret = SDL_BuildAudioCVT(&wav_cvt,
74 wav_spec.format, wav_spec.channels, wav_spec.freq,
75 obtained->format, obtained->channels, obtained->freq);
76
77 /* Check that the convert was built */
78 if(ret==-1){
79 fprintf(stderr, "Couldn't build converter!
80 ");
81 SDL_CloseAudio();
82 free(obtained);
83 SDL_FreeWAV(wav_buf);
84 }
85
86 /* Setup for conversion */
87 wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
88 wav_cvt.len=wav_len;
89 memcpy(wav_cvt.buf, wav_buf, wav_len);
90
91 /* We can delete to original WAV data now */
92 SDL_FreeWAV(wav_buf);
93
94 /* And now we're ready to convert */
95 SDL_ConvertAudio(&wav_cvt);
96
97 /* do whatever */
98 .
99 .
100 .
101 .
102
103
105 SDL_BuildAudioCVT, SDL_AudioCVT
106
107
108
109SDL Tue 11 Sep 2001, 22:58 SDL_ConvertAudio(3)