-
Notifications
You must be signed in to change notification settings - Fork 5
/
drum-circle
225 lines (164 loc) · 6.71 KB
/
drum-circle
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
#!/usr/bin/env perl
use strict;
use warnings;
# For local development:
use Data::Dumper::Compact qw(ddc);
use Getopt::Long qw(GetOptions);
use MIDI::Drummer::Tiny ();
use MIDI::Util qw(midi_dump ticks);
use Music::Duration::Partition ();
use Pod::Usage qw(pod2usage);
my %opts = (
drummers => 4, # The number of drummers
bpm => 90, # Beats per minute
extend => 4, # Number of beats to play after all drummers have entered
measures => 4, # Number of bars each drummer plays before the next joins
beats => 4, # Number of beats that a drummer plays per phrase motif
pool => 'qn den en sn', # Pool of possible phrase durations
);
GetOptions( \%opts,
'drummers=i',
'bpm=i',
'extend=i',
'measures=i',
'beats=i',
'pool=s',
'help|?',
'man',
) or pod2usage(2);
pod2usage(1) if $opts{help};
pod2usage(-exitval => 0, -verbose => 2) if $opts{man};
# Setup a drum score, etc
my $d = MIDI::Drummer::Tiny->new(
file => "$0.mid",
bpm => $opts{bpm},
bars => $opts{drummers} * $opts{measures},
reverb => 15, # We're outside
);
# Collect the percussion instruments you wish to hear
my @drums = (
# $d->mute_hi_conga, $d->cabasa, $d->maracas, $d->hi_bongo, $d->mute_triangle, # 5 QUIET ONES
$d->low_bongo, $d->open_hi_conga, $d->low_conga, $d->short_guiro, $d->claves, $d->hi_wood_block, $d->low_wood_block, # 7
$d->high_agogo, $d->low_agogo, $d->tambourine, $d->cowbell, $d->open_triangle, # 5
# $d->vibraslap, $d->high_timbale, $d->low_timbale, $d->mute_cuica, $d->open_cuica, # 5
# $d->hi_tom, $d->hi_mid_tom, $d->low_mid_tom, $d->low_tom, $d->hi_floor_tom, $d->low_floor_tom, # 6 drum kit toms
# $d->kick, $d->snare, $d->open_hh, $d->pedal_hh, # 4 Western backbeat
);
print 'There are ', scalar(@drums), " known percussion instruments.\n";
die "Can't have more drummers ($opts{drummers}) than instruments!\n"
if $opts{drummers} > @drums;
# Split the given space-separated string of durations into a list
my $pool = [ split /\s+/, $opts{pool} ];
# Make a phrase generator
my $generator = Music::Duration::Partition->new(
size => $opts{beats},
pool => $pool,
);
my %seen; # Drums that have been selected
# Get the amount of time to rest = beats * 96
my $rest = 'd' . ($opts{beats} * ticks($d->score));
# Common phrase() arguments
my %common = (
options => \%opts,
drummer => $d,
generator => $generator,
drums => \@drums,
seen => \%seen,
rest => $rest,
width => length($opts{drummers}),
);
# Build the code-ref MIDI phrases played by each drummer
my @phrases;
push @phrases, phrase(%common, phrase => $_)
for 1 .. $opts{drummers};
$d->score->synch(@phrases); # Play the phrases simultaneously
$d->write; # Write the score to a MIDI file
sub phrase {
my (%args) = @_;
# Get an unseen drum to play
my $drum = $args{drums}->[ int rand $args{drums}->@* ];
while ($args{seen}->{$drum}++) {
$drum = $args{drums}->[ int rand $args{drums}->@* ];
}
my $drum_name = midi_dump('notenum2percussion')->{$drum};
my $motif = $args{generator}->motif; # Create a rhythmic phrase
# Tell them what they've won!
printf "%*d. %-19s: %s", $args{width}, $args{phrase}, $drum_name, ddc($motif);
# Either rest or play the motif
my $phrase = sub {
for my $n (1 .. $args{drummer}->bars + $args{options}->{extend}) {
# If we are not up yet, then rest
if ($n < ($args{phrase} * $args{options}->{measures})) {
$args{drummer}->rest($args{rest});
next;
}
# Otherwise play a rhythmic phrase!
for my $duration (@$motif) {
# Get a fluctuating velocity between f and fff
my $velocity = 'v' . (96 + int(rand 32));
$args{drummer}->note($duration, $drum, $velocity);
}
}
};
return $phrase;
}
__END__
=head1 NAME
drum-circle
=head1 SYNOPSIS
$ perl drum-circle --help # -? or --man
$ perl drum-circle # use defaults
$ perl drum-circle --drummers=11 --bpm=120 --extend=2 --measures=3
$ perl drum-circle --beats=5 --pool='thn tqn ten tsn' # 5/4 triplets? YMMV
# Then for instance:
$ timidity -c ~/timidity.cfg drum-circle.mid # On *nix
# Windows plays MIDI with the "Legacy Media Player" YMMV...
=head1 DESCRIPTION
This program simulates a "drum circle", which may include friendly
hippies.
=head1 THE CODE
=head2 Setup
1. Set the number of B<drummers> to play. This is C<4> by default and
up to C<30> if all percussion instruments are uncommented in the code.
2. Set the number of B<extend> beats to play after all drummers have
joined the circle. This is C<4> by default.
3. Declare a L<MIDI::Drummer::Tiny> instance, that will be the beating
heart of the program. This uses the beats per minute (B<bpm> default
C<90>) and the number of B<measures> each drummer plays.
4. Declare the known percussion instruments. (Use the source, Luke.)
5. Instantiate a rhythmic phrase generator with the given number of
B<beats> and a B<pool> of possible durations.
6. Build the phrases to play, one for each drummer.
=head2 Sync
Synchronize the phrase parts, so that they are played simultaneously.
=head2 Write
Finally, write the score to a MIDI file, named after the program
itself.
=head2 Phrase
This is the meat of the program, utilizing all the things we have
setup. The subroutine generates a phrase (a C<CODE> reference), which
is added to the list of phrases that are then played together.
1. Get an unseen instrument to use for a player.
2. Generate a rhythmic phrase (A.K.A. "motif").
3. Create an anonymous subroutine that either rests or plays the
motif for the number of total beats.
3.1. The resting is done depending on the drummer entry order (into
the circle). If we are not up yet, then rest.
3.2. If not resting, play the phrase continuously until the end.
3.2.1. For each hit, get a random velocity between soft (C<f>) and
loud (C<fff>). This gives the sound a bit of dynamic texture.
4. Return this anonymous subroutine to be gathered into a list of all
drummer parts.
=head1 REFERENCES
The writeup: L<https://ology.github.io/2020/11/01/imitating-a-drum-circle/>
The Wikipedia entry: L<https://en.wikipedia.org/wiki/Drum_circle>
=head1 SEE ALSO
A real-time implementation: F<drum-circle.pl> in the
L<MIDI::RtMidi::ScorePlayer|https://metacpan.org/dist/MIDI-RtMidi-ScorePlayer> distribution.
=head1 AUTHOR
Gene Boggs <gene.boggs@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2020-2024 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut