Skip to content

Instantly share code, notes, and snippets.

@timo
Forked from jonathanstowe/live sine
Created March 15, 2016 17:38
Show Gist options
  • Save timo/d97a284b884274e9e375 to your computer and use it in GitHub Desktop.
Save timo/d97a284b884274e9e375 to your computer and use it in GitHub Desktop.
#!perl6
use NativeCall;
my $samplerate = 44100;
my $frequency = 440;
sub gen-sin(Int $sample-rate, Int $frequency) {
my num $spf = $sample-rate.Num / $frequency.Num;
my num $twopi = (2 * pi).Num;
my $range = (0..$spf);
do loop {
slip $range.map({ sin(($_.Num/($spf)) * $twopi) });
}
}
my $now = now;
my $sin-generator = gen-sin($samplerate, $frequency);
for $sin-generator.rotor(256) -> @a {
my $c = CArray[num32].new(flat @a Z @a);
say now - $now;
$now = now;
last if $++ > 100;
}
# vim: expandtab shiftwidth=4 ft=perl6
@jonathanstowe
Copy link

:) I'll try and plug this in to https://github.com/jonathanstowe/Audio-PortAudio/blob/master/examples/play-sine later to see if it works cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment