Commit | Line | Data |
8fde61e3 |
1 | # Mixer.pm |
2 | # |
3 | # a SDL module for manipulating the SDL_mixer lib. |
4d2f5fe1 |
4 | # |
8fde61e3 |
5 | # Copyright (C) 2000,2002 David J. Goehrig |
4d2f5fe1 |
6 | # Copyright (C) 2009 Kartik Thakore |
8fde61e3 |
7 | |
8 | package SDL::Mixer; |
9 | use strict; |
10 | use SDL; |
11 | use SDL::Sound; |
12 | use SDL::Music; |
13 | |
14 | BEGIN { |
15 | } |
16 | |
17 | $SDL::Mixer::initialized = 0; |
18 | |
19 | sub new { |
20 | my $proto = shift; |
21 | my $class = ref($proto) || $proto; |
22 | my $self = {}; |
23 | my %options = @_; |
24 | my $frequency = $options{-frequency} || $options{-rate} || SDL::MIX_DEFAULT_FREQUENCY(); |
25 | my $format = $options{-format} || SDL::MIX_DEFAULT_FORMAT(); |
26 | my $channels = $options{-channels} || SDL::MIX_DEFAULT_CHANNELS(); |
27 | my $size = $options{-size} || 4096; |
28 | unless ( $SDL::Mixer::initialized ) { |
29 | SDL::MixOpenAudio($frequency,$format,$channels,$size ) && |
30 | die SDL::GetError(); |
31 | $SDL::Mixer::initialized = 1; |
32 | } else { |
33 | ++$SDL::Mixer::initialized; |
34 | } |
35 | bless $self,$class; |
36 | return $self; |
37 | } |
38 | |
39 | sub DESTROY { |
40 | my $self = shift; |
41 | --$SDL::Mixer::initialized; |
42 | unless ($SDL::Mixer::initialized) { |
43 | SDL::MixCloseAudio(); |
44 | } |
45 | } |
46 | |
47 | |
48 | sub query_spec () { |
49 | my ($status,$freq,$format,$channels) = SDL::MixQuerySpec(); |
50 | my %hash = ( -status => $status, -frequency => $freq, |
51 | -format => $format, -channels => $channels ); |
52 | return \%hash; |
53 | } |
54 | |
55 | sub reserve_channels ($$) { |
56 | my ($self,$channels) = @_; |
57 | return SDL::MixReserveChannels($channels); |
58 | } |
59 | |
60 | sub allocate_channels ($$) { |
61 | my ($self,$channels) = @_; |
62 | return SDL::MixAllocateChannels($channels); |
63 | } |
64 | |
65 | sub group_channel ($$$) { |
66 | my ($self,$channel,$group) = @_; |
67 | return SDL::MixGroupChannel($channel, $group); |
68 | } |
69 | |
70 | sub group_channels ($$$$) { |
71 | my ($self,$from,$to,$group) = @_; |
72 | return SDL::MixGroupChannels($from,$to,$group); |
73 | } |
74 | |
75 | sub group_available ($$) { |
76 | my ($self,$group) = @_; |
77 | return SDL::MixGroupAvailable($group); |
78 | } |
79 | |
80 | sub group_count ($$) { |
81 | my ($self,$group) = @_; |
82 | return SDL::MixGroupCount($group); |
83 | } |
84 | |
85 | sub group_oldest ($$) { |
86 | my ($self,$group) = @_; |
87 | return SDL::MixGroupOldest($group); |
88 | } |
89 | |
90 | sub group_newer ($$) { |
91 | my ($self,$group) = @_; |
92 | return SDL::MixGroupNewer($group); |
93 | } |
94 | |
95 | sub play_channel ($$$$;$) { |
96 | my ($self,$channel,$chunk,$loops,$ticks) = @_; |
97 | $ticks ||= -1; |
98 | return SDL::MixPlayChannelTimed($channel,$$chunk,$loops,$ticks); |
99 | } |
100 | |
101 | sub play_music ($$$) { |
102 | my ($self,$music,$loops) = @_; |
103 | return SDL::MixPlayMusic($$music,$loops); |
104 | } |
105 | |
106 | sub fade_in_channel ($$$$$;$) { |
107 | my ($self,$channel,$chunk,$loops,$ms,$ticks) = @_; |
108 | $ticks ||= -1; |
109 | return SDL::MixFadeInChannelTimed($channel,$$chunk,$loops,$ms,$ticks); |
110 | } |
111 | |
112 | sub fade_in_music ($$$$) { |
113 | my ($self,$music,$loops,$ms) = @_; |
114 | return SDL::MixFadeInMusic($$music,$loops,$ms); |
115 | } |
116 | |
117 | sub channel_volume ($$$) { |
118 | my ($self,$channel,$volume) = @_; |
119 | return SDL::MixVolume($channel,$volume); |
120 | } |
121 | |
122 | sub music_volume ($$) { |
123 | my ($self,$volume) = @_; |
124 | return SDL::MixVolumeMusic($volume); |
125 | } |
126 | |
ae1cb2f6 |
127 | sub mix_set_panning($$$$) { |
128 | my ($self,$channel,$left,$right) = @_; |
129 | return SDL::MixSetPanning($channel,$left,$right); |
130 | } |
131 | |
4d2f5fe1 |
132 | |
8fde61e3 |
133 | sub halt_channel ($$) { |
134 | my ($self,$channel) = @_; |
135 | return SDL::MixHaltChannel($channel); |
136 | } |
137 | |
138 | sub halt_group ($$) { |
139 | my ($self,$group) = @_; |
140 | return SDL::MixHaltGroup($group); |
141 | } |
142 | |
143 | sub halt_music (){ |
144 | return SDL::MixHaltMusic(); |
145 | } |
146 | |
147 | sub channel_expire ($$$) { |
148 | my ($self,$channel,$ticks) = @_; |
149 | return SDL::MixExpireChannel($channel,$ticks); |
150 | } |
151 | |
152 | sub fade_out_channel ($$$) { |
153 | my ($self,$channel,$ms) = @_; |
154 | return SDL::MixFadeOutChannel($channel,$ms); |
155 | } |
156 | |
157 | sub fade_out_group ($$$) { |
158 | my ($self,$group,$ms) = @_; |
159 | return SDL::MixFadeOutGroup($group,$ms); |
160 | } |
161 | |
162 | sub fade_out_music ($$) { |
163 | my ($self,$ms) = @_; |
164 | return SDL::MixFadeOutMusic($ms); |
165 | } |
166 | |
167 | sub fading_music () { |
168 | return SDL::MixFadingMusic(); |
169 | } |
170 | |
171 | sub fading_channel ($$) { |
172 | my ($self,$channel) = @_; |
173 | return SDL::MixFadingChannel($channel); |
174 | } |
175 | |
176 | sub pause ($$) { |
177 | my ($self,$channel) = @_; |
178 | SDL::MixPause($channel); |
179 | } |
180 | |
181 | sub resume ($$) { |
182 | my ($self,$channel) = @_; |
183 | SDL::MixResume($channel); |
184 | } |
185 | |
186 | sub paused ($$) { |
187 | my ($self,$channel) = @_; |
188 | return SDL::MixPaused($channel); |
189 | } |
190 | |
191 | sub pause_music () { |
192 | SDL::MixPauseMusic(); |
193 | } |
194 | |
195 | sub resume_music () { |
196 | SDL::MixResumeMusic(); |
197 | } |
198 | |
199 | sub rewind_music (){ |
200 | SDL::MixRewindMusic(); |
201 | } |
202 | |
203 | sub music_paused (){ |
204 | return SDL::MixPausedMusic(); |
205 | } |
206 | |
207 | sub playing ($$) { |
208 | my ($self,$channel) = @_; |
209 | return SDL::MixPlaying($channel); |
210 | } |
211 | |
4d2f5fe1 |
212 | sub mix_volume_chunk($$$) { |
213 | my ($self, $chunk, $volume) = @_; |
214 | return SDL::MixVolumeChunk($chunk, $volume); |
215 | } |
216 | |
8fde61e3 |
217 | sub playing_music () { |
218 | return SDL::MixPlayingMusic(); |
219 | } |
220 | |
221 | 1; |
222 | |
223 | __END__; |
224 | |
225 | =pod |
226 | |
227 | =head1 NAME |
228 | |
229 | SDL::Mixer - a SDL perl extension |
230 | |
231 | =head1 SYNOPSIS |
232 | |
233 | $mixer = new SDL::Mixer -frequency => MIX_DEFAULT_FREQUENCY, |
234 | -format => MIX_DEFAULT_FORMAT, |
235 | -channels => MIX_DEFAULT_CHANNELS, |
236 | -size => 4096; |
237 | |
238 | =head1 DESCRIPTION |
239 | |
240 | SDL::Mixer allows you access to the SDL mixer library, enablig sound and |
241 | music volume setting, playing, pausing and resuming, as well as fading |
242 | the sound and music in and out. |
243 | |
244 | =head1 METHODS |
245 | |
246 | =head2 new() |
247 | |
248 | $mixer = SDL::Mixer->new( -frequency => MIX_DEFAULT_FREQUENCY, |
249 | -format => MIX_DEFAULT_FORMAT, |
250 | -channels => MIX_DEFAULT_CHANNELS, |
251 | -size => 4096); |
252 | |
253 | Creates a new SDL::Mixer object. C<$size> is the buffer size in bytes. |
254 | |
255 | =head2 query_spec() |
256 | |
257 | my $specs = SDL::Mixer::query_spec(); |
258 | |
259 | Returns a hash reference, containing the following keys and their respective |
260 | values: |
261 | |
262 | -status |
263 | -frequency |
264 | -channels |
265 | -format |
266 | |
267 | =head2 reserve_channels |
268 | |
269 | $mixer->reserve_channels(4); |
270 | |
271 | Reserve so many channels. |
272 | |
273 | =head2 allocate_channels() |
274 | |
275 | $mixer->reserve_channels(2); |
276 | |
277 | Allocate so many channels. |
278 | |
279 | =head2 group_channel(channel,group) |
280 | |
281 | Group the channel number C<$channel> into group C<$group>. |
282 | |
283 | =head2 group_channels(from,to,group) |
284 | |
285 | Groups a range of channels |
286 | |
287 | =head2 group_available(group) |
288 | |
289 | Return true when the group is available. |
290 | |
291 | =head2 group_count(group) |
292 | |
293 | Returns the number of channels in the group |
294 | |
295 | =head2 group_oldest() |
296 | |
297 | |
298 | =head2 group_newer() |
299 | |
300 | |
301 | =head2 play_channel() |
302 | |
303 | |
304 | =head2 play_music() |
305 | |
306 | Play C<$music> C<$loop> times. |
307 | |
308 | =head2 fade_in_channel(channel,chunk,loops,ms,ticks) |
309 | |
310 | Fades a channel in |
311 | |
312 | =head2 fade_in_music(music,loops,ms) |
313 | |
314 | Fades the music in over a number of ms, looping as it does |
315 | |
316 | =head2 channel_volume(channel,volume) |
317 | |
318 | Sets the volume for a single channel |
319 | |
320 | =head2 mucis_volume(volume) |
321 | |
322 | Set the volume for the music. |
323 | |
324 | =head2 halt_channel(channel) |
325 | |
326 | Stops a specific channel |
327 | |
328 | =head2 halt_group(group) |
329 | |
330 | Stops a group of channels |
331 | |
332 | =head2 halt_music() |
333 | |
334 | Stops the music |
335 | |
336 | =head2 channel_expire(channel,ticks) |
337 | |
338 | Ignores the channel after C<ticks> has expired |
339 | |
340 | =head2 fade_out_channel(channel,ms) |
341 | |
342 | Fade the channel number C<$channel> in C<$ms> ms out. |
343 | |
344 | =head2 fade_out_group(group,ms) |
345 | |
346 | Fade the channel group C<$group> in C<$ms> ms out. |
347 | |
348 | =head2 fade_out_music(ms) |
349 | |
350 | Fade the music in C<$ms> ms out. |
351 | |
352 | =head2 fading_music() |
353 | |
354 | Return true when the music is currently fading in or out. |
355 | |
356 | =head2 fading_channel() |
357 | |
358 | Return true when the channel number C<$channel> is currently fading in or out. |
359 | |
360 | =head2 pause( channel ) |
361 | |
362 | Pause the channel C<$channel>. |
363 | |
364 | =head2 resume(channel) |
365 | |
366 | Resume the channel C<$channel>. |
367 | |
368 | =head2 paused() |
369 | |
370 | Return true when the channel is currently paused. |
371 | |
372 | =head2 pause_music() |
373 | |
374 | Pause the music play. |
375 | |
376 | =head2 resume_music() |
377 | |
378 | Resume the music play. |
379 | |
380 | =head2 rewind_music() |
381 | |
382 | Resets the music file to the beginning |
383 | |
384 | =head2 music_paused() |
385 | |
386 | Return true when the music is currently paused. |
387 | |
388 | =head2 playing() |
389 | |
390 | Return true when the channel is currently playing. |
391 | |
392 | =head2 playing_music () |
393 | |
394 | Return true when the music is currently playing. |
395 | |
ae1cb2f6 |
396 | =head2 sub mix_set_panning(channel,left,right) |
397 | |
398 | Set panning for mixer, Use MIX_CHANNEL_POST to process the postmix stream |
399 | |
400 | Volume for the left channel, range is 0(silence) to 255(loud) |
401 | |
402 | Volume for the right channel, range is 0(silence) to 255(loud) |
403 | |
8fde61e3 |
404 | =head1 AUTHORS |
405 | |
ae1cb2f6 |
406 | Kartik Thakore |
8fde61e3 |
407 | David J. Goehrig, basic doc added by Tels <http://bloodgate.com>. |
408 | |
409 | =head1 SEE ALSO |
410 | |
411 | L<perl>, L<SDL::Music> and L<SDL::Sound>. |
412 | |
413 | =cut |