/
euclid
180 lines (141 loc) · 4.88 KB
/
euclid
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
#!perl6
use v6;
=begin pod
=head1 NAME
euclid - generate drum pattern using the Euclidean algorithm
=head1 SYNOPSIS
examples/euclid --list-drumkits
examples/euclid --kit=<kit name> --list-instruments
examples/euclid --kit=<kit name> [--bars=<1>] [--file=<Str>] [--part=<$instruments,$slots,$fills> [--part=<...>]...] ...
=head1 DESCRIPTION
This programme allows you to generate Hydrogen patterns using the rhythmic device
described in http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf (which is
incidentally a good read even if you aren't particulary interested in making
drum patterns.)
The programme generates a Hydrogen song containing a single pattern for one
or more instruments of the specified number of bars (the default is 1,) using
the drumkit specified by name. Each part is specified as a triplet of the
instrument id number (which can be determined with the C<list-instruments>
option,) the total number of C<slots> and the number of C<fills> (the explanation
of these is in the paper linked above,) So for instance something like a Samba
on the cowbell (instrument 15,) of a TR808909 kit I have, one might do:
perl6 -Ilib examples/euclid --kit=TR808909 --part=15,16,7
You can specify as many parts as your kit has instruments (which is typically
a maximum of 32.)
If a file isn't specified then the song is output to STDOUT.
=end pod
# From Björklund via http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf
class Euclid
{
method build_string(Int $level, @count, @remainder )
{
my @bitmap;
if ($level == -1)
{
@bitmap.append: 0;
}
elsif ( $level == -2 )
{
@bitmap.append: 1;
}
else
{
for 0 .. @count[$level] - 1
{
@bitmap.append: self.build_string($level-1, @count, @remainder);
}
if (@remainder[$level] != 0)
{
@bitmap.append: self.build_string($level-2, @count, @remainder);
}
}
flat @bitmap;
}
method compute_bitmap(Int $num_slots, Int $num_pulses)
{
my @count;
my @remainder;
my $divisor = $num_slots - $num_pulses;
@remainder[0] = $num_pulses;
my $level = 0;
while (@remainder[$level] > 1 )
{
@count[$level] = $divisor / @remainder[$level];
@remainder[$level+1] = $divisor % @remainder[$level];
$divisor = @remainder[$level];
$level++;
}
@count[$level] = $divisor;
my @bits = self.build_string($level, @count, @remainder);
while (@bits[0] == 0)
{
@bits = @bits.rotate(1);
}
flat @bits;
}
}
use Audio::Hydrogen;
use Audio::Hydrogen::Song;
multi MAIN(Bool :$list-drumkits!) {
for Audio::Hydrogen.new.drumkits -> $dk {
say $dk.name;
}
}
multi MAIN(Str :$kit!, Bool :$list-instruments!) {
my $hydrogen = Audio::Hydrogen.new;
if $hydrogen.drumkits.grep({ $_.name eq $kit }).first -> $di {
for $di.drumkit.instruments -> $instrument {
if $instrument.name {
say sprintf "%3i : %s", $instrument.id, $instrument.name ;
}
}
}
else {
say "Can't find drumkit $kit";
exit;
}
}
multi MAIN(Str :$kit!, Int:D :$bars = 1, Str :$file, :$part ) {
my $dk;
my $hydrogen = Audio::Hydrogen.new;
if $hydrogen.drumkits.grep({ $_.name eq $kit }).first -> $di {
my $drumkit = $di.drumkit();
my $pattern = Audio::Hydrogen::Pattern.new(name => "Pattern 1", size => ($bars * 192));
my @notes;
for $part.list -> $part {
my ($instrument, $slots, $fills) = $part.split(',',:skip-empty).map({$_.Int});
my $slot-pos = (192 * $bars) / $slots;
if $instrument.defined && $slots.defined && $fills.defined {
my @fills = Euclid.new.compute_bitmap($slots, $fills);
my $pos = 0;
for @fills -> $fill {
if $fill {
my $position = Int($pos * $slot-pos);
my $note = Audio::Hydrogen::Pattern::Note.new(:$position, :$instrument);
@notes.append($note);
}
$pos++;
}
}
else {
say "part should be '<instrument id>,<slots>,<fills>";
exit;
}
}
$pattern.note-list.append: @notes.sort({ $_.position });
my $song = Audio::Hydrogen::Song.new(instruments => $drumkit.instruments, version => Version.new("0.9.5"));
$song.patterns.append($pattern);
my $out-xml = $song.to-xml;
if $file.defined {
$file.IO.spurt($out-xml);
}
else {
say $out-xml;
}
}
else {
say "Can't find drumkit $kit";
exit;
}
}
# vim: expandtab shiftwidth=4 ft=perl6