Tiny.pm

filter: Statementstmt 51.0% 179 / 351 Branchbran 36.4% 35 / 96 Conditioncond 43.3% 13 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 73.9% 34 / 46 Podpod 95.2% 20 / 21 totaltotal 50.0% 281 / 562 CC 111 SCAR 36.7
CC
111
Cov
48%
count_in
90.0
metronome5
90.0
metronome7
90.0
CRAP 39.2
« Tiny/Syncopate.pm ↑ Summary Tiny/Grooves.pm »
1 package MIDI::Drummer::Tiny;
2 6 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Glorified metronome
5  
6 6 our $VERSION = '0.7102';
7  
8 6 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util 1.26 qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Math::Bezier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use MIDI::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 dura_size
17 reverse_dump
18 set_time_signature
19 ticks
20 6 );
21 6 ▶use Music::Duration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use Music::RhythmSet::Util qw(upsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 ▶use MIDI::Drummer::Tiny::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 ▶use Data::Dumper::Compact qw(ddc);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 6 ▶use constant STRAIGHT => 50; # Swing percent
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #pod =head1 SYNOPSIS
33 #pod
34 #pod use MIDI::Drummer::Tiny;
35 #pod
36 #pod my $d = MIDI::Drummer::Tiny->new(
37 #pod file => 'drums.mid',
38 #pod bpm => 100,
39 #pod volume => 100,
40 #pod signature => '5/4',
41 #pod bars => 8,
42 #pod reverb => 0,
43 #pod soundfont => '/you/soundfonts/TR808.sf2', # option
44 #pod #kick => 36, # Override default patch
45 #pod #snare => 40, # "
46 #pod );
47 #pod
48 #pod $d->metronome5;
49 #pod
50 #pod $d->set_time_sig('4/4');
51 #pod $d->count_in(1); # Closed hi-hat for 1 bar
52 #pod
53 #pod $d->metronome4($d->bars, $d->closed_hh, $d->eighth, 60); # 60% swing!
54 #pod
55 #pod $d->rest($d->whole);
56 #pod
57 #pod $d->flam($d->quarter, $d->snare);
58 #pod $d->crescendo_roll([50, 127, 1], $d->eighth, $d->thirtysecond);
59 #pod $d->note($d->sixteenth, $d->crash1);
60 #pod $d->accent_note(127, $d->sixteenth, $d->crash2);
61 #pod
62 #pod # Alternate kick and snare
63 #pod $d->note($d->quarter, $d->open_hh, $_ % 2 ? $d->kick : $d->snare)
64 #pod for 1 .. $d->beats * $d->bars;
65 #pod
66 #pod # Same but with beat-strings:
67 #pod $d->sync_patterns(
68 #pod $d->open_hh => [ '1111' ],
69 #pod $d->snare => [ '0101' ],
70 #pod $d->kick => [ '1010' ],
71 #pod ) for 1 .. $d->bars;
72 #pod
73 #pod my $patterns = [
74 #pod your_function(5, 16), # e.g. a euclidean function
75 #pod your_function(7, 16), # ...
76 #pod ];
77 #pod $d->pattern( instrument => $d->kick, patterns => $patterns ); # see doc...
78 #pod
79 #pod $d->add_fill('...'); # see doc...
80 #pod
81 #pod print 'Count: ', $d->counter, "\n";
82 #pod
83 #pod # As a convenience, and sometimes necessity:
84 #pod $d->set_bpm(200); # handy for tempo changes
85 #pod $d->set_channel; # reset back to 9 if ever changed
86 #pod
87 #pod $d->write;
88 #pod
89 #pod =head1 DESCRIPTION
90 #pod
91 #pod This module provides handy defaults and tools to produce a MIDI score
92 #pod with drum parts. It is full of tools to construct a score with drum
93 #pod parts. It is not a traditional "drum machine." Rather, it contains
94 #pod methods to construct a drum machine, or play "as a drummer might."
95 #pod
96 #pod Below, the term "spec" refers to a note length duration, like an
97 #pod eighth or quarter note, for instance.
98 #pod
99 #pod =for Pod::Coverage BUILD
100 #pod
101 #pod =cut
102  
103 13 ▶sub BUILD ( $self, $args_ref ) {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
104 13 ▶ return unless $self->setup;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->setup 0 13 13
105  
106 13 $self->score->noop( 'c' . $self->channel, 'V' . $self->volume );
107  
108 13 $self->score->set_tempo( int( 60_000_000 / $self->bpm ) );
109  
110 13 $self->score->control_change( $self->channel, 91, $self->reverb );
111  
112 # Add a TS to the score but don't reset the beats if given
113 13 $self->set_time_sig( $self->signature, !$args_ref->{beats} );
114 13 return;
115 }
116  
117 #pod =attr verbose
118 #pod
119 #pod Default: C<0>
120 #pod
121 #pod =attr file
122 #pod
123 #pod This the MIDI file name to write. It can be a string, a
124 #pod L<Types::Path::Tiny/Path>, or a L<Types::Standard/FileHandle>.
125 #pod
126 #pod Default: C<MIDI-Drummer.mid>
127 #pod
128 #pod =cut
129  
130 6 has file => (
131 is => 'rw',
132 isa => MIDI_File,
133 coerce => 1,
134 default => 'MIDI-Drummer.mid',
135 );
136  
137 #pod =attr soundfont
138 #pod
139 #pod $soundfont = $d->soundfont;
140 #pod
141 #pod This is the location of the soundfont file. It can be a string or a
142 #pod L<Types::Path::Tiny/File>.
143 #pod
144 #pod =cut
145  
146 6 has soundfont => (
147 is => 'rw',
148 isa => Soundfont_File,
149 coerce => 1,
150 );
151  
152 #pod =attr score
153 #pod
154 #pod Default: C<MIDI::Simple-E<gt>new_score>
155 #pod
156 #pod =method sync
157 #pod
158 #pod $d->sync(@code_refs);
159 #pod
160 #pod This is a simple pass-through to the B<score> C<synch> method.
161 #pod
162 #pod This allows simultaneous playing of multiple "tracks" defined by code
163 #pod references.
164 #pod
165 #pod =cut
166  
167 6 has score => (
168 is => 'lazy',
169 isa => InstanceOf ['MIDI::Simple'],
170 handles => { sync => 'synch' },
171 );
172  
173 13 ▶sub _build_score {
Subroutine _build_score: called CC=1 SCAR=0.0
174 13 my ($self) = @_;
175 13 return MIDI::Simple->new_score;
176 }
177  
178 #pod =attr reverb
179 #pod
180 #pod Default: C<15>
181 #pod
182 #pod =attr channel
183 #pod
184 #pod Default: C<9>
185 #pod
186 #pod =method set_channel
187 #pod
188 #pod $d->set_channel;
189 #pod $d->set_channel($channel);
190 #pod
191 #pod Reset the channel to C<9> by default, or the given argument if
192 #pod different.
193 #pod
194 #pod =cut
195  
196 6 has channel => (
197 is => 'rwp',
198 isa => Channel,
199 default => 9,
200 );
201  
202 2 ▶sub set_channel ( $self, $channel = 9 ) {
Subroutine set_channel: called CC=2 SCAR=6.9
Pod documented
203 2 $self->score->noop("c$channel");
204 2 $self->_set_channel($channel);
205 2 return;
206 }
207  
208 #pod =attr volume
209 #pod
210 #pod Default: C<100>
211 #pod
212 #pod =method set_volume
213 #pod
214 #pod $d->set_volume;
215 #pod $d->set_volume($volume);
216 #pod
217 #pod Set the volume (L<MIDI::Drummer::Tiny::Types::MIDI/Velocity>) to the given argument.
218 #pod
219 #pod If not given a B<volume> argument, this method mutes (sets to C<0>).
220 #pod
221 #pod =cut
222  
223 6 has volume => (
224 is => 'rwp',
225 isa => Velocity,
226 default => 100,
227  
228 );
229  
230 2 ▶sub set_volume ( $self, $volume = 0 ) {
Subroutine set_volume: called CC=2 SCAR=6.9
Pod documented
231 2 $self->score->noop("V$volume");
232 2 $self->_set_volume($volume);
233 2 return;
234 }
235  
236 #pod =attr bpm
237 #pod
238 #pod Default: C<120>
239 #pod
240 #pod =method set_bpm
241 #pod
242 #pod $d->set_bpm($bpm);
243 #pod
244 #pod Return or set the beats per minute.
245 #pod
246 #pod =cut
247  
248 6 has bpm => (
249 is => 'rw',
250 isa => PosInt,
251 default => 120,
252 writer => 'set_bpm',
253 trigger => 1,
254 );
255  
256 1 ▶sub _trigger_bpm ( $self, $bpm = 120 ) { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Subroutine _trigger_bpm: called CC=2 SCAR=6.9
257 1 $self->score->set_tempo( int( 60_000_000 / $bpm ) );
258 1 return;
259 }
260  
261 #pod =attr bars
262 #pod
263 #pod Default: C<4>
264 #pod
265 #pod =attr signature
266 #pod
267 #pod Default: C<4/4>
268 #pod
269 #pod B<beats> / B<divisions>
270 #pod
271 #pod =attr beats
272 #pod
273 #pod Computed from the B<signature>, if not given in the constructor.
274 #pod
275 #pod Default: C<4>
276 #pod
277 #pod =attr divisions
278 #pod
279 #pod Computed from the B<signature>.
280 #pod
281 #pod Default: C<4>
282 #pod
283 #pod =attr setup
284 #pod
285 #pod Run the commands in the C<BUILD> method that set-up new midi score
286 #pod events like tempo, time signature, etc.
287 #pod
288 #pod Default: C<1>
289 #pod
290 #pod =attr counter
291 #pod
292 #pod $d->counter( $d->counter + $duration );
293 #pod $count = $d->counter;
294 #pod
295 #pod Beat counter of durations, where a quarter-note is equal to 1. An
296 #pod eighth-note is 0.5, etc.
297 #pod
298 #pod This is automatically accumulated each time a C<rest> or C<note> is
299 #pod added to the score.
300 #pod
301 #pod =cut
302  
303 6 my %attr_defaults = (
304 ro => {
305 verbose => 0,
306 reverb => 15,
307 bars => 4,
308 },
309 rw => {
310 signature => '4/4',
311 beats => 4,
312 divisions => 4,
313 setup => 1,
314 counter => 0,
315 },
316 );
317 6 for my $is ( keys %attr_defaults ) {
318 12 for my $attr ( keys %{ $attr_defaults{$is} } ) {
319 has $attr => (
320 is => $is,
321 48 default => $attr_defaults{$is}{$attr},
322 );
323 }
324 }
325  
326 #pod =kit metronome
327 #pod
328 #pod =over
329 #pod
330 #pod =item click, bell
331 #pod
332 #pod =back
333 #pod
334 #pod =kit hi-hats
335 #pod
336 #pod =over
337 #pod
338 #pod =item open_hh, closed_hh, pedal_hh
339 #pod
340 #pod =back
341 #pod
342 #pod =kit crash and splash cymbals
343 #pod
344 #pod =over
345 #pod
346 #pod =item crash1, crash2, splash, china
347 #pod
348 #pod =back
349 #pod
350 #pod =kit ride cymbals
351 #pod
352 #pod =over
353 #pod
354 #pod =item ride1, ride2, ride_bell
355 #pod
356 #pod =back
357 #pod
358 #pod =kit snare drums and handclaps
359 #pod
360 #pod =over
361 #pod
362 #pod =item snare, acoustic_snare, electric_snare, side_stick, clap
363 #pod
364 #pod Where the B<snare> is by default the same as the B<acoustic_snare> but
365 #pod can be overridden with the B<electric_snare> (C<40>).
366 #pod
367 #pod =back
368 #pod
369 #pod =kit tom-toms
370 #pod
371 #pod =over
372 #pod
373 #pod =item hi_tom, hi_mid_tom, low_mid_tom, low_tom, hi_floor_tom, low_floor_tom
374 #pod
375 #pod =back
376 #pod
377 #pod =kit bass drums
378 #pod
379 #pod =over
380 #pod
381 #pod =item kick, acoustic_bass, electric_bass
382 #pod
383 #pod Where the B<kick> is by default the same as the B<acoustic_bass> but
384 #pod can be overridden with the B<electric_bass> (C<36>).
385 #pod
386 #pod =back
387 #pod
388 #pod =kit auxiliary percussion
389 #pod
390 #pod =over
391 #pod
392 #pod =item tambourine, cowbell, vibraslap
393 #pod
394 #pod =back
395 #pod
396 #pod =kit Latin and African drums
397 #pod
398 #pod =over
399 #pod
400 #pod =item hi_bongo, low_bongo, mute_hi_conga, open_hi_conga, low_conga, high_timbale, low_timbale
401 #pod
402 #pod =back
403 #pod
404 #pod =kit Latin and African auxiliary percussion
405 #pod
406 #pod =over
407 #pod
408 #pod =item high_agogo, low_agogo, cabasa, maracas, short_whistle, long_whistle, short_guiro, long_guiro, claves, hi_wood_block, low_wood_block, mute_cuica, open_cuica
409 #pod
410 #pod =back
411 #pod
412 #pod =kit triangles
413 #pod
414 #pod =over
415 #pod
416 #pod =item mute_triangle, open_triangle
417 #pod
418 #pod =back
419 #pod
420 #pod =cut
421  
422 6 ▶use constant PERCUSSION_START => 33;
Subroutine BEGIN: called CC=1 SCAR=0.0
423 6 for my $sound ( qw(
424 click
425 bell
426 acoustic_bass
427 electric_bass
428 side_stick
429 acoustic_snare
430 clap
431 electric_snare
432 low_floor_tom
433 closed_hh
434 hi_floor_tom
435 pedal_hh
436 low_tom
437 open_hh
438 low_mid_tom
439 hi_mid_tom
440 crash1
441 hi_tom
442 ride1
443 china
444 ride_bell
445 tambourine
446 splash
447 cowbell
448 crash2
449 vibraslap
450 ride2
451 hi_bongo
452 low_bongo
453 mute_hi_conga
454 open_hi_conga
455 low_conga
456 high_timbale
457 low_timbale
458 high_agogo
459 low_agogo
460 cabasa
461 maracas
462 short_whistle
463 long_whistle
464 short_guiro
465 long_guiro
466 claves
467 hi_wood_block
468 low_wood_block
469 mute_cuica
470 open_cuica
471 mute_triangle
472 open_triangle
473 ) )
474 {
475 294 state $percussion_note;
476 294 has $sound => (
477 is => 'ro',
478 isa => PercussionNote,
479 default => PERCUSSION_START + $percussion_note++,
480 );
481 }
482 6 has kick => (
483 is => 'ro',
484 isa => PercussionNote,
485 default => 35, # Alt: 36
486 );
487 6 has snare => (
488 is => 'ro',
489 isa => PercussionNote,
490 default => 38, # Alt: 40
491 );
492  
493 #pod =duration whole notes
494 #pod
495 #pod =over
496 #pod
497 #pod =item whole, triplet_whole, dotted_whole, double_dotted_whole
498 #pod
499 #pod =back
500 #pod
501 #pod =duration half notes
502 #pod
503 #pod =over
504 #pod
505 #pod =item half, triplet_half, dotted_half, double_dotted_half
506 #pod
507 #pod =back
508 #pod
509 #pod =duration quarter notes
510 #pod
511 #pod =over
512 #pod
513 #pod =item quarter, triplet_quarter, dotted_quarter, double_dotted_quarter
514 #pod
515 #pod =back
516 #pod
517 #pod =duration eighth notes
518 #pod
519 #pod =over
520 #pod
521 #pod =item eighth, triplet_eighth, dotted_eighth, double_dotted_eighth
522 #pod
523 #pod =back
524 #pod
525 #pod =duration sixteenth notes
526 #pod
527 #pod =over
528 #pod
529 #pod =item sixteenth, triplet_sixteenth, dotted_sixteenth, double_dotted_sixteenth
530 #pod
531 #pod =back
532 #pod
533 #pod =duration thirty-secondth notes
534 #pod
535 #pod =over
536 #pod
537 #pod =item thirtysecond, triplet_thirtysecond, dotted_thirtysecond, double_dotted_thirtysecond
538 #pod
539 #pod =back
540 #pod
541 #pod =duration sixty-fourth notes
542 #pod
543 #pod =over
544 #pod
545 #pod =item sixtyfourth, triplet_sixtyfourth, dotted_sixtyfourth, double_dotted_sixtyfourth
546 #pod
547 #pod =back
548 #pod
549 #pod =duration one-twenty-eighth notes
550 #pod
551 #pod =over
552 #pod
553 #pod =item onetwentyeighth, triplet_onetwentyeighth, dotted_onetwentyeighth, double_dotted_onetwentyeighth
554 #pod
555 #pod =back
556 #pod
557 #pod =cut
558  
559 6 my %basic_note_durations = (
560 whole => 'w',
561 half => 'h',
562 quarter => 'q',
563 eighth => 'e',
564 sixteenth => 's',
565 thirtysecond => 'x',
566 sixtyfourth => 'y',
567 onetwentyeighth => 'z',
568 );
569  
570 6 my %duration_prefixes = (
571 triplet => 't',
572 dotted => 'd',
573 double_dotted => 'dd',
574 );
575  
576 6 for my $basic_duration ( keys %basic_note_durations ) {
577 48 my $duration = $basic_note_durations{$basic_duration};
578  
579 48 has $basic_duration => (
580 is => 'ro',
581 isa => Duration,
582 default => "${duration}n",
583 );
584  
585 48 for my $prefix ( keys %duration_prefixes ) {
586 144 has "${prefix}_${basic_duration}" => (
587 is => 'ro',
588 isa => Duration,
589 default => "$duration_prefixes{$prefix}${duration}n",
590 );
591 }
592 }
593  
594 #pod =method new
595 #pod
596 #pod $d = MIDI::Drummer::Tiny->new(%arguments);
597 #pod
598 #pod Return a new C<MIDI::Drummer::Tiny> object and add a time signature
599 #pod event to the score.
600 #pod
601 #pod =method note
602 #pod
603 #pod $d->note( $d->quarter, $d->closed_hh, $d->kick );
604 #pod $d->note( 'qn', 42, 35 ); # Same thing
605 #pod
606 #pod Add notes to the score.
607 #pod
608 #pod This method takes the same arguments as L<MIDI::Simple/"Parameters for n/r/noop">.
609 #pod
610 #pod It also keeps track of the beat count with the C<counter> attribute.
611 #pod
612 #pod =cut
613  
614 122 ▶sub note ( $self, @spec ) {
Subroutine note: called CC=2 SCAR=7.1
Pod documented
615 122 ▶ my $size
Branch: 1 branch
BranchTrueFalseTotal
$spec[0] =~ /^d(\d+)$/u ? : 0 122 122
616 = $spec[0] =~ /^d(\d+)$/
617 ? $1 / ticks( $self->score )
618 : dura_size( $spec[0] );
619  
620 # carp __PACKAGE__,' L',__LINE__,' ',,"$spec[0]\n";
621 # carp __PACKAGE__,' L',__LINE__,' ',,"$size\n";
622 122 $self->counter( $self->counter + $size );
623 122 return $self->score->n(@spec);
624 }
625  
626 #pod =method accent_note
627 #pod
628 #pod $d->accent_note($accent_value, $d->sixteenth, $d->snare);
629 #pod
630 #pod Play an accented note.
631 #pod
632 #pod For instance, this can be a "ghosted note", where the B<accent> is a
633 #pod smaller number (< 50). Or a note that is greater than the normal
634 #pod score volume.
635 #pod
636 #pod =cut
637  
638 0 ▶sub accent_note ( $self, $accent, @spec ) {
Subroutine accent_note: not called CC=1 SCAR=6.9
Pod documented
639 0 my $resume = $self->score->Volume;
640 0 $self->score->Volume($accent);
641 0 $self->note(@spec);
642 0 return $self->score->Volume($resume);
643 }
644  
645 #pod =method rest
646 #pod
647 #pod $d->rest( $d->quarter );
648 #pod
649 #pod Add a rest to the score.
650 #pod
651 #pod This method takes the same arguments as L<MIDI::Simple/"Parameters for n/r/noop">.
652 #pod
653 #pod It also keeps track of the beat count with the C<counter> attribute.
654 #pod
655 #pod =cut
656  
657 556 ▶sub rest ( $self, @spec ) {
Subroutine rest: called CC=2 SCAR=7.1
Pod documented
658 556 ▶ my $size
Branch: 1 branch
BranchTrueFalseTotal
$spec[0] =~ /^d(\d+)$/u ? : 0 556 556
659 = $spec[0] =~ /^d(\d+)$/
660 ? $1 / ticks( $self->score )
661 : dura_size( $spec[0] );
662  
663 # carp __PACKAGE__,' L',__LINE__,' ',,"$spec[0] => $size\n";
664 556 $self->counter( $self->counter + $size );
665 556 return $self->score->r(@spec);
666 }
667  
668 #pod =method count_in
669 #pod
670 #pod $d->count_in;
671 #pod $d->count_in($bars);
672 #pod $d->count_in({ bars => $bars, patch => $patch });
673 #pod
674 #pod Play a patch for the number of beats times the number of bars.
675 #pod
676 #pod If no bars are given, the object setting is used. If no patch is
677 #pod given, the closed hihat is used.
678 #pod
679 #pod =cut
680  
681 0 ▶sub count_in ( $self, $args_ref ) {
Subroutine count_in: not called CC=9 SCAR=45.0
Pod documented
682  
683 0 my $bars = $self->bars;
684 0 my $patch = $self->pedal_hh;
685 0 my $accent = $self->closed_hh;
686  
687 0 ▶ if ( $args_ref && ref $args_ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args_ref and ref $args_ref) { } 0 0 0
elsif ($args_ref) { } 0 0 0
Condition0%0/3
$args_ref and ref $args_ref
!ll&&!rl&&r
000
MC/DC0%0/2
$args_ref and ref $args_ref
$args_ref ref $args_ref
Truth table0%0/3
A and B
A= $args_refB= ref $args_ref
ABresult
0X0
100
111
688 0 ▶ $bars = $args_ref->{bars} if defined $args_ref->{bars};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args_ref->{"bars"} 0 0 0
689 0 ▶ $patch = $args_ref->{patch} if defined $args_ref->{patch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args_ref->{"patch"} 0 0 0
690 0 ▶ $accent = $args_ref->{accent} if defined $args_ref->{accent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args_ref->{"accent"} 0 0 0
691 }
692 elsif ($args_ref) {
693 0 $bars = $args_ref; # given a simple integer
694 }
695  
696 0 my $j = 1;
697 0 for my $i ( 1 .. $self->beats * $bars ) {
698 0 ▶ if ( $i == $self->beats * $j - $self->beats + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $self->beats * $j - $self->beats + 1) { } 0 0 0
699 0 $self->accent_note( 127, $self->quarter, $accent );
700 0 $j++;
701 }
702 else {
703 0 $self->note( $self->quarter, $patch );
704 }
705 }
706 0 return;
707 }
708  
709 #pod =method metronome3
710 #pod
711 #pod $d->metronome3;
712 #pod $d->metronome3($bars);
713 #pod $d->metronome3($bars, $cymbal);
714 #pod $d->metronome3($bars, $cymbal, $tempo);
715 #pod $d->metronome3($bars, $cymbal, $tempo, $swing);
716 #pod
717 #pod Add a steady 3/x beat to the score.
718 #pod
719 #pod Defaults for all metronome methods:
720 #pod
721 #pod bars: The object B<bars>
722 #pod cymbal: B<closed_hh>
723 #pod tempo: B<quarter-note>
724 #pod swing: 50 percent = straight-time
725 #pod
726 #pod =cut
727  
728 0 ▶sub metronome3 (
Subroutine metronome3: not called CC=7 SCAR=40.3
Pod documented
729 $self,
730 0 $bars = $self->bars,
731 0 $cymbal = $self->closed_hh,
732 0 $tempo = $self->quarter,
733 0 $swing = 50 # percent
734 )
735 {
736 0 my $x = dura_size($tempo) * ticks( $self->score );
737 0 my $y = sprintf '%0.f', ( $swing / 100 ) * $x;
738 0 my $z = $x - $y;
739 0 for ( 1 .. $bars ) {
740 0 $self->note( "d$x", $cymbal, $self->kick );
741 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
742 0 $self->note( "d$y", $cymbal );
743 0 $self->note( "d$z", $cymbal );
744 }
745 else {
746 0 $self->note( "d$x", $cymbal );
747 }
748 0 $self->note( "d$x", $cymbal, $self->snare );
749 }
750 0 return;
751 }
752  
753 #pod =method metronome4
754 #pod
755 #pod $d->metronome4;
756 #pod $d->metronome4($bars);
757 #pod $d->metronome4($bars, $cymbal);
758 #pod $d->metronome4($bars, $cymbal, $tempo);
759 #pod $d->metronome4($bars, $cymbal, $tempo, $swing);
760 #pod
761 #pod Add a steady 4/x beat to the score.
762 #pod
763 #pod =cut
764  
765 0 ▶sub metronome4 (
Subroutine metronome4: not called CC=8 SCAR=42.8
Pod documented
766 $self,
767 0 $bars = $self->bars,
768 0 $cymbal = $self->closed_hh,
769 0 $tempo = $self->quarter,
770 0 $swing = 50 # percent
771 )
772 {
773 0 my $x = dura_size($tempo) * ticks( $self->score );
774 0 my $y = sprintf '%0.f', ( $swing / 100 ) * $x;
775 0 my $z = $x - $y;
776 0 for my $n ( 1 .. $bars ) {
777 0 $self->note( "d$x", $cymbal, $self->kick );
778 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
779 0 $self->note( "d$y", $cymbal );
780 0 $self->note( "d$z", $cymbal );
781 }
782 else {
783 0 $self->note( "d$x", $cymbal );
784 }
785 0 $self->note( "d$x", $cymbal, $self->snare );
786 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
787 0 $self->note( "d$y", $cymbal );
788 0 $self->note( "d$z", $cymbal );
789 }
790 else {
791 0 $self->note( "d$x", $cymbal );
792 }
793 }
794 0 return;
795 }
796  
797 #pod =method metronome5
798 #pod
799 #pod $d->metronome5;
800 #pod $d->metronome5($bars);
801 #pod $d->metronome5($bars, $cymbal);
802 #pod $d->metronome5($bars, $cymbal, $tempo);
803 #pod $d->metronome5($bars, $cymbal, $tempo, $swing);
804 #pod
805 #pod Add a 5/x beat to the score.
806 #pod
807 #pod =cut
808  
809 0 ▶sub metronome5 (
Subroutine metronome5: not called CC=9 SCAR=45.0
Pod documented
810 $self,
811 0 $bars = $self->bars,
812 0 $cymbal = $self->closed_hh,
813 0 $tempo = $self->quarter,
814 0 $swing = 50 # percent
815 )
816 {
817 0 my $x = dura_size($tempo) * ticks( $self->score );
818 0 my $half = $x / 2;
819 0 my $y = sprintf '%0.f', ( $swing / 100 ) * $x;
820 0 my $z = $x - $y;
821  
822 0 for my $n ( 1 .. $bars ) {
823 0 $self->note( "d$x", $cymbal, $self->kick );
824 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
825 0 $self->note( "d$y", $cymbal );
826 0 $self->note( "d$z", $cymbal );
827 }
828 else {
829 0 $self->note( "d$x", $cymbal );
830 }
831 0 $self->note( "d$x", $cymbal, $self->snare );
832 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
833 0 $self->note( "d$y", $cymbal );
834 0 $self->note( "d$z", $cymbal );
835 }
836 else {
837 0 $self->note( "d$x", $cymbal );
838 }
839 0 ▶ if ( $n % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2) { } 0 0 0
840 0 $self->note( "d$x", $cymbal );
841 }
842 else {
843 0 $self->note( "d$half", $cymbal );
844 0 $self->note( "d$half", $self->kick );
845 }
846 }
847 0 return;
848 }
849  
850 #pod =method metronome6
851 #pod
852 #pod $d->metronome6;
853 #pod $d->metronome6($bars);
854 #pod $d->metronome6($bars, $cymbal);
855 #pod $d->metronome6($bars, $cymbal, $tempo);
856 #pod $d->metronome6($bars, $cymbal, $tempo, $swing);
857 #pod
858 #pod Add a 6/x beat to the score.
859 #pod
860 #pod =cut
861  
862 0 ▶sub metronome6 (
Subroutine metronome6: not called CC=8 SCAR=42.8
Pod documented
863 $self,
864 0 $bars = $self->bars,
865 0 $cymbal = $self->closed_hh,
866 0 $tempo = $self->quarter,
867 0 $swing = 50 # percent
868 )
869 {
870 0 my $x = dura_size($tempo) * ticks( $self->score );
871 0 my $y = sprintf '%0.f', ( $swing / 100 ) * $x;
872 0 my $z = $x - $y;
873  
874 0 for my $n ( 1 .. $bars ) {
875 0 $self->note( "d$x", $cymbal, $self->kick );
876 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
877 0 $self->note( "d$y", $cymbal );
878 0 $self->note( "d$z", $cymbal );
879 }
880 else {
881 0 $self->note( "d$x", $cymbal );
882 }
883 0 $self->note( "d$x", $cymbal );
884 0 $self->note( "d$x", $cymbal, $self->snare );
885 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
886 0 $self->note( "d$y", $cymbal );
887 0 $self->note( "d$z", $cymbal );
888 }
889 else {
890 0 $self->note( "d$x", $cymbal );
891 }
892 0 $self->note( "d$x", $cymbal );
893 }
894 0 return;
895 }
896  
897 #pod =method metronome7
898 #pod
899 #pod $d->metronome7;
900 #pod $d->metronome7($bars);
901 #pod $d->metronome7($bars, $cymbal);
902 #pod $d->metronome7($bars, $cymbal, $tempo);
903 #pod $d->metronome7($bars, $cymbal, $tempo, $swing);
904 #pod
905 #pod Add a 7/x beat to the score.
906 #pod
907 #pod =cut
908  
909 0 ▶sub metronome7 (
Subroutine metronome7: not called CC=9 SCAR=45.0
Pod documented
910 $self,
911 0 $bars = $self->bars,
912 0 $cymbal = $self->closed_hh,
913 0 $tempo = $self->quarter,
914 0 $swing = 50 # percent
915 )
916 {
917 0 my $x = dura_size($tempo) * ticks( $self->score );
918 0 my $y = sprintf '%0.f', ( $swing / 100 ) * $x;
919 0 my $z = $x - $y;
920  
921 0 for my $n ( 1 .. $bars ) {
922 0 $self->note( "d$x", $cymbal, $self->kick );
923 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
924 0 $self->note( "d$y", $cymbal );
925 0 $self->note( "d$z", $cymbal );
926 }
927 else {
928 0 $self->note( "d$x", $cymbal );
929 }
930 0 $self->note( "d$x", $cymbal );
931 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
932 0 $self->note( "d$y", $cymbal, $self->kick );
933 0 $self->note( "d$z", $cymbal );
934 }
935 else {
936 0 $self->note( "d$x", $cymbal, $self->kick );
937 }
938 0 $self->note( "d$x", $cymbal, $self->snare );
939 0 ▶ if ( $swing > STRAIGHT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($swing > 50) { } 0 0 0
940 0 $self->note( "d$y", $cymbal );
941 0 $self->note( "d$z", $cymbal );
942 }
943 else {
944 0 $self->note( "d$x", $cymbal );
945 }
946 0 $self->note( "d$x", $cymbal );
947 }
948 0 return;
949 }
950  
951 #pod =method metronome44
952 #pod
953 #pod $d->metronome44;
954 #pod $d->metronome44($bars);
955 #pod $d->metronome44($bars, $flag);
956 #pod $d->metronome44($bars, $flag, $cymbal);
957 #pod
958 #pod Add a steady quarter-note based 4/4 beat to the score.
959 #pod
960 #pod If a B<flag> is provided to include alternating eighth-note kicks in
961 #pod the groove.
962 #pod
963 #pod =cut
964  
965 0 ▶sub metronome44 (
Subroutine metronome44: not called CC=8 SCAR=42.8
Pod documented
966 0 $self, $bars = $self->bars,
967 0 $flag = 0, $cymbal = $self->closed_hh,
968 )
969 {
970 0 my $i = 0;
971 0 for my $n ( 1 .. $self->beats * $bars ) {
972 0 ▶ if ( $n % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2 == 0) { } 0 0 0
973 0 $self->note( $self->quarter, $cymbal, $self->snare );
974 }
975 else {
976 0 ▶ if ( $flag == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == 0) { } 0 0 0
977 0 $self->note( $self->quarter, $cymbal, $self->kick );
978 }
979 else {
980 0 ▶ if ( $i % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i % 2 == 0) { } 0 0 0
981 0 $self->note( $self->quarter, $cymbal, $self->kick );
982 }
983 else {
984 0 $self->note( $self->eighth, $cymbal, $self->kick );
985 0 $self->note( $self->eighth, $self->kick );
986 }
987 }
988 0 $i++;
989 }
990 }
991 0 return;
992 }
993  
994 #pod =method flam
995 #pod
996 #pod $d->flam($spec);
997 #pod $d->flam( $spec, $grace_note );
998 #pod $d->flam( $spec, $grace_note, $patch );
999 #pod $d->flam( $spec, $grace_note, $patch, $accent );
1000 #pod
1001 #pod Add a "flam" to the score, where a ghosted 64th gracenote is played
1002 #pod before the primary note.
1003 #pod
1004 #pod If not provided the B<snare> is used for the B<grace> and B<patch>
1005 #pod patches. Also, 1/2 of the score volume is used for the B<accent>
1006 #pod if that is not given.
1007 #pod
1008 #pod If the B<grace> note is given as a literal C<'r'>, rest instead of
1009 #pod adding a note to the score.
1010 #pod
1011 #pod =cut
1012  
1013 0 ▶sub flam (
Subroutine flam: not called CC=5 SCAR=34.0
Pod documented
1014 $self, $spec,
1015 0 $grace = $self->snare,
1016 0 $patch = $self->snare,
1017 0 $accent = sprintf '%0.f',
1018 $self->score->Volume / 2
1019 )
1020 {
1021 my ( $x, $y )
1022 0 = @MIDI::Simple::Length{ $spec, $self->sixtyfourth }; ## no critic (Variables::ProhibitPackageVars)
1023 0 my $z = sprintf '%0.f', ( $x - $y ) * ticks( $self->score );
1024 0 ▶ if ( $grace eq 'r' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grace eq 'r') { } 0 0 0
1025 0 $self->rest( $self->sixtyfourth );
1026 }
1027 else {
1028 0 $self->accent_note( $accent, $self->sixtyfourth, $grace );
1029 }
1030 0 return $self->note( 'd' . $z, $patch );
1031 }
1032  
1033 #pod =method roll
1034 #pod
1035 #pod $d->roll( $length, $spec );
1036 #pod $d->roll( $length, $spec, $patch );
1037 #pod
1038 #pod Add a drum roll to the score, where the B<patch> is played for
1039 #pod duration B<length> in B<spec> increments.
1040 #pod
1041 #pod If not provided the B<snare> is used for the B<patch>.
1042 #pod
1043 #pod =cut
1044  
1045 0 ▶sub roll ( $self, $length, $spec, $patch = $self->snare ) {
Subroutine roll: not called CC=3 SCAR=24.8
Pod documented
1046 0 my ( $x, $y ) = @MIDI::Simple::Length{ $length, $spec }; ## no critic (Variables::ProhibitPackageVars)
1047 0 my $z = sprintf '%0.f', $x / $y;
1048 0 $self->note( $spec, $patch ) for 1 .. $z;
1049 0 return;
1050 }
1051  
1052 #pod =method crescendo_roll
1053 #pod
1054 #pod $d->crescendo_roll( [$start, $end, $bezier], $length, $spec );
1055 #pod $d->crescendo_roll( [$start, $end, $bezier], $length, $spec, $patch );
1056 #pod
1057 #pod Add a drum roll to the score, where the B<patch> is played for
1058 #pod duration B<length> in B<spec> notes, at increasing or decreasing
1059 #pod volumes from B<start> to B<end>.
1060 #pod
1061 #pod If not provided the B<snare> is used for the B<patch>.
1062 #pod
1063 #pod If true, the B<bezier> flag will render the crescendo with a curve,
1064 #pod rather than as a straight line.
1065 #pod
1066 #pod | *
1067 #pod | *
1068 #pod vol | *
1069 #pod | *
1070 #pod |*
1071 #pod ---------------
1072 #pod time
1073 #pod
1074 #pod =cut
1075  
1076 0 ▶sub crescendo_roll ( $self, $span_ref, $length, $spec,
Subroutine crescendo_roll: not called CC=8 SCAR=42.8
Pod documented
1077 0 $patch = $self->snare )
1078 {
1079 0 my ( $i, $j, $k ) = $span_ref->@*;
1080 0 my ( $x, $y ) = @MIDI::Simple::Length{ $length, $spec }; ## no critic (Variables::ProhibitPackageVars)
1081 0 my $z = sprintf '%0.f', $x / $y;
1082 0 ▶ if ($k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k) { } 0 0 0
1083 0 my $bezier = Math::Bezier->new( 1, $i, $z, $i, $z, $j, );
1084 0 ▶ for ( my $n = 0 ; $n <= 1 ; $n += ( 1 / ( $z - 1 ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 1) 0 0 0
1085 0 my ( undef, $v ) = $bezier->point($n);
1086 0 $v = sprintf '%0.f', $v;
1087  
1088 # carp (__PACKAGE__,' ',__LINE__," $n INC: $v\n");
1089 0 $self->accent_note( $v, $spec, $patch );
1090 }
1091 }
1092 else {
1093 0 my $v = sprintf '%0.f', ( $j - $i ) / ( $z - 1 );
1094  
1095 # carp (__PACKAGE__,' ',__LINE__," VALUE: $v\n");
1096 0 for my $n ( 1 .. $z ) {
1097 0 ▶ if ( $n == $z ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == $z) 0 0 0
1098 0 ▶ if ( $i < $j ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $j) { } 0 0 0
elsif ($i > $j) { } 0 0 0
1099 0 $i += $j - $i;
1100 }
1101 elsif ( $i > $j ) {
1102 0 $i -= $i - $j;
1103 }
1104 }
1105  
1106 # carp (__PACKAGE__,' ',__LINE__," $n INC: $i\n");
1107 0 $self->accent_note( $i, $spec, $patch );
1108 0 $i += $v;
1109 }
1110 }
1111 0 return;
1112 }
1113  
1114 #pod =method pattern
1115 #pod
1116 #pod $d->pattern( patterns => \@patterns );
1117 #pod $d->pattern( patterns => \@patterns, instrument => $d->kick );
1118 #pod $d->pattern( patterns => \@patterns, instrument => $d->kick, %options );
1119 #pod
1120 #pod Play a given set of beat B<patterns> with the given B<instrument>.
1121 #pod
1122 #pod The B<patterns> are an arrayref of "beat-strings". By default these
1123 #pod are made of contiguous ones and zeros, meaning "strike" or "rest".
1124 #pod For example:
1125 #pod
1126 #pod patterns => [qw( 0101 0101 0110 0110 )],
1127 #pod
1128 #pod This method accumulates the number of beats in the object's B<counter>
1129 #pod attribute.
1130 #pod
1131 #pod The B<vary> option is a hashref of coderefs, keyed by single character
1132 #pod tokens, like the digits 0-9. Each coderef duration should add up to
1133 #pod the given B<duration> option. The single argument that is given to a
1134 #pod coderef is the object itself. It is used like: C<my $self = shift;>.
1135 #pod
1136 #pod These patterns can be generated with any custom function, as in the
1137 #pod L</SYNOPSIS>. For instance, you could use the L<Creating::Rhythms>
1138 #pod module to generate Euclidean patterns.
1139 #pod
1140 #pod Defaults:
1141 #pod
1142 #pod instrument: snare
1143 #pod patterns: [] (i.e. empty!)
1144 #pod Options:
1145 #pod duration: quarter-note
1146 #pod beats: given by constructor
1147 #pod repeat: 1
1148 #pod negate: 0 (flip the bit values)
1149 #pod vary:
1150 #pod 0 => sub { $self->rest( $args{duration} ) },
1151 #pod 1 => sub { $self->note( $args{duration}, $args{instrument} ) },
1152 #pod
1153 #pod =cut
1154  
1155 34 ▶sub pattern ( $self, %args ) {
Subroutine pattern: called CC=16 SCAR=30.9
Pod documented
1156 34 ▶ $args{instrument} ||= $self->snare;
Condition33%1/3
$args{"instrument"} ||= $self->snare
l!l&&r!l&&!r
3400
MC/DC0%0/2
$args{"instrument"} ||= $self->snare
$args{"instrument"} $self->snare
Truth table33%1/3
A ||= B
A= $args{"instrument"}B= $self->snare
ABresult
1X1
011
000
1157 34 ▶ $args{patterns} ||= [];
Condition50%1/2
$args{"patterns"} ||= []
l!l
340
MC/DC0%0/1
$args{"patterns"} ||= []
$args{"patterns"}
Truth table50%1/2
A
A= $args{"patterns"}
Aresult
11
00
1158 34 ▶ $args{beats} ||= $self->beats;
Condition33%1/3
$args{"beats"} ||= $self->beats
l!l&&r!l&&!r
0340
MC/DC0%0/2
$args{"beats"} ||= $self->beats
$args{"beats"} $self->beats
Truth table33%1/3
A ||= B
A= $args{"beats"}B= $self->beats
ABresult
1X1
011
000
1159 34 ▶ $args{negate} ||= 0;
Condition50%1/2
$args{"negate"} ||= 0
l!l
034
MC/DC0%0/1
$args{"negate"} ||= 0
$args{"negate"}
Truth table50%1/2
A
A= $args{"negate"}
Aresult
11
00
1160 34 ▶ $args{repeat} ||= 1;
Condition50%1/2
$args{"repeat"} ||= 1
l!l
034
MC/DC0%0/1
$args{"repeat"} ||= 1
$args{"repeat"}
Truth table50%1/2
A
A= $args{"repeat"}
Aresult
11
00
1161  
1162 34 ▶ return unless $args{patterns}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$args{"patterns"}} 0 34 34
1163  
1164 # set size and duration
1165 34 my $size;
1166 34 ▶ if ( $args{duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'duration'}) { } 33 1 34
1167 33 ▶ $size = dura_size( $args{duration} ) || 1;
Condition50%1/2
dura_size($args{'duration'}) || 1
l!l
330
MC/DC0%0/1
dura_size($args{'duration'}) || 1
dura_size($args{'duration'})
Truth table50%1/2
A
A= dura_size($args{'duration'})
Aresult
11
00
1168 }
1169 else {
1170 1 $size = 4 / length( $args{patterns}->[0] );
1171 1 my $dump = reverse_dump('length');
1172 1 ▶ $args{duration} = $dump->{$size} || $self->quarter;
Condition33%1/3
$dump->{$size} || $self->quarter
l!l&&r!l&&!r
010
MC/DC0%0/2
$dump->{$size} || $self->quarter
$dump->{$size} $self->quarter
Truth table33%1/3
A || B
A= $dump->{$size}B= $self->quarter
ABresult
1X1
011
000
1173 }
1174  
1175 # set the default beat-string variations
1176 $args{vary} ||= {
1177 556 ▶ 0 => sub { $self->rest( $args{duration} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1178 121 ▶ 1 => sub { $self->note( $args{duration}, $args{instrument} ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1179 34 ▶ };
Condition50%1/2
$args{"vary"} ||= {0, sub { $self->rest($args{"duration"}); } , 1, sub { $self->note($args{"duration"}, $args{"instrument"}); } }
l!l
034
MC/DC0%0/1
$args{"vary"} ||= {0, sub { $self->rest($args{"duration"}); } , 1, sub { $self->note($args{"duration"}, $args{"instrument"}); } }
$args{"vary"}
Truth table50%1/2
A
A= $args{"vary"}
Aresult
11
00
1180  
1181 34 for my $pattern ( $args{patterns}->@* ) {
1182 34 ▶ $pattern =~ tr/01/10/ if $args{negate};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"negate"} 0 34 34
1183  
1184 34 ▶ next if $pattern =~ /^0+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /^0+$/u 0 34 34
1185  
1186 34 for ( 1 .. $args{repeat} ) {
1187 34 for my $bit ( split //, $pattern ) {
1188 677 $args{vary}{$bit}->( $self, %args );
1189 }
1190 }
1191 }
1192 34 return;
1193 }
1194  
1195 #pod =method sync_patterns
1196 #pod
1197 #pod $d->sync_patterns( $instrument1 => $patterns1, $inst2 => $pats2, ... );
1198 #pod $d->sync_patterns(
1199 #pod $d->open_hh => [ '11111111') ],
1200 #pod $d->snare => [ '0101' ],
1201 #pod $d->kick => [ '1010' ],
1202 #pod duration => $d->eighth, # render all notes at this level of granularity
1203 #pod ) for 1 .. $d->bars;
1204 #pod
1205 #pod Execute the C<pattern> method for multiple voices.
1206 #pod
1207 #pod If a C<duration> is provided, this will be used for each pattern
1208 #pod (primarily for the B<add_fill> method).
1209 #pod
1210 #pod =cut
1211  
1212 12 ▶sub sync_patterns ( $self, %patterns ) {
Subroutine sync_patterns: called CC=2 SCAR=7.0
Pod documented
1213 12 my $master_duration = delete $patterns{duration};
1214  
1215 12 my @subs;
1216 12 for my $instrument ( keys %patterns ) {
1217 33 ▶ push @subs, sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
1218 $self->pattern(
1219 instrument => $instrument,
1220 33 ▶ patterns => $patterns{$instrument},
Branch: 1 branch
BranchTrueFalseTotal
$master_duration ? : 33 0 33
1221 $master_duration
1222 ? ( duration => $master_duration )
1223 : (),
1224 );
1225 33 };
1226 }
1227  
1228 12 return $self->sync(@subs);
1229 }
1230  
1231 #pod =method add_fill
1232 #pod
1233 #pod $d->add_fill( $fill, $instrument1 => $patterns1, $inst2 => $pats2, ... );
1234 #pod $d->add_fill(
1235 #pod sub {
1236 #pod my $self = shift;
1237 #pod return {
1238 #pod duration => 16, # sixteenth note fill
1239 #pod $self->open_hh => '00000000',
1240 #pod $self->snare => '11111111',
1241 #pod $self->kick => '00000000',
1242 #pod };
1243 #pod },
1244 #pod $d->open_hh => [ '11111111' ], # example phrase
1245 #pod $d->snare => [ '0101' ], # "
1246 #pod $d->kick => [ '1010' ], # "
1247 #pod );
1248 #pod
1249 #pod Add a fill to the beat pattern. That is, replace the end of the given
1250 #pod beat-string phrase with a fill. The fill is given as the first
1251 #pod argument and should be a coderef that returns a hashref. The default
1252 #pod is a three-note, eighth-note snare fill.
1253 #pod
1254 #pod =cut
1255  
1256 12 ▶sub add_fill ( $self, $fill = undef, %patterns ) {
Subroutine add_fill: called CC=19 SCAR=29.5
Pod documented
1257 11 ▶ $fill //= sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
1258 11 duration => 8,
1259 $self->open_hh => '000',
1260 $self->snare => '111',
1261 $self->kick => '000',
1262 12 ▶ } };
Condition66%2/3
$fill //= sub { +{"duration", 8, $self->open_hh, "000", $self->snare, 111, $self->kick, "000"}; }
l!l&&r!l&&!r
1110
MC/DC0%0/2
$fill //= sub { +{"duration", 8, $self->open_hh, "000", $self->snare, 111, $self->kick, "000"}; }
$fill sub { +{"duration", 8, $self->open_hh, "000", $self->snare, 111, $self->kick, "000"}; }
Truth table66%2/3
A //= B
A= $fillB= sub { +{"duration", 8, $self->open_hh, "000", $self->snare, 111, $self->kick, "000"}; }
ABresult
1X1
011
000
1263 12 my $fill_patterns = $fill->($self);
1264 12 ▶ carp 'Fill: ', ddc($fill_patterns) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1265 12 ▶ my $fill_duration = delete $fill_patterns->{duration} || 8;
Condition50%1/2
delete $fill_patterns->{'duration'} || 8
l!l
120
MC/DC0%0/1
delete $fill_patterns->{'duration'} || 8
delete $fill_patterns->{'duration'}
Truth table50%1/2
A
A= delete $fill_patterns->{'duration'}
Aresult
11
00
1266 12 my $fill_length = length( ( values %$fill_patterns )[0] );
1267  
1268 12 my %lengths;
1269 12 for my $instrument ( keys %patterns ) {
1270 $lengths{$instrument}
1271 33 = sum0 map { length $_ } $patterns{$instrument}->@*;
1272 }
1273  
1274 12 my $lcm = _multilcm( $fill_duration, values %lengths );
1275 12 ▶ carp "LCM: $lcm\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1276  
1277 12 my $size = 4 / $lcm;
1278 12 my $dump = reverse_dump('length');
1279 my $master_duration
1280 12 ▶ = $dump->{$size} || $self->eighth; # XXX this || is not right
Condition66%2/3
$dump->{$size} || $self->eighth
l!l&&r!l&&!r
1020
MC/DC0%0/2
$dump->{$size} || $self->eighth
$dump->{$size} $self->eighth
Truth table66%2/3
A || B
A= $dump->{$size}B= $self->eighth
ABresult
1X1
011
000
1281 12 ▶ carp "Size: $size, Duration: $master_duration\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1282  
1283 12 ▶ my $fill_chop
Branch: 1 branch
BranchTrueFalseTotal
$fill_duration == $lcm ? : 7 5 12
1284 = $fill_duration == $lcm
1285 ? $fill_length
1286 : int( $lcm / $fill_length ) + 1;
1287 12 ▶ carp "Chop: $fill_chop\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1288  
1289 12 my %fresh_patterns;
1290 12 for my $instrument ( keys %patterns ) {
1291  
1292 # get a single "flattened" pattern as an arrayref
1293 my $pattern
1294 33 = [ map { split //, $_ } $patterns{$instrument}->@* ];
1295  
1296 # the fresh pattern is possibly upsized with the LCM
1297 33 ▶ $fresh_patterns{$instrument}
Branch: 1 branch
BranchTrueFalseTotal
@$pattern < $lcm ? : 29 4 33
1298 = $pattern->@* < $lcm
1299 ? [ join '', upsize( $pattern, $lcm )->@* ]
1300 : [ join '', $pattern->@* ];
1301 }
1302 12 ▶ carp 'Patterns: ', ddc( \%fresh_patterns ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1303  
1304 12 my %replacement;
1305 12 for my $instrument ( keys $fill_patterns->%* ) {
1306  
1307 # get a single "flattened" pattern as a zero-pre-padded arrayref
1308 my $pattern = [
1309 split //, sprintf '%0*s',
1310 36 $fill_duration, $fill_patterns->{$instrument} ];
1311  
1312 # the fresh pattern string is possibly upsized with the LCM
1313 36 ▶ my $fresh
Branch: 1 branch
BranchTrueFalseTotal
@$pattern < $lcm ? : 15 21 36
1314 = $pattern->@* < $lcm
1315 ? join '', upsize( $pattern, $lcm )->@*
1316 : join '', $pattern->@*;
1317  
1318 # the replacement string is the tail of the fresh pattern string
1319 36 $replacement{$instrument} = substr $fresh, -$fill_chop;
1320 }
1321 12 ▶ carp 'Replacements: ', ddc( \%replacement ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 11 12
1322  
1323 12 my %replaced;
1324 12 for my $instrument ( keys %fresh_patterns ) {
1325  
1326 # get the string to replace
1327 33 my $string = join '', $fresh_patterns{$instrument}->@*;
1328  
1329 # replace the tail of the string
1330 33 my $pos = length $replacement{$instrument};
1331 33 substr $string, -$pos, $pos, $replacement{$instrument};
1332 33 ▶ carp "$instrument: $string\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 33 33
1333  
1334 # prepare the replaced pattern for syncing
1335 33 $replaced{$instrument} = [$string];
1336 }
1337  
1338 12 $self->sync_patterns( %replaced, duration => $master_duration, );
1339  
1340 12 return \%replaced;
1341 }
1342  
1343 #pod =method set_time_sig
1344 #pod
1345 #pod $d->set_time_sig;
1346 #pod $d->set_time_sig('5/4');
1347 #pod $d->set_time_sig( '5/4', 0 );
1348 #pod
1349 #pod Add a time signature event to the score, and reset the B<beats> and
1350 #pod B<divisions> object attributes.
1351 #pod
1352 #pod If a ratio argument is given, set the B<signature> object attribute to
1353 #pod it. If the 2nd argument flag is C<0>, the B<beats> and B<divisions>
1354 #pod are B<not> reset.
1355 #pod
1356 #pod =cut
1357  
1358 14 ▶sub set_time_sig ( $self, $time_signature, $set = 1 ) {
Subroutine set_time_sig: called CC=4 SCAR=13.9
Pod documented
1359 14 ▶ $self->signature($time_signature) if $time_signature;
Branch: 1 branch
BranchTrueFalseTotal
if $time_signature 14 0 14
1360 14 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 13 1 14
1361 13 my ( $beats, $divisions ) = split /\//, $self->signature;
1362 13 $self->beats($beats);
1363 13 $self->divisions($divisions);
1364 }
1365 14 return set_time_signature( $self->score, $self->signature );
1366 }
1367  
1368 #pod =method write
1369 #pod
1370 #pod Output the score as a MIDI file with the module L</file> attribute as
1371 #pod the file name.
1372 #pod
1373 #pod =cut
1374  
1375 0 ▶sub write ($self) { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
1376 0 return $self->score->write_score( $self->file );
1377 }
1378  
1379 # lifted from https://www.perlmonks.org/?node_id=56906
1380 33 ▶sub _gcf ( $x, $y ) {
Subroutine _gcf: called CC=2 SCAR=6.9
1381 33 ▶ ( $x, $y ) = ( $y, $x % $y ) while $y;
Branch: 1 branch
BranchTrueFalseTotal
$y and do { () = ($y, $x % $y) } 42 33 75
1382 33 return $x;
1383 }
1384  
1385 33 ▶sub _lcm ( $x, $y ) { return $x * $y / _gcf( $x, $y ) }
Subroutine _lcm: called CC=1 SCAR=0.0
1386  
1387 12 ▶sub _multilcm { ## no critic (Subroutines::RequireArgUnpacking)
Subroutine _multilcm: called CC=2 SCAR=6.9
1388 12 my $x = shift;
1389 12 ▶ $x = _lcm( $x, shift ) while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { $x = _lcm($x, shift()) } 33 12 45
1390 12 return $x;
1391 }
1392  
1393 6 1;
1394  
1395 __END__
« Tiny/Syncopate.pm ↑ Summary Tiny/Grooves.pm »