1<HTML 2><HEAD 3><TITLE 4>SDL_OpenAudio</TITLE 5><META 6NAME="GENERATOR" 7CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ 8"><LINK 9REL="HOME" 10TITLE="SDL Library Documentation" 11HREF="index.html"><LINK 12REL="UP" 13TITLE="Audio" 14HREF="audio.html"><LINK 15REL="PREVIOUS" 16TITLE="SDL_AudioSpec" 17HREF="sdlaudiospec.html"><LINK 18REL="NEXT" 19TITLE="SDL_PauseAudio" 20HREF="sdlpauseaudio.html"></HEAD 21><BODY 22CLASS="REFENTRY" 23BGCOLOR="#FFF8DC" 24TEXT="#000000" 25LINK="#0000ee" 26VLINK="#551a8b" 27ALINK="#ff0000" 28><DIV 29CLASS="NAVHEADER" 30><TABLE 31SUMMARY="Header navigation table" 32WIDTH="100%" 33BORDER="0" 34CELLPADDING="0" 35CELLSPACING="0" 36><TR 37><TH 38COLSPAN="3" 39ALIGN="center" 40>SDL Library Documentation</TH 41></TR 42><TR 43><TD 44WIDTH="10%" 45ALIGN="left" 46VALIGN="bottom" 47><A 48HREF="sdlaudiospec.html" 49ACCESSKEY="P" 50>Prev</A 51></TD 52><TD 53WIDTH="80%" 54ALIGN="center" 55VALIGN="bottom" 56></TD 57><TD 58WIDTH="10%" 59ALIGN="right" 60VALIGN="bottom" 61><A 62HREF="sdlpauseaudio.html" 63ACCESSKEY="N" 64>Next</A 65></TD 66></TR 67></TABLE 68><HR 69ALIGN="LEFT" 70WIDTH="100%"></DIV 71><H1 72><A 73NAME="SDLOPENAUDIO" 74></A 75>SDL_OpenAudio</H1 76><DIV 77CLASS="REFNAMEDIV" 78><A 79NAME="AEN6650" 80></A 81><H2 82>Name</H2 83>SDL_OpenAudio -- Opens the audio device with the desired parameters.</DIV 84><DIV 85CLASS="REFSYNOPSISDIV" 86><A 87NAME="AEN6653" 88></A 89><H2 90>Synopsis</H2 91><DIV 92CLASS="FUNCSYNOPSIS" 93><A 94NAME="AEN6654" 95></A 96><P 97></P 98><PRE 99CLASS="FUNCSYNOPSISINFO" 100>#include "SDL.h"</PRE 101><P 102><CODE 103><CODE 104CLASS="FUNCDEF" 105>int <B 106CLASS="FSFUNC" 107>SDL_OpenAudio</B 108></CODE 109>(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);</CODE 110></P 111><P 112></P 113></DIV 114></DIV 115><DIV 116CLASS="REFSECT1" 117><A 118NAME="AEN6660" 119></A 120><H2 121>Description</H2 122><P 123>This function opens the audio device with the <TT 124CLASS="PARAMETER" 125><I 126>desired</I 127></TT 128> parameters, and 129returns 0 if successful, placing the actual hardware parameters in the 130structure pointed to by <TT 131CLASS="PARAMETER" 132><I 133>obtained</I 134></TT 135>. If <TT 136CLASS="PARAMETER" 137><I 138>obtained</I 139></TT 140> is NULL, the audio 141data passed to the callback function will be guaranteed to be in the 142requested format, and will be automatically converted to the hardware 143audio format if necessary. This function returns -1 if it failed 144to open the audio device, or couldn't set up the audio thread.</P 145><P 146>To open the audio device a <TT 147CLASS="PARAMETER" 148><I 149>desired</I 150></TT 151> <A 152HREF="sdlaudiospec.html" 153><SPAN 154CLASS="STRUCTNAME" 155>SDL_AudioSpec</SPAN 156></A 157> must be created. 158<PRE 159CLASS="PROGRAMLISTING" 160>SDL_AudioSpec *desired; 161. 162. 163desired = malloc(sizeof(SDL_AudioSpec));</PRE 164> 165You must then fill this structure with your desired audio specifications.</P 166><P 167></P 168><DIV 169CLASS="VARIABLELIST" 170><DL 171><DT 172><SPAN 173CLASS="STRUCTNAME" 174>desired</SPAN 175>-><TT 176CLASS="STRUCTFIELD" 177><I 178>freq</I 179></TT 180></DT 181><DD 182><P 183>The desired audio frequency in samples-per-second.</P 184></DD 185><DT 186><SPAN 187CLASS="STRUCTNAME" 188>desired</SPAN 189>-><TT 190CLASS="STRUCTFIELD" 191><I 192>format</I 193></TT 194></DT 195><DD 196><P 197>The desired audio format (see <A 198HREF="sdlaudiospec.html" 199><SPAN 200CLASS="STRUCTNAME" 201>SDL_AudioSpec</SPAN 202></A 203>)</P 204></DD 205><DT 206><SPAN 207CLASS="STRUCTNAME" 208>desired</SPAN 209>-><TT 210CLASS="STRUCTFIELD" 211><I 212>samples</I 213></TT 214></DT 215><DD 216><P 217>The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq</P 218></DD 219><DT 220><SPAN 221CLASS="STRUCTNAME" 222>desired</SPAN 223>-><TT 224CLASS="STRUCTFIELD" 225><I 226>callback</I 227></TT 228></DT 229><DD 230><P 231>This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling <A 232HREF="sdllockaudio.html" 233><TT 234CLASS="FUNCTION" 235>SDL_LockAudio</TT 236></A 237> and <A 238HREF="sdlunlockaudio.html" 239><TT 240CLASS="FUNCTION" 241>SDL_UnlockAudio</TT 242></A 243> in your code. The callback prototype is: 244<PRE 245CLASS="PROGRAMLISTING" 246>void callback(void *userdata, Uint8 *stream, int len);</PRE 247> 248<TT 249CLASS="PARAMETER" 250><I 251>userdata</I 252></TT 253> is the pointer stored in <TT 254CLASS="STRUCTFIELD" 255><I 256>userdata</I 257></TT 258> field of the <SPAN 259CLASS="STRUCTNAME" 260>SDL_AudioSpec</SPAN 261>. <TT 262CLASS="PARAMETER" 263><I 264>stream</I 265></TT 266> is a pointer to the audio buffer you want to fill with information and <TT 267CLASS="PARAMETER" 268><I 269>len</I 270></TT 271> is the length of the audio buffer in bytes.</P 272></DD 273><DT 274><SPAN 275CLASS="STRUCTNAME" 276>desired</SPAN 277>-><TT 278CLASS="STRUCTFIELD" 279><I 280>userdata</I 281></TT 282></DT 283><DD 284><P 285>This pointer is passed as the first parameter to the <TT 286CLASS="FUNCTION" 287>callback</TT 288> function.</P 289></DD 290></DL 291></DIV 292><P 293><TT 294CLASS="FUNCTION" 295>SDL_OpenAudio</TT 296> reads these fields from the <TT 297CLASS="PARAMETER" 298><I 299>desired</I 300></TT 301> <SPAN 302CLASS="STRUCTNAME" 303>SDL_AudioSpec</SPAN 304> structure pass to the function and attempts to find an audio configuration matching your <TT 305CLASS="PARAMETER" 306><I 307>desired</I 308></TT 309>. As mentioned above, if the <TT 310CLASS="PARAMETER" 311><I 312>obtained</I 313></TT 314> parameter is <TT 315CLASS="LITERAL" 316>NULL</TT 317> then SDL with convert from your <TT 318CLASS="PARAMETER" 319><I 320>desired</I 321></TT 322> audio settings to the hardware settings as it plays.</P 323><P 324>If <TT 325CLASS="PARAMETER" 326><I 327>obtained</I 328></TT 329> is <TT 330CLASS="LITERAL" 331>NULL</TT 332> then the <TT 333CLASS="PARAMETER" 334><I 335>desired</I 336></TT 337> <SPAN 338CLASS="STRUCTNAME" 339>SDL_AudioSpec</SPAN 340> is your working specification, otherwise the <TT 341CLASS="PARAMETER" 342><I 343>obtained</I 344></TT 345> <SPAN 346CLASS="STRUCTNAME" 347>SDL_AudioSpec</SPAN 348> becomes the working specification and the <TT 349CLASS="PARAMETER" 350><I 351>desirec</I 352></TT 353> specification can be deleted. The data in the working specification is used when building <SPAN 354CLASS="STRUCTNAME" 355>SDL_AudioCVT</SPAN 356>'s for converting loaded data to the hardware format.</P 357><P 358><TT 359CLASS="FUNCTION" 360>SDL_OpenAudio</TT 361> calculates the <TT 362CLASS="STRUCTFIELD" 363><I 364>size</I 365></TT 366> and <TT 367CLASS="STRUCTFIELD" 368><I 369>silence</I 370></TT 371> fields for both the <TT 372CLASS="PARAMETER" 373><I 374>desired</I 375></TT 376> and <TT 377CLASS="PARAMETER" 378><I 379>obtained</I 380></TT 381> specifications. The <TT 382CLASS="STRUCTFIELD" 383><I 384>size</I 385></TT 386> field stores the total size of the audio buffer in bytes, while the <TT 387CLASS="STRUCTFIELD" 388><I 389>silence</I 390></TT 391> stores the value used to represent silence in the audio buffer</P 392><P 393>The audio device starts out playing <TT 394CLASS="STRUCTFIELD" 395><I 396>silence</I 397></TT 398> when it's opened, and should be enabled for playing by calling <A 399HREF="sdlpauseaudio.html" 400><TT 401CLASS="FUNCTION" 402>SDL_PauseAudio</TT 403>(<TT 404CLASS="PARAMETER" 405><I 406>0</I 407></TT 408>)</A 409> when you are ready for your audio <TT 410CLASS="STRUCTFIELD" 411><I 412>callback</I 413></TT 414> function to be called. Since the audio driver may modify the requested <TT 415CLASS="STRUCTFIELD" 416><I 417>size</I 418></TT 419> of the audio buffer, you should allocate any local mixing buffers after you open the audio device.</P 420></DIV 421><DIV 422CLASS="REFSECT1" 423><A 424NAME="AEN6747" 425></A 426><H2 427>Examples</H2 428><PRE 429CLASS="PROGRAMLISTING" 430>/* Prototype of our callback function */ 431void my_audio_callback(void *userdata, Uint8 *stream, int len); 432 433/* Open the audio device */ 434SDL_AudioSpec *desired, *obtained; 435SDL_AudioSpec *hardware_spec; 436 437/* Allocate a desired SDL_AudioSpec */ 438desired = malloc(sizeof(SDL_AudioSpec)); 439 440/* Allocate space for the obtained SDL_AudioSpec */ 441obtained = malloc(sizeof(SDL_AudioSpec)); 442 443/* 22050Hz - FM Radio quality */ 444desired->freq=22050; 445 446/* 16-bit signed audio */ 447desired->format=AUDIO_S16LSB; 448 449/* Mono */ 450desired->channels=0; 451 452/* Large audio buffer reduces risk of dropouts but increases response time */ 453desired->samples=8192; 454 455/* Our callback function */ 456desired->callback=my_audio_callback; 457 458desired->userdata=NULL; 459 460/* Open the audio device */ 461if ( SDL_OpenAudio(desired, obtained) < 0 ){ 462 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); 463 exit(-1); 464} 465/* desired spec is no longer needed */ 466free(desired); 467hardware_spec=obtained; 468. 469. 470/* Prepare callback for playing */ 471. 472. 473. 474/* Start playing */ 475SDL_PauseAudio(0);</PRE 476></DIV 477><DIV 478CLASS="REFSECT1" 479><A 480NAME="AEN6750" 481></A 482><H2 483>See Also</H2 484><P 485><A 486HREF="sdlaudiospec.html" 487><TT 488CLASS="FUNCTION" 489>SDL_AudioSpec</TT 490></A 491>, 492<A 493HREF="sdllockaudio.html" 494><TT 495CLASS="FUNCTION" 496>SDL_LockAudio</TT 497></A 498>, 499<A 500HREF="sdlunlockaudio.html" 501><TT 502CLASS="FUNCTION" 503>SDL_UnlockAudio</TT 504></A 505>, 506<A 507HREF="sdlpauseaudio.html" 508><TT 509CLASS="FUNCTION" 510>SDL_PauseAudio</TT 511></A 512></P 513></DIV 514><DIV 515CLASS="NAVFOOTER" 516><HR 517ALIGN="LEFT" 518WIDTH="100%"><TABLE 519SUMMARY="Footer navigation table" 520WIDTH="100%" 521BORDER="0" 522CELLPADDING="0" 523CELLSPACING="0" 524><TR 525><TD 526WIDTH="33%" 527ALIGN="left" 528VALIGN="top" 529><A 530HREF="sdlaudiospec.html" 531ACCESSKEY="P" 532>Prev</A 533></TD 534><TD 535WIDTH="34%" 536ALIGN="center" 537VALIGN="top" 538><A 539HREF="index.html" 540ACCESSKEY="H" 541>Home</A 542></TD 543><TD 544WIDTH="33%" 545ALIGN="right" 546VALIGN="top" 547><A 548HREF="sdlpauseaudio.html" 549ACCESSKEY="N" 550>Next</A 551></TD 552></TR 553><TR 554><TD 555WIDTH="33%" 556ALIGN="left" 557VALIGN="top" 558>SDL_AudioSpec</TD 559><TD 560WIDTH="34%" 561ALIGN="center" 562VALIGN="top" 563><A 564HREF="audio.html" 565ACCESSKEY="U" 566>Up</A 567></TD 568><TD 569WIDTH="33%" 570ALIGN="right" 571VALIGN="top" 572>SDL_PauseAudio</TD 573></TR 574></TABLE 575></DIV 576></BODY 577></HTML 578>