PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SDLx/Sound.pm

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