in reply to Re^2: building a "realtime" software synth
in thread building a "realtime" software synth
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):
This works perfectly on my machine.#!/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); }
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):
Hope this clarifies.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(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: building a "realtime" software synth
by b4e (Sexton) on Jul 20, 2004 at 15:57 UTC | |
by Joost (Canon) on Jul 20, 2004 at 16:11 UTC | |
by b4e (Sexton) on Jul 20, 2004 at 16:24 UTC | |
by Joost (Canon) on Jul 20, 2004 at 16:29 UTC | |
by b4e (Sexton) on Jul 20, 2004 at 16:39 UTC | |
|