-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheuclidean-drums.pl
More file actions
184 lines (164 loc) · 5.64 KB
/
Copy patheuclidean-drums.pl
File metadata and controls
184 lines (164 loc) · 5.64 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
#!/usr/bin/env perl
# Play an external MIDI device, like a drum machine or sequencer.
# The arguments are verbose-or-not, midi port to play, beats per minute
# Examples: perl euclidean-drums.pl 1 usb
# perl euclidean-drums.pl 0 keys 60
use v5.36;
use IO::Async::Loop ();
use IO::Async::Timer::Periodic ();
use Math::Prime::XS qw(primes);
use MIDI::RtMidi::FFI::Device ();
use MIDI::Util qw(dura_size);
use Music::CreatingRhythms ();
use Music::Duration::Partition ();
use Time::HiRes qw(sleep);
my $verbose = shift // 0;
my $name = shift || 'usb'; # MIDI sequencer device
my $bpm = shift || 120;
my $drums = {
kick => { num => 36, chan => 0 },
snare => { num => 38, chan => 1 },
hihat => { num => 42, chan => 2 },
crash => { num => 49, chan => 3 },
};
my $notes = [qw(60 64 67)];
my $divisions = 4; # handy universal divisor
my $clocks_per_beat = 24; # clock ticks per beat
my $per_sec = 60 / $bpm; # how long is a beat?
my $clock_interval = $per_sec / $clocks_per_beat; # seconds / bpm / ppqn
my $beats = 16; # beats in a phrase
my $beat_interval = $per_sec / $divisions; # 16th-note resolution
my %primes = ( # for syncopated drum patterns
all => [primes($beats)],
to_5 => [primes(5)],
to_7 => [primes(7)],
);
my $ticks = 0; # clock ticks
my $beat_count = 0; # ...
my $toggle = 0; # part A or B?
my $filled = 0; # did we just fill?
my $hats = 0; # toggle 1st hihat beat
my $midi_out = RtMidiOut->new;
$midi_out->open_virtual_port('RtMidiOut');
$midi_out->open_port_by_name(qr/\Q$name/i);
say "Sending MIDI to $name" if $verbose;
$SIG{INT} = sub {
say "\nStop" if $verbose;
exit;
};
my $increment = 0;
my $mcr = Music::CreatingRhythms->new;
my $loop = IO::Async::Loop->new;
my $timer = IO::Async::Timer::Periodic->new(
interval => $clock_interval,
on_tick => sub {
# $midi_out->clock;
$ticks++;
if ($ticks % $clocks_per_beat == 0) {
my $size = rand() < 0.4 ? 2 : 4;
if ($beat_count % ($divisions - 1) == 0) {
adjust_drums($drums, \%primes, \$toggle);
if ($beat_count > 0) {
if ($size == 2) {
part($midi_out, $drums, $beats, $size);
}
say "Fill size $size" if $verbose;
fill($midi_out, $size);
$filled = 1;
}
}
adjust_cymbal($drums, \$filled);
$increment++;
say "Part $increment" if $verbose;
say join ',', map { $_ . '=' . join '', $drums->{$_}{pat}->@* } sort keys %$drums
if $verbose;
part($midi_out, $drums, $beats, 4);
$beat_count++;
}
},
);
$timer->start;
$loop->add($timer);
$loop->run;
sub part($midi_out, $drums, $beats, $size) {
my $end = $size == 2 ? $beats / 2 : $beats;
for my $i (0 .. $end - 1) {
my %simul = map { $_ => $drums->{$_}{pat}[$i] } keys %$drums;
play_simul($midi_out, $beat_interval, $drums, \%simul);
}
}
sub fill($midi_out, $size) {
my $mdp = Music::Duration::Partition->new(
size => $size,
pool => [qw(qn en sn)],
weights => [1, 2, 1],
groups => [0, 0, 2],
);
my $motif = $mdp->motif;
for my $duration (@$motif) {
midi_msg($midi_out, 'note_on', $drums->{snare}{chan}, $drums->{snare}{num}, velocity(-10, 10, 64));
sleep(dura_size($duration) * $per_sec * 0.9);
midi_msg($midi_out, 'note_off', $drums->{snare}{chan}, $drums->{snare}{num}, 0);
sleep(dura_size($duration) * $per_sec * 0.1);
}
}
sub play_simul($midi_out, $beat_interval, $drums, $simul) {
my $i = 0;
for my $drum (keys %$simul) {
if ($simul->{$drum} == 1) {
$midi_out->send_event('note_on', $drums->{$drum}{chan}, $drums->{$drum}{num}, 127);
}
else { # rest
$midi_out->send_event('note_on', $drums->{$drum}{chan}, $drums->{$drum}{num}, 0);
}
}
sleep($beat_interval * 0.9);
$i = 0;
for my $drum (keys %$simul) {
$midi_out->send_event('note_off', $drums->{$drum}{chan}, $drums->{$drum}{num}, 0);
}
sleep($beat_interval * 0.1);
}
sub adjust_cymbal($drums, $filled) {
if ($$filled) {
$drums->{crash}{pat}[0] = 1;
$drums->{hihat}{pat}[0] = 0;
}
else {
$drums->{crash}{pat}[0] = 0;
$drums->{hihat}{pat}[0] = $hats; # restore bit
}
$$filled = 0;
}
sub adjust_drums($drums, $primes, $toggle) {
my ($p, $q, $r) = map { $primes->{$_}[ int rand $primes->{$_}->@* ] } sort keys %$primes;
if ($$toggle == 0) { # part A
$drums->{kick}{pat} = $mcr->euclid($q, $beats);
$drums->{snare}{pat} = $mcr->rotate_n($r, $mcr->euclid(2, $beats));
$drums->{hihat}{pat} = $mcr->euclid($p, $beats);
$$toggle = 1;
}
else { # part B
$drums->{kick}{pat} = [1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1];
$drums->{snare}{pat} = [0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0];
$drums->{hihat}{pat} = $mcr->euclid($p, $beats);
$$toggle = 0;
}
$hats = $drums->{hihat}{pat}[0]; # save bit
$drums->{crash}{pat} = [ (0) x $beats ];
$drums->{crash}{num} = random_note($notes);
$drums->{snare}{num} = random_note($notes);
$drums->{kick}{num} = random_note($notes);
$drums->{hihat}{num} = random_note($notes);
}
sub midi_msg($midi_out, $event, $channel, $note, $velocity) {
$midi_out->send_event($event, $channel, $note, $velocity);
}
sub velocity($min, $max, $offset) {
my $random = $offset + int(rand($max - $min + 1)) + $min;
return $random;
}
sub random_note($notes) {
my $random = $notes->[ int rand @$notes ] - 24;
return $random;
}