Tiny/Grooves.pm

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