Tiny/Grooves.pm

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