Description: |
UPDATE: June 11, 2012. This script was written way back when for an older version of SDL. SDL has been improved and rewritten since 2004, so see the reply node below Re: Tk Game Sound demo-with SDL for a more recent version using SDL-2.541
In SDL sound sampler-mixer I showed a snippet for a sound mixer in SDL. Well, since Tk is more widely used, and has an easier widget set to deal with, here is a Tk version. The script and/or sample sounds can be downloaded at ztk-sound-demo. This script has volume control for each sound. P.S. I have no error checkig in there for SDL, so if you don't have the proper sound files in the same directory, it will harmlessly segfault. Get the sample sounds from the tgz package, or insert your own. |
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Pane;
use SDL;
use SDL::Mixer;
use SDL::Music;
# a demo for using multi channel sound in Tk
# by zentara
$|++;
my $mw = tkinit;
$mw->geometry('600x300+100+100');
####setup mixer
# See
# http://aspn.activestate.com/ASPN/CodeDoc/SDL-sdlpl/SDL/Mixer.html
my $mixer = SDL::Mixer->new(
-frequency => MIX_DEFAULT_FREQUENCY, # 22050
-format => MIX_DEFAULT_FORMAT, # AUDIO_S16
-channels => MIX_DEFAULT_CHANNELS, # 8
-size => 1024 #higher numbers give sloppy reponse
#as buffers continue to empty
);
# provides 8 channels of
# 16 bit audio at 22050 Hz. and a single channel of music.
#background can be mp3,ogg or wav (mp3 needs smpeg libs)
my $music = new SDL::Music('1bb.mp3');
#sound effects must be wav
my $sound0 = new SDL::Sound('cannon.wav');
$sound0->volume(128); #max
my $sound1 = new SDL::Sound('explosion.wav');
$sound1->volume(128); #max
$mixer->music_volume(MIX_MAX_VOLUME); #128
###############################################################
######setup volume indicators#######################
my $music_vol = 128;
my $chan0_vol = 128; #cannon
my $chan1_vol = 128; #explosion
my $tframe = $mw->Frame(-background =>'black')->pack(-fill=>'x');
$tframe->Label(-background =>'black', -foreground => 'lightyellow',
-text => 'Music Vol: ', )->pack(-side=>'left');
$tframe->Entry(-background =>'black', -foreground => 'lightyellow',
-textvariable => \$music_vol , -width => 3,
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -text => ' ',-width=>1,
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -foreground => 'lightgreen',
-text => 'Cannon Vol: ', )->pack(-side=>'left');
$tframe->Entry(-background =>'black', -foreground => 'lightgreen',
-textvariable => \$chan0_vol , -width => 3,
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -text => ' ', -width=>1,
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -foreground => 'lightsteelblue',
-text => 'Explosion Vol: ', )->pack(-side=>'left');
$tframe->Entry(-background =>'black', -foreground => 'lightsteelblue',
-textvariable => \$chan1_vol , -width => 3,
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -text => ' ', -width=>1,
)->pack(-side=>'left');
my $sound = 1;
$tframe->Checkbutton(
-text => 'Sound',
-background => 'lightskyblue',
-variable => \$sound,
-command => \&set_sound,
)->pack(-side =>'left');
$tframe->Button(-text => 'Exit', -background =>'red',
-command => sub{ exit })->pack(-side=>'right');
#################################################################
my $pane = $mw->Scrolled('Pane',
-background =>'black',
-scrollbars=>'oe',
sticky=>'w')->pack(-side => "left", -anchor => "n",
-fill=>'both',-expand=>1);
my %keyprs = (
'm' => {
text => 'music on/off',
command => \&music_toggle
},
'e' => {
text => 'explosion',
command => sub{ $mixer->play_channel( 1, $sound1, 0 )
+ }
},
'c' => {
text => 'cannon',
command => sub{ $mixer->play_channel( 0, $sound0, 0 )
+ }
},
'q' => {
text => 'Exit Program',
command => sub{ exit }
},
'space' => {
text => 'Pause Music',
command => \&pause_toggle
},
'plus' => {
text => 'Increase Background Music Volume',
command => sub{ &music_vol(1) }
},
'minus' => {
text => 'Decrease Background Music Volume',
command => sub{ &music_vol(-1) }
},
'F1' => {
text => 'Increase Cannon Volume',
command => sub{
$chan0_vol += 1;
if($chan0_vol > 128){$chan0_vol = 128}
$mixer->channel_volume(0,$chan0_vol);
}
},
'Control-F1' => {
text => 'Decrease Cannon Volume',
command => sub{
$chan0_vol -= 1;
if($chan0_vol < 0){$chan0_vol = 0}
$mixer->channel_volume(0,$chan0_vol);
}
},
'F2' => {
text => 'Increase Explosion Volume',
command => sub{
$chan1_vol += 1;
if($chan1_vol > 128){$chan1_vol = 128}
$mixer->channel_volume(1,$chan1_vol);
}
},
'Control-F2' => {
text => 'Decrease Explosion Volume',
command => sub{
$chan1_vol -= 1;
if($chan1_vol < 0){$chan1_vol = 0}
$mixer->channel_volume(1,$chan1_vol);
}
},
);
#hack to expand pane
$pane->Label(-text=>'',-background=>'red',-width =>100)
->pack(-fill=>'x',-expand=>1 );
my @order = qw(m e c space plus minus F1 Control-F1 F2 Control-F2 q);
foreach(@order){
$pane->Label(-text => "$_ -> $keyprs{$_}{'text'}",
# -font => 'big',
-background => 'cyan',
-anchor => 'w',
-borderwidth =>2,
)->pack( -fill =>'x',-expand=>1);
$mw->bind("<$_>", $keyprs{$_}{'command'});
}
MainLoop;
############################################
sub music_toggle{
if( $mixer->playing_music() ){
$mixer->halt_music();
}else{
$mixer->play_music( $music, 100 );
}
}
#############################################
sub music_vol{
$music_vol += $_[0];
if($music_vol < 0){$music_vol = 0}
if($music_vol > 128){$music_vol = 128}
#return if ! $mixer->playing_music();
$mixer->music_volume($music_vol);
}
##########################################
sub pause_toggle{
return if ! $mixer->playing_music();
if( $mixer->music_paused() ){
$mixer->resume_music();
}else{ $mixer->pause_music() }
}
#############################################
sub set_sound{
if($sound == 0){
$mixer->music_volume(0);
$mixer->channel_volume(0,0);
$mixer->channel_volume(1,0);
return;
}
if($sound == 1){
$mixer->music_volume($music_vol);
$mixer->channel_volume(0,$chan0_vol);
$mixer->channel_volume(1,$chan1_vol);
return;
}
}
__END__
Re: Tk Game Sound demo-with SDL
by zentara (Archbishop) on Jun 11, 2012 at 19:29 UTC
|
Here is a similar script utilizing Tk and SDL-2.541. There are some glitches, but for the most part it works. See ztk-sound-demo-SDL-new for the sample audio files used in the script.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Pane;
use SDL;
use SDL::Mixer;
use SDL::Mixer::Channels;
use SDL::Mixer::Samples;
# a demo for using multi channel sound in Tk
# by zentara, utilizing the latest SDL version SDL-2.541
# make more than the default 2 channels, we make 8, but need 3
printf("We got %d channels!\n", SDL::Mixer::Channels::allocate_channel
+s( 8 ) );
SDL::init(SDL_INIT_AUDIO);
#mono 1 or stereo 2
SDL::Mixer::open_audio( 44100, SDL::Constants::AUDIO_S16, 1, 4096 );
my $sound1 = SDL::Mixer::Samples::load_WAV('1bb.wav');
my $sound2 = SDL::Mixer::Samples::load_WAV('explosion.wav');
my $sound3 = SDL::Mixer::Samples::load_WAV('cannon.wav');
foreach my $chan(1..3){
SDL::Mixer::Channels::volume( $chan, 128 ); # set max volume
}
# setup Tk
$|++;
my $mw = tkinit;
$mw->geometry('900x500+100+100');
$mw->protocol('WM_DELETE_WINDOW' => sub { SDL::Mixer::close_audio(); e
+xit; });
$mw->fontCreate('big', -size=> 16 );
######setup volume indicators#######################
my $chan1_vol = 128; #music
my $chan2_vol = 128; #explosion
my $chan3_vol = 128; #cannon
my $tframe = $mw->Frame(-background =>'black')->pack(-fill=>'x');
$tframe->Label(-background =>'black', -foreground => 'lightyellow',
-font => 'big', -text => 'Music Vol: ', )->pack(-side=>'lef
+t');
$tframe->Entry(-readonlybackground =>'black', -foreground => 'lightyel
+low',
-textvariable => \$chan1_vol , -width => 3,
-font => 'big', -takefocus => 0, -state => 'readonly'
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -text => ' ',-width=>1,
-font => 'big',
)->pack(-side=>'left');
$tframe->Label(-background =>'black', -foreground => 'lightsteelblue',
-font => 'big', -text => 'Explosion Vol: ', )->pack(-side=>
+'left');
$tframe->Entry(-readonlybackground =>'black', -foreground => 'lightste
+elblue',
-font => 'big', -textvariable => \$chan2_vol , -width => 3,
-takefocus => 0, -state => 'readonly' )->pack(-side=>'left'
+);
$tframe->Label(-background =>'black', -text => ' ', -width=>1,
-font => 'big', )->pack(-side=>'left');
$tframe->Label(-background =>'black', -foreground => 'lightgreen',
-font => 'big', -text => 'Cannon Vol: ', )->pack(-side=>'le
+ft');
$tframe->Entry(-readonlybackground =>'black', -foreground => 'lightgre
+en',
-textvariable => \$chan3_vol , -width => 3,
-font => 'big', -takefocus => 0, -state => 'readonly')->pac
+k(-side=>'left');
$tframe->Label(-background =>'black', -text => ' ', -width=>1,
-font => 'big', )->pack(-side=>'left');
my $sound = 1;
$tframe->Checkbutton(
-text => 'Sound',
-background => 'lightskyblue',
-variable => \$sound,
-command => \&set_sound,
-font => 'big')->pack(-side =>'left');
$tframe->Button(-text => 'Exit',
-background =>'red',
-font => 'big',
-command => sub{
SDL::Mixer::close_audio();
exit;
})->pack(-side=>'right');
#################################################################
my $pane = $mw->Scrolled('Pane',
-background =>'black',
-scrollbars=>'oe',
sticky=>'w')->pack(-side => "left", -anchor => "n",
-fill=>'both',-expand=>1);
my %keyprs = (
'm' => {
text => 'music pause/resume',
command => \&music_toggle
},
'e' => {
text => 'explosion',
command => sub{
SDL::Mixer::Channels::play_channel( 2, $sound2, 0
+); # 1 time
}
},
'c' => {
text => 'cannon',
command => sub{
SDL::Mixer::Channels::play_channel( 3, $sound3, 0
+); # 1 time
}
},
'q' => {
text => 'Exit Program',
command => sub{ SDL::Mixer::close_audio(); exit; }
},
'plus' => {
text => 'Increase Background Music Volume',
command => sub{ &music_vol(1) }
},
'minus' => {
text => 'Decrease Background Music Volume',
command => sub{ &music_vol(-1) }
},
'F1' => {
text => 'Increase Explosion Volume',
command => sub{
$chan2_vol += 1;
if($chan2_vol > 128){$chan2_vol = 128}
SDL::Mixer::Channels::volume( 2, $chan2_v
+ol );
}
},
'Control-F1' => {
text => 'Decrease Explosion Volume',
command => sub{
$chan2_vol -= 1;
if($chan2_vol < 0){$chan2_vol = 0}
SDL::Mixer::Channels::volume( 2, $chan2_v
+ol );
}
},
'F2' => {
text => 'Increase Cannon Volume',
command => sub{
$chan3_vol += 1;
if($chan3_vol > 128){$chan3_vol = 128}
SDL::Mixer::Channels::volume( 3, $chan3_v
+ol );
}
},
'Control-F2' => {
text => 'Decrease Cannon Volume',
command => sub{
$chan3_vol -= 1;
if($chan3_vol < 0){$chan3_vol = 0}
SDL::Mixer::Channels::volume( 3, $chan3_v
+ol );
}
},
);
#hack to expand pane
$pane->Label(-text=>'', -font => 'big', -background=>'red',-width =>10
+0)
->pack(-fill=>'x',-expand=>1 );
my @order = qw(m e c plus minus F1 Control-F1 F2 Control-F2 q);
foreach(@order){
$pane->Label(-text => "$_ -> $keyprs{$_}{'text'}",
-font => 'big',
-background => 'cyan',
-anchor => 'w',
-borderwidth =>2,
)->pack( -fill =>'x',-expand=>1);
$mw->bind("<$_>", $keyprs{$_}{'command'});
}
# start music
my $music_play = 1; # to toggle music on/pause
SDL::Mixer::Channels::play_channel( 1, $sound1, -1 ); #continous loop
MainLoop;
############################################
sub music_toggle{
if( $music_play == 0 ){
SDL::Mixer::Channels::resume(1);
$music_play = 1;
}else{
SDL::Mixer::Channels::pause(1);
$music_play = 0;
}
}
#############################################
sub music_vol{
$chan1_vol += $_[0];
if($chan1_vol < 0){$chan1_vol = 0}
if($chan1_vol > 128){$chan1_vol = 128}
SDL::Mixer::Channels::volume( 1, $chan1_vol );
}
##########################################
sub set_sound{
# the -1 setting described in SDL::Mixer::Channels didn't
# seem to work for non-looping sounds, so I use a volume hack below
if($sound == 0){
print "soundoff\n";
SDL::Mixer::Channels::pause(1);
SDL::Mixer::Channels::volume( 2, 0 );
SDL::Mixer::Channels::volume( 3, 0 );
return;
}
if($sound == 1){
print "soundon\n";
SDL::Mixer::Channels::resume(1);
SDL::Mixer::Channels::volume( 2, $chan2_vol );
SDL::Mixer::Channels::volume( 3, $chan3_vol );
return;
}
}
__END__
| [reply] [d/l] |
|
|