0
|
1 <HTML
|
|
2 ><HEAD
|
|
3 ><TITLE
|
|
4 >SDL_ConvertAudio</TITLE
|
|
5 ><META
|
|
6 NAME="GENERATOR"
|
|
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.61
|
|
8 "><LINK
|
|
9 REL="HOME"
|
|
10 TITLE="SDL Library Documentation"
|
|
11 HREF="index.html"><LINK
|
|
12 REL="UP"
|
|
13 TITLE="Audio"
|
|
14 HREF="audio.html"><LINK
|
|
15 REL="PREVIOUS"
|
|
16 TITLE="SDL_BuildAudioCVT"
|
|
17 HREF="sdlbuildaudiocvt.html"><LINK
|
|
18 REL="NEXT"
|
|
19 TITLE="SDL_MixAudio"
|
|
20 HREF="sdlmixaudio.html"></HEAD
|
|
21 ><BODY
|
|
22 CLASS="REFENTRY"
|
|
23 BGCOLOR="#FFF8DC"
|
|
24 TEXT="#000000"
|
|
25 LINK="#0000ee"
|
|
26 VLINK="#551a8b"
|
|
27 ALINK="#ff0000"
|
|
28 ><DIV
|
|
29 CLASS="NAVHEADER"
|
|
30 ><TABLE
|
|
31 WIDTH="100%"
|
|
32 BORDER="0"
|
|
33 CELLPADDING="0"
|
|
34 CELLSPACING="0"
|
|
35 ><TR
|
|
36 ><TH
|
|
37 COLSPAN="3"
|
|
38 ALIGN="center"
|
|
39 >SDL Library Documentation</TH
|
|
40 ></TR
|
|
41 ><TR
|
|
42 ><TD
|
|
43 WIDTH="10%"
|
|
44 ALIGN="left"
|
|
45 VALIGN="bottom"
|
|
46 ><A
|
|
47 HREF="sdlbuildaudiocvt.html"
|
|
48 >Prev</A
|
|
49 ></TD
|
|
50 ><TD
|
|
51 WIDTH="80%"
|
|
52 ALIGN="center"
|
|
53 VALIGN="bottom"
|
|
54 ></TD
|
|
55 ><TD
|
|
56 WIDTH="10%"
|
|
57 ALIGN="right"
|
|
58 VALIGN="bottom"
|
|
59 ><A
|
|
60 HREF="sdlmixaudio.html"
|
|
61 >Next</A
|
|
62 ></TD
|
|
63 ></TR
|
|
64 ></TABLE
|
|
65 ><HR
|
|
66 ALIGN="LEFT"
|
|
67 WIDTH="100%"></DIV
|
|
68 ><H1
|
|
69 ><A
|
|
70 NAME="SDLCONVERTAUDIO"
|
|
71 >SDL_ConvertAudio</A
|
|
72 ></H1
|
|
73 ><DIV
|
|
74 CLASS="REFNAMEDIV"
|
|
75 ><A
|
|
76 NAME="AEN6426"
|
|
77 ></A
|
|
78 ><H2
|
|
79 >Name</H2
|
|
80 >SDL_ConvertAudio -- Convert audio data to a desired audio format.</DIV
|
|
81 ><DIV
|
|
82 CLASS="REFSYNOPSISDIV"
|
|
83 ><A
|
|
84 NAME="AEN6429"
|
|
85 ></A
|
|
86 ><H2
|
|
87 >Synopsis</H2
|
|
88 ><DIV
|
|
89 CLASS="FUNCSYNOPSIS"
|
|
90 ><A
|
|
91 NAME="AEN6430"
|
|
92 ></A
|
|
93 ><P
|
|
94 ></P
|
|
95 ><PRE
|
|
96 CLASS="FUNCSYNOPSISINFO"
|
|
97 >#include "SDL.h"</PRE
|
|
98 ><P
|
|
99 ><CODE
|
|
100 ><CODE
|
|
101 CLASS="FUNCDEF"
|
|
102 >int <B
|
|
103 CLASS="FSFUNC"
|
|
104 >SDL_ConvertAudio</B
|
|
105 ></CODE
|
|
106 >(SDL_AudioCVT *cvt);</CODE
|
|
107 ></P
|
|
108 ><P
|
|
109 ></P
|
|
110 ></DIV
|
|
111 ></DIV
|
|
112 ><DIV
|
|
113 CLASS="REFSECT1"
|
|
114 ><A
|
|
115 NAME="AEN6436"
|
|
116 ></A
|
|
117 ><H2
|
|
118 >Description</H2
|
|
119 ><P
|
|
120 ><TT
|
|
121 CLASS="FUNCTION"
|
|
122 >SDL_ConvertAudio</TT
|
|
123 > takes one parameter, <TT
|
|
124 CLASS="PARAMETER"
|
|
125 ><I
|
|
126 >cvt</I
|
|
127 ></TT
|
|
128 >, which was previously initilized. Initilizing a <A
|
|
129 HREF="sdlaudiocvt.html"
|
|
130 ><SPAN
|
|
131 CLASS="STRUCTNAME"
|
|
132 >SDL_AudioCVT</SPAN
|
|
133 ></A
|
|
134 > is a two step process. First of all, the structure must be passed to <A
|
|
135 HREF="sdlbuildaudiocvt.html"
|
|
136 ><TT
|
|
137 CLASS="FUNCTION"
|
|
138 >SDL_BuildAudioCVT</TT
|
|
139 ></A
|
|
140 > along with source and destination format parameters. Secondly, the <SPAN
|
|
141 CLASS="STRUCTNAME"
|
|
142 >cvt</SPAN
|
|
143 >-><TT
|
|
144 CLASS="STRUCTFIELD"
|
|
145 ><I
|
|
146 >buf</I
|
|
147 ></TT
|
|
148 > and <SPAN
|
|
149 CLASS="STRUCTNAME"
|
|
150 >cvt</SPAN
|
|
151 >-><TT
|
|
152 CLASS="STRUCTFIELD"
|
|
153 ><I
|
|
154 >len</I
|
|
155 ></TT
|
|
156 > fields must be setup. <SPAN
|
|
157 CLASS="STRUCTNAME"
|
|
158 >cvt</SPAN
|
|
159 >-><TT
|
|
160 CLASS="STRUCTFIELD"
|
|
161 ><I
|
|
162 >buf</I
|
|
163 ></TT
|
|
164 > should point to the audio data and <SPAN
|
|
165 CLASS="STRUCTNAME"
|
|
166 >cvt</SPAN
|
|
167 >-><TT
|
|
168 CLASS="STRUCTFIELD"
|
|
169 ><I
|
|
170 >len</I
|
|
171 ></TT
|
|
172 > should be set to the length of the audio data in bytes. Remember, the length of the buffer pointed to by <TT
|
|
173 CLASS="STRUCTFIELD"
|
|
174 ><I
|
|
175 >buf</I
|
|
176 ></TT
|
|
177 > show be <TT
|
|
178 CLASS="STRUCTFIELD"
|
|
179 ><I
|
|
180 >len</I
|
|
181 ></TT
|
|
182 >*<TT
|
|
183 CLASS="STRUCTFIELD"
|
|
184 ><I
|
|
185 >len_mult</I
|
|
186 ></TT
|
|
187 > bytes in length.</P
|
|
188 ><P
|
|
189 >Once the <SPAN
|
|
190 CLASS="STRUCTNAME"
|
|
191 >SDL_AudioCVT</SPAN
|
|
192 >structure is initilized then we can pass it to <TT
|
|
193 CLASS="FUNCTION"
|
|
194 >SDL_ConvertAudio</TT
|
|
195 >, which will convert the audio data pointer to by <SPAN
|
|
196 CLASS="STRUCTNAME"
|
|
197 >cvt</SPAN
|
|
198 >-><TT
|
|
199 CLASS="STRUCTFIELD"
|
|
200 ><I
|
|
201 >buf</I
|
|
202 ></TT
|
|
203 >. If <TT
|
|
204 CLASS="FUNCTION"
|
|
205 >SDL_ConvertAudio</TT
|
|
206 > returned <SPAN
|
|
207 CLASS="RETURNVALUE"
|
|
208 >0</SPAN
|
|
209 > then the conversion was completed successfully, otherwise <SPAN
|
|
210 CLASS="RETURNVALUE"
|
|
211 >-1</SPAN
|
|
212 > is returned.</P
|
|
213 ><P
|
|
214 >If the conversion completed successfully then the converted audio data can be read from <SPAN
|
|
215 CLASS="STRUCTNAME"
|
|
216 >cvt</SPAN
|
|
217 >-><TT
|
|
218 CLASS="STRUCTFIELD"
|
|
219 ><I
|
|
220 >buf</I
|
|
221 ></TT
|
|
222 >. The amount of valid, converted, audio data in the buffer is equal to <SPAN
|
|
223 CLASS="STRUCTNAME"
|
|
224 >cvt</SPAN
|
|
225 >-><TT
|
|
226 CLASS="STRUCTFIELD"
|
|
227 ><I
|
|
228 >len</I
|
|
229 ></TT
|
|
230 >*<TT
|
|
231 CLASS="STRUCTFIELD"
|
|
232 ><I
|
|
233 >cvt</I
|
|
234 ></TT
|
|
235 >-><SPAN
|
|
236 CLASS="STRUCTNAME"
|
|
237 >len_ratio</SPAN
|
|
238 >.</P
|
|
239 ></DIV
|
|
240 ><DIV
|
|
241 CLASS="REFSECT1"
|
|
242 ><A
|
|
243 NAME="AEN6471"
|
|
244 ></A
|
|
245 ><H2
|
|
246 >Examples</H2
|
|
247 ><PRE
|
|
248 CLASS="PROGRAMLISTING"
|
|
249 >/* Converting some WAV data to hardware format */
|
|
250 void my_audio_callback(void *userdata, Uint8 *stream, int len);
|
|
251
|
|
252 SDL_AudioSpec *desired, *obtained;
|
|
253 SDL_AudioSpec wav_spec;
|
|
254 SDL_AudioCVT wav_cvt;
|
|
255 Uint32 wav_len;
|
|
256 Uint8 *wav_buf;
|
|
257 int ret;
|
|
258
|
|
259 /* Allocated audio specs */
|
|
260 desired=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
|
|
261 obtained=(SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
|
|
262
|
|
263 /* Set desired format */
|
|
264 desired->freq=22050;
|
|
265 desired->format=AUDIO_S16LSB;
|
|
266 desired->samples=8192;
|
|
267 desired->callback=my_audio_callback;
|
|
268 desired->userdata=NULL;
|
|
269
|
|
270 /* Open the audio device */
|
|
271 if ( SDL_OpenAudio(desired, obtained) < 0 ){
|
|
272 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
|
|
273 exit(-1);
|
|
274 }
|
|
275
|
|
276 free(desired);
|
|
277
|
|
278 /* Load the test.wav */
|
|
279 if( SDL_LoadWAV("test.wav", &wav_spec, &wav_buf, &wav_len) == NULL ){
|
|
280 fprintf(stderr, "Could not open test.wav: %s\n", SDL_GetError());
|
|
281 SDL_CloseAudio();
|
|
282 free(obtained);
|
|
283 exit(-1);
|
|
284 }
|
|
285
|
|
286 /* Build AudioCVT */
|
|
287 ret = SDL_BuildAudioCVT(&wav_cvt,
|
|
288 wav_spec.format, wav_spec.channels, wav_spec.freq,
|
|
289 obtained->format, obtained->channels, obtained->freq);
|
|
290
|
|
291 /* Check that the convert was built */
|
|
292 if(ret==-1){
|
|
293 fprintf(stderr, "Couldn't build converter!\n");
|
|
294 SDL_CloseAudio();
|
|
295 free(obtained);
|
|
296 SDL_FreeWAV(wav_buf);
|
|
297 }
|
|
298
|
|
299 /* Setup for conversion */
|
|
300 wav_cvt.buf=(Uint8 *)malloc(wav_len*wav_cvt.len_mult);
|
|
301 wav_cvt.len=wav_len;
|
|
302 memcpy(wav_cvt.buf, wav_buf, wav_len);
|
|
303
|
|
304 /* We can delete to original WAV data now */
|
|
305 SDL_FreeWAV(wav_buf);
|
|
306
|
|
307 /* And now we're ready to convert */
|
|
308 SDL_ConvertAudio(&wav_cvt);
|
|
309
|
|
310 /* do whatever */
|
|
311 .
|
|
312 .
|
|
313 .
|
|
314 .
|
|
315 </PRE
|
|
316 ></DIV
|
|
317 ><DIV
|
|
318 CLASS="REFSECT1"
|
|
319 ><A
|
|
320 NAME="AEN6474"
|
|
321 ></A
|
|
322 ><H2
|
|
323 >See Also</H2
|
|
324 ><P
|
|
325 ><A
|
|
326 HREF="sdlbuildaudiocvt.html"
|
|
327 ><TT
|
|
328 CLASS="FUNCTION"
|
|
329 >SDL_BuildAudioCVT</TT
|
|
330 ></A
|
|
331 >,
|
|
332 <A
|
|
333 HREF="sdlaudiocvt.html"
|
|
334 ><TT
|
|
335 CLASS="FUNCTION"
|
|
336 >SDL_AudioCVT</TT
|
|
337 ></A
|
|
338 ></P
|
|
339 ></DIV
|
|
340 ><DIV
|
|
341 CLASS="NAVFOOTER"
|
|
342 ><HR
|
|
343 ALIGN="LEFT"
|
|
344 WIDTH="100%"><TABLE
|
|
345 WIDTH="100%"
|
|
346 BORDER="0"
|
|
347 CELLPADDING="0"
|
|
348 CELLSPACING="0"
|
|
349 ><TR
|
|
350 ><TD
|
|
351 WIDTH="33%"
|
|
352 ALIGN="left"
|
|
353 VALIGN="top"
|
|
354 ><A
|
|
355 HREF="sdlbuildaudiocvt.html"
|
|
356 >Prev</A
|
|
357 ></TD
|
|
358 ><TD
|
|
359 WIDTH="34%"
|
|
360 ALIGN="center"
|
|
361 VALIGN="top"
|
|
362 ><A
|
|
363 HREF="index.html"
|
|
364 >Home</A
|
|
365 ></TD
|
|
366 ><TD
|
|
367 WIDTH="33%"
|
|
368 ALIGN="right"
|
|
369 VALIGN="top"
|
|
370 ><A
|
|
371 HREF="sdlmixaudio.html"
|
|
372 >Next</A
|
|
373 ></TD
|
|
374 ></TR
|
|
375 ><TR
|
|
376 ><TD
|
|
377 WIDTH="33%"
|
|
378 ALIGN="left"
|
|
379 VALIGN="top"
|
|
380 >SDL_BuildAudioCVT</TD
|
|
381 ><TD
|
|
382 WIDTH="34%"
|
|
383 ALIGN="center"
|
|
384 VALIGN="top"
|
|
385 ><A
|
|
386 HREF="audio.html"
|
|
387 >Up</A
|
|
388 ></TD
|
|
389 ><TD
|
|
390 WIDTH="33%"
|
|
391 ALIGN="right"
|
|
392 VALIGN="top"
|
|
393 >SDL_MixAudio</TD
|
|
394 ></TR
|
|
395 ></TABLE
|
|
396 ></DIV
|
|
397 ></BODY
|
|
398 ></HTML
|
|
399 > |