docs for convert_audio and AudioCVT->build
[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="#DESCRIPTION">DESCRIPTION</a></li>
8 <li><a href="#METHODS">METHODS</a>
9 <ul><li><a href="#open_audio">open_audio</a></li>
10 <li><a href="#PauseAudio">PauseAudio </a></li>
11 <li><a href="#GetAudioStatus">GetAudioStatus </a></li>
12 <li><a href="#LoadWAV">LoadWAV </a></li>
13 <li><a href="#FreeWAV">FreeWAV </a></li>
14 <li><a href="#convert_audio">convert_audio</a></li>
15 <li><a href="#MixAudio">MixAudio </a></li>
16 <li><a href="#LockAudio">LockAudio</a></li>
17 <li><a href="#UnlockAudio">UnlockAudio</a></li>
18 <li><a href="#CloseAudio">CloseAudio </a>
19 </li>
20 </ul>
21 </li>
22 </ul><hr />
23 <!-- INDEX END -->
24
25 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
26 <div id="NAME_CONTENT">
27 <p>SDL::Audio -- SDL Bindings for Audio</p>
28
29 </div>
30 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
31 <div id="CATEGORY_CONTENT">
32 <p>TODO, Core, Audio</p>
33
34 </div>
35 <h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
36 <div id="DESCRIPTION_CONTENT">
37
38
39
40
41
42 </div>
43 <h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
44 <div id="METHODS_CONTENT">
45
46 </div>
47 <h2 id="open_audio">open_audio</h2>
48 <div id="open_audio_CONTENT">
49 <p>Opens the audio device with the desired parameters.</p>
50
51 </div>
52 <h2 id="PauseAudio">PauseAudio </h2>
53 <div id="PauseAudio_CONTENT">
54 <p>Pauses and unpauses the audio callback processing</p>
55
56 </div>
57 <h2 id="GetAudioStatus">GetAudioStatus </h2>
58 <div id="GetAudioStatus_CONTENT">
59 <p>Gets the current audio state</p>
60
61 </div>
62 <h2 id="LoadWAV">LoadWAV </h2>
63 <div id="LoadWAV_CONTENT">
64 <p>Loads a WAVE file</p>
65
66 </div>
67 <h2 id="FreeWAV">FreeWAV </h2>
68 <div id="FreeWAV_CONTENT">
69 <p>Frees previously opened WAV data</p>
70
71 </div>
72 <h2 id="convert_audio">convert_audio</h2>
73 <div id="convert_audio_CONTENT">
74 <pre> SDL::Audio-&gt;convert_audio( cvt, data, len )
75
76 </pre>
77 <p>Converts audio data to a desired audio format.</p>
78 <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. 
79 First of all, the structure must be created via <code>SDL::AudioCVT-</code>build&gt; along with source and destination format parameters. Secondly, 
80 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 
81 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 
82 <code>len*len_mult</code> bytes in length.</p>
83 <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 
84 by <code>data</code>. If <code>convert_audio</code> fails <code>undef</code> is returned, otherwise the converted <code>SDL::AudioCVT</code> structure.</p>
85 <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, 
86 audio data in the buffer is equal to <code>cvt-</code>len*cvt-&gt;len_ratio&gt;. </p>
87 <p>Example:</p>
88 <pre> use SDL;
89  use SDL::Audio;
90  use SDL::AudioSpec;
91  use SDL::AudioCVT;
92
93  # Converting some WAV data to hardware format
94
95  my $desired  = SDL::AudioSpec-&gt;new();
96  my $obtained = SDL::AudioSpec-&gt;new();
97
98  # Set desired format
99  $desired-&gt;freq(22050);
100  $desired-&gt;channels(1);
101  $desired-&gt;format(AUDIO_S16);
102  $desired-&gt;samples(8192);
103
104  # Open the audio device
105  if( SDL::Audio::open_audio($desired, $obtained) &lt; 0 )
106  {
107      printf( STDERR &quot;Couldn't open audio: %s\n&quot;, SDL::get_error() );
108      exit(-1);
109  }
110
111  # Load the test.wav
112  my $wav_ref = SDL::Audio::load_wav('C:/SDL_perl/test/data/sample.wav', $obtained);
113
114  unless( $wav_ref )
115  {
116      printf( STDERR &quot;Could not open sample.wav: %s\n&quot;, SDL::get_error() );
117      SDL::Audio::close_audio();
118      exit(-1);
119  }
120
121  my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};
122
123  # Build AudioCVT
124  my $wav_cvt = SDL::AudioCVT-&gt;build( $wav_spec-&gt;format, $wav_spec-&gt;channels, $wav_spec-&gt;freq,
125                                      $obtained-&gt;format, $obtained-&gt;channels, $obtained-&gt;freq); 
126
127  # Check that the convert was built
128  unless( $wav_cvt )
129  {
130      printf( STDERR &quot;Couldn't build converter!\n&quot; );
131      SDL::Audio::close_audio();
132      SDL::Audio::free_wav($wav_buf);
133  }
134
135  # And now we're ready to convert
136  SDL::Audio::convert_audio($wav_cvt, $wav_buf, $wav_len);
137
138  # We can delete to original WAV data now
139  SDL::Audio::free_wav($wav_buf);
140
141 </pre>
142 <p><strong>TODO</strong>: What to do with it? How to use callback? See http://www.libsdl.org/cgi/docwiki.cgi/SDL_ConvertAudio</p>
143
144 </div>
145 <h2 id="MixAudio">MixAudio </h2>
146 <div id="MixAudio_CONTENT">
147 <p>Mixes audio data</p>
148
149 </div>
150 <h2 id="LockAudio">LockAudio</h2>
151 <div id="LockAudio_CONTENT">
152 <p>Locks out the callback function</p>
153
154 </div>
155 <h2 id="UnlockAudio">UnlockAudio</h2>
156 <div id="UnlockAudio_CONTENT">
157 <p>Unlocks the callback function</p>
158
159 </div>
160 <h2 id="CloseAudio">CloseAudio </h2>
161 <div id="CloseAudio_CONTENT">
162 <p>Shuts down audio processing and closes the audio device.  </p>
163
164 </div>
165 </div>