Merge branch 'master' of git.shadowcat.co.uk:SDL-Site
[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>
c7e8d3c6 18<li><a href="#close">close </a></li>
8758037a 19</ul>
b82df135 20</li>
c7e8d3c6 21<li><a href="#AUTHORS">AUTHORS</a>
22</li>
b82df135 23</ul><hr />
24<!-- INDEX END -->
25
26<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
27<div id="NAME_CONTENT">
260482f9 28<p>SDL::Audio - SDL Bindings for Audio</p>
b82df135 29
30</div>
31<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
32<div id="CATEGORY_CONTENT">
260482f9 33<p>Core, Audio</p>
b82df135 34
35</div>
fb5f7900 36<h1 id="CONSTANTS">CONSTANTS</h1><p><a href="#TOP" class="toplink">Top</a></p>
37<div id="CONSTANTS_CONTENT">
3549ee6a 38<p>The constants are exported by default. You can avoid this by doing:</p>
39<pre> use SDL::Audio ();
fb5f7900 40
41</pre>
3549ee6a 42<p>and access them directly:</p>
fb5f7900 43<pre> SDL::Audio::AUDIO_S16SYS;
44
45</pre>
46<p>or by choosing the export tags below:</p>
47<p>Export tag: ':format'</p>
48<pre> AUDIO_U8
49 AUDIO_S8
50 AUDIO_U16LSB
51 AUDIO_S16LSB
52 AUDIO_U16MSB
53 AUDIO_S16MSB
54 AUDIO_U16
55 AUDIO_S16
56 AUDIO_U16SYS
57 AUDIO_S16SYS
58
59</pre>
60<p>Export tag: ':status'</p>
61<pre> SDL_AUDIO_STOPPED
62 SDL_AUDIO_PLAYING
63 SDL_AUDIO_PAUSED
64
65</pre>
66
67</div>
8758037a 68<h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
69<div id="METHODS_CONTENT">
70
71</div>
e3282ccf 72<h2 id="open">open</h2>
73<div id="open_CONTENT">
e3282ccf 74<pre> use SDL;
75 use SDL::Audio;
76
77 SDL::init(SDL_INIT_AUDIO);
78
79 my $desired = SDL::AudioSpec-&gt;new();
80
81 my $obtained;
82
15c551d8 83 SDL::Audio::open( $desired, $obtained );
e3282ccf 84
85 # $obtained-&gt;... (A new SDL::AudioSpec now);
86
87</pre>
e3282ccf 88<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>
89<p>To open the audio device a desired SDL::AudioSpec must be created.</p>
90<pre> my $desired = SDL::AudioSpec-&gt;new();
91
92</pre>
93<p>You must then fill this structure with your desired audio specifications.</p>
94<dl>
95 <dt>The desired audio frequency in samples-per-second. </dt>
96 <dd>
97<pre> $desired-&gt;freq
98
99</pre>
100 </dd>
55bbf7a2 101 <dt>The desired audio format. See <a href="SDL-AudioSpec.html">SDL::AudioSpec</a></dt>
e3282ccf 102 <dd>
103<pre> $desired-&gt;format
104
105</pre>
106 </dd>
107 <dt>The desired channels (1 for mono, 2 for stereo, 4 for surround, 6 for surround with center and lfe). </dt>
108 <dd>
109<pre> $desired-&gt;channels
110
111</pre>
112 </dd>
113 <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>
114 <dd>
115<pre> $desired-&gt;samples
116
117</pre>
118 </dd>
119 <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>
120 <dd>
121 <p>THIS IS NOT READY YET</p>
122<pre> $desired-&gt;callback
123
15c551d8 124 my $callback = sub{ my ($userdata, $stream, $len) = @_; };
e3282ccf 125
126 $userdata is a reference stored in the userdata field of the SDL::AudioSpec.
127 $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.
128
129 $desired-&gt;userdata
130
131 This pointer is passed as the first parameter to the callback function.
132
133</pre>
134 </dd>
135</dl>
136<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 137<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 138<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>
139<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>
140
141</div>
142<h2 id="pause">pause</h2>
143<div id="pause_CONTENT">
15c551d8 144<pre> pause( $bool )
145
146</pre>
147<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
148start playing sound. This is so you can safely initialize data for your callback function after opening the audio device. Silence will
149be written to the audio device during the pause.</p>
8758037a 150
151</div>
15c551d8 152<h2 id="get_status">get_status</h2>
153<div id="get_status_CONTENT">
154<pre> int get_status();
155
156</pre>
157<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 158
159</div>
15c551d8 160<h2 id="load_wav">load_wav </h2>
161<div id="load_wav_CONTENT">
162<pre> SDL::AudioSpec load_wav( $filename, $spec );
163
164</pre>
165<p>This function loads a WAVE file into memory.</p>
166<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>
167to 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
168with <code>SDL::Audio::free_wav</code> when you are done with it.</p>
169<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.
170Currently raw, MS-ADPCM and IMA-ADPCM WAVE files are supported.</p>
171<p>Example:</p>
172<pre> use SDL;
173 use SDL::Audio;
174 use SDL::AudioSpec;
175
176 SDL::init(SDL_INIT_AUDIO);
177
178 # Converting some WAV data to hardware format
179
180 my $desired = SDL::AudioSpec-&gt;new();
181 my $obtained = SDL::AudioSpec-&gt;new();
182
183 # Set desired format
184 $desired-&gt;freq(22050);
185 $desired-&gt;channels(1);
186 $desired-&gt;format(AUDIO_S16);
187 $desired-&gt;samples(8192);
188
189 # Open the audio device
190 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
191 {
192 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
193 exit(-1);
194 }
195
196 # Load the test.wav
197 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
198
199 unless( $wav_ref )
200 {
201 warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
202 SDL::Audio::close_audio();
203 SDL::quit;
204 exit(-1);
205 }
206
207 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
208
209</pre>
8758037a 210
211</div>
15c551d8 212<h2 id="free_wav">free_wav </h2>
213<div id="free_wav_CONTENT">
214<pre> free_wav( $buffer )
215
216</pre>
217<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
218by <code>load_wav</code>. </p>
8758037a 219
220</div>
e3282ccf 221<h2 id="convert">convert</h2>
222<div id="convert_CONTENT">
223<pre> SDL::Audio-&gt;convert( cvt, data, len )
8758037a 224
e3282ccf 225</pre>
8758037a 226<p>Converts audio data to a desired audio format.</p>
fb2dc882 227<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.
228First of all, the structure must be created via <code>SDL::AudioCVT-&gt;build</code> along with source and destination format parameters. Secondly,
e3282ccf 229the <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
230once 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
231<code>len*len_mult</code> bytes in length.</p>
fb2dc882 232<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
233by <code>data</code>. If <code>convert</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
234<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,
235audio data in the buffer is equal to <code>cvt-&gt;len*cvt-&gt;len_ratio</code>. </p>
e3282ccf 236<p>Example:</p>
237<pre> use SDL;
238 use SDL::Audio;
239 use SDL::AudioSpec;
240 use SDL::AudioCVT;
241
242 SDL::init(SDL_INIT_AUDIO);
243
244 # Converting some WAV data to hardware format
245
246 my $desired = SDL::AudioSpec-&gt;new();
247 my $obtained = SDL::AudioSpec-&gt;new();
248
249 # Set desired format
250 $desired-&gt;freq(22050);
251 $desired-&gt;channels(1);
252 $desired-&gt;format(AUDIO_S16);
253 $desired-&gt;samples(8192);
254
255 # Open the audio device
256 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
257 {
258 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
259 exit(-1);
260 }
261
262 # Load the test.wav
263 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
264
265 unless( $wav_ref )
266 {
15c551d8 267 warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
e3282ccf 268 SDL::Audio::close_audio();
269 SDL::quit;
270 exit(-1);
271 }
272
273 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
274
275 # Build AudioCVT
276 my $wav_cvt = SDL::AudioCVT-&gt;build( $wav_spec-&gt;format, $wav_spec-&gt;channels, $wav_spec-&gt;freq,
277 $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq);
278
279 # Check that the convert was built
280 if( $wav_cvt == -1 )
281 {
15c551d8 282 warn( &quot;Couldn't build converter!\n&quot; );
e3282ccf 283 SDL::Audio::close();
284 SDL::Audio::free_wav($wav_buf);
285 SDL::quit();
286 exit(-1);
287 }
288
289 # And now we're ready to convert
290 SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
291
e3282ccf 292 # We can freeto original WAV data now
293 SDL::Audio::free_wav($wav_buf);
294
e3282ccf 295</pre>
296<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 297
298</div>
15c551d8 299<h2 id="mix">mix</h2>
300<div id="mix_CONTENT">
8758037a 301<p>Mixes audio data</p>
15c551d8 302<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 303
304</div>
e3282ccf 305<h2 id="lock">lock</h2>
306<div id="lock_CONTENT">
15c551d8 307<pre> lock();
308
309</pre>
310<p>The lock manipulated by these functions protects the callback function. During a <code>lock</code> period, you can be guaranteed that the callback
311function is not running. Do not call this from the callback function or you will cause deadlock.</p>
8758037a 312
313</div>
e3282ccf 314<h2 id="unlock">unlock</h2>
315<div id="unlock_CONTENT">
15c551d8 316<pre> unlock();
317
318</pre>
319<p>Unlocks a previous <code>lock</code> call.</p>
8758037a 320
321</div>
e3282ccf 322<h2 id="close">close </h2>
323<div id="close_CONTENT">
15c551d8 324<pre> close();
325
326</pre>
8758037a 327<p>Shuts down audio processing and closes the audio device. </p>
328
329</div>
c7e8d3c6 330<h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
331<div id="AUTHORS_CONTENT">
1dbe1697 332<p>See <a href="/SDL.html#AUTHORS">/SDL.html#AUTHORS</a>.</p>
c7e8d3c6 333
334</div>
b82df135 335</div>