Skip to content

Instantly share code, notes, and snippets.

@vi
Last active December 16, 2015 14:20
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vi/5447962 to your computer and use it in GitHub Desktop.
Output music from Perl
#!/usr/bin/perl
# Output monophonic Mozart - Rondo Alla Turca to stdout
# Implemented by Vitaly "_Vi" Shukela
# Usage: ./rondo.pl | aplay -f cd
# Used mozk331c.mid as reference
our $vq = 0.2;
sub tone($$) {
my $freq = shift;
my $len = shift;
$len = int($len * 44100);
my $vol = 500 + 10000000/$freq;
$vol = 20000 if $vol > 20000;
$vol*=$vq;
for my $i (0..$len) {
my $b = int(sin($i*2*3.14159264358*$freq/44100)*$vol*(1-0.5*$i/$len));
$b+=65535 if($b<0);
print pack("vv", $b, $b);
}
}
sub note($$) {
my $num = shift;
my $len = shift;
tone(440*(2**($num/12)), $len);
}
sub parse($) {
$_ = shift;
if (not m!^(.*)([0-9A-F])!) {
#print STDERR "Error\n";
return;
}
my $l = $2;
my $tmp = $1; $tmp =~ s![^ #]!!g; my $n = length($tmp);
#my $n=length($1 =~ s![^ #]!!gr);
$l = 16 if $l eq '0';
$l = ord($l)-ord('A')+10 unless $l eq int($l);
#print STDERR "l=$l n=$n\n";
note($n, $l/16.0);
}
sub p1($) {
my $v = shift;
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # |2# # # # # | # # # # # |");
parse("| # # # # #2| # # # # # | # # # # # |");
parse("| # # # # # |2# # # # # | # # # # # |");
parse("| # # # # # | # 8 # # # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # 2 # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # | # 2 # # # | # # # # # |");
parse("| # # # # # | # # #8# # | # # # # # |");
parse("| # # # # # | # # # 2 # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | # 8 # # # |");
if ($v == 0) {
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | # 4 # # # |");
parse("| # # # # # | # # # # 1 | # # # # # |");
parse("| # # # # # | # # # # # |1# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # 4 | # # # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # 1 | # # # # # |");
parse("| # # # # # | # # # # # |1# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # 4 | # # # # # |");
parse("| # # # # # | # # # # # |3# # # # # |");
parse("| # # # # # | # # # # 1 | # # # # # |");
parse("| # # # # # | # # # # # |1# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # 4 | # # # # # |");
parse("| # # # # # | # # # #4# | # # # # # |");
parse("| # # # # # | # # #8 # | # # # # # |");
} else {
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # | # 4 # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # # 4 # | # # # # # |");
parse("| # # # # # | # # 4# # # | # # # # # |");
parse("| # # # # # | # 8 # # # | # # # # # |");
parse("| # # # # # | #6# # # # | # # # # # |");
parse("| # # # # # |1# # # # # | # # # # # |");
parse("| # # # # # | 1 # # # # | # # # # # |");
parse("| # # # # # |8# # # # # | # # # # # |");
parse("| # # # # # | # # # # # | # # # # # |");
}
}
sub p2($) {
my $v = shift;
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # # 4 # | # # # # # |");
parse("| # # # # # | # # # # 4 | # # # # # |");
parse("| # # # # # | # # # # 4 | # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # 2 | # # # # # |");
parse("| # # # # # | # # # 2 # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 4# # # | # # # # # |") unless $v;
parse("| # # # # 4 | # # # # # | # # # # # |") unless $v;
parse("| # # # # # | # # 8# # # | # # # # # |") if $v;
}
sub p3($) {
my $v = shift;
parse("| # # # # # | # 4 # # # | # # # # # |");
parse("| # # # # # | # # 4# # # | # # # # # |");
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # # 2 # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # 2 # # # | # # # # # |");
parse("| # # # # # | #4# # # # | # # # # # |") unless $v;
parse("| # # #4# # | # # # # # | # # # # # |") unless $v;
parse("| # # # # # | #8# # # # | # # # # # |") if $v;
}
sub p4() {
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # | # #8 # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # | # #4 # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # # #4# | # # # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # | # #8 # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # | # #4 # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # # #4# | # # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
parse("| # # # # # | # # # # #4| # # # # # |");
parse("| # # # # # | # # #4# # | # # # # # |");
parse("| # # # # # | # # # # # |8# # # # # |");
}
sub p5($) {
my $v = shift;
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # 2 # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # 2 # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # 2 # # # |");
if ($v == 0) {
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # 2 # # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2 # # | # # # # # |");
parse("| # # # # # | # 2 # # # | # # # # # |");
parse("| # # # # # | # #8 # # # | # # # # # |");
} else {
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # 2 # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | 2 # # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # 2 # | # # # # # |");
parse("| # # # # # | # # # #8# | # # # # # |");
}
}
sub p6($) {
my $v = shift;
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # |2# # # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
if (!$v) {
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # |2# # # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # # #2# | # # # # # |");
parse("| # # # # # | # # # # #2| # # # # # |");
parse("| # # # # # | # # # # # |2# # # # # |");
parse("| # # # # # | # # # # # | 4 # # # # |");
parse("| # # # # # | # # # # # | #4# # # # |");
} else {
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | # # #2# # | # # # # # |");
parse("| # # # # # |2# # # # # | # # # # # |");
parse("| # # # # # | # #2 # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # | # # 2# # # | # # # # # |");
parse("| # # # # #2| # # # # # | # # # # # |");
parse("| # # # # # | #2# # # # | # # # # # |");
parse("| # # # # # |8# # # # # | # # # # # |");
}
}
sub p7($) {
my $v = shift;
parse("| # # # # # | # # # # # | # #0 # # # |");
parse("| # # # # # | # # # # # | # #0 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 2# # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # | # #2 # # # |");
parse("| # # # # # | # # # # # | # # 0# # # |");
if (not $v) {
parse("| # # # # # | # # # # # | # 1 # # # |");
parse("| # # # # # | # # # # # | # #3 # # # |");
parse("| # # # # # | # # # # # | # 1 # # # |");
parse("| # # # # # | # # # # # | # #3 # # # |");
parse("| # # # # # | # # # # # | # 1 # # # |");
parse("| # # # # # | # # # # # | # #3 # # # |");
parse("| # # # # # | # # # # # | # 1 # # # |");
parse("| # # # # # | # # # # # | # #3 # # # |");
parse("| # # # # # | # # # # # | #C# # # # |");
parse("| # # # # # | # # # # # | # # #4# # |");
} else {
parse("| # # # # # | # # # # # | #2# # # # |");
parse("| # # # # # | # # # # # | # #E # # # |");
parse("| # # # # # | # # # # # | 1 # # # # |");
parse("| # # # # # | # # # # # | #3# # # # |");
parse("| # # # # # | # # # # # | 1 # # # # |");
parse("| # # # # # | # # # # # | #3# # # # |");
parse("| # # # # # | # # # # # | 1 # # # # |");
parse("| # # # # # | # # # # # | #3# # # # |");
parse("| # # # # # | # # # # # | 1 # # # # |");
parse("| # # # # # | # # # # # | #3# # # # |");
if($v!=2) {
parse("| # # # # # | # # # # # |0# # # # # |");
}
}
}
p1(0);
p1(0);
p2(0);
p2(1);
p3(0);
p3(1);
p1(1);
p2(0);
p2(1);
p3(0);
p3(1);
p1(1);
$vq=0.4;
p4();
p4();
$vq=0.2;
p5(0);
p5(0);
$vq=0.4;
p6(0);
p6(1);
$vq=0.2;
p5(1);
$vq=0.4;
p6(0);
p6(1);
$vq=0.2;
p5(1);
$vq=0.4;
p4();
p4();
$vq=0.2;
p1(0);
p1(0);
p2(0);
p2(1);
p3(0);
p3(1);
p1(1);
p2(0);
p2(1);
p3(0);
p3(1);
p1(1);
$vq=0.4;
p4();
p4();
$vq=0.5;
p7(0);
p7(1);
$vq=0.3;
p7(0);
$vq=0.5;
p7(2);
parse("| # # # # # | # # # # # |C# # # # # |");
parse("| # # # # # | # # # # # | # #4 # # # |");
parse("| # # # # # | # # # # # |C# # # # # |");
parse("| # # # # # | # # # # # | # # #4# # |");
parse("| # # # # # | # # # # # |C# # # # # |");
parse("| # # # # # | # # # # # | # #4 # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | # #4 # # # |");
parse("| # # # # # | # # # # # |4# # # # # |");
parse("| # # # # # | # # # # # | # # #4# # |");
parse("| # # # # # | # # # # # |8# # # # # |");
parse("| # # # # # | # # # # # |8# # # # # |");
parse("| # # # # # | # # # # # |0# # # # # |");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment