forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiloparsec
127 lines (96 loc) · 2.57 KB
/
kiloparsec
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env perl
# MP4: https://youtu.be/vstdXn_ufVY
use strict;
use warnings;
# Prefer my local library
use lib '/Users/gene/sandbox/MIDI-Util/lib';
use MIDI::Util qw(setup_score set_chan_patch);
use Music::Scales qw/ get_scale_notes /;
use Music::Voss qw/ powers /;
my $max = shift || 64;
my $bpm = shift || 200;
my $scale_note = 'C';
my $alto_scale_name = 'minor';
my $tenor_scale_name = 'pminor';
my $baritone_scale_name = 'pminor';
my $alto_patch = 69;
my $tenor_patch = 70;
my $baritone_patch = 42;
my $quarter = 'qn';
my $half = 'hn';
my $whole = 'wn';
my $score = setup_score( lead_in => 0, bpm => $bpm );
$score->synch(
\&alto,
\&tenor,
\&baritone,
);
$score->write_score("$0.mid");
sub alto {
set_chan_patch( $score, 0, $alto_patch );
my ( $scale, $genf ) = get_genf( $scale_note, 5, $alto_scale_name );
my $i = 0;
for my $n ( 0 .. $max - 1 ) {
if ( $n % 4 == 0 ) {
for ( 1 .. 4 ) {
$score->n( $quarter, $scale->[ $genf->($n) ] );
}
if ( $i % 3 == 0 ) {
$score->n( $quarter, $scale->[ $genf->($n) ] );
}
}
else {
$score->r($whole);
}
$i++;
}
$score->r($whole);
}
sub tenor {
set_chan_patch( $score, 1, $tenor_patch );
my ( $scale, $genf ) = get_genf( $scale_note, 4, $tenor_scale_name );
for my $n ( 0 .. $max - 1 ) {
if ( $n < 8 ) {
$score->r($whole);
}
else {
if ( $n % 2 == 0 ) {
$score->n( $half, $scale->[ $genf->($n) ] );
}
else {
$score->r($half);
}
if ( $n % 3 == 0 ) {
$score->n( $half, $scale->[ $genf->($n) ] );
}
else {
$score->r($half);
}
}
}
$score->n( $whole, $scale->[0] );
}
sub baritone {
set_chan_patch( $score, 2, $baritone_patch );
my ( $scale, $genf ) = get_genf( $scale_note, 3, $baritone_scale_name );
for my $n ( 0 .. $max - 1 ) {
if ( $n < 8 ) {
$score->r($whole);
}
else {
$score->n( $whole, $scale->[ $genf->($n) ] );
}
}
$score->n( $whole, $scale->[0] );
}
sub get_genf {
my ( $note, $octave, $name ) = @_;
my @scale = map { $_ . $octave } get_scale_notes( $note, $name );
for ( @scale ) {
s/#/s/;
s/b/f/;
}
my $seed = [ map { sub { int rand 2 } } @scale ];
my $genf = powers( calls => $seed );
return \@scale, $genf;
}