forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barycenter
149 lines (121 loc) · 3.28 KB
/
barycenter
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
#!/usr/bin/env perl
use strict;
use warnings;
use AI::Genetic;
use List::Util qw/ uniq /;
use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(MIDI-Util Music-Interval-Barycentric);
use MIDI::Util qw(setup_score set_chan_patch); # https://metacpan.org/release/MIDI-Util
use Music::Interval::Barycentric; # https://metacpan.org/release/Music-Interval-Barycentric
use Music::Chord::Namer 'chordname';
use Music::Note;
my $bpm = shift || 200;
my $patch = shift || 0;
my $population = 100;
my $iterations = 2;
my $max = 48; # Number of results
my $top = 8; # Highest value of chromosome
my $base = 60; # Base of middle C MIDI values
my @fittest = evo($max);
for my $fit (@fittest) {
my @named;
for my $n (sort { $a <=> $b } @$fit) {
my $note = Music::Note->new($n + $base, 'midinum');
push @named, $note->format('isobase');
}
printf "[%d,%d,%d] => (%s,%s,%s) => %s\n", @$fit, @named, scalar chordname(@named);
}
my $score = setup_score(bpm => $bpm, patch => $patch);
my $count = 0;
$score->synch(
\&fit,
\&drums,
);
$score->write_score("$0.mid");
sub fit {
for my $fit (@fittest[0 .. 7]) {
legato($fit);
}
for my $fit (@fittest[8 .. 15]) {
staccato($fit);
}
for my $fit (@fittest[16 .. 23]) {
leading($fit);
}
for my $fit (@fittest[24 .. 31]) {
legato($fit);
}
for my $fit (@fittest[32 .. 39]) {
staccato($fit);
}
for my $fit (@fittest[40 .. 47]) {
legato($fit);
}
# Resolution
$score->n('qn', 65, 69, 72); # IV
$score->r('qn');
$score->n('qn', 67, 71, 74); # V
$score->r('qn');
$score->n('wn', 60, 64, 67); # I
}
sub drums {
set_chan_patch($score, 9, 44);
for my $duration (('qn') x $count) { # Measures of quarter-notes
$score->n($duration, 44, 'm'); # hi-hat
}
$score->n('qn', 38, 'm'); # snare
$score->n('qn', 35, 'm'); # kick
$score->n('qn', 38, 'm'); # snare
$score->n('qn', 35, 'm'); # kick
$score->n('wn', 49, 'm'); # crash
}
sub legato {
my ($fit) = @_;
$score->n('qn', map { $base + $_ } @$fit);
$score->r('qn');
$count += 2;
}
sub staccato {
my ($fit) = @_;
for my $note (@$fit) {
$score->n('sn', $base + $note);
$score->r('sn');
$count += 0.5;
}
}
sub leading {
my ($fit) = @_;
$score->n('qn', $base + $fit->[0]);
$score->n('qn', map { $base + $_ } @{ $fit }[1,2]);
$count += 2;
}
sub evo {
my $max = shift;
my $ga = AI::Genetic->new(
-fitness => \&fitness,
-type => 'listvector',
-population => $population,
-crossover => 0.95,
-mutation => 0.01,
);
my $items = [ ([ 0 .. 11 ]) x 3 ]; # Chromatic triad
$ga->init($items);
$ga->evolve('rouletteTwoPoint', $iterations);
my @genes;
for my $fit ($ga->getFittest($max)) {
push @genes, $fit->{GENES};
}
return @genes;
}
sub fitness {
my $chromosome = shift;
return 0
if @$chromosome > uniq(@$chromosome); # Duplicates not allowed
# Chords with adjacent half-steps are not allowed
for my $i (@$chromosome) {
for my $j (@$chromosome) {
return 0
if ($j == $i + 1) || ($j == $i - 1);
}
}
return $top - evenness_index($chromosome);
}