Applied patch to allow fast App init
[sdlgit/SDL_perl.git] / lib / SDL / Mixer.pm
CommitLineData
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
8package SDL::Mixer;
9use strict;
10use SDL;
11use SDL::Sound;
12use SDL::Music;
13
14BEGIN {
15}
16
17$SDL::Mixer::initialized = 0;
18
19sub 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
39sub DESTROY {
40 my $self = shift;
41 --$SDL::Mixer::initialized;
42 unless ($SDL::Mixer::initialized) {
43 SDL::MixCloseAudio();
44 }
45}
46
47
48sub 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
55sub reserve_channels ($$) {
56 my ($self,$channels) = @_;
57 return SDL::MixReserveChannels($channels);
58}
59
60sub allocate_channels ($$) {
61 my ($self,$channels) = @_;
62 return SDL::MixAllocateChannels($channels);
63}
64
65sub group_channel ($$$) {
66 my ($self,$channel,$group) = @_;
67 return SDL::MixGroupChannel($channel, $group);
68}
69
70sub group_channels ($$$$) {
71 my ($self,$from,$to,$group) = @_;
72 return SDL::MixGroupChannels($from,$to,$group);
73}
74
75sub group_available ($$) {
76 my ($self,$group) = @_;
77 return SDL::MixGroupAvailable($group);
78}
79
80sub group_count ($$) {
81 my ($self,$group) = @_;
82 return SDL::MixGroupCount($group);
83}
84
85sub group_oldest ($$) {
86 my ($self,$group) = @_;
87 return SDL::MixGroupOldest($group);
88}
89
90sub group_newer ($$) {
91 my ($self,$group) = @_;
92 return SDL::MixGroupNewer($group);
93}
94
95sub play_channel ($$$$;$) {
96 my ($self,$channel,$chunk,$loops,$ticks) = @_;
97 $ticks ||= -1;
98 return SDL::MixPlayChannelTimed($channel,$$chunk,$loops,$ticks);
99}
100
101sub play_music ($$$) {
102 my ($self,$music,$loops) = @_;
103 return SDL::MixPlayMusic($$music,$loops);
104}
105
106sub 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
112sub fade_in_music ($$$$) {
113 my ($self,$music,$loops,$ms) = @_;
114 return SDL::MixFadeInMusic($$music,$loops,$ms);
115}
116
117sub channel_volume ($$$) {
118 my ($self,$channel,$volume) = @_;
119 return SDL::MixVolume($channel,$volume);
120}
121
122sub music_volume ($$) {
123 my ($self,$volume) = @_;
124 return SDL::MixVolumeMusic($volume);
125}
126
4d2f5fe1 127
8fde61e3 128sub halt_channel ($$) {
129 my ($self,$channel) = @_;
130 return SDL::MixHaltChannel($channel);
131}
132
133sub halt_group ($$) {
134 my ($self,$group) = @_;
135 return SDL::MixHaltGroup($group);
136}
137
138sub halt_music (){
139 return SDL::MixHaltMusic();
140}
141
142sub channel_expire ($$$) {
143 my ($self,$channel,$ticks) = @_;
144 return SDL::MixExpireChannel($channel,$ticks);
145}
146
147sub fade_out_channel ($$$) {
148 my ($self,$channel,$ms) = @_;
149 return SDL::MixFadeOutChannel($channel,$ms);
150}
151
152sub fade_out_group ($$$) {
153 my ($self,$group,$ms) = @_;
154 return SDL::MixFadeOutGroup($group,$ms);
155}
156
157sub fade_out_music ($$) {
158 my ($self,$ms) = @_;
159 return SDL::MixFadeOutMusic($ms);
160}
161
162sub fading_music () {
163 return SDL::MixFadingMusic();
164}
165
166sub fading_channel ($$) {
167 my ($self,$channel) = @_;
168 return SDL::MixFadingChannel($channel);
169}
170
171sub pause ($$) {
172 my ($self,$channel) = @_;
173 SDL::MixPause($channel);
174}
175
176sub resume ($$) {
177 my ($self,$channel) = @_;
178 SDL::MixResume($channel);
179}
180
181sub paused ($$) {
182 my ($self,$channel) = @_;
183 return SDL::MixPaused($channel);
184}
185
186sub pause_music () {
187 SDL::MixPauseMusic();
188}
189
190sub resume_music () {
191 SDL::MixResumeMusic();
192}
193
194sub rewind_music (){
195 SDL::MixRewindMusic();
196}
197
198sub music_paused (){
199 return SDL::MixPausedMusic();
200}
201
202sub playing ($$) {
203 my ($self,$channel) = @_;
204 return SDL::MixPlaying($channel);
205}
206
4d2f5fe1 207sub mix_volume_chunk($$$) {
208 my ($self, $chunk, $volume) = @_;
209 return SDL::MixVolumeChunk($chunk, $volume);
210}
211
8fde61e3 212sub playing_music () {
213 return SDL::MixPlayingMusic();
214}
215
2161;
217
218__END__;
219
220=pod
221
222=head1 NAME
223
224SDL::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
235SDL::Mixer allows you access to the SDL mixer library, enablig sound and
236music volume setting, playing, pausing and resuming, as well as fading
237the 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
248Creates 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
254Returns a hash reference, containing the following keys and their respective
255values:
256
257 -status
258 -frequency
259 -channels
260 -format
261
262=head2 reserve_channels
263
264 $mixer->reserve_channels(4);
265
266Reserve so many channels.
267
268=head2 allocate_channels()
269
270 $mixer->reserve_channels(2);
271
272Allocate so many channels.
273
274=head2 group_channel(channel,group)
275
276Group the channel number C<$channel> into group C<$group>.
277
278=head2 group_channels(from,to,group)
279
280Groups a range of channels
281
282=head2 group_available(group)
283
284Return true when the group is available.
285
286=head2 group_count(group)
287
288Returns 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
301Play C<$music> C<$loop> times.
302
303=head2 fade_in_channel(channel,chunk,loops,ms,ticks)
304
305Fades a channel in
306
307=head2 fade_in_music(music,loops,ms)
308
309Fades the music in over a number of ms, looping as it does
310
311=head2 channel_volume(channel,volume)
312
313Sets the volume for a single channel
314
315=head2 mucis_volume(volume)
316
317Set the volume for the music.
318
319=head2 halt_channel(channel)
320
321Stops a specific channel
322
323=head2 halt_group(group)
324
325Stops a group of channels
326
327=head2 halt_music()
328
329Stops the music
330
331=head2 channel_expire(channel,ticks)
332
333Ignores the channel after C<ticks> has expired
334
335=head2 fade_out_channel(channel,ms)
336
337Fade the channel number C<$channel> in C<$ms> ms out.
338
339=head2 fade_out_group(group,ms)
340
341Fade the channel group C<$group> in C<$ms> ms out.
342
343=head2 fade_out_music(ms)
344
345Fade the music in C<$ms> ms out.
346
347=head2 fading_music()
348
349Return true when the music is currently fading in or out.
350
351=head2 fading_channel()
352
353Return true when the channel number C<$channel> is currently fading in or out.
354
355=head2 pause( channel )
356
357Pause the channel C<$channel>.
358
359=head2 resume(channel)
360
361Resume the channel C<$channel>.
362
363=head2 paused()
364
365Return true when the channel is currently paused.
366
367=head2 pause_music()
368
369Pause the music play.
370
371=head2 resume_music()
372
373Resume the music play.
374
375=head2 rewind_music()
376
377Resets the music file to the beginning
378
379=head2 music_paused()
380
381Return true when the music is currently paused.
382
383=head2 playing()
384
385Return true when the channel is currently playing.
386
387=head2 playing_music ()
388
389Return true when the music is currently playing.
390
391=head1 AUTHORS
392
393David J. Goehrig, basic doc added by Tels <http://bloodgate.com>.
394
395=head1 SEE ALSO
396
397L<perl>, L<SDL::Music> and L<SDL::Sound>.
398
399=cut