forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bach-choral-freq
231 lines (177 loc) · 5.04 KB
/
bach-choral-freq
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env perl
use strict;
use warnings;
# Run bach-choral.R and divide-up the data file, before running this.
# Then: perl % ~/Documents/data/jsbach_chorals_harmony/bach-choral-000206b_.csv
use lib '/Users/gene/sandbox/MIDI-Util/lib';
use MIDI::Util qw(setup_score);
use Text::CSV;
use Graph::Weighted;
use List::Util::WeightedChoice qw(choose_weighted);
use Music::Chord::Note;
use MIDI::Simple;
use Music::Tempo;
use List::Util qw(shuffle);
my $file = shift || die "Usage: perl $0 file.csv\n";
my $note_number = shift || 15;
my ( $index, $full_score ) = read_bach($file);
my $weights = build_weights($index);
my $g = Graph::Weighted->new;
$g->populate($weights);
#$g->dump();
my @notes = collect_notes( $g, $note_number );
my ( $melody, $bassline, $chords ) = split_records( $full_score, @notes );
my $score = setup_score();
my @parts = (
\&melody,
\&bassline,
\&chords,
);
$score->synch(@parts);
my $name = "$0.mid";
$score->write_score($name);
#system( 'timidity', $name );
sub read_bach {
my ($file) = @_;
my $csv = Text::CSV->new ( { binary => 1 } )
or die 'Cannot use CSV: ' . Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", $file
or die "Can't read $file: $!";
my $full_score;
my $index;
my $previous;
$csv->getline($fh); # Skip the header.
while ( my $row = $csv->getline($fh) ) {
my $notes = '';
for my $note ( 3 .. 14 ) {
$notes .= $row->[$note] eq 'YES' ? 1 : 0;
}
my $chord = $row->[17];
my $bassnote = $row->[15];
my $current = join( ',', $notes, $bassnote, $chord );
push @$full_score, $current;
if ($previous) {
$index->{$previous}{$current}++;
}
$previous = $current;
}
$csv->eof or $csv->error_diag();
close $fh;
return ( $index, $full_score );
}
sub build_weights {
my ($index) = @_;
for my $position ( keys %$index ) {
my $n = 0;
$n += $index->{$position}{$_}
for keys %{ $index->{$position} };
for my $child ( keys %{ $index->{$position} } ) {
$weights->{$position}{$child} = $index->{$position}{$child} / $n;
}
}
return $weights;
}
sub collect_notes {
my ( $pitch, $n ) = @_;
my @v = $pitch->vertices;
my $vertex = $v[ int( rand $pitch->vertices ) ];
my @notes;
for my $i ( 1 .. $n ) {
push @notes, $vertex;
if ( $i < $n ) {
$vertex = next_vertex( $pitch, $vertex );
}
}
return @notes;
}
sub next_vertex {
my ( $graph, $vertex ) = @_;
my $successors = [];
for my $successor ( $graph->successors($vertex) ) {
push @$successors, {
vertex => $successor,
weight => $graph->get_cost( [ $vertex, $successor ] ),
};
}
my $choice = choose_weighted( $successors, sub { $_[0]->{weight} } );
return $choice->{vertex};
}
sub split_records {
my ( $full_score, @records ) = @_;
my ( @melody, @bitstrings, @bassline, @chords );
# Parse records into musical parts.
my $counter = 0;
for my $record (@records) {
my ( $bitstring, $bass, $chord ) = split /,/, $record;
push @bitstrings, $bitstring;
push @bassline, $bass;
push @chords, $chord;
if ( $counter == $#records ) {
my ( $last_bitstring, $last_bass, $last_chord ) = split /,/, $full_score->[-1];
push @bitstrings, $last_bitstring;
push @bassline, $last_bass;
push @chords, $last_chord;
}
$counter++;
}
# Build melody.
my @note_index = (qw( C Cs D Ds E F Fs G Gs A As B ));
for my $bitstring (@bitstrings) {
my $i = 0;
my @notes;
for my $bit ( split '', $bitstring ) {
push @notes, $note_index[$i]
if $bit;
$i++;
}
my $duration =
@notes == 2 ? 'hn' :
@notes == 3 ? 'thn' :
@notes == 4 ? 'qn' : 'tqn';
for my $note (shuffle @notes) {
push @melody, [ $duration, $note ];
}
}
# Fix-up bass notes.
for (@bassline) {
s/b/f/;
s/#/s/;
}
# Fix-up chord names.
for (@chords) {
s/\s+//;
s/_M$//;
s/_//;
s/^(\w+)#M$/$1#/;
s/^(\w+)bM$/$1b/;
s/^([\w\#]+)M4$/$1sus4/;
s/^([\w\#]+)d6$/$1d/;
s/^([\w\#]+)m4$/$1m/;
s/^([\w\#]+)M6$/$1\6/;
s/^([\w\#]+)d/$1dim/;
}
return \@melody, \@bassline, \@chords;
}
sub melody {
$score->Channel(3);
$score->patch_change( 3, 73 );
$score->Octave(5);
$score->n( @$_ ) for @$melody;
}
sub bassline {
$score->Channel(2);
$score->patch_change( 2, 32 );
$score->Octave(3);
$score->n( 'wn', $_ ) for @$bassline;
}
sub chords {
$score->Channel(1);
$score->patch_change( 1, 5 );
$score->Octave(5);
my $cn = Music::Chord::Note->new();
for my $chord (@$chords) {
my @tone = $cn->chord($chord);
s/#/s/ for @tone;
$score->n( 'wn', @tone );
}
}