Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Simple tone to audio card speakers by zentara
in thread Simple tone to audio card speakers by cronk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found