Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Making Sounds

by fluffyvoidwarrior (Monk)
on Nov 15, 2010 at 10:16 UTC ( [id://871431]=perlquestion: print w/replies, xml ) Need Help??

fluffyvoidwarrior has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks Dumb question I'm sure but.... What's the easiest way to make sounds from a perl script. I'm running Ubuntu on a laptop with a soundcard and digital audio but no old fashioned system speaker to make beeps on. I simply want to generate warning sounds and alarms on the fly as events occur. I've tried using echo and bash will beep the soundcard but echo through perl with backticks won't.

Replies are listed 'Best First'.
Re: Making Sounds
by zentara (Archbishop) on Nov 15, 2010 at 12:56 UTC
    I simply want to generate warning sounds and alarms on the fly as events occur.

    You can write direct to the /dev/dsp to make a variety of beep-like sounds of various frequencies. I take manual control over the Tk loop, to keep the eventloop from blocking while generating tones. You could make different sounds for different signals.

    #!/usr/bin/perl use warnings; use strict; use Tk qw/MainLoop tkinit DoOneEvent exit DONT_WAIT ALL_EVENTS/; use Audio::DSP; $| = 1; my $sample_rate = 8000; my $channels = 1; my $format = AFMT_S16_LE; my $buffer = 1; my $dsp = new Audio::DSP(buffer => $buffer, channels => $channels, format => $format, rate => $sample_rate); $dsp->init() || die $dsp->errstr(); ############################################################ my $freq_adj = .1; my $vol = .5; my @speeds = (8000,11025,22050,44100); ############################################################## my $mw = tkinit; my $stop = 0; my $adjust = 10000; $mw->Scale(-from => 0, -to => 10000, -variable => \$adjust, -orient => 'horizontal', -showvalue => 1, -command => sub { $freq_adj = $adjust/10000 + .01 ; $mw->DoOneEvent( DONT_WAIT | ALL_EVENTS ); })->pack; my $volume = 50; $mw->Scale(-from => 0, -to => 100, -variable => \$volume, -orient => 'horizontal', -showvalue => 1, -command => sub { $vol = $volume/100 + .01 ; $mw->DoOneEvent( DONT_WAIT | ALL_EVENTS ); })->pack; $mw->Button( -text => 'Start', -command => sub { $stop = 0; &generate }, )->pack(); $mw->Button( -text => 'Stop', -command => sub { $stop = 1; }, )->pack(); $mw->Button( -text => 'Speed', -command => sub { push @speeds, shift @speeds; $dsp->speed($speeds[0]); } )->pack(); $mw->Button( -text => 'Quit', -command => sub {$stop = 0;$dsp->close; exit(0) }, )->pack(); #MainLoop; $mw->DoOneEvent( MainLoop ); ################################################# sub generate{ while(1){ make_tone($freq_adj, $vol); if($stop){return}; } } sub make_tone { my $rad = 0; my ($freq_adj, $vol) = @_; while ( $rad < 6.283 ){ if($stop){last} $rad += $freq_adj; my $raw = ($vol*32768) * sin($rad); #max times my $num = pack( 'V', $raw ); $dsp->dwrite($num); $mw->DoOneEvent( DONT_WAIT | ALL_EVENTS ); } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Making Sounds
by moritz (Cardinal) on Nov 15, 2010 at 10:29 UTC
    but echo through perl with backticks won't

    That might be related to the fact that you get /bin/echo that way, not the shell built-in.

    You can try system q[bash -c 'echo ...']; instead. Or something like system qq[mplayer $sound_file];

Re: Making Sounds
by kcott (Archbishop) on Nov 15, 2010 at 11:41 UTC

    In many systems, there is a way to convert audible alerts (e.g. a beep) to visual alerts (e.g. a brief flash of the screen). These can be configured on a per-application basis (e.g. .Xdefaults file, --visualbell option and so on) so check you don't have something like that in place.

    From Perl, have you tried:

    print "\a";

    You mentioned generating these sounds in response to certain events. There may be something related to the code monitoring these events which can do this. Without seeing any code, I can't be specific but, as an example, from Perl/Tk's event loop you can do this:

    $widget->bell();

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://871431]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-19 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found