-
Notifications
You must be signed in to change notification settings - Fork 5
/
gravity-well
167 lines (124 loc) · 3.73 KB
/
gravity-well
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env perl
use strict;
use warnings;
# Prefer my local libraries
use MIDI::Util qw(setup_score set_chan_patch);
use MIDI::Drummer::Tiny;
use Music::Cadence;
use Music::Scales;
use Music::Voss qw/ powers /;
my $max = shift || 16;
my $note = shift || 'C';
my $bpm = shift || 100;
my $leads = shift || '2 7';
my $top_scale = 'minor';
my $bottom_scale = 'pminor';
my $top_patch = 0;
my $bottom_patch = 42;
my $bottom_notes = [];
my $eighth = 'en';
my $quarter = 'qn';
my $half = 'hn';
my $whole = 'wn';
my $score = setup_score( lead_in => 0, bpm => $bpm );
$score->synch(
\&top,
\&bottom,
\&drums,
);
$score->write_score("$0.mid");
sub top {
set_chan_patch( $score, 0, $top_patch );
my ( $scale, $genf ) = get_genf( $note, 5, $top_scale );
my @leaders = split /\s+/, $leads;
my $mc = Music::Cadence->new(
key => $note,
scale => $top_scale,
octave => 4,
format => 'midi',
);
my $i = 0; # Cadence alternation
my $j = 0; # Imperfect variation/inversion
for my $n ( 1 .. $max ) {
if ( $i % 2 == 0 ) {
$score->n( $eighth, $scale->[ $genf->($n) ] ) for 1 .. 2;
$score->n( $quarter, $scale->[ $genf->($n) ] );
}
else {
$score->n( $quarter, $scale->[ $genf->($n) ] ) for 1 .. 2;
}
# Add a cadence after every 4th iteration
if ( $n % 4 == 0 ) {
my $chords;
if ( $i % 2 == 0 ) {
$chords = $mc->cadence(
type => 'half',
leading => $leaders[ int rand @leaders ],
);
}
else {
$chords = $mc->cadence(
type => 'imperfect',
$j % 2 == 0
? ( variation => 2 )
: ( inversion => { 1 => 1, 2 => 1 } ),
);
$j++;
}
( $i, $bottom_notes ) = truncate_start( $bottom_notes, $chords, $score, $half, $i );
}
}
my $chords = $mc->cadence(
type => 'deceptive',
variation => int( rand 2 ) + 1,
);
( $i, $bottom_notes ) = truncate_start( $bottom_notes, $chords, $score, $half );
$chords = $mc->cadence( type => 'plagal' );
( $i, $bottom_notes ) = truncate_start( $bottom_notes, $chords, $score, $half );
}
sub bottom {
set_chan_patch( $score, 1, $bottom_patch );
s/4/3/ for @$bottom_notes;
my ( $scale, $genf ) = get_genf( $note, 3, $bottom_scale );
my $i = 0;
for my $n ( 1 .. $max ) {
$score->n( $half, $scale->[ $genf->($n) ] );
if ( $n % 4 == 0 ) {
$score->n( $half, $bottom_notes->[$i] );
$score->n( $half, $bottom_notes->[ ++$i ] );
$i++;
}
}
$score->n( $half, $bottom_notes->[$i] );
$score->n( $half, $bottom_notes->[ ++$i ] ) for 1 .. 3;
}
sub get_genf {
my ( $note, $octave, $type ) = @_;
my @scale = map { $_ . $octave } get_scale_notes( $note, $type );
# Transform to MIDI accidentals
for ( @scale ) {
s/#/s/;
s/b/f/;
}
my $seed = [ map { sub { int rand 2 } } @scale ];
my $genf = powers( calls => $seed );
return \@scale, $genf;
}
sub truncate_start {
my ( $notes, $chords, $score, $duration, $i ) = @_;
push @$notes, $chords->[0][0], $chords->[1][0];
# Remove the first note of each chord
shift @$_ for @$chords;
$score->n( $duration, @$_ ) for @$chords;
$i++;
return $i, $notes;
}
sub drums {
my $d = MIDI::Drummer::Tiny->new(
score => $score,
bpm => $bpm,
);
for my $n ( 1 .. $max * 3 ) {
$d->note( $d->quarter, $d->open_hh, $n % 2 ? $d->kick : $d->snare )
}
}