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