File Coverage

blib/lib/Game/Asset/SDLSound/Manager.pm
Criterion Covered Total %
statement 24 37 64.8
branch 0 2 0.0
condition n/a
subroutine 8 11 72.7
pod 3 3 100.0
total 35 53 66.0


line stmt bran cond sub pod time code
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 5     5   3549 use strict;
  5         7  
  5         142  
27 5     5   19 use warnings;
  5         5  
  5         129  
28 5     5   18 use Moose;
  5         6  
  5         27  
29 5     5   21720 use namespace::autoclean;
  5         7  
  5         45  
30 5     5   274 use SDL ();
  5         6  
  5         64  
31 5     5   15 use SDL::Mixer ();
  5         4  
  5         50  
32 5     5   14 use SDL::Mixer::Music ();
  5         7  
  5         972  
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 0     0 1   my ($self) = @_;
63 0 0         return if $self->_is_init_done;
64              
65 0           SDL::init( SDL::SDL_INIT_AUDIO );
66 0           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 0           SDL::Mixer::open_audio(
74             $self->freq,
75             $self->format,
76             $self->channels,
77             $self->chunksize,
78             );
79              
80 0           $self->_is_init_done( 1 );
81 0           return;
82             }
83              
84             sub is_playing
85             {
86 0     0 1   my ($self) = @_;
87 0           return SDL::Mixer::Music::playing_music();
88             }
89              
90             sub finish
91             {
92 0     0 1   my ($self) = @_;
93 0           SDL::Mixer::quit;
94 0           $self->_is_init_done( 0 );
95 0           return;
96             }
97              
98 5     5   22 no Moose;
  5         5  
  5         23  
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