Skip to content

Commit

Permalink
Merge branch 'SDLx_sound'
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed Sep 16, 2010
2 parents c0f86ef + f2dab7b commit c25576f
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -14,6 +14,7 @@ Revision history for Perl extension SDL_perl.
- Added drawing for circles in SDLx::Surface [kthakore] - Added drawing for circles in SDLx::Surface [kthakore]
- Fixed test issues [FROGGS] - Fixed test issues [FROGGS]
- Added Layer and Layer Managers [FROGGS] - Added Layer and Layer Managers [FROGGS]
- Added SDLx::Sound [rflipo]


* 2.511 06 August 2010 * 2.511 06 August 2010
- Added fixes for image and gfx uses in Surface [kthakore, garu] - Added fixes for image and gfx uses in Surface [kthakore, garu]
Expand Down
5 changes: 4 additions & 1 deletion README
Expand Up @@ -23,6 +23,9 @@ The SDL Perl 2009 Development Team


Nick: nferraz Nick: nferraz
Name: Nelson Ferraz Name: Nelson Ferraz

Nick: monsenhor
Name: Ricardo Filipo


Maintainance Maintainance
Nick: kthakore Nick: kthakore
Expand All @@ -47,4 +50,4 @@ MacOSX Experimental Usage


Use the SDLPerl executable made in the bundle and call your scripts Use the SDLPerl executable made in the bundle and call your scripts


%SDLPerl.app/Contents/MacOS/SDLPerl yourScript.pl %SDLPerl.app/Contents/MacOS/SDLPerl yourScript.pl
62 changes: 62 additions & 0 deletions examples/SDLx/SDLx_Sound.pl
@@ -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 );


102 changes: 102 additions & 0 deletions lib/SDLx/Sound.pm
@@ -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
125 changes: 125 additions & 0 deletions lib/pods/SDLx/Sound.pod
@@ -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.


0 comments on commit c25576f

Please sign in to comment.