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