in reply to Re^2: building a "realtime" software synth
in thread building a "realtime" software synth

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.

Replies are listed 'Best First'.
Re^4: building a "realtime" software synth
by b4e (Sexton) on Jul 20, 2004 at 15:57 UTC
    this is basically excactly what i want.
    thanks!!

    apperently i overlooked the Audio::Play module while scanning through cpan...
    i am currently trying to install the Audio-1.029 module (which will hopefully work, since i had problems installing c-pan stuff fairly often...)
    is there any other way to get the Audio-1.029 module without having to do the makefile thing ? perfect would be all the files and some readme saying where to put them (i hope this question isn't too annoying...)
      No, you need to compile the C parts of the modules, so either you download a precompiled PPM file (if you have ActiveState perl), or you need to do the full
      perl Makefile.pl make make test make install
      Routine.

      You'll need MS Visual C compiler and nmake if you have ActiveState perl, or you can try cygwin, which has perl and gcc and make, so you can compile everything using free software.

        fair enough...

        problem is : when i am running 'perl makefile.pl' the message "Testing for Network Audio Server (NAS)" appears and then nothing happens, ie the application is stuck (i've left it for about 10 minutes or so...)

        i've got cygwin on my machine (especially for the cpan modules), but embarassingly enough i don't really know how to use it for this purpose.

        i'm sure you get a lot of this 'how do i install cpan modules' questions, so i hope this isn't to tedious for you.