Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Making Sounds

by zentara (Archbishop)
on Nov 15, 2010 at 12:56 UTC ( [id://871464]=note: print w/replies, xml ) Need Help??


in reply to Making Sounds

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-19 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found