Skip to content

Instantly share code, notes, and snippets.

@HaraldJoerg
Created April 12, 2023 18:36
Show Gist options
  • Save HaraldJoerg/7b6efc314718a0f69c971232ac118653 to your computer and use it in GitHub Desktop.
Save HaraldJoerg/7b6efc314718a0f69c971232ac118653 to your computer and use it in GitHub Desktop.
(Linux) sound with Perl and a few lines of PDL
use PDL;
my $freq = 440;
my $rate = 8000; # default for aplay
my $samples = pdl[0..$rate-1];
open (my $aplay,'|-','aplay');
my $raw_sound = byte(zeroes($rate));
my $amplitude = 64;
for (1..5) {
$raw_sound += byte($amplitude*(1+sin($samples*$_*$freq*6.28/$rate)));
$amplitude /= 2;
print $aplay $raw_sound->get_dataref->$*;
}
close $aplay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment