File Coverage

blib/lib/SDLx/Sound.pm
Criterion Covered Total %
statement 18 61 29.5
branch 0 22 0.0
condition 0 3 0.0
subroutine 6 17 35.2
pod 0 9 0.0
total 24 112 21.4


line stmt bran cond sub pod time code
1             package SDLx::Sound;
2 2     2   3577 use strict;
  2         6  
  2         81  
3 2     2   14 use warnings;
  2         5  
  2         58  
4 2     2   15 use Carp;
  2         5  
  2         408  
5              
6 2     2   12 use SDL;
  2         5  
  2         350  
7             #use SDL::Audio;
8             #use SDL::AudioSpec;
9 2     2   1255 use SDL::Mixer;
  2         7  
  2         440  
10 2     2   1332 use SDL::Mixer::Music;
  2         5  
  2         2858  
11             #use SDL::Mixer::Channels;
12             #use SDL::Mixer::Samples;
13             #use SDL::Mixer::MixChunk;
14              
15             # SDL::Mixer must be inited only one time
16             my $audioInited = undef;
17              
18             sub new {
19 0     0 0   my $class = shift;
20 0           my $self = {@_};
21 0           bless ($self, $class);
22 0 0         _initAudio() unless $audioInited;
23 0           $self->{supported} = _initMixer();
24 0           return $self;
25             }
26              
27             sub _initAudio {
28 0     0     SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 );
29 0           my ($status, $freq, $format, $channels) = @{ SDL::Mixer::query_spec() };
  0            
30 0 0         $audioInited = 1 if $status == 1;
31 0           return ($status, $freq, $format, $channels); #TODO: Save this information in $self;
32             }
33              
34             sub _initMixer {
35 0     0     my $init_flags = SDL::Mixer::init( MIX_INIT_MP3 | MIX_INIT_MOD | MIX_INIT_FLAC | MIX_INIT_OGG );
36            
37 0           my %init = ();
38              
39             # Short circuit if we have and older version of SDL_Mixer
40 0 0         return \%init unless $init_flags;
41              
42 0 0         $init{ mp3 } = 1 if $init_flags & MIX_INIT_MP3;
43 0 0         $init{ mod } = 1 if $init_flags & MIX_INIT_MOD;
44 0 0         $init{ flac } = 1 if $init_flags & MIX_INIT_FLAC;
45 0 0         $init{ ogg } = 1 if $init_flags & MIX_INIT_OGG;
46              
47 0           return \%init
48             }
49              
50             sub load {
51 0     0 0   my $self = shift;
52 0           $self->{files} = {@_};
53             }
54              
55             sub unload {
56 0     0 0   my $self = shift;
57 0           $self->{files} = {};
58             }
59              
60             sub play {
61 0     0 0   my $self = shift;
62 0 0 0       $self->{files} = {@_} if $#_ > 0 && @_;
63 0           my $play = 1;
64 0 0         if (-e $_[0]) {
65 0 0         my $music = SDL::Mixer::Music::load_MUS($_[0])
66             or Carp::croak 'Sound file not found: ' . SDL::get_error();
67 0           SDL::Mixer::Music::volume_music(85);
68 0 0         if (SDL::Mixer::Music::play_music($music, -1)<0) {
69 0           print("Can't play!\n". SDL::get_error()."\n");
70 0           $play = 0;
71             }
72             } else {
73 0           carp("No newline ".$self->{files}."\n".$_[0]."\n");
74 0           $play = 0;
75             }
76 0           return $play;
77             }
78              
79 0     0 0   sub loud {
80             }
81              
82             sub pause {
83 0     0 0   my $self = shift;
84 0           SDL::Mixer::Music::pause_music();
85            
86             }
87              
88             sub resume {
89 0     0 0   my $self = shift;
90 0           SDL::Mixer::Music::resume_music();
91            
92             }
93              
94              
95             sub stop {
96 0     0 0   my $self = shift;
97 0           SDL::Mixer::Music::halt_music();
98             #SDL::Mixer::quit();
99             }
100              
101 0     0 0   sub fade {
102             }
103              
104              
105              
106             1; # End of SDLx::Sound