-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
508 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/perl | ||
#========================================================================== | ||
# | ||
# FILE: SDLx_Sound.pl | ||
# | ||
# USAGE: ./examples/SDLx_Sound.pl | ||
# | ||
# | ||
# DESCRIPTION: Sound tests | ||
# A SDLx::Sound can play, pause, resume and stop | ||
# | ||
# OPTIONS: --- | ||
# REQUIREMENTS: --- | ||
# BUGS: --- | ||
# NOTES: --- | ||
# AUTHOR: Ricardo Filipo (rf), ricardo.filipo@gmail.com | ||
# COMPANY: Mito-Lógica design e soluções de comunicação ltda | ||
# VERSION: 1.0 | ||
# CREATED: 16-08-2010 21:47:33 | ||
# REVISION: --- | ||
#========================================================================== | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use lib 'lib'; | ||
use SDL; | ||
use SDLx::Sound; | ||
use SDLx::App; | ||
use SDL::Event; | ||
use SDL::Events; | ||
|
||
my $app = SDLx::App->new( | ||
height => 120, | ||
width => 480, | ||
depth => 16, | ||
title => 'Sound example', | ||
); | ||
my $snd = SDLx::Sound->new(); | ||
|
||
# load and play a sound | ||
my $play = $snd->play('test/data/sample.wav'); | ||
|
||
# pause or resume on keydown | ||
my %actions = ( | ||
SDL_QUIT() => sub { exit(0); }, | ||
SDL_KEYDOWN() => | ||
sub { | ||
print "Ai\n"; | ||
if($play){ | ||
$snd->pause; | ||
$play=0; | ||
}else{ | ||
$snd->resume; | ||
$play=1; | ||
} | ||
}, | ||
); | ||
|
||
$app->loop( \%actions ); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package SDLx::Sound; | ||
|
||
use Carp; | ||
|
||
use SDL; | ||
#use SDL::Audio; | ||
#use SDL::AudioSpec; | ||
use SDL::Mixer; | ||
use SDL::Mixer::Music; | ||
#use SDL::Mixer::Channels; | ||
#use SDL::Mixer::Samples; | ||
#use SDL::Mixer::MixChunk; | ||
|
||
# SDL::Mixer must be inited only one time | ||
my $audioInited = undef; | ||
|
||
sub new { | ||
my $class = shift; | ||
my $self = {@_}; | ||
bless ($self, $class); | ||
_initAudio() unless $audio_ok; | ||
$self->{supported} = _initMixer(); | ||
return $self; | ||
} | ||
|
||
sub _initAudio { | ||
SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ); | ||
my ($status, $freq, $format, $channels) = @{ SDL::Mixer::query_spec() }; | ||
$audioInited = 1 if $status == 1; | ||
return ($status, $freq, $format, $channels); #TODO: Save this information in $self; | ||
} | ||
|
||
sub _initMixer { | ||
my $init_flags = SDL::Mixer::init( MIX_INIT_MP3 | MIX_INIT_MOD | MIX_INIT_FLAC | MIX_INIT_OGG ); | ||
|
||
my %init = (); | ||
|
||
$init{ mp3 } = 1 if $init_flags & MIX_INIT_MP3; | ||
$init{ mod } = 1 if $init_flags & MIX_INIT_MOD; | ||
$init{ flac } = 1 if $init_flags & MIX_INIT_FLAC; | ||
$init{ ogg } = 1 if $init_flags & MIX_INIT_OGG; | ||
|
||
return \%$init | ||
} | ||
|
||
sub load { | ||
my $self = shift; | ||
$self->{files} = {@_}; | ||
} | ||
|
||
sub unload { | ||
my $self = shift; | ||
$self->{files} = {}; | ||
} | ||
|
||
sub play { | ||
my $self = shift; | ||
$self->{files} = {@_} if @_; | ||
my $play = 1; | ||
if (-e $_[0]) { | ||
my $music = SDL::Mixer::Music::load_MUS($_[0]) | ||
or Carp::croak 'Sound file not found: ' . SDL::get_error(); | ||
SDL::Mixer::Music::volume_music(85); | ||
if (SDL::Mixer::Music::play_music($music, -1)<0) { | ||
print("Can't play!\n". SDL::get_error()."\n"); | ||
$play = 0; | ||
} | ||
} else { | ||
carp("No newline ".$self->{files}."\n".$_[0]."\n"); | ||
$play = 0; | ||
} | ||
return $play; | ||
} | ||
|
||
sub loud { | ||
} | ||
|
||
sub pause { | ||
my $self = shift; | ||
SDL::Mixer::Music::pause_music(); | ||
|
||
} | ||
|
||
sub resume { | ||
my $self = shift; | ||
SDL::Mixer::Music::resume_music(); | ||
|
||
} | ||
|
||
|
||
sub stop { | ||
my $self = shift; | ||
SDL::Mixer::Music::halt_music(); | ||
#SDL::Mixer::quit(); | ||
} | ||
|
||
sub fade { | ||
} | ||
|
||
|
||
|
||
1; # End of SDLx::Sound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
|
||
=head1 NAME | ||
|
||
SDLx::Sound | ||
|
||
=head1 CATEGORY | ||
|
||
Extension | ||
|
||
=head1 SYNOPSIS | ||
|
||
use SDLx::Sound; | ||
|
||
my $snd = SDLx::Sound->new(); | ||
|
||
# loads and plays a single sound now | ||
$snd->play('myfile.wav'); | ||
|
||
# load a single file | ||
$snd->load('theSound.aif'); | ||
|
||
# plays it or all loaded files | ||
$snd->play(); | ||
|
||
# more sounds | ||
my %files = ( | ||
chanell_01 => "/my_sound1.wav", | ||
chanell_02 => "/my_sound2.ogg" | ||
); | ||
|
||
# times sounds bangs | ||
my %times = ( | ||
chanell_01 => 0, # start | ||
chanell_01 => 1256, # miliseconds | ||
chanell_02 => 2345 | ||
); | ||
|
||
# Load files in channels for realtime play | ||
$snd->load(%files); | ||
|
||
# sets sound channel_01 loudness | ||
$snd->loud('channel_01', 80); # loud at 80% | ||
$snd->play(%times); # play loaded files at times | ||
$snd->play; # play again | ||
|
||
# plays sound channel_01 at 578 miliseconds from now | ||
$snd->play('channel_01', 578); | ||
|
||
# fades sound | ||
$snd->fade('channel_02', 2345, 3456, -20); | ||
|
||
# in a single act do the wole Sound | ||
my $snd = SDLx::Sound->new( | ||
files => ( | ||
chanell_01 => "/my_sound1.wav", | ||
chanell_02 => "/my_sound2.ogg" | ||
|
||
), | ||
loud => ( | ||
channel_01 => 80, | ||
channel_02 => 75 | ||
), | ||
times => ( | ||
chanell_01 => 0, # start | ||
chanell_01 => 1256, # miliseconds | ||
chanell_02 => 2345 | ||
), | ||
fade => ( | ||
chanell_02 => [2345, 3456, -20] | ||
) | ||
)->play(); | ||
|
||
=head1 DESCRIPTION | ||
|
||
|
||
You can think about the SDLx::Sound at 2 aproaches. | ||
|
||
=over 4 | ||
|
||
=item * A simple sound or | ||
|
||
=item * The sound of your game or app. | ||
|
||
=back | ||
|
||
Your application will say what the best aproach. | ||
|
||
In a taste that resembles to perl and to SDL, our SDLx:Sound hooks at SDL::Audio and SDL::Mixer with a gracefull and simple interface that can offer to monks a modern perlish way to manage sounds. | ||
|
||
An SDLx::Sound object can load sounds from filesystem, play it, adjust this loudness level or stops the sound. | ||
|
||
Each sound will play in the next available channel, so it can be handled isolately. | ||
|
||
=head1 METHODS | ||
|
||
=head2 new | ||
|
||
Returns a new instance of SDLx::Sound | ||
|
||
=head2 load | ||
|
||
|
||
=head2 play | ||
|
||
$sdlx_sound->play('file.wav'); | ||
|
||
Play a file | ||
|
||
=head2 pause | ||
|
||
=head2 resume | ||
|
||
=head2 stop | ||
|
||
|
||
=head1 AUTHORS | ||
|
||
See L<SDL/AUTHORS>. | ||
|
||
=head1 COPYRIGHT & LICENSE | ||
|
||
This program is free software; you can redistribute it and/or modify it | ||
under the same terms as Perl itself. | ||
|
||
|
Oops, something went wrong.