Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

SDL sound sampler-mixer

by zentara (Archbishop)
on Dec 25, 2004 at 00:46 UTC ( [id://417375] : CUFP . print w/replies, xml ) Need Help??

Someone asked in a linux newsgroup, how to have a keypress play a sound. Here is what I came up with. The biggest problem was sharing the dsp, which SDL::Mixer allows...that way sounds can be played simultaneously, instead of 1 sound cutting the other off.

I put the script in a little package with the sample sounds at z-sampler

UPDATE 10/25/2004 Added a text menu.

#!/usr/bin/perl use SDL; use SDL::Mixer; use SDL::Music; use SDL::App; use SDL::Event; use SDL::Rect; use SDL::Color; use SDL::Tool::Font; # See # http://aspn.activestate.com/ASPN/CodeDoc/SDL-sdlpl/SDL/Mixer.html $|++; $width = 400; $height = 300; my $app = new SDL::App( -title => "Sampler", -width => 400, -height => 300, -depth => 8 ); my $rect = SDL::Rect->new( -height => 250, -width => 360, -x => 20, -y => 20, ); my $color = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0xff, ); $app->fill( $rect, $color ); $app->update($rect); my $fontobj = new SDL::Tool::Font( -italic => 1, #-normal -bold -italic -underline + -ttfont => 'arial.ttf', -size => 20, -fg => $SDL::Color::black, -bg => $SDL::Color::black, ); #SDL::Tool::Font::print( $app, 30,30,"hello World"); $fontobj->print( $app, 30, 30, "m ->music" ); $fontobj->print( $app, 30, 50, "c ->cannon" ); $fontobj->print( $app, 30, 70, "e ->explosion" ); $fontobj->print( $app, 30, 120, "esc ->quit" ); $app->update($rect); $mixer = SDL::Mixer->new( -frequency => MIX_DEFAULT_FREQUENCY, -format => MIX_DEFAULT_FORMAT, -channels => MIX_DEFAULT_CHANNELS, -size => 4096 ); # provides 8 channels of # 16 bit audio at 22050 Hz. and a single channel of music. my $music = new SDL::Music('1bb.mp3'); #background can be mp3,o +gg or wav my $sound0 = new SDL::Sound('cannon.wav'); #effects must be wav $sound0->volume(128); #max my $sound1 = new SDL::Sound('explosion.wav'); $sound1->volume(128); #max $mixer->music_volume(MIX_MAX_VOLUME); my $event = new SDL::Event; my %events = ( SDL_KEYUP() => sub { my ($e) = @_; my $key = SDL::GetKeyName( $e->key_sym() ); print "$key up\n"; if ( $key eq 'm' ) { $mixer->play_music( $music, 10 ) } if ( $key eq 'c' ) { $mixer->play_channel( 0, $sound0, 0 ) } if ( $key eq 'e' ) { $mixer->play_channel( 1, $sound1, 0 ) } }, SDL_KEYDOWN() => sub { my ($e) = @_; if ( $e->key_sym() == SDLK_ESCAPE ) { exit } my $key = SDL::GetKeyName( $e->key_sym() ); print "$key down\n"; }, SDL_QUIT() => sub { exit }, ); $app->loop( \%events );

Replies are listed 'Best First'.
Re: SDL sound sampler-mixer
by Discipulus (Canon) on Jan 04, 2005 at 15:24 UTC
    wonderful think !
    but.. I have trouble to install correctly both SDL and OpenGL in my ActivePerl ... some guideline ?

    thanks lorenzo*

      Try crazyisomniacs ppm repository I did manage to get SDL working on WindowsME, but I avoid Windows except for testing, so maybe someone else can be more helpful. There also is a dll for SDL-win32 at libsdl.org

      Windows make the the easy things hard, and the hard things impossible. :-)


      I'm not really a human, but I play one on earth. flash japh