-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOMB.pl
More file actions
197 lines (178 loc) · 5.67 KB
/
OMB.pl
File metadata and controls
197 lines (178 loc) · 5.67 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper::Compact qw(ddc);
use MIDI::Drummer::Tiny ();
use Music::Duration::Partition ();
my $bpm = shift || 70; # Beats per minute
my $size = shift || 4; # Motif duration in quarter-notes
my $max = shift || 4; # Repeats
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $bpm,
volume => 127,
bars => 4,
);
my @fills = map { my $sub = 'fill_' . $_; \&$sub } 1 .. 6;
$d->score->synch(
\&hihat,
\&kick,
\&snare,
) for 1 .. $max;
$d->write;
sub hihat {
my $roll = int rand 2; # "roll" as in dice
my $pool = $roll ? [qw(qn en sn)] : [qw(qn en)];
_part('Hihat', $d->closed_hh, $pool)
for 1 .. $d->bars;
}
sub kick {
my $roll = int rand 2; # "roll" as in dice
my $pool = $roll ? [qw(qn en)] : [qw(hn dqn qn)];
_part('Kick', $d->kick, $pool)
for 1 .. $d->bars;
}
sub snare {
for (1 .. $d->bars) {
my $roll = int rand 2; # "roll" as in dice
print "Snare: $roll\n";
for (1 .. $d->bars * 2 - 1) {
for my $n (1 .. $size) {
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 _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 * 2 - 1) {
for my $duration (@$motif) {
$d->note($duration, $note);
}
}
# Rest while we fill
$d->rest('wn');
}
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;
}