0
|
1 <HTML
|
|
2 ><HEAD
|
|
3 ><TITLE
|
|
4 >Audio Examples</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="Examples"
|
|
14 HREF="guideexamples.html"><LINK
|
|
15 REL="PREVIOUS"
|
|
16 TITLE="Event Examples"
|
|
17 HREF="guideeventexamples.html"><LINK
|
|
18 REL="NEXT"
|
|
19 TITLE="CDROM Examples"
|
|
20 HREF="guidecdromexamples.html"></HEAD
|
|
21 ><BODY
|
|
22 CLASS="SECT1"
|
|
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="guideeventexamples.html"
|
|
48 >Prev</A
|
|
49 ></TD
|
|
50 ><TD
|
|
51 WIDTH="80%"
|
|
52 ALIGN="center"
|
|
53 VALIGN="bottom"
|
|
54 >Chapter 4. Examples</TD
|
|
55 ><TD
|
|
56 WIDTH="10%"
|
|
57 ALIGN="right"
|
|
58 VALIGN="bottom"
|
|
59 ><A
|
|
60 HREF="guidecdromexamples.html"
|
|
61 >Next</A
|
|
62 ></TD
|
|
63 ></TR
|
|
64 ></TABLE
|
|
65 ><HR
|
|
66 ALIGN="LEFT"
|
|
67 WIDTH="100%"></DIV
|
|
68 ><DIV
|
|
69 CLASS="SECT1"
|
|
70 ><H1
|
|
71 CLASS="SECT1"
|
|
72 ><A
|
|
73 NAME="GUIDEAUDIOEXAMPLES"
|
|
74 >Audio Examples</A
|
|
75 ></H1
|
|
76 ><P
|
|
77 ></P
|
|
78 ><DIV
|
|
79 CLASS="SECT2"
|
|
80 ><H2
|
|
81 CLASS="SECT2"
|
|
82 ><A
|
|
83 NAME="AEN343"
|
|
84 >Opening the audio device</A
|
|
85 ></H2
|
|
86 ><P
|
|
87 ><PRE
|
|
88 CLASS="PROGRAMLISTING"
|
|
89 > SDL_AudioSpec wanted;
|
|
90 extern void fill_audio(void *udata, Uint8 *stream, int len);
|
|
91
|
|
92 /* Set the audio format */
|
|
93 wanted.freq = 22050;
|
|
94 wanted.format = AUDIO_S16;
|
|
95 wanted.channels = 2; /* 1 = mono, 2 = stereo */
|
|
96 wanted.samples = 1024; /* Good low-latency value for callback */
|
|
97 wanted.callback = fill_audio;
|
|
98 wanted.userdata = NULL;
|
|
99
|
|
100 /* Open the audio device, forcing the desired format */
|
|
101 if ( SDL_OpenAudio(&wanted, NULL) < 0 ) {
|
|
102 fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
|
|
103 return(-1);
|
|
104 }
|
|
105 return(0);</PRE
|
|
106 ></P
|
|
107 ></DIV
|
|
108 ><DIV
|
|
109 CLASS="SECT2"
|
|
110 ><H2
|
|
111 CLASS="SECT2"
|
|
112 ><A
|
|
113 NAME="AEN347"
|
|
114 >Playing audio</A
|
|
115 ></H2
|
|
116 ><P
|
|
117 ><PRE
|
|
118 CLASS="PROGRAMLISTING"
|
|
119 > static Uint8 *audio_chunk;
|
|
120 static Uint32 audio_len;
|
|
121 static Uint8 *audio_pos;
|
|
122
|
|
123 /* The audio function callback takes the following parameters:
|
|
124 stream: A pointer to the audio buffer to be filled
|
|
125 len: The length (in bytes) of the audio buffer
|
|
126 */
|
|
127 void fill_audio(void *udata, Uint8 *stream, int len)
|
|
128 {
|
|
129 /* Only play if we have data left */
|
|
130 if ( audio_len == 0 )
|
|
131 return;
|
|
132
|
|
133 /* Mix as much data as possible */
|
|
134 len = ( len > audio_len ? audio_len : len );
|
|
135 SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME)
|
|
136 audio_pos += len;
|
|
137 audio_len -= len;
|
|
138 }
|
|
139
|
|
140 /* Load the audio data ... */
|
|
141
|
|
142 ;;;;;
|
|
143
|
|
144 audio_pos = audio_chunk;
|
|
145
|
|
146 /* Let the callback function play the audio chunk */
|
|
147 SDL_PauseAudio(0);
|
|
148
|
|
149 /* Do some processing */
|
|
150
|
|
151 ;;;;;
|
|
152
|
|
153 /* Wait for sound to complete */
|
|
154 while ( audio_len > 0 ) {
|
|
155 SDL_Delay(100); /* Sleep 1/10 second */
|
|
156 }
|
|
157 SDL_CloseAudio();</PRE
|
|
158 ></P
|
|
159 ></DIV
|
|
160 ></DIV
|
|
161 ><DIV
|
|
162 CLASS="NAVFOOTER"
|
|
163 ><HR
|
|
164 ALIGN="LEFT"
|
|
165 WIDTH="100%"><TABLE
|
|
166 WIDTH="100%"
|
|
167 BORDER="0"
|
|
168 CELLPADDING="0"
|
|
169 CELLSPACING="0"
|
|
170 ><TR
|
|
171 ><TD
|
|
172 WIDTH="33%"
|
|
173 ALIGN="left"
|
|
174 VALIGN="top"
|
|
175 ><A
|
|
176 HREF="guideeventexamples.html"
|
|
177 >Prev</A
|
|
178 ></TD
|
|
179 ><TD
|
|
180 WIDTH="34%"
|
|
181 ALIGN="center"
|
|
182 VALIGN="top"
|
|
183 ><A
|
|
184 HREF="index.html"
|
|
185 >Home</A
|
|
186 ></TD
|
|
187 ><TD
|
|
188 WIDTH="33%"
|
|
189 ALIGN="right"
|
|
190 VALIGN="top"
|
|
191 ><A
|
|
192 HREF="guidecdromexamples.html"
|
|
193 >Next</A
|
|
194 ></TD
|
|
195 ></TR
|
|
196 ><TR
|
|
197 ><TD
|
|
198 WIDTH="33%"
|
|
199 ALIGN="left"
|
|
200 VALIGN="top"
|
|
201 >Event Examples</TD
|
|
202 ><TD
|
|
203 WIDTH="34%"
|
|
204 ALIGN="center"
|
|
205 VALIGN="top"
|
|
206 ><A
|
|
207 HREF="guideexamples.html"
|
|
208 >Up</A
|
|
209 ></TD
|
|
210 ><TD
|
|
211 WIDTH="33%"
|
|
212 ALIGN="right"
|
|
213 VALIGN="top"
|
|
214 >CDROM Examples</TD
|
|
215 ></TR
|
|
216 ></TABLE
|
|
217 ></DIV
|
|
218 ></BODY
|
|
219 ></HTML
|
|
220 > |