SDLSound/Manager.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 SCAR 0.0
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« SDLSound.pm ↑ Summary
1 # Copyright (c) 2016 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package Game::Asset::SDLSound::Manager;
25 $Game::Asset::SDLSound::Manager::VERSION = '0.1';
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use SDL ();
Subroutine BEGIN: called CC=1 SCAR=2.6
31 use SDL::Mixer ();
32 use SDL::Mixer::Music ();
33  
34 has '_is_init_done' => (
35 is => 'rw',
36 isa => 'Bool',
37 default => 0,
38 );
39 has 'freq' => (
40 is => 'ro',
41 isa => 'Int',
42 default => 44_100,
43 );
44 has 'format' => (
45 is => 'ro',
46 default => SDL::Mixer::MIX_DEFAULT_FORMAT,
47 );
48 has 'channels' => (
49 is => 'ro',
50 isa => 'Int',
51 default => 2,
52 );
53 has 'chunksize' => (
54 is => 'ro',
55 isa => 'Int',
56 default => 4096,
57 );
58  
59  
60 sub init
61 {
62 my ($self) = @_;
63 return if $self->_is_init_done;
64  
65 SDL::init( SDL::SDL_INIT_AUDIO );
66 SDL::Mixer::init(
67 SDL::Mixer::MIX_INIT_FLAC
68 | SDL::Mixer::MIX_INIT_MOD
69 | SDL::Mixer::MIX_INIT_MP3
70 | SDL::Mixer::MIX_INIT_OGG
71 );
72  
73 SDL::Mixer::open_audio(
74 $self->freq,
75 $self->format,
76 $self->channels,
77 $self->chunksize,
78 );
79  
80 $self->_is_init_done( 1 );
81 return;
82 }
83  
84 sub is_playing
85 {
86 my ($self) = @_;
87 return SDL::Mixer::Music::playing_music();
88 }
89  
90 sub finish
91 {
92 my ($self) = @_;
93 SDL::Mixer::quit;
94 $self->_is_init_done( 0 );
95 return;
96 }
97  
98 no Moose;
99 __PACKAGE__->meta->make_immutable;
100 1;
101 __END__
102  
103  
104 =head1 NAME
105  
106 Game::Asset::SDLSound::Manager - Manage the environment for playing sounds in SDL
107  
108 =head1 ATTRIBUTES
109  
110 =head2 freq
111  
112 Sampling frequency. The default is 44,100.
113  
114 =head2 format
115  
116 Output format. The default is C<SDL::Mixer::MIX_DEFAULT_FORMAT>.
117  
118 =head2 channels
119  
120 Number of channels to output. The default is 2.
121  
122 =head2 chunksize
123  
124 The size of the chunks to send at one time. The default is 4096.
125  
126 =head1 METHODS
127  
128 =head2 init
129  
130 Intitilizes the SDL mixer subsystem with the information provided in the
131 attributes above.
132  
133 =head2 is_playing
134  
135 Returns true if a sound is currently playing.
136  
137 =head2 finish
138  
139 Cleans up the SDL mixer subsystem.
140  
141 =head1 LICENSE
142  
143 Copyright (c) 2016 Timm Murray
144 All rights reserved.
145  
146 Redistribution and use in source and binary forms, with or without
147 modification, are permitted provided that the following conditions are met:
148  
149 * Redistributions of source code must retain the above copyright notice,
150 this list of conditions and the following disclaimer.
151 * Redistributions in binary form must reproduce the above copyright
152 notice, this list of conditions and the following disclaimer in the
153 documentation and/or other materials provided with the distribution.
154  
155 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
156 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
157 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
158 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
159 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
160 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
161 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
162 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
163 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
164 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
165 POSSIBILITY OF SUCH DAMAGE.
166  
167 =cut
« SDLSound.pm ↑ Summary