update
[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>
7<li><a href="#DESCRIPTION">DESCRIPTION</a></li>
8<li><a href="#METHODS">METHODS</a>
e3282ccf 9<ul><li><a href="#open">open</a>
10<ul><li><a href="#Synopsis">Synopsis</a></li>
11<li><a href="#Description">Description</a></li>
12</ul>
13</li>
14<li><a href="#pause">pause</a></li>
8758037a 15<li><a href="#GetAudioStatus">GetAudioStatus </a></li>
e3282ccf 16<li><a href="#load_WAV">load_WAV </a></li>
17<li><a href="#free_WAV">free_WAV </a></li>
18<li><a href="#convert">convert</a></li>
8758037a 19<li><a href="#MixAudio">MixAudio </a></li>
e3282ccf 20<li><a href="#lock">lock</a></li>
21<li><a href="#unlock">unlock</a></li>
22<li><a href="#close">close </a>
8758037a 23</li>
24</ul>
b82df135 25</li>
26</ul><hr />
27<!-- INDEX END -->
28
29<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
30<div id="NAME_CONTENT">
31<p>SDL::Audio -- SDL Bindings for Audio</p>
32
33</div>
34<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
35<div id="CATEGORY_CONTENT">
36<p>TODO, Core, Audio</p>
37
38</div>
8758037a 39<h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
40<div id="DESCRIPTION_CONTENT">
41
42
43
44
45
46</div>
47<h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
48<div id="METHODS_CONTENT">
49
50</div>
e3282ccf 51<h2 id="open">open</h2>
52<div id="open_CONTENT">
8758037a 53<p>Opens the audio device with the desired parameters.</p>
54
55</div>
e3282ccf 56<h3 id="Synopsis">Synopsis</h3>
57<div id="Synopsis_CONTENT">
58<pre> use SDL;
59 use SDL::Audio;
60
61 SDL::init(SDL_INIT_AUDIO);
62
63 my $desired = SDL::AudioSpec-&gt;new();
64
65 my $obtained;
66
67 SDL::open_audio($desired, $obtained);
68
69 # $obtained-&gt;... (A new SDL::AudioSpec now);
70
71</pre>
72
73</div>
74<h3 id="Description">Description</h3>
75<div id="Description_CONTENT">
76<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>
77<p>To open the audio device a desired SDL::AudioSpec must be created.</p>
78<pre> my $desired = SDL::AudioSpec-&gt;new();
79
80</pre>
81<p>You must then fill this structure with your desired audio specifications.</p>
82<dl>
83 <dt>The desired audio frequency in samples-per-second. </dt>
84 <dd>
85<pre> $desired-&gt;freq
86
87</pre>
88 </dd>
89 <dt>The desired audio format. See <cite>SDL::AudioSpec</cite></dt>
90 <dd>
91<pre> $desired-&gt;format
92
93</pre>
94 </dd>
95 <dt>The desired channels (1 for mono, 2 for stereo, 4 for surround, 6 for surround with center and lfe). </dt>
96 <dd>
97<pre> $desired-&gt;channels
98
99</pre>
100 </dd>
101 <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>
102 <dd>
103<pre> $desired-&gt;samples
104
105</pre>
106 </dd>
107 <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>
108 <dd>
109 <p>THIS IS NOT READY YET</p>
110<pre> $desired-&gt;callback
111
112 my $callback= sub{ my ($userdata, $stream, $len) = @_; };
113
114 $userdata is a reference stored in the userdata field of the SDL::AudioSpec.
115 $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.
116
117 $desired-&gt;userdata
118
119 This pointer is passed as the first parameter to the callback function.
120
121</pre>
122 </dd>
123</dl>
124<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>
125<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 <cite>SDL::AudioCVT</cite>'s for converting loaded data to the hardware format.</p>
126<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>
127<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>
128
129</div>
130<h2 id="pause">pause</h2>
131<div id="pause_CONTENT">
8758037a 132<p>Pauses and unpauses the audio callback processing</p>
133
134</div>
135<h2 id="GetAudioStatus">GetAudioStatus </h2>
136<div id="GetAudioStatus_CONTENT">
137<p>Gets the current audio state</p>
138
139</div>
e3282ccf 140<h2 id="load_WAV">load_WAV </h2>
141<div id="load_WAV_CONTENT">
8758037a 142<p>Loads a WAVE file</p>
143
144</div>
e3282ccf 145<h2 id="free_WAV">free_WAV </h2>
146<div id="free_WAV_CONTENT">
8758037a 147<p>Frees previously opened WAV data</p>
148
149</div>
e3282ccf 150<h2 id="convert">convert</h2>
151<div id="convert_CONTENT">
152<pre> SDL::Audio-&gt;convert( cvt, data, len )
8758037a 153
e3282ccf 154</pre>
8758037a 155<p>Converts audio data to a desired audio format.</p>
e3282ccf 156<p><code>convert_audio</code> takes as first parameter <code>cvt</code>, which was previously initialized. Initializing a <code>SDL::AudioCVT</code> is a two step process.
157First of all, the structure must be created via <code>SDL::AudioCVT-</code>build&gt; along with source and destination format parameters. Secondly,
158the <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
159once 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
160<code>len*len_mult</code> bytes in length.</p>
161<p>Once the <code>SDL::AudioCVT</code> structure is initialized, we can pass it to <code>convert_audio</code>, which will convert the audio data pointed to
162by <code>data</code>. If <code>convert_audio</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
163<p>If the conversion completed successfully then the converted audio data can be read from <code>cvt-</code>buf&gt;. The amount of valid, converted,
164audio data in the buffer is equal to <code>cvt-</code>len*cvt-&gt;len_ratio&gt;. </p>
165<p>Example:</p>
166<pre> use SDL;
167 use SDL::Audio;
168 use SDL::AudioSpec;
169 use SDL::AudioCVT;
170
171 SDL::init(SDL_INIT_AUDIO);
172
173 # Converting some WAV data to hardware format
174
175 my $desired = SDL::AudioSpec-&gt;new();
176 my $obtained = SDL::AudioSpec-&gt;new();
177
178 # Set desired format
179 $desired-&gt;freq(22050);
180 $desired-&gt;channels(1);
181 $desired-&gt;format(AUDIO_S16);
182 $desired-&gt;samples(8192);
183
184 # Open the audio device
185 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
186 {
187 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
188 exit(-1);
189 }
190
191 # Load the test.wav
192 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
193
194 unless( $wav_ref )
195 {
196 warn(&quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
197 SDL::Audio::close_audio();
198 SDL::quit;
199 exit(-1);
200 }
201
202 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
203
204 # Build AudioCVT
205 my $wav_cvt = SDL::AudioCVT-&gt;build( $wav_spec-&gt;format, $wav_spec-&gt;channels, $wav_spec-&gt;freq,
206 $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq);
207
208 # Check that the convert was built
209 if( $wav_cvt == -1 )
210 {
211 warn(&quot;Couldn't build converter!\n&quot; );
212 SDL::Audio::close();
213 SDL::Audio::free_wav($wav_buf);
214 SDL::quit();
215 exit(-1);
216 }
217
218 # And now we're ready to convert
219 SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
220
221
222
223
224 # We can freeto original WAV data now
225 SDL::Audio::free_wav($wav_buf);
226
227
228
229
230
231
232
233</pre>
234<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 235
236</div>
237<h2 id="MixAudio">MixAudio </h2>
238<div id="MixAudio_CONTENT">
239<p>Mixes audio data</p>
240
241</div>
e3282ccf 242<h2 id="lock">lock</h2>
243<div id="lock_CONTENT">
8758037a 244<p>Locks out the callback function</p>
245
246</div>
e3282ccf 247<h2 id="unlock">unlock</h2>
248<div id="unlock_CONTENT">
8758037a 249<p>Unlocks the callback function</p>
250
251</div>
e3282ccf 252<h2 id="close">close </h2>
253<div id="close_CONTENT">
8758037a 254<p>Shuts down audio processing and closes the audio device. </p>
255
256</div>
b82df135 257</div>