Tiny/Grooves.pm

filter: Statementstmt 85.0% 97 / 114 Branchbran 72.2% 26 / 36 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 90.9% 30 / 33 Podpod 100% 6 / 6 totaltotal 81.1% 164 / 202 CC 26 SCAR 13.8
CC
26
Cov
80%
search
9.1
_build__grooves
8.0
_build_share_file
6.0
CRAP 4.0
« Tiny.pm ↑ Summary Tiny/SwingFills.pm »
1 package MIDI::Drummer::Tiny::Grooves;
2 2 $MIDI::Drummer::Tiny::Grooves::VERSION = '0.7020';
3 2 our $AUTHORITY = 'cpan:GENE';
4  
5 # ABSTRACT: Common drum grooves
6  
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Data::Dumper::Compact qw(ddc); # debugging
10 2 ▶use File::ShareDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MIDI::Drummer::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #pod =head1 SYNOPSIS
16 #pod
17 #pod use MIDI::Drummer::Tiny ();
18 #pod use MIDI::Drummer::Tiny::Grooves ();
19 #pod
20 #pod my $drummer = MIDI::Drummer::Tiny->new(
21 #pod file => "grooves.mid",
22 #pod kick => 36,
23 #pod );
24 #pod
25 #pod my $grooves = MIDI::Drummer::Tiny::Grooves->new(
26 #pod drummer => $drummer
27 #pod );
28 #pod
29 #pod # get a random groove from all known grooves
30 #pod my $groove = $grooves->get_groove;
31 #pod
32 #pod my $set = $grooves->all_grooves;
33 #pod
34 #pod # get a random groove from a set
35 #pod $groove = $grooves->get_groove(0, $set);
36 #pod # get a numbered groove
37 #pod $groove = $grooves->get_groove(42);
38 #pod print "42. $groove->{cat}\n$groove->{name}";
39 #pod
40 #pod my $density = $grooves->density($groove);
41 #pod
42 #pod # searching
43 #pod $set = $grooves->search({ cat => 'house' });
44 #pod $set = $grooves->search({ name => 'deep' }, $set); # refine search
45 #pod $set = $grooves->search({ cat => 'house', name => 'deep' }); # same
46 #pod
47 #pod # funk and soul patterns have WAY too much crashing imho:
48 #pod $set = $grooves->search({ cat => 'funk' });
49 #pod $groove = $grooves->get_groove(0, $set); # random funk groove
50 #pod $groove = $grooves->swap_pat($groove, 'crash', 'closed');
51 #pod
52 #pod # add grooves to the score
53 #pod for my $i (sort keys %$set) {
54 #pod $groove = $set->{$i};
55 #pod print "$i. $groove->{cat}\n$groove->{name}]\n";
56 #pod $grooves->groove($groove->{groove}); # a bit redundant - ugh!
57 #pod }
58 #pod
59 #pod $drummer->write;
60 #pod # then:
61 #pod # > timidity grooves.mid
62 #pod
63 #pod =head1 DESCRIPTION
64 #pod
65 #pod Return the common grooves, as listed in the "Pocket Operations", that
66 #pod are L<linked below|/SEE ALSO>. There are a total of 269 known drum
67 #pod patterns.
68 #pod
69 #pod A groove is a numbered and named hash reference, with the following
70 #pod structure:
71 #pod
72 #pod 1 => {
73 #pod cat => "Basic Patterns",
74 #pod name => "ONE AND SEVEN & FIVE AND THIRTEEN",
75 #pod groove => {
76 #pod kick => { num => $self->kick, pat => ['1000001000000000'] },
77 #pod snare => { num => $self->snare, pat => ['0000100000001000'] },
78 #pod ...
79 #pod },
80 #pod },
81 #pod
82 #pod The known groove categories are:
83 #pod
84 #pod Afro-Cuban,
85 #pod Basic Patterns,
86 #pod Breaks,
87 #pod Breaks - Kick,
88 #pod Breaks - Snare,
89 #pod Drum Rolls,
90 #pod Drum and Bass,
91 #pod Dub,
92 #pod EDM,
93 #pod Electro,
94 #pod Funk and Soul,
95 #pod Ghost Snares,
96 #pod Hip-Hop,
97 #pod House,
98 #pod Hybrid Breaks With Alternate Endings,
99 #pod Irregular Breaks,
100 #pod Miami Bass,
101 #pod Pop,
102 #pod Reggaeton,
103 #pod Rock,
104 #pod Rolling Breaks,
105 #pod Standard Breaks
106 #pod
107 #pod =cut
108  
109 #pod =head1 ACCESSORS
110 #pod
111 #pod =head2 drummer
112 #pod
113 #pod $drummer = $grooves->drummer;
114 #pod $grooves->drummer($drummer);
115 #pod
116 #pod The L<MIDI::Drummer::Tiny> object. If not given in the constructor, a
117 #pod new one is created when a method is called.
118 #pod
119 #pod =cut
120  
121 has drummer => (
122 is => 'rw',
123 4 ▶ isa => sub { die "Invalid drummer object" unless ref($_[0]) eq 'MIDI::Drummer::Tiny' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "MIDI::Drummer::Tiny" 0 4 4
124 4 ▶ default => sub { MIDI::Drummer::Tiny->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 2 );
126  
127 #pod =head2 duration
128 #pod
129 #pod $duration = $grooves->duration;
130 #pod $grooves->duration($duration);
131 #pod
132 #pod The "resolution" duration that is given to the
133 #pod L<MIDI::Drummer::Tiny/sync_patterns> method.
134 #pod
135 #pod This is initialized to the sixteenth duration of the drummer
136 #pod L<MIDI::Drummer::Tiny> object.
137 #pod
138 #pod =cut
139  
140 2 has duration => (
141 is => 'lazy',
142 );
143 0 ▶sub _build_duration { shift->drummer->sixteenth }
Subroutine _build_duration: not called CC=1 SCAR=6.9
144  
145 #pod =head2 kick, rimshot, snare, clap, conga, cowbell, shaker, closed, open, crash, hi_tom, mid_tom, low_tom
146 #pod
147 #pod $kick = $grooves->kick;
148 #pod $grooves->kick(36);
149 #pod
150 #pod The drum patches that are used by the grooves.
151 #pod
152 #pod Each is initialized to a corresponding patch of the drummer
153 #pod L<MIDI::Drummer::Tiny> object that is given to, or created by the
154 #pod constructor.
155 #pod
156 #pod =cut
157  
158 #pod =head2 return_patterns
159 #pod
160 #pod $return_patterns = $grooves->return_patterns;
161 #pod
162 #pod Either return the raw patterns of 16 beats or C<synch>'ed B<drummer>
163 #pod object phrases from the B<groove()> method.
164 #pod
165 #pod Default: C<0>
166 #pod
167 #pod =cut
168  
169 has return_patterns => (
170 is => 'rw',
171 4 ▶ isa => sub { die "Not a Boolean" unless $_[0] =~ /^[01]$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[01]$/ 0 4 4
172 3 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
173 2 );
174  
175 #pod =head2 share_file
176 #pod
177 #pod $share_file = $grooves->share_file;
178 #pod
179 #pod The path to the F<drum-pattern-bit-strings.txt> file.
180 #pod
181 #pod Default: The installed distribution directory/file.
182 #pod
183 #pod =cut
184  
185 has share_file => (
186 is => 'rw',
187 4 ▶ isa => sub { die "Invalid share_file" unless -e $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_[0] 0 4 4
188 2 builder => '_build_share_file',
189 );
190  
191 sub _build_share_file {
192 0 ▶ my ($self) = @_;
Subroutine _build_share_file: not called CC=2 SCAR=17.9
193 0 my $file = '/drum-pattern-bit-strings.txt';
194 0 my $path = dist_dir('MIDI-Drummer-Tiny') . $file;
195 0 ▶ $path = 'share' . $file unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
196 0 return $path;
197 }
198  
199 2 has _grooves => (
200 is => 'lazy',
201 builder => '_build__grooves',
202 );
203 sub _build__grooves {
204 4 ▶ my ($self) = @_;
Subroutine _build__grooves: called CC=8 SCAR=20.8
205 4 my %mapping = (
206 BD => 'kick',
207 SN => 'snare',
208 RS => 'rimshot',
209 CH => 'closed',
210 OH => 'open',
211 CY => 'crash',
212 CB => 'cowbell',
213 CL => 'clap',
214 SH => 'shaker',
215 HT => 'hi_tom',
216 MT => 'mid_tom',
217 LT => 'low_tom',
218 HC => 'conga',
219 );
220 4 my $path = $self->share_file;
221 4 my @contents = path($path)->lines;
222 4 my (%grooves, $cat, $name, %patterns);
223 4 my $i = 0;
224 4 for my $line (@contents) {
225 5844 chomp $line;
226 5844 ▶ chop $line if $line =~ /\r$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\r$/ 5840 4 5844
227 5844 ▶ if ($line =~ /^Instrument/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^Instrument/) { } 1076 4768 5844
elsif ($line =~ /^([A-Z][A-Z]),([01]+)$/) { } 3532 1236 4768
elsif ($line =~ /^\* (.+)$/) { } 88 1148 1236
228 1076 next;
229 }
230 elsif ($line =~ /^([A-Z][A-Z]),([01]+)$/) {
231 3532 ▶ my $mapping = $mapping{$1} || next;
Condition100%2/2
$mapping{$1} || (next)
l!l
3252280
MC/DC100%1/1
$mapping{$1} || (next)
$mapping{$1}
Truth table100%2/2
A
A= $mapping{$1}
Aresult
11
00
232 3252 my $val = { num => $self->$mapping, pat => [$2] };
233 3252 $patterns{$mapping} = $val;
234 }
235 elsif ($line =~ /^\* (.+)$/) {
236 88 $cat = $1;
237 }
238 else {
239 1148 ▶ if (keys %patterns) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %patterns) 1072 76 1148
240 # print ddc \%patterns;
241 1072 $grooves{++$i} = {
242 cat => $cat,
243 name => $name,
244 groove => { %patterns },
245 };
246 }
247 1148 %patterns = ();
248 1148 $name = $line;
249 }
250 }
251 # print ddc \%grooves;
252 4 return \%grooves;
253 }
254  
255 2 for my $patch (qw(
256 kick
257 rimshot
258 snare
259 clap
260 conga
261 cowbell
262 shaker
263 closed
264 open
265 crash
266 hi_tom
267 mid_tom
268 low_tom
269 )) {
270 26 has $patch => (
271 is => 'lazy',
272 builder => '_build_' . $patch,
273 );
274 }
275 4 ▶sub _build_kick { shift->drummer->kick }
Subroutine _build_kick: called CC=1 SCAR=0.0
276 4 ▶sub _build_rimshot { shift->drummer->side_stick }
Subroutine _build_rimshot: called CC=1 SCAR=0.0
277 4 ▶sub _build_snare { shift->drummer->snare }
Subroutine _build_snare: called CC=1 SCAR=0.0
278 4 ▶sub _build_clap { shift->drummer->clap }
Subroutine _build_clap: called CC=1 SCAR=0.0
279 4 ▶sub _build_conga { shift->drummer->open_hi_conga }
Subroutine _build_conga: called CC=1 SCAR=0.0
280 4 ▶sub _build_cowbell { shift->drummer->cowbell }
Subroutine _build_cowbell: called CC=1 SCAR=0.0
281 4 ▶sub _build_shaker { shift->drummer->maracas }
Subroutine _build_shaker: called CC=1 SCAR=0.0
282 4 ▶sub _build_closed { shift->drummer->closed_hh }
Subroutine _build_closed: called CC=1 SCAR=0.0
283 4 ▶sub _build_open { shift->drummer->open_hh }
Subroutine _build_open: called CC=1 SCAR=0.0
284 4 ▶sub _build_crash { shift->drummer->crash1 }
Subroutine _build_crash: called CC=1 SCAR=0.0
285 4 ▶sub _build_hi_tom { shift->drummer->hi_mid_tom }
Subroutine _build_hi_tom: called CC=1 SCAR=0.0
286 4 ▶sub _build_mid_tom { shift->drummer->low_mid_tom }
Subroutine _build_mid_tom: called CC=1 SCAR=0.0
287 4 ▶sub _build_low_tom { shift->drummer->low_tom }
Subroutine _build_low_tom: called CC=1 SCAR=0.0
288  
289 #pod =head1 METHODS
290 #pod
291 #pod =head2 new
292 #pod
293 #pod $grooves = MIDI::Drummer::Tiny::Grooves->new;
294 #pod $grooves = MIDI::Drummer::Tiny::Grooves->new(%arguments);
295 #pod
296 #pod Return a new C<MIDI::Drummer::Tiny::Grooves> object.
297 #pod
298 #pod =head2 get_groove
299 #pod
300 #pod $groove = $grooves->get_groove($groove_number);
301 #pod $groove = $grooves->get_groove; # random groove
302 #pod $groove = $grooves->get_groove(0, $set); # random groove of set
303 #pod $groove = $grooves->get_groove($groove_number, $set); # numbered groove of set
304 #pod
305 #pod Return a numbered or random groove from either the given B<set> or
306 #pod all known grooves.
307 #pod
308 #pod =cut
309  
310 sub get_groove {
311 1 ▶ my ($self, $groove_number, $set) = @_;
Subroutine get_groove: called CC=3 SCAR=12.3
Pod documented
312 1 ▶ unless (keys %$set) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$set) 1 0 1
313 1 $set = $self->all_grooves;
314 }
315 1 ▶ unless ($groove_number) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($groove_number) 0 1 1
316 0 my @keys = keys %$set;
317 0 $groove_number = $keys[ int rand @keys ];
318 }
319 1 return $set->{$groove_number};
320 }
321  
322 #pod =head2 all_grooves
323 #pod
324 #pod $all = $grooves->all_grooves;
325 #pod
326 #pod Return all the known grooves as a hash reference.
327 #pod
328 #pod =cut
329  
330 sub all_grooves {
331 5 ▶ my ($self) = @_;
Subroutine all_grooves: called CC=1 SCAR=0.0
Pod documented
332 5 return $self->_grooves();
333 }
334  
335 #pod =head2 search
336 #pod
337 #pod $set = $grooves->search({ cat => $x, name => $y }); # search all grooves
338 #pod $set = $grooves->search({ cat => $x, name => $y }, $set); # search a subset
339 #pod
340 #pod Return the found grooves with names matching the B<cat> or B<name>
341 #pod strings and given an optional set of grooves to search in.
342 #pod
343 #pod =cut
344  
345 sub search {
346 3 ▶ my ($self, $args, $set) = @_;
Subroutine search: called CC=9 SCAR=22.1
Pod documented
347 3 ▶ unless ($set && keys %$set) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($set and keys %$set) 3 0 3
Condition33%1/3
$set and keys %$set
!ll&&!rl&&r
300
MC/DC0%0/2
$set and keys %$set
$set keys %$set
Truth table33%1/3
A and B
A= $setB= keys %$set
ABresult
0X0
100
111
348 3 $set = $self->all_grooves;
349 }
350 3 my $found = {};
351 3 ▶ if ($args->{cat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"cat"}) 2 1 3
352 2 my $string = lc $args->{cat};
353 2 for my $k (keys %$set) {
354 536 ▶ if (lc($set->{$k}{cat}) =~ /$string/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($set->{$k}{"cat"}) =~ /$string/) 13 523 536
355 13 $found->{$k} = $set->{$k};
356 }
357 }
358 }
359 3 ▶ if ($args->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"name"}) 1 2 3
360 1 my $string = lc $args->{name};
361 1 for my $k (keys %$set) {
362 268 ▶ if (lc($set->{$k}{name}) =~ /$string/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($set->{$k}{"name"}) =~ /$string/) 3 265 268
363 3 $found->{$k} = $set->{$k};
364 }
365 }
366 }
367 3 return $found;
368 }
369  
370 #pod =head2 groove
371 #pod
372 #pod $grooves->groove(\%patterns);
373 #pod
374 #pod Add the patterns to the score. If the B<return_patterns> attribute is
375 #pod on, the patterns are just returned.
376 #pod
377 #pod =cut
378  
379 sub groove {
380 1 ▶ my ($self, $patterns) = @_;
Subroutine groove: called CC=2 SCAR=7.9
Pod documented
381 1 ▶ if ($self->return_patterns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_patterns) { } 1 0 1
382 1 return map { $_ => [ split '', $patterns->{$_}{pat}[0] ] } keys %$patterns;
383 }
384 else {
385 $self->drummer->sync_patterns(
386 0 (map { $patterns->{$_}{num} => $patterns->{$_}{pat} } keys %$patterns),
387 duration => $self->duration,
388 );
389 }
390 }
391  
392 #pod =head2 swap_pat
393 #pod
394 #pod $pat = $grooves->swap_pat($pattern, 'crash', 'closed');
395 #pod
396 #pod =cut
397  
398 sub swap_pat {
399 1 ▶ my ($self, $pat, $source, $dest) = @_;
Subroutine swap_pat: called CC=3 SCAR=11.8
Pod documented
400 1 ▶ if (!exists $pat->{$dest} && exists $pat->{$source}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $pat->{$dest} and exists $pat->{$source}) 1 0 1
Condition33%1/3
not exists $pat->{$dest} and exists $pat->{$source}
!ll&&!rl&&r
001
MC/DC0%0/2
not exists $pat->{$dest} and exists $pat->{$source}
not exists $pat->{$dest} exists $pat->{$source}
Truth table33%1/3
not(A) and B
A= not exists $pat->{$dest}B= exists $pat->{$source}
ABresult
0X0
100
111
401 1 my $x = delete $pat->{$source};
402 1 $pat->{$dest} = { num => $self->$dest, pat => $x->{pat} };
403 }
404 1 return $pat;
405 }
406  
407 #pod =head2 density
408 #pod
409 #pod $density = $grooves->density($groove);
410 #pod
411 #pod Return the total number of strikes. This is basically the number of
412 #pod C<1>s in the instrument pattern.
413 #pod
414 #pod =cut
415  
416 sub density {
417 0 ▶ my ($self, $pat) = @_;
Subroutine density: not called CC=2 SCAR=17.9
Pod documented
418 0 my $density = 0;
419 0 for my $instrument (keys $pat->{groove}->%*) {
420 0 print "$instrument\n";
421 0 my $ones =()= $pat->{groove}{$instrument}{pat} =~ /1/g;
422 0 $density += $ones;
423 }
424 0 return $density;
425 }
426  
427 2 1;
428  
429 __END__
« Tiny.pm ↑ Summary Tiny/SwingFills.pm »