/lib/pods/SDL/Audio.pod

http://github.com/PerlGameDev/SDL · Unknown · 294 lines · 185 code · 109 blank · 0 comment · 0 complexity · c5fc848a066488d93a558fcf034a6b1f MD5 · raw file

  1. =pod
  2. =head1 NAME
  3. SDL::Audio - SDL Bindings for Audio
  4. =head1 CATEGORY
  5. Core, Audio
  6. =head1 CONSTANTS
  7. The constants are exported by default. You can avoid this by doing:
  8. use SDL::Audio ();
  9. and access them directly:
  10. SDL::Audio::AUDIO_S16SYS;
  11. or by choosing the export tags below:
  12. Export tag: ':format'
  13. AUDIO_U8
  14. AUDIO_S8
  15. AUDIO_U16LSB
  16. AUDIO_S16LSB
  17. AUDIO_U16MSB
  18. AUDIO_S16MSB
  19. AUDIO_U16
  20. AUDIO_S16
  21. AUDIO_U16SYS
  22. AUDIO_S16SYS
  23. Export tag: ':status'
  24. SDL_AUDIO_STOPPED
  25. SDL_AUDIO_PLAYING
  26. SDL_AUDIO_PAUSED
  27. =head1 METHODS
  28. =head2 open
  29. use SDL;
  30. use SDL::Audio;
  31. SDL::init(SDL_INIT_AUDIO);
  32. my $desired = SDL::AudioSpec->new();
  33. my $obtained;
  34. SDL::Audio::open( $desired, $obtained );
  35. # $obtained->... (A new SDL::AudioSpec now);
  36. 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.
  37. To open the audio device a desired SDL::AudioSpec must be created.
  38. my $desired = SDL::AudioSpec->new();
  39. You must then fill this structure with your desired audio specifications.
  40. =over
  41. =item The desired audio frequency in samples-per-second.
  42. $desired->freq
  43. =item The desired audio format. See L<SDL::AudioSpec>
  44. $desired->format
  45. =item The desired channels (1 for mono, 2 for stereo, 4 for surround, 6 for surround with center and lfe).
  46. $desired->channels
  47. =item 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
  48. $desired->samples
  49. =item 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.
  50. THIS IS NOT READY YET
  51. $desired->callback
  52. my $callback = sub{ my ($userdata, $stream, $len) = @_; };
  53. $userdata is a reference stored in the userdata field of the SDL::AudioSpec.
  54. $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.
  55. $desired->userdata
  56. This pointer is passed as the first parameter to the callback function.
  57. =back
  58. 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.
  59. 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 L<SDL::AudioCVT>'s for converting loaded data to the hardware format.
  60. 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
  61. 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.
  62. =head2 pause
  63. pause( $bool )
  64. This function pauses and unpauses the audio callback processing. It should be called with C<$bool = 0> after opening the audio device to
  65. start playing sound. This is so you can safely initialize data for your callback function after opening the audio device. Silence will
  66. be written to the audio device during the pause.
  67. =head2 get_status
  68. int get_status();
  69. Returns either C<SDL_AUDIO_STOPPED>, C<SDL_AUDIO_PLAYING> or C<SDL_AUDIO_PAUSED> depending on the current audio state.
  70. =head2 load_wav
  71. SDL::AudioSpec load_wav( $filename, $spec );
  72. This function loads a WAVE file into memory.
  73. If this function succeeds, it returns the given C<SDL::AudioSpec>, filled with the audio data format of the wave data, and sets C<buf>
  74. to a buffer containing the audio data, and sets C<len> to the length of that audio buffer, in bytes. You need to free the audio buffer
  75. with C<SDL::Audio::free_wav> when you are done with it.
  76. 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.
  77. Currently raw, MS-ADPCM and IMA-ADPCM WAVE files are supported.
  78. Example:
  79. use SDL;
  80. use SDL::Audio;
  81. use SDL::AudioSpec;
  82. SDL::init(SDL_INIT_AUDIO);
  83. # Converting some WAV data to hardware format
  84. my $desired = SDL::AudioSpec->new();
  85. my $obtained = SDL::AudioSpec->new();
  86. # Set desired format
  87. $desired->freq(22050);
  88. $desired->channels(1);
  89. $desired->format(AUDIO_S16);
  90. $desired->samples(8192);
  91. # Open the audio device
  92. if( SDL::Audio::open($desired, $obtained) < 0 )
  93. {
  94. printf( STDERR "Couldn't open audio: %s\n", SDL::get_error() );
  95. exit(-1);
  96. }
  97. # Load the test.wav
  98. my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
  99. unless( $wav_ref )
  100. {
  101. warn( "Could not open sample.wav: %s\n", SDL::get_error() );
  102. SDL::Audio::close_audio();
  103. SDL::quit;
  104. exit(-1);
  105. }
  106. my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
  107. =head2 free_wav
  108. free_wav( $buffer )
  109. After a WAVE file has been opened with C<load_wav> its data can eventually be freed with C<free_wav>. C<buffer> is the buffer created
  110. by C<load_wav>.
  111. =head2 convert
  112. SDL::Audio->convert( cvt, data, len )
  113. Converts audio data to a desired audio format.
  114. C<convert> takes as first parameter C<cvt>, which was previously initialized. Initializing a C<SDL::AudioCVT> is a two step process.
  115. First of all, the structure must be created via C<SDL::AudioCVT-E<gt>build> along with source and destination format parameters. Secondly,
  116. the C<data> and C<len> fields must be setup. C<data> should point to the audio data buffer being source and destination at
  117. once and C<len> should be set to the buffer length in bytes. Remember, the length of the buffer pointed to by buf should be
  118. C<len*len_mult> bytes in length.
  119. Once the C<SDL::AudioCVT> structure is initialized, we can pass it to C<convert>, which will convert the audio data pointed to
  120. by C<data>. If C<convert> fails C<undef> is returned, otherwise the converted C<SDL::AudioCVT> structure.
  121. If the conversion completed successfully then the converted audio data can be read from C<cvt-E<gt>buf>. The amount of valid, converted,
  122. audio data in the buffer is equal to C<cvt-E<gt>len*cvt-E<gt>len_ratio>.
  123. Example:
  124. use SDL;
  125. use SDL::Audio;
  126. use SDL::AudioSpec;
  127. use SDL::AudioCVT;
  128. SDL::init(SDL_INIT_AUDIO);
  129. # Converting some WAV data to hardware format
  130. my $desired = SDL::AudioSpec->new();
  131. my $obtained = SDL::AudioSpec->new();
  132. # Set desired format
  133. $desired->freq(22050);
  134. $desired->channels(1);
  135. $desired->format(AUDIO_S16);
  136. $desired->samples(8192);
  137. # Open the audio device
  138. if( SDL::Audio::open($desired, $obtained) < 0 )
  139. {
  140. printf( STDERR "Couldn't open audio: %s\n", SDL::get_error() );
  141. exit(-1);
  142. }
  143. # Load the test.wav
  144. my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obtained);
  145. unless( $wav_ref )
  146. {
  147. warn( "Could not open sample.wav: %s\n", SDL::get_error() );
  148. SDL::Audio::close_audio();
  149. SDL::quit;
  150. exit(-1);
  151. }
  152. my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
  153. # Build AudioCVT
  154. my $wav_cvt = SDL::AudioCVT->build( $wav_spec->format, $wav_spec->channels, $wav_spec->freq,
  155. $obtained->format, $obtained->channels, $obtained->freq);
  156. # Check that the convert was built
  157. if( $wav_cvt == -1 )
  158. {
  159. warn( "Couldn't build converter!\n" );
  160. SDL::Audio::close();
  161. SDL::Audio::free_wav($wav_buf);
  162. SDL::quit();
  163. exit(-1);
  164. }
  165. # And now we're ready to convert
  166. SDL::Audio::convert($wav_cvt, $wav_buf, $wav_len);
  167. # We can free original WAV data now
  168. SDL::Audio::free_wav($wav_buf);
  169. B<TODO>: What to do with it? How to use callback? See http://www.libsdl.org/cgi/docwiki.cgi/SDL_ConvertAudio
  170. =head2 mix
  171. Mixes audio data
  172. B<Not implemented yet>. See: L<http://www.libsdl.org/cgi/docwiki.cgi/SDL_MixAudio>
  173. =head2 lock
  174. lock();
  175. The lock manipulated by these functions protects the callback function. During a C<lock> period, you can be guaranteed that the callback
  176. function is not running. Do not call this from the callback function or you will cause deadlock.
  177. =head2 unlock
  178. unlock();
  179. Unlocks a previous C<lock> call.
  180. =head2 close
  181. close();
  182. Shuts down audio processing and closes the audio device.
  183. =head1 AUTHORS
  184. See L<SDL/AUTHORS>.
  185. =cut