docs for audio
[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>
fb2dc882 156<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.
157First of all, the structure must be created via <code>SDL::AudioCVT-&gt;build</code> along with source and destination format parameters. Secondly,
e3282ccf 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>
fb2dc882 161<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
162by <code>data</code>. If <code>convert</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-&gt;buf</code>. The amount of valid, converted,
164audio data in the buffer is equal to <code>cvt-&gt;len*cvt-&gt;len_ratio</code>. </p>
e3282ccf 165<p>Example:</p>
166<pre> use SDL;
167 use SDL::Audio;
168 use SDL::AudioSpec;
169 use SDL::AudioCVT;
fb2dc882 170 use Devel::Peek;
e3282ccf 171
172 SDL::init(SDL_INIT_AUDIO);
173
174 # Converting some WAV data to hardware format
175
176 my $desired = SDL::AudioSpec-&gt;new();
177 my $obtained = SDL::AudioSpec-&gt;new();
178
179 # Set desired format
180 $desired-&gt;freq(22050);
181 $desired-&gt;channels(1);
182 $desired-&gt;format(AUDIO_S16);
183 $desired-&gt;samples(8192);
184
185 # Open the audio device
186 if( SDL::Audio::open($desired, $obtained) &lt; 0 )
187 {
188 printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
189 exit(-1);
190 }
191
192 # Load the test.wav
193 my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
194
195 unless( $wav_ref )
196 {
197 warn(&quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
198 SDL::Audio::close_audio();
199 SDL::quit;
200 exit(-1);
201 }
202
203 my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
204
205 # Build AudioCVT
206 my $wav_cvt = SDL::AudioCVT-&gt;build( $wav_spec-&gt;format, $wav_spec-&gt;channels, $wav_spec-&gt;freq,
207 $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq);
208
209 # Check that the convert was built
210 if( $wav_cvt == -1 )
211 {
212 warn(&quot;Couldn't build converter!\n&quot; );
213 SDL::Audio::close();
214 SDL::Audio::free_wav($wav_buf);
215 SDL::quit();
216 exit(-1);
217 }
218
219 # And now we're ready to convert
220 SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
221
e3282ccf 222 # We can freeto original WAV data now
223 SDL::Audio::free_wav($wav_buf);
224
e3282ccf 225</pre>
226<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 227
228</div>
229<h2 id="MixAudio">MixAudio </h2>
230<div id="MixAudio_CONTENT">
231<p>Mixes audio data</p>
232
233</div>
e3282ccf 234<h2 id="lock">lock</h2>
235<div id="lock_CONTENT">
8758037a 236<p>Locks out the callback function</p>
237
238</div>
e3282ccf 239<h2 id="unlock">unlock</h2>
240<div id="unlock_CONTENT">
8758037a 241<p>Unlocks the callback function</p>
242
243</div>
e3282ccf 244<h2 id="close">close </h2>
245<div id="close_CONTENT">
8758037a 246<p>Shuts down audio processing and closes the audio device. </p>
247
248</div>
b82df135 249</div>