-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalgo-progression
150 lines (114 loc) · 3.83 KB
/
algo-progression
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
#!/usr/bin/env perl
# Play a random rock progression with a randomized walking bassline.
# Example:
# $ perl algo-progression 120 1 Amv-Amc 2
# $ perl algo-progression 70 2 Amv-DMc 1 42 35
# $ timidity %.mid
use strict;
use warnings;
use Music::Chord::Note ();
use Music::Scales qw(get_scale_notes);
use Data::Dataset::ChordProgressions ();
use Music::Bassline::Generator ();
use MIDI::Drummer::Tiny ();
use MIDI::Util qw(set_chan_patch midi_format);
use Music::Duration::Partition ();
my $bpm = shift || 100;
my $parts = shift || 'Amv-DMc-Emv-DMc'; # <Note><Major|minor><verse|chorus> phrases
my $reps = shift || 1; # The number of times to repeat an individual phrase
my $multi = shift || 1; # The number of times the phrases are repeated
my $chords_patch = shift || 89;
my $bass_patch = shift || 35;
my @parts = split /-/, $parts;
my $channel = 0;
my $octave = 5;
my @progressions;
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $bpm,
bars => 4 * @parts * $reps,
reverb => 10,
);
$d->sync(
\&drums,
\&chords,
\&bass,
);
$d->write;
sub drums {
$d->metronome4($multi * $d->bars, 1);
}
sub chords {
set_chan_patch($d->score, $channel++, $chords_patch);
my $cn = Music::Chord::Note->new;
my %data = Data::Dataset::ChordProgressions::as_hash();
my @accum; # Note accumulator
for my $part (@parts) {
my ($note, $section, $scale, $pool);
# Set the pool of possible progressions given scale and section
if ($part =~ /^([A-G][#b]?)(M|m)(v|c)$/) {
($note, $scale, $section) = ($1, $2, $3);
$scale = $scale eq 'M' ? 'major' : 'minor';
$section = $section eq 'v' ? 'verse' : 'chorus';
$pool = $data{rock}{$scale}{$section};
}
# Set the transposition map
my %note_map;
@note_map{ get_scale_notes('C', $scale) } = get_scale_notes($note, $scale);
# Get a random progression
my $progression = $pool->[int rand @$pool];
# Transpose the progression chords from C
(my $named = $progression->[0]) =~ s/([A-G][#b]?)/$note_map{$1}/g;
# Keep track of the progressions used
push @progressions, $named;
print "$note $scale: $named, $progression->[1]\n";
my @chords = split /-/, $named;
# Add each chord to the score
for my $j (1 .. $reps) {
for my $chord (@chords) {
$chord =~ s/sus2/add9/;
$chord =~ s/6sus4/sus4/;
my @notes = $cn->chord_with_octave($chord, $octave);
@notes = midi_format(@notes);
push @accum, \@notes;
}
}
}
for my $j (1 .. $multi) {
print "\t", join ', ', map { "[@$_]" } @accum;
for my $n (@accum) {
$d->note($d->whole, @$n);
}
print "\n";
}
}
sub bass {
set_chan_patch($d->score, $channel++, $bass_patch);
my $mdp = Music::Duration::Partition->new(
size => 4,
pool => [qw/ dhn hn qn en /],
weights => [ 1, 2, 3, 1 ],
);
my $motif1 = $mdp->motif;
my $motif2 = $mdp->motif;
my $bassline = Music::Bassline::Generator->new(
octave => 2,
guitar => 1,
verbose => 0,
scale => sub { $_[0] =~ /^[A-G][#b]?m/ ? 'pminor' : 'pentatonic' },
);
for (1 .. $reps * $multi) {
for my $p (@progressions) {
my @chords = split /-/, $p;
my $i = 0;
for my $chord (@chords) {
$chord =~ s/sus2/add9/;
$chord =~ s/6sus4/sus4/;
my $m = $i % 2 == 0 ? $motif2 : $motif1;
my $notes = $bassline->generate($chord, scalar(@$m));
$mdp->add_to_score($d->score, $m, $notes);
$i++;
}
}
}
}