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

In reply to Re: Making Sounds by zentara
in thread Making Sounds by fluffyvoidwarrior

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.