Update index page on mailing list
[sdlgit/SDL-Site.git] / pages / SDL-Audio.html-inc
CommitLineData
b82df135 1<div class="pod">
2<!-- INDEX START -->
3<h3 id="TOP">Index</h3>
4
5<ul><li><a href="#NAME">NAME</a></li>
8758037a 6<li><a href="#CATEGORY">CATEGORY</a></li>
fb5f7900 7<li><a href="#CONSTANTS">CONSTANTS</a></li>
8758037a 8<li><a href="#METHODS">METHODS</a>
15c551d8 9<ul><li><a href="#open">open</a></li>
e3282ccf 10<li><a href="#pause">pause</a></li>
15c551d8 11<li><a href="#get_status">get_status</a></li>
12<li><a href="#load_wav">load_wav </a></li>
13<li><a href="#free_wav">free_wav </a></li>
e3282ccf 14<li><a href="#convert">convert</a></li>
15c551d8 15<li><a href="#mix">mix</a></li>
e3282ccf 16<li><a href="#lock">lock</a></li>
17<li><a href="#unlock">unlock</a></li>
18<li><a href="#close">close </a>
8758037a 19</li>
20</ul>
b82df135 21</li>
22</ul><hr />
23<!-- INDEX END -->
24
25<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
26<div id="NAME_CONTENT">
260482f9 27<p>SDL::Audio - SDL Bindings for Audio</p>
b82df135 28
29</div>
30<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
31<div id="CATEGORY_CONTENT">
260482f9 32<p>Core, Audio</p>
b82df135 33
34</div>
fb5f7900 35<h1 id="CONSTANTS">CONSTANTS</h1><p><a href="#TOP" class="toplink">Top</a></p>
36<div id="CONSTANTS_CONTENT">
3549ee6a 37<p>The constants are exported by default. You can avoid this by doing:</p>
38<pre> use SDL::Audio ();
fb5f7900 39
40</pre>
3549ee6a 41<p>and access them directly:</p>
fb5f7900 42<pre> SDL::Audio::AUDIO_S16SYS;
43
44</pre>
45<p>or by choosing the export tags below:</p>
46<p>Export tag: ':format'</p>
47<pre> AUDIO_U8
48 AUDIO_S8
49 AUDIO_U16LSB
50 AUDIO_S16LSB
51 AUDIO_U16MSB
52 AUDIO_S16MSB
53 AUDIO_U16
54 AUDIO_S16
55 AUDIO_U16SYS
56 AUDIO_S16SYS
57
58</pre>
59<p>Export tag: ':status'</p>
60<pre> SDL_AUDIO_STOPPED
61 SDL_AUDIO_PLAYING
62 SDL_AUDIO_PAUSED
63
64</pre>
65
66</div>
8758037a 67<h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
68<div id="METHODS_CONTENT">
69
70</div>
e3282ccf 71<h2 id="open">open</h2>
72<div id="open_CONTENT">
e3282ccf 73<pre> use SDL;
74 use SDL::Audio;
75
76 SDL::init(SDL_INIT_AUDIO);
77
78 my $desired = SDL::AudioSpec-&gt;new();
79
80 my $obtained;
81
15c551d8 82 SDL::Audio::open( $desired, $obtained );
e3282ccf 83
84 # $obtained-&gt;... (A new SDL::AudioSpec now);
85
86</pre>
e3282ccf 87<p>This function opens the audio device with the desired parameters, and returns 0 if successful, placing the actual hardware parameters in the structure pointed to by obtained. If obtained is NULL, the audio data passed to the callback function will be guaranteed to be in the requested format, and will be automatically converted to the hardware audio format if necessary. This function returns -1 if it failed to open the audio device, or couldn't set up the audio thread.</p>
88<p>To open the audio device a desired SDL::AudioSpec must be created.</p>
89<pre> my $desired = SDL::AudioSpec-&gt;new();
90
91</pre>
92<p>You must then fill this structure with your desired audio specifications.</p>
93<dl>
94 <dt>The desired audio frequency in samples-per-second. </dt>
95 <dd>
96<pre> $desired-&gt;freq
97
98</pre>
99 </dd>
55bbf7a2 100 <dt>The desired audio format. See <a href="SDL-AudioSpec.html">SDL::AudioSpec</a></dt>
e3282ccf 101 <dd>
102<pre> $desired-&gt;format
103
104</pre>
105 </dd>
106 <dt>The desired channels (1 for mono, 2 for stereo, 4 for surround, 6 for surround with center and lfe). </dt>
107 <dd>
108<pre> $desired-&gt;channels
109
110</pre>
111 </dd>
112 <dt>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 </dt>
113 <dd>
114<pre> $desired-&gt;samples
115
116</pre>
117 </dd>
118 <dt>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 SDL::Audio::lock and SDL::Audio::unlock in your code. </dt>
119 <dd>
120 <p>THIS IS NOT READY YET</p>
121<pre> $desired-&gt;callback
122
15c551d8 123 my $callback = sub{ my ($userdata, $stream, $len) = @_; };
e3282ccf 124
125 $userdata is a reference stored in the userdata field of the SDL::AudioSpec.
126 $stream is a pointer to the audio buffer you want to fill with information and $len is the length of the audio buffer in bytes.
127
128 $desired-&gt;userdata
129
130 This pointer is passed as the first parameter to the callback function.
131
132</pre>
133 </dd>
134</dl>
135<p>SDL::Audio::open reads these fields from the desired SDL::AudioSpec structure passed to the function and attempts to find an audio configuration matching your desired. As mentioned above, if the obtained parameter is NULL then SDL with convert from your desired audio settings to the hardware settings as it plays.</p>
55bbf7a2 136<p>If obtained is NULL then the desired SDL::AudioSpec is your working specification, otherwise the obtained SDL::AudioSpec becomes the working specification and the desired specification can be deleted. The data in the working specification is used when building <a href="SDL-AudioCVT.html">SDL::AudioCVT</a>'s for converting loaded data to the hardware format.</p>
e3282ccf 137<p>SDL::Audio::open calculates the size and silence fields for both the $desired and $obtained specifications. The size field stores the total size of the audio buffer in bytes, while the silence stores the value used to represent silence in the audio buffer</p>
138<p>The audio device starts out playing silence when it's opened, and should be enabled for playing by calling SDL::Audio::pause(0) when you are ready for your audio callback function to be called. Since the audio driver may modify the requested size of the audio buffer, you should allocate any local mixing buffers after you open the audio device. </p>
139
140</div>
141<h2 id="pause">pause</h2>
142<div id="pause_CONTENT">
15c551d8 143<pre> pause( $bool )
144
145</pre>
146<p>This function pauses and unpauses the audio callback processing. It should be called with <code>$bool = 0</code> after opening the audio device to
147start playing sound. This is so you can safely initialize data for your callback function after opening the audio device. Silence will
148be written to the audio device during the pause.</p>
8758037a 149
150</div>
15c551d8 151<h2 id="get_status">get_status</h2>
152<div id="get_status_CONTENT">
153<pre> int get_status();
154
155</pre>
156<p>Returns either <code>SDL_AUDIO_STOPPED</code>, <code>SDL_AUDIO_PLAYING</code> or <code>SDL_AUDIO_PAUSED</code> depending on the current audio state. </p>
8758037a 157
158</div>
15c551d8 159<h2 id="load_wav">load_wav </h2>
160<div id="load_wav_CONTENT">
161<pre> SDL::AudioSpec load_wav( $filename, $spec );
162
163</pre>
164<p>This function loads a WAVE file into memory.</p>
165<p>If this function succeeds, it returns the given <code>SDL::AudioSpec</code>, filled with the audio data format of the wave data, and sets <code>buf</code>
166to a buffer containing the audio data, and sets <code>len</code> to the length of that audio buffer, in bytes. You need to free the audio buffer
167with <code>SDL::Audio::free_wav</code> when you are done with it.</p>
168<p>This function returns NULL and sets the SDL error message if the wave file cannot be opened, uses an unknown data format, or is corrupt.
169Currently raw, MS-ADPCM and IMA-ADPCM WAVE files are supported.</p>
170<p>Example:</p>
171<pre> use SDL;
172 use SDL::Audio;
173 use SDL::AudioSpec;
174
175 SDL::init(SDL_INIT_AUDIO);
176
177 # Converting some WAV data to hardware format
178
179 my $desired = SDL::AudioSpec-&gt;new();
180 my $obtained = SDL::AudioSpec-&gt;new();
181
182 # Set desired format
183 $desired-&gt;freq(22050);
184 $desired-&gt;channels(1);
185 $desired-&gt;format(AUDIO_S16);
186 $desired-&gt;samples(8192);
187
188 # Open the audio device
189 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
190 {
191 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
192 exit(-1);
193 }
194
195 # Load the test.wav
196 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
197
198 unless( $wav_ref )
199 {
200 warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
201 SDL::Audio::close_audio();
202 SDL::quit;
203 exit(-1);
204 }
205
206 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
207
208</pre>
8758037a 209
210</div>
15c551d8 211<h2 id="free_wav">free_wav </h2>
212<div id="free_wav_CONTENT">
213<pre> free_wav( $buffer )
214
215</pre>
216<p>After a WAVE file has been opened with <code>load_wav</code> its data can eventually be freed with <code>free_wav</code>. <code>buffer</code> is the buffer created
217by <code>load_wav</code>. </p>
8758037a 218
219</div>
e3282ccf 220<h2 id="convert">convert</h2>
221<div id="convert_CONTENT">
222<pre> SDL::Audio-&gt;convert( cvt, data, len )
8758037a 223
e3282ccf 224</pre>
8758037a 225<p>Converts audio data to a desired audio format.</p>
fb2dc882 226<p><code>convert</code> takes as first parameter <code>cvt</code>, which was previously initialized. Initializing a <code>SDL::AudioCVT</code> is a two step process.
227First of all, the structure must be created via <code>SDL::AudioCVT-&gt;build</code> along with source and destination format parameters. Secondly,
e3282ccf 228the <code>data</code> and <code>len</code> fields must be setup. <code>data</code> should point to the audio data buffer beeing source and destination at
229once and <code>len</code> should be set to the buffer length in bytes. Remember, the length of the buffer pointed to by buf should be
230<code>len*len_mult</code> bytes in length.</p>
fb2dc882 231<p>Once the <code>SDL::AudioCVT</code> structure is initialized, we can pass it to <code>convert</code>, which will convert the audio data pointed to
232by <code>data</code>. If <code>convert</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
233<p>If the conversion completed successfully then the converted audio data can be read from <code>cvt-&gt;buf</code>. The amount of valid, converted,
234audio data in the buffer is equal to <code>cvt-&gt;len*cvt-&gt;len_ratio</code>. </p>
e3282ccf 235<p>Example:</p>
236<pre> use SDL;
237 use SDL::Audio;
238 use SDL::AudioSpec;
239 use SDL::AudioCVT;
240
241 SDL::init(SDL_INIT_AUDIO);
242
243 # Converting some WAV data to hardware format
244
245 my $desired = SDL::AudioSpec-&gt;new();
246 my $obtained = SDL::AudioSpec-&gt;new();
247
248 # Set desired format
249 $desired-&gt;freq(22050);
250 $desired-&gt;channels(1);
251 $desired-&gt;format(AUDIO_S16);
252 $desired-&gt;samples(8192);
253
254 # Open the audio device
255 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
256 {
257 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
258 exit(-1);
259 }
260
261 # Load the test.wav
262 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
263
264 unless( $wav_ref )
265 {
15c551d8 266 warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
e3282ccf 267 SDL::Audio::close_audio();
268 SDL::quit;
269 exit(-1);
270 }
271
272 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
273
274 # Build AudioCVT
275 my $wav_cvt = SDL::AudioCVT-&gt;build( $wav_spec-&gt;format, $wav_spec-&gt;channels, $wav_spec-&gt;freq,
276 $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq);
277
278 # Check that the convert was built
279 if( $wav_cvt == -1 )
280 {
15c551d8 281 warn( &quot;Couldn't build converter!\n&quot; );
e3282ccf 282 SDL::Audio::close();
283 SDL::Audio::free_wav($wav_buf);
284 SDL::quit();
285 exit(-1);
286 }
287
288 # And now we're ready to convert
289 SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
290
e3282ccf 291 # We can freeto original WAV data now
292 SDL::Audio::free_wav($wav_buf);
293
e3282ccf 294</pre>
295<p><strong>TODO</strong>: What to do with it? How to use callback? See http://www.libsdl.org/cgi/docwiki.cgi/SDL_ConvertAudio</p>
8758037a 296
297</div>
15c551d8 298<h2 id="mix">mix</h2>
299<div id="mix_CONTENT">
8758037a 300<p>Mixes audio data</p>
15c551d8 301<p><strong>Not implemented yet</strong>. See: <a href="http://www.libsdl.org/cgi/docwiki.cgi/SDL_MixAudio">http://www.libsdl.org/cgi/docwiki.cgi/SDL_MixAudio</a></p>
8758037a 302
303</div>
e3282ccf 304<h2 id="lock">lock</h2>
305<div id="lock_CONTENT">
15c551d8 306<pre> lock();
307
308</pre>
309<p>The lock manipulated by these functions protects the callback function. During a <code>lock</code> period, you can be guaranteed that the callback
310function is not running. Do not call this from the callback function or you will cause deadlock.</p>
8758037a 311
312</div>
e3282ccf 313<h2 id="unlock">unlock</h2>
314<div id="unlock_CONTENT">
15c551d8 315<pre> unlock();
316
317</pre>
318<p>Unlocks a previous <code>lock</code> call.</p>
8758037a 319
320</div>
e3282ccf 321<h2 id="close">close </h2>
322<div id="close_CONTENT">
15c551d8 323<pre> close();
324
325</pre>
8758037a 326<p>Shuts down audio processing and closes the audio device. </p>
327
328</div>
b82df135 329</div>