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

Simple tone to audio card speakers

by cronk (Initiate)
on Jan 20, 2007 at 16:27 UTC ( #595680=perlquestion: print w/ replies, xml ) Need Help??
cronk has asked for the wisdom of the Perl Monks concerning the following question:

I currently use Audio::Beep to produce simple sine wave tones to my pc speaker. beep(324,1500) 324Hz for 1500 milliseconds for example. Is there a way or CPAN package that will allow such simple requests to be played through the standard speakers via the sound card? Thanks.

Comment on Simple tone to audio card speakers
Re: Simple tone to audio card speakers
by idsfa (Vicar) on Jan 20, 2007 at 16:37 UTC

    The Audio namespace seems to have several options. It looks like Audio::Play is the most portable.

    use Audio::Data; use Audio::Play; $audio = Audio::Data->new(...) $svr = Audio::Play->new; $svr->play($audio);

    Audio::DSP or Audio::OSS may be good options on Linux.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: Simple tone to audio card speakers
by Joost (Canon) on Jan 20, 2007 at 16:37 UTC
    Audio::Play and SDL can both play sample files in a fairly portable way. Audio::Play can also take a buffer of generated samples. I'm currently working on a module that can generate all kinds of audio streams, but it doesn't really have as simple an interface as Audio::Beep.

    If you want to experiment, you can also try my very new Audio::PortAudio module (should be on CPAN soon - i uploaded it 2 hours ago). It does more or less the same thing Audio::Play does, but also does input and multiple channels.

Re: Simple tone to audio card speakers
by zentara (Archbishop) on Jan 20, 2007 at 17:36 UTC
    The Audio::DSP module allows you to write generated tones to the soundcard. The PDL::Audio module generates realistic bird calls from data. :-) The script below, generates a series of numbers, that is fed to the soundcard, play with it for different tones.
    #!/usr/bin/perl use warnings; use strict; use Audio::DSP; $| = 1; my $sample_rate = 22050; #44100 gives best sound, but highest cpu #8000 works my $channels = 1; my $format = AFMT_S16_LE; #8 bit won't work on SBLive my $buffer = 4096; 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 $toggle = 1; while(1){ make_tone($freq_adj, $vol); if(($freq_adj > 4) or ($freq_adj < .1)){ $toggle *= -1;} $freq_adj += ($toggle * .001); #higher increment numbers are slower #0 is pure tone } sub make_tone { my $rad = 0; my ($freq_adj, $vol) = @_; while ( $rad < 6.283 ){ $rad += $freq_adj; my $raw = ($vol*32768) * sin($rad); #max times my $num = pack( 'V', $raw ); $dsp->dwrite($num); } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Strange for both Audio::DSP and Audio::port I get a vast number of the same errors when trying to install either....
        Well I got the Audio::DSP to install on a different linux box, so I am playing with it now. Would it be too much to ask what the code would be to play a tone of 300 Hz? Thanks :-) I figured out what the frequency control is. I appears that Audio::DSP doe not lend itself to playing a tune. Am I mistaken?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-10-30 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls