a578126fb134bfb88991ecb63efc55ba77951119
[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 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
127
128 sub halt_channel ($$) {
129         my ($self,$channel) = @_;
130         return SDL::MixHaltChannel($channel);
131 }
132
133 sub halt_group ($$) {
134         my ($self,$group) = @_;
135         return SDL::MixHaltGroup($group);
136 }
137
138 sub halt_music (){
139         return SDL::MixHaltMusic();
140 }
141
142 sub channel_expire ($$$) {
143         my ($self,$channel,$ticks) = @_;
144         return SDL::MixExpireChannel($channel,$ticks);
145 }
146
147 sub fade_out_channel ($$$) {
148         my ($self,$channel,$ms) = @_;
149         return SDL::MixFadeOutChannel($channel,$ms);
150 }
151
152 sub fade_out_group ($$$) {
153         my ($self,$group,$ms) = @_;
154         return SDL::MixFadeOutGroup($group,$ms);
155 }
156
157 sub fade_out_music ($$) {
158         my ($self,$ms) = @_;
159         return SDL::MixFadeOutMusic($ms);
160 }
161
162 sub fading_music () {
163         return SDL::MixFadingMusic();
164 }
165
166 sub fading_channel ($$) {
167         my ($self,$channel) = @_;
168         return SDL::MixFadingChannel($channel);
169 }
170         
171 sub pause ($$) {
172         my ($self,$channel) = @_;
173         SDL::MixPause($channel);
174 }
175
176 sub resume ($$) {
177         my ($self,$channel) = @_;
178         SDL::MixResume($channel);
179 }
180
181 sub paused ($$) {
182         my ($self,$channel) = @_;
183         return SDL::MixPaused($channel);
184 }
185
186 sub pause_music () {
187         SDL::MixPauseMusic();
188 }
189
190 sub resume_music () {
191         SDL::MixResumeMusic();
192 }
193
194 sub rewind_music (){
195         SDL::MixRewindMusic();
196 }
197
198 sub music_paused (){
199         return SDL::MixPausedMusic();
200 }
201
202 sub playing ($$) {
203         my ($self,$channel) = @_;
204         return SDL::MixPlaying($channel);
205 }
206
207 sub mix_volume_chunk($$$) {
208         my ($self, $chunk, $volume) = @_;
209         return SDL::MixVolumeChunk($chunk, $volume);
210 }
211
212 sub playing_music () {
213         return SDL::MixPlayingMusic();
214 }
215
216 1;
217
218 __END__;
219
220 =pod
221
222 =head1 NAME
223
224 SDL::Mixer - a SDL perl extension
225
226 =head1 SYNOPSIS
227
228   $mixer = new SDL::Mixer       -frequency => MIX_DEFAULT_FREQUENCY,
229                                 -format => MIX_DEFAULT_FORMAT,
230                                 -channels => MIX_DEFAULT_CHANNELS,
231                                 -size => 4096;
232
233 =head1 DESCRIPTION
234
235 SDL::Mixer allows you access to the SDL mixer library, enablig sound and
236 music volume setting, playing, pausing and resuming, as well as fading
237 the sound and music in and out.
238
239 =head1 METHODS
240   
241 =head2 new()
242
243         $mixer = SDL::Mixer->new(       -frequency => MIX_DEFAULT_FREQUENCY,
244                                         -format    => MIX_DEFAULT_FORMAT,
245                                         -channels  => MIX_DEFAULT_CHANNELS,
246                                         -size      => 4096);
247
248 Creates a new SDL::Mixer object. C<$size> is the buffer size in bytes.
249
250 =head2 query_spec()
251
252         my $specs = SDL::Mixer::query_spec();
253
254 Returns a hash reference, containing the following keys and their respective
255 values:
256
257         -status
258         -frequency
259         -channels
260         -format
261
262 =head2 reserve_channels
263
264         $mixer->reserve_channels(4);
265
266 Reserve so many channels.
267
268 =head2 allocate_channels()
269
270         $mixer->reserve_channels(2);
271
272 Allocate so many channels.
273
274 =head2 group_channel(channel,group)
275
276 Group the channel number C<$channel> into group C<$group>.
277
278 =head2 group_channels(from,to,group)
279
280 Groups a range of channels
281
282 =head2 group_available(group)
283
284 Return true when the group is available.
285
286 =head2 group_count(group)
287
288 Returns the number of channels in the group
289
290 =head2 group_oldest()
291
292
293 =head2 group_newer()
294
295
296 =head2 play_channel()
297
298
299 =head2 play_music()
300
301 Play C<$music> C<$loop> times.
302
303 =head2 fade_in_channel(channel,chunk,loops,ms,ticks)
304
305 Fades a channel in
306
307 =head2 fade_in_music(music,loops,ms)
308
309 Fades the music in over a number of ms, looping as it does
310
311 =head2 channel_volume(channel,volume)
312
313 Sets the volume for a single channel
314
315 =head2 mucis_volume(volume)
316
317 Set the volume for the music.
318
319 =head2 halt_channel(channel)
320
321 Stops a specific channel
322
323 =head2 halt_group(group)
324
325 Stops a group of channels
326
327 =head2 halt_music()
328
329 Stops the music
330
331 =head2 channel_expire(channel,ticks)
332
333 Ignores the channel after C<ticks> has expired
334
335 =head2 fade_out_channel(channel,ms)
336
337 Fade the channel number C<$channel> in C<$ms> ms out.
338
339 =head2 fade_out_group(group,ms)
340         
341 Fade the channel group C<$group> in C<$ms> ms out.
342
343 =head2 fade_out_music(ms)
344         
345 Fade the music in C<$ms> ms out.
346
347 =head2 fading_music()
348
349 Return true when the music is currently fading in or out.
350
351 =head2 fading_channel()
352
353 Return true when the channel number C<$channel> is currently fading in or out.
354
355 =head2 pause( channel )
356
357 Pause the channel C<$channel>.
358
359 =head2 resume(channel)
360
361 Resume the channel C<$channel>.
362
363 =head2 paused()
364
365 Return true when the channel is currently paused.
366
367 =head2 pause_music()
368
369 Pause the music play.
370
371 =head2 resume_music()
372         
373 Resume the music play.
374
375 =head2 rewind_music()
376
377 Resets the music file to the beginning
378
379 =head2 music_paused()
380
381 Return true when the music is currently paused.
382
383 =head2 playing()
384
385 Return true when the channel is currently playing.
386
387 =head2 playing_music ()
388
389 Return true when the music is currently playing.
390
391 =head1 AUTHORS 
392
393 David J. Goehrig, basic doc added by Tels <http://bloodgate.com>.
394
395 =head1 SEE ALSO
396
397 L<perl>, L<SDL::Music> and L<SDL::Sound>.
398
399 =cut