forked from ology/Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand-drums
200 lines (180 loc) · 5.71 KB
/
rand-drums
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
#!/usr/bin/env perl
use strict;
use warnings;
# Use local libs
use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(MIDI-Drummer-Tiny Music-Duration Music-Duration-Partition);
use Data::Dumper::Compact qw(ddc);
use MIDI::Drummer::Tiny;
use Music::Duration::Partition;
my $bpm = shift || 120; # Beats per minute
my $size = 4; # Motif duration in quarter-notes
# Instantiate a new drummer
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $bpm,
volume => 127,
bars => 4,
);
# Declare the available fills
my @fills = map { my $sub = 'fill_' . $_; \&$sub } 1 .. 6;
# Play the parts simultaneously
$d->score->synch(
\&hihat,
\&kick,
\&snare,
);
# Write the MIDI to a file
$d->write;
sub hihat {
_part('Hihat', $d->closed_hh, [qw(qn en)])
for 1 .. $d->bars;
}
sub kick {
_part('Kick', $d->kick, [qw(hn dqn qn en)])
for 1 .. $d->bars;
}
sub _part {
my ($name, $note, $pool) = @_;
# Instantiate a new rhythmic phrase generator
my $mdp = Music::Duration::Partition->new(size => $size, pool => $pool);
# Get a random rhythmic phrase
my $motif = $mdp->motif;
print "$name: ", ddc($motif);
# Play the phrase for the number of bars less one
for my $n (1 .. $d->bars - 1) {
for my $duration (@$motif) {
$d->note($duration, $note);
}
}
# Rest while we fill
$d->rest('wn');
}
sub snare {
for my $n (1 .. $d->bars) {
my $roll = int rand 2; # "roll" as in dice
print "Snare: $roll\n";
for my $n (1 .. $d->bars - 1) {
for my $n (1 .. $size) {
# Either play on 2 & 4 or just 3
if ($roll) {
if ($n % 2 == 0) {
$d->note('qn', $d->snare);
}
else {
$d->rest('qn');
}
}
else {
if ($n % 3 == 0) {
$d->note('qn', $d->snare);
}
else {
$d->rest('qn');
}
}
}
}
# Fill for a bar!
fill();
}
}
sub fill {
my $mdp = Music::Duration::Partition->new(
size => $size,
pool => [qw(qn en sn)],
weights => [5, 10, 5],
groups => [0, 0, 2],
);
my $motif = $mdp->motif;
warn 'Fill: ', ddc($motif);
my $fill = @fills[ int rand @fills ];
for my $i (0 .. $#$motif) {
my $patch = $fill->($i);
$d->note($motif->[$i], $patch);
}
}
# Descend the kit
sub fill_1 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i == 0 || $i == 1;
$patch = $d->hi_tom if $i == 2;
$patch = $d->hi_mid_tom if $i == 3;
$patch = $d->low_mid_tom if $i == 4;
$patch = $d->low_tom if $i == 5;
$patch = $d->hi_floor_tom if $i == 6;
$patch = $d->low_floor_tom if $i == 7;
return $patch;
}
# Descend the kit but alternate with the snare
sub fill_2 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i % 2 == 0;
$patch = $d->hi_tom if $i == 1;
$patch = $d->hi_mid_tom if $i == 3;
$patch = $d->low_mid_tom if $i == 5;
$patch = $d->low_tom if $i == 7;
$patch = $d->hi_floor_tom if $i == 9;
$patch = $d->low_floor_tom if $i == 11;
return $patch;
}
# Descend the kit in twos
sub fill_3 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i == 0 || $i == 1;
$patch = $d->hi_tom if $i == 2 || $i == 3;
$patch = $d->hi_mid_tom if $i == 4 || $i == 5;
$patch = $d->low_mid_tom if $i == 6 || $i == 7;
$patch = $d->low_tom if $i == 8 || $i == 9;
$patch = $d->hi_floor_tom if $i == 10 || $i == 11;
$patch = $d->low_floor_tom if $i == 12 || $i == 13;
return $patch;
}
# Descend the kit and possibly strike a cymbal
sub fill_4 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i == 0 || $i == 1;
$patch = _or_cymbal($d->hi_tom) if $i == 2;
$patch = _or_cymbal($d->hi_mid_tom) if $i == 3;
$patch = _or_cymbal($d->low_mid_tom) if $i == 4;
$patch = _or_cymbal($d->low_tom) if $i == 5;
$patch = _or_cymbal($d->hi_floor_tom) if $i == 6;
$patch = _or_cymbal($d->low_floor_tom) if $i == 7;
return $patch;
}
# Descend the kit alternating with the snare but possibly strike a cymbal
sub fill_5 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i % 2 == 0;
$patch = _or_cymbal($d->hi_tom) if $i == 1;
$patch = _or_cymbal($d->hi_mid_tom) if $i == 3;
$patch = _or_cymbal($d->low_mid_tom) if $i == 5;
$patch = _or_cymbal($d->low_tom) if $i == 7;
$patch = _or_cymbal($d->hi_floor_tom) if $i == 9;
$patch = _or_cymbal($d->low_floor_tom) if $i == 11;
return $patch;
}
# Descend the kit in twos but possibly strike a cymbal
sub fill_6 {
my ($i) = @_;
my $patch;
$patch = $d->snare if $i == 0 || $i == 1;
$patch = _or_cymbal($d->hi_tom) if $i == 2 || $i == 3;
$patch = _or_cymbal($d->hi_mid_tom) if $i == 4 || $i == 5;
$patch = _or_cymbal($d->low_mid_tom) if $i == 6 || $i == 7;
$patch = _or_cymbal($d->low_tom) if $i == 8 || $i == 9;
$patch = _or_cymbal($d->hi_floor_tom) if $i == 10 || $i == 11;
$patch = _or_cymbal($d->low_floor_tom) if $i == 12 || $i == 13;
return $patch;
}
sub _or_cymbal {
my ($patch) = @_;
my @cymbals = qw(crash1 crash2 splash china);
my $cymbal = $cymbals[int rand @cymbals];
# Return a cymbal 4/10 times
return int(rand 10) < 4 ? $d->$cymbal : $patch;
}