forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-fibo2
37 lines (26 loc) · 1.14 KB
/
base-fibo2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl
# https://www.youtube.com/watch?v=azyCk4jWhLg <- Audio rendering
use strict;
use warnings;
use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(MIDI-Util);
use MIDI::Util qw(setup_score);
use Math::BaseConvert;
use Math::Fibonacci 'series';
my $limit = shift || 16; # Number of Fibonaccis to use
my @scale = @ARGV ? @ARGV : qw( C4 Ds4 F4 G4 As4 C5 ); # Notes to choose from
my $modulo = @scale; # Size of the scale being used
my $notes = join '|', @scale; # "OR regular expression" of alternating notes
Math::BaseConvert::dig( \@scale ); # Set the base digits to the scale notes
my @series = series($limit); # Get the Fibonacci numbers
my $score = setup_score( patch => 42 );
# For each Fibonacci number...
for my $n ( @series ) {
my $x = cnv( $n, 10, $modulo ); # Base-convert it to a note string
my @phrase = $x =~ /($notes)/g; # Separate the note string into an array
# Output the progress:
printf "%d => %s (%d)\n", $n, join( ' ', @phrase ), scalar(@phrase);
for my $i ( @phrase ) {
$score->n('qn', $i); # Add each note of the phrase to the MIDI score
}
}
$score->write_score( $0 . '.mid' );