Re: sounds

On Wed, 20 Sep 2006 08:21:20 +0100
Dirk Koopman <djk tobit co uk> wrote:

What is the approved way of making various sounds? From:

a) simple beeps (a la 0x07 to stdout)
 Just print it.   print chr(07);

b) other short sounds (less than, say, 2 seconds).
c) longer noises.

If it's just 1 sound at a time, you can use Audio::Dsp

If you want multiple channels(play sounds simultaneously), 
you need SDL. In the example below,
you can substitute the SDL event loop with the Gtk event loop.

There is also the Audio::Beep and the Midi::Realtime modules.
Midi::Realtime will let you generate any tone your midi sequencer
can generate.

use SDL;
use SDL::Mixer;
use SDL::Music;
use SDL::App;
use SDL::Event;


my $app = new SDL::App( -title => "Sampler", 
             -width => 400, -height =>300, -depth => 8);

$mixer = SDL::Mixer->new(-frequency => MIX_DEFAULT_FREQUENCY,
-format    => MIX_DEFAULT_FORMAT,
-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,ogg 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


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 },

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

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]