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