Hmmm. I know for a fact that Audio::Play (should you decide to use it) uses floats for sample data and most pcm output hardware uses 16-bit words per sample. Your code assumes its 8-bits.

It's also probably more efficient to generate an @array of data first (say 100 samples) and then pack() it into a string and write it (Audio::Play can do this for you):

#!/usr/bin/perl -w use strict; use Audio::Play; use Audio::Data; my $player = Audio::Play->new(); while (1) { my $audio = Audio::Data->new( rate => 44100 ); my @samples = map { sin ( 2 * 3.14 * $_ / 100 ) / 8 } 0 .. 99; # +generate 100 samples as perl numbers $audio->data(@samples); # will pack() samples into floats $player->play($audio); }
This works perfectly on my machine.

Update:

To clarify: you really don't want to calculate and then play each sample seperately. It's incredibly inefficient so even most C - based softsynths don't do that.

What you want is a low latency between dragging a slider, and hearing the sound change. IMHO checking the slider state once for every 100 samples is enough:

Say you're running at 44100 Hz (normal CD sample rate), then playing 100 samples takes 100/44100 =~ 0.0023 seconds. That's probably fast enough for anything except really exact keyboard input, provided you don't have to sync your ouput with audio streams generated from outside your machine.

So, what you'd do is (pseudocode):

while (1) { check_for_input(); update_parameters_for_new_input(); #possibly using heavy math generate_100_samples_as_fast_as_possible(); push_samples_into_soundcard_buffer(); }
Hope this clarifies.
Joost.

In reply to Re^3: building a "realtime" software synth by Joost
in thread building a "realtime" software synth by b4e

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.