updated docs
[sdlgit/SDL-Site.git] / pages / SDL-Audio.html-inc
index 269cefb..b8c5b8f 100644 (file)
 
 <ul><li><a href="#NAME">NAME</a></li>
 <li><a href="#CATEGORY">CATEGORY</a></li>
-<li><a href="#DESCRIPTION">DESCRIPTION</a></li>
+<li><a href="#CONSTANTS">CONSTANTS</a></li>
 <li><a href="#METHODS">METHODS</a>
-<ul><li><a href="#open_audio">open_audio</a></li>
-<li><a href="#PauseAudio">PauseAudio </a></li>
-<li><a href="#GetAudioStatus">GetAudioStatus </a></li>
-<li><a href="#LoadWAV">LoadWAV </a></li>
-<li><a href="#FreeWAV">FreeWAV </a></li>
-<li><a href="#convert_audio">convert_audio</a></li>
-<li><a href="#MixAudio">MixAudio </a></li>
-<li><a href="#LockAudio">LockAudio</a></li>
-<li><a href="#UnlockAudio">UnlockAudio</a></li>
-<li><a href="#CloseAudio">CloseAudio </a>
-</li>
+<ul><li><a href="#open">open</a></li>
+<li><a href="#pause">pause</a></li>
+<li><a href="#get_status">get_status</a></li>
+<li><a href="#load_wav">load_wav </a></li>
+<li><a href="#free_wav">free_wav </a></li>
+<li><a href="#convert">convert</a></li>
+<li><a href="#mix">mix</a></li>
+<li><a href="#lock">lock</a></li>
+<li><a href="#unlock">unlock</a></li>
+<li><a href="#close">close </a></li>
 </ul>
 </li>
+<li><a href="#AUTHORS">AUTHORS</a>
+</li>
 </ul><hr />
 <!-- INDEX END -->
 
 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
 <div id="NAME_CONTENT">
-<p>SDL::Audio -- SDL Bindings for Audio</p>
+<p>SDL::Audio - SDL Bindings for Audio</p>
 
 </div>
 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
 <div id="CATEGORY_CONTENT">
-<p>TODO, Core, Audio</p>
+<p>Core, Audio</p>
 
 </div>
-<h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
-<div id="DESCRIPTION_CONTENT">
+<h1 id="CONSTANTS">CONSTANTS</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="CONSTANTS_CONTENT">
+<p>The constants are exported by default. You can avoid this by doing:</p>
+<pre> use SDL::Audio ();
 
+</pre>
+<p>and access them directly:</p>
+<pre> SDL::Audio::AUDIO_S16SYS;
 
+</pre>
+<p>or by choosing the export tags below:</p>
+<p>Export tag: ':format'</p>
+<pre> AUDIO_U8
+ AUDIO_S8
+ AUDIO_U16LSB
+ AUDIO_S16LSB
+ AUDIO_U16MSB
+ AUDIO_S16MSB
+ AUDIO_U16
+ AUDIO_S16 
+ AUDIO_U16SYS
+ AUDIO_S16SYS
 
+</pre>
+<p>Export tag: ':status'</p>
+<pre> SDL_AUDIO_STOPPED
+ SDL_AUDIO_PLAYING
+ SDL_AUDIO_PAUSED
 
+</pre>
 
 </div>
 <h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
 <div id="METHODS_CONTENT">
 
 </div>
-<h2 id="open_audio">open_audio</h2>
-<div id="open_audio_CONTENT">
-<p>Opens the audio device with the desired parameters.</p>
+<h2 id="open">open</h2>
+<div id="open_CONTENT">
+<pre>  use SDL;
+  use SDL::Audio;
+
+  SDL::init(SDL_INIT_AUDIO);
+
+  my $desired = SDL::AudioSpec-&gt;new();
+
+  my $obtained;
+
+  SDL::Audio::open( $desired, $obtained );
+
+  # $obtained-&gt;... (A new SDL::AudioSpec now);
+
+</pre>
+<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>
+<p>To open the audio device a desired SDL::AudioSpec must be created.</p>
+<pre>  my $desired = SDL::AudioSpec-&gt;new();
+
+</pre>
+<p>You must then fill this structure with your desired audio specifications.</p>
+<dl>
+       <dt>The desired audio frequency in samples-per-second. </dt>
+       <dd>
+<pre>    $desired-&gt;freq
+
+</pre>
+       </dd>
+       <dt>The desired audio format. See <a href="SDL-AudioSpec.html">SDL::AudioSpec</a></dt>
+       <dd>
+<pre>    $desired-&gt;format
+
+</pre>
+       </dd>
+       <dt>The desired channels (1 for mono, 2 for stereo, 4 for surround, 6 for surround with center and lfe). </dt>
+       <dd>
+<pre>    $desired-&gt;channels
+
+</pre>
+       </dd>
+       <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>
+       <dd>
+<pre>    $desired-&gt;samples
+
+</pre>
+       </dd>
+       <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>
+       <dd>
+               <p>THIS IS NOT READY YET</p>
+<pre>    $desired-&gt;callback
+
+    my $callback = sub{ my ($userdata, $stream, $len) = @_;  };
+
+    $userdata is a reference stored in the userdata field of the SDL::AudioSpec. 
+    $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.
+
+    $desired-&gt;userdata
+
+    This pointer is passed as the first parameter to the callback function. 
+
+</pre>
+       </dd>
+</dl>
+<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>
+<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>
+<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>
+<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>
 
 </div>
-<h2 id="PauseAudio">PauseAudio </h2>
-<div id="PauseAudio_CONTENT">
-<p>Pauses and unpauses the audio callback processing</p>
+<h2 id="pause">pause</h2>
+<div id="pause_CONTENT">
+<pre> pause( $bool )
+
+</pre>
+<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 
+start playing sound. This is so you can safely initialize data for your callback function after opening the audio device. Silence will 
+be written to the audio device during the pause.</p>
 
 </div>
-<h2 id="GetAudioStatus">GetAudioStatus </h2>
-<div id="GetAudioStatus_CONTENT">
-<p>Gets the current audio state</p>
+<h2 id="get_status">get_status</h2>
+<div id="get_status_CONTENT">
+<pre> int get_status();
+
+</pre>
+<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>
 
 </div>
-<h2 id="LoadWAV">LoadWAV </h2>
-<div id="LoadWAV_CONTENT">
-<p>Loads a WAVE file</p>
+<h2 id="load_wav">load_wav </h2>
+<div id="load_wav_CONTENT">
+<pre> SDL::AudioSpec load_wav( $filename, $spec );
+
+</pre>
+<p>This function loads a WAVE file into memory.</p>
+<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> 
+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 
+with <code>SDL::Audio::free_wav</code> when you are done with it.</p>
+<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. 
+Currently raw, MS-ADPCM and IMA-ADPCM WAVE files are supported.</p>
+<p>Example:</p>
+<pre> use SDL;
+ use SDL::Audio;
+ use SDL::AudioSpec;
+
+ SDL::init(SDL_INIT_AUDIO);
+
+ # Converting some WAV data to hardware format
+
+ my $desired  = SDL::AudioSpec-&gt;new();
+ my $obtained = SDL::AudioSpec-&gt;new();
+
+ # Set desired format
+ $desired-&gt;freq(22050);
+ $desired-&gt;channels(1);
+ $desired-&gt;format(AUDIO_S16);
+ $desired-&gt;samples(8192);
+
+ # Open the audio device
+ if( SDL::Audio::open($desired, $obtained) &lt; 0 )
+ {
+     printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
+     exit(-1);
+ }
+
+ # Load the test.wav
+ my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
+
+ unless( $wav_ref )
+ {
+     warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
+     SDL::Audio::close_audio();
+     SDL::quit;
+     exit(-1);
+ }
+
+ my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
+
+</pre>
 
 </div>
-<h2 id="FreeWAV">FreeWAV </h2>
-<div id="FreeWAV_CONTENT">
-<p>Frees previously opened WAV data</p>
+<h2 id="free_wav">free_wav </h2>
+<div id="free_wav_CONTENT">
+<pre> free_wav( $buffer )
+
+</pre>
+<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 
+by <code>load_wav</code>. </p>
 
 </div>
-<h2 id="convert_audio">convert_audio</h2>
-<div id="convert_audio_CONTENT">
-<pre> SDL::Audio-&gt;convert_audio( cvt, data, len )
+<h2 id="convert">convert</h2>
+<div id="convert_CONTENT">
+<pre> SDL::Audio-&gt;convert( cvt, data, len )
 
 </pre>
 <p>Converts audio data to a desired audio format.</p>
-<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. 
-First of all, the structure must be created via <code>SDL::AudioCVT-</code>build&gt; along with source and destination format parameters. Secondly, 
+<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. 
+First of all, the structure must be created via <code>SDL::AudioCVT-&gt;build</code> along with source and destination format parameters. Secondly, 
 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 
 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 
 <code>len*len_mult</code> bytes in length.</p>
-<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 
-by <code>data</code>. If <code>convert_audio</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
-<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, 
-audio data in the buffer is equal to <code>cvt-</code>len*cvt-&gt;len_ratio&gt;. </p>
+<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 
+by <code>data</code>. If <code>convert</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
+<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, 
+audio data in the buffer is equal to <code>cvt-&gt;len*cvt-&gt;len_ratio</code>. </p>
 <p>Example:</p>
 <pre> use SDL;
  use SDL::Audio;
  use SDL::AudioSpec;
  use SDL::AudioCVT;
 
+ SDL::init(SDL_INIT_AUDIO);
+
  # Converting some WAV data to hardware format
 
  my $desired  = SDL::AudioSpec-&gt;new();
@@ -102,19 +253,20 @@ audio data in the buffer is equal to <code>cvt-</code>len*cvt-&gt;len_ratio&gt;.
  $desired-&gt;samples(8192);
 
  # Open the audio device
- if( SDL::Audio::open_audio($desired, $obtained) &lt; 0 )
+ if( SDL::Audio::open($desired, $obtained) &lt; 0 )
  {
      printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
      exit(-1);
  }
 
  # Load the test.wav
- my $wav_ref = SDL::Audio::load_wav('C:/SDL_perl/test/data/sample.wav', $obtained);
+ my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
 
  unless( $wav_ref )
  {
-     printf( STDERR &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
+     warn( &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
      SDL::Audio::close_audio();
+     SDL::quit;
      exit(-1);
  }
 
@@ -125,41 +277,59 @@ audio data in the buffer is equal to <code>cvt-</code>len*cvt-&gt;len_ratio&gt;.
                                      $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq); 
 
  # Check that the convert was built
- unless( $wav_cvt )
+ if( $wav_cvt == -1 )
  {
-     printf( STDERR &quot;Couldn't build converter!\n&quot; );
-     SDL::Audio::close_audio();
+     warn( &quot;Couldn't build converter!\n&quot; );
+     SDL::Audio::close();
      SDL::Audio::free_wav($wav_buf);
+     SDL::quit();
+     exit(-1);
  }
 
  # And now we're ready to convert
- SDL::Audio::convert_audio($wav_cvt, $wav_buf, $wav_len);
+ SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
 
- # We can delete to original WAV data now
+ # We can freeto original WAV data now
  SDL::Audio::free_wav($wav_buf);
 
 </pre>
 <p><strong>TODO</strong>: What to do with it? How to use callback? See http://www.libsdl.org/cgi/docwiki.cgi/SDL_ConvertAudio</p>
 
 </div>
-<h2 id="MixAudio">MixAudio </h2>
-<div id="MixAudio_CONTENT">
+<h2 id="mix">mix</h2>
+<div id="mix_CONTENT">
 <p>Mixes audio data</p>
+<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>
 
 </div>
-<h2 id="LockAudio">LockAudio</h2>
-<div id="LockAudio_CONTENT">
-<p>Locks out the callback function</p>
+<h2 id="lock">lock</h2>
+<div id="lock_CONTENT">
+<pre> lock();
+
+</pre>
+<p>The lock manipulated by these functions protects the callback function. During a <code>lock</code> period, you can be guaranteed that the callback 
+function is not running. Do not call this from the callback function or you will cause deadlock.</p>
 
 </div>
-<h2 id="UnlockAudio">UnlockAudio</h2>
-<div id="UnlockAudio_CONTENT">
-<p>Unlocks the callback function</p>
+<h2 id="unlock">unlock</h2>
+<div id="unlock_CONTENT">
+<pre> unlock();
+
+</pre>
+<p>Unlocks a previous <code>lock</code> call.</p>
 
 </div>
-<h2 id="CloseAudio">CloseAudio </h2>
-<div id="CloseAudio_CONTENT">
+<h2 id="close">close </h2>
+<div id="close_CONTENT">
+<pre> close();
+
+</pre>
 <p>Shuts down audio processing and closes the audio device.  </p>
 
 </div>
+<h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="AUTHORS_CONTENT">
+<p>See <b>AUTHORS</b> in <cite>SDL</cite>.</p>
+
+</div>
 </div>
\ No newline at end of file