| 1 | package Music::SimpleDrumMachine; | ||
| 2 | our $AUTHORITY = 'cpan:GENE'; | ||
| 3 | |||
| 4 | # ABSTRACT: Simple 16th-note-phrase Drummer | ||
| 5 | |||
| 6 | our $VERSION = '0.0702'; | ||
| 7 | |||
| 8 | 2 | ▶ | use v5.36; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use feature 'try'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | # use Data::Dumper::Compact qw(ddc); | ||
| 15 | 2 | ▶ | use IO::Async::Loop (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use IO::Async::Timer::Periodic (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 2 | ▶ | use List::Util::WeightedChoice qw(choose_weighted); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 0 | ▶ | use MIDI::RtMidi::FFI::Device (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use MIDI::Util qw(setup_score set_chan_patch); | ||
| 20 | use Music::Duration::Partition (); | ||
| 21 | use namespace::clean; | ||
| 22 | |||
| 23 | no warnings 'experimental::try'; | ||
| 24 | |||
| 25 | |||
| 26 | has add_drums => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => sub { croak "$_[0] is not an array-ref" unless ref($_[0]) eq 'ARRAY' }, | ||
| 29 | default => sub { [] }, | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | has beats => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => sub { croak "$_[0] is not an integer" unless $_[0] =~ /^\d+$/ }, | ||
| 36 | default => sub { 16 }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | |||
| 40 | has bars => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => sub { croak "$_[0] is not a positive integer" unless $_[0] =~ /^[1-9]\d*$/ }, | ||
| 43 | default => sub { 4 }, | ||
| 44 | ); | ||
| 45 | |||
| 46 | |||
| 47 | has bpm => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => sub { croak "$_[0] is not an integer" unless $_[0] =~ /^\d+$/ }, | ||
| 50 | default => sub { 120 }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | |||
| 54 | has chan => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => sub { croak "$_[0] is not valid" unless $_[0] =~ /^-?\d+$/ }, | ||
| 57 | default => sub { 9 }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | |||
| 61 | has divisions => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => sub { croak "$_[0] is not an integer" unless $_[0] =~ /^\d+$/ }, | ||
| 64 | default => sub { 4 }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | has drums => ( | ||
| 69 | is => 'rw', | ||
| 70 | isa => sub { croak "$_[0] is not a hash-ref" unless ref($_[0]) eq 'HASH' }, | ||
| 71 | builder => '_build_drums', | ||
| 72 | ); | ||
| 73 | |||
| 74 | sub _build_drums { | ||
| 75 | my ($self) = @_; | ||
| 76 | my $drums = { | ||
| 77 | kick => { num => 36, chan => $self->chan < 0 ? 0 : $self->chan, pat => [] }, | ||
| 78 | snare => { num => 38, chan => $self->chan < 0 ? 1 : $self->chan, pat => [] }, | ||
| 79 | closed => { num => 42, chan => $self->chan < 0 ? 2 : $self->chan, pat => [] }, | ||
| 80 | fillcrash => { num => 49, chan => $self->chan < 0 ? 3 : $self->chan, pat => [] }, # used for fills | ||
| 81 | open => { num => 46, chan => $self->chan < 0 ? 4 : $self->chan, pat => [] }, | ||
| 82 | rimshot => { num => 37, chan => $self->chan < 0 ? 5 : $self->chan, pat => [] }, # actually, "side-stick" | ||
| 83 | clap => { num => 39, chan => $self->chan < 0 ? 6 : $self->chan, pat => [] }, | ||
| 84 | shaker => { num => 70, chan => $self->chan < 0 ? 7 : $self->chan, pat => [] }, | ||
| 85 | cowbell => { num => 56, chan => $self->chan < 0 ? 8 : $self->chan, pat => [] }, | ||
| 86 | crash => { num => 57, chan => $self->chan < 0 ? 10 : $self->chan, pat => [] }, # skip 9 | ||
| 87 | hi_tom => { num => 48, chan => $self->chan < 0 ? 11 : $self->chan, pat => [] }, | ||
| 88 | mid_tom => { num => 47, chan => $self->chan < 0 ? 12 : $self->chan, pat => [] }, | ||
| 89 | low_tom => { num => 45, chan => $self->chan < 0 ? 13 : $self->chan, pat => [] }, | ||
| 90 | conga => { num => 63, chan => $self->chan < 0 ? 14 : $self->chan, pat => [] }, | ||
| 91 | kick2 => { num => 35, chan => $self->chan < 0 ? 15 : $self->chan, pat => [] }, | ||
| 92 | snare2 => { num => 40, chan => $self->chan < 0 ? 16 : $self->chan, pat => [] }, # Bogus channels now. | ||
| 93 | pedal => { num => 44, chan => $self->chan < 0 ? 19 : $self->chan, pat => [] }, # If multi-timbral: | ||
| 94 | low_floor => { num => 41, chan => $self->chan < 0 ? 17 : $self->chan, pat => [] }, # redefine with add_drums | ||
| 95 | hi_floor => { num => 43, chan => $self->chan < 0 ? 18 : $self->chan, pat => [] }, | ||
| 96 | hihi_tom => { num => 50, chan => $self->chan < 0 ? 20 : $self->chan, pat => [] }, | ||
| 97 | ride => { num => 51, chan => $self->chan < 0 ? 21 : $self->chan, pat => [] }, | ||
| 98 | ride_bell => { num => 53, chan => $self->chan < 0 ? 22 : $self->chan, pat => [] }, | ||
| 99 | ride2 => { num => 59, chan => $self->chan < 0 ? 23 : $self->chan, pat => [] }, | ||
| 100 | china => { num => 52, chan => $self->chan < 0 ? 24 : $self->chan, pat => [] }, | ||
| 101 | splash => { num => 55, chan => $self->chan < 0 ? 25 : $self->chan, pat => [] }, | ||
| 102 | tamborine => { num => 54, chan => $self->chan < 0 ? 26 : $self->chan, pat => [] }, | ||
| 103 | vibraslap => { num => 58, chan => $self->chan < 0 ? 27 : $self->chan, pat => [] }, | ||
| 104 | hi_bongo => { num => 60, chan => $self->chan < 0 ? 28 : $self->chan, pat => [] }, | ||
| 105 | low_bongo => { num => 61, chan => $self->chan < 0 ? 29 : $self->chan, pat => [] }, | ||
| 106 | mute_conga => { num => 62, chan => $self->chan < 0 ? 30 : $self->chan, pat => [] }, | ||
| 107 | low_conga => { num => 64, chan => $self->chan < 0 ? 31 : $self->chan, pat => [] }, | ||
| 108 | hi_timbale => { num => 65, chan => $self->chan < 0 ? 32 : $self->chan, pat => [] }, | ||
| 109 | low_timbale => { num => 66, chan => $self->chan < 0 ? 33 : $self->chan, pat => [] }, | ||
| 110 | hi_agogo => { num => 67, chan => $self->chan < 0 ? 34 : $self->chan, pat => [] }, | ||
| 111 | low_agogo => { num => 68, chan => $self->chan < 0 ? 35 : $self->chan, pat => [] }, | ||
| 112 | cabasa => { num => 69, chan => $self->chan < 0 ? 36 : $self->chan, pat => [] }, | ||
| 113 | whistle => { num => 71, chan => $self->chan < 0 ? 37 : $self->chan, pat => [] }, | ||
| 114 | long_whistle => { num => 72, chan => $self->chan < 0 ? 38 : $self->chan, pat => [] }, | ||
| 115 | guiro => { num => 73, chan => $self->chan < 0 ? 39 : $self->chan, pat => [] }, | ||
| 116 | long_guiro => { num => 74, chan => $self->chan < 0 ? 40 : $self->chan, pat => [] }, | ||
| 117 | claves => { num => 75, chan => $self->chan < 0 ? 41 : $self->chan, pat => [] }, | ||
| 118 | wood_block => { num => 76, chan => $self->chan < 0 ? 42 : $self->chan, pat => [] }, | ||
| 119 | low_block => { num => 77, chan => $self->chan < 0 ? 43 : $self->chan, pat => [] }, | ||
| 120 | mute_cuica => { num => 78, chan => $self->chan < 0 ? 44 : $self->chan, pat => [] }, | ||
| 121 | open_cuica => { num => 79, chan => $self->chan < 0 ? 45 : $self->chan, pat => [] }, | ||
| 122 | mute_tri => { num => 80, chan => $self->chan < 0 ? 46 : $self->chan, pat => [] }, | ||
| 123 | open_tri => { num => 81, chan => $self->chan < 0 ? 47 : $self->chan, pat => [] }, | ||
| 124 | }; | ||
| 125 | return $drums; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | has fill_crash => ( | ||
| 130 | is => 'rw', | ||
| 131 | isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ }, | ||
| 132 | default => sub { 1 }, | ||
| 133 | ); | ||
| 134 | |||
| 135 | |||
| 136 | has fills => ( | ||
| 137 | is => 'lazy', | ||
| 138 | builder => '_build_fills', | ||
| 139 | ); | ||
| 140 | sub _build_fills { | ||
| 141 | my ($self) = @_; | ||
| 142 | return { _default_fill => sub { $self->_default_fill } }; | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | has filling => ( | ||
| 147 | is => 'rw', | ||
| 148 | isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ }, | ||
| 149 | default => sub { 1 }, | ||
| 150 | ); | ||
| 151 | |||
| 152 | |||
| 153 | has next_fill => ( | ||
| 154 | is => 'rw', | ||
| 155 | default => sub { '_default_fill' }, | ||
| 156 | ); | ||
| 157 | |||
| 158 | |||
| 159 | has next_part => ( | ||
| 160 | is => 'rw', | ||
| 161 | default => sub { '_default_part' }, | ||
| 162 | ); | ||
| 163 | |||
| 164 | |||
| 165 | has parts => ( | ||
| 166 | is => 'lazy', | ||
| 167 | builder => '_build_parts', | ||
| 168 | ); | ||
| 169 | sub _build_parts { | ||
| 170 | my ($self) = @_; | ||
| 171 | return { _default_part => sub { $self->_default_part } }; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | has save => ( | ||
| 176 | is => 'ro', | ||
| 177 | default => 0, | ||
| 178 | ); | ||
| 179 | |||
| 180 | |||
| 181 | has score => ( | ||
| 182 | is => 'lazy', | ||
| 183 | builder => '_build_score', | ||
| 184 | ); | ||
| 185 | sub _build_score { | ||
| 186 | my ($self) = @_; | ||
| 187 | my $score = setup_score( | ||
| 188 | bpm => $self->bpm, | ||
| 189 | lead_in => 0, | ||
| 190 | ); | ||
| 191 | if ($self->chan < 0) { | ||
| 192 | warn "Multi-timbral isn't supported by a single-track score; using channel 9.\n"; | ||
| 193 | set_chan_patch($score, 9); | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | set_chan_patch($score, $self->chan); | ||
| 197 | } | ||
| 198 | return $score; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | has port_name => ( | ||
| 203 | is => 'ro', | ||
| 204 | default => sub { 'usb' }, | ||
| 205 | required => 1, | ||
| 206 | ); | ||
| 207 | |||
| 208 | |||
| 209 | has ppqn => ( | ||
| 210 | is => 'ro', | ||
| 211 | isa => sub { croak "$_[0] is not an integer" unless $_[0] =~ /^\d+$/ }, | ||
| 212 | default => sub { 24 }, | ||
| 213 | ); | ||
| 214 | |||
| 215 | |||
| 216 | has prefill_part => ( | ||
| 217 | is => 'ro', | ||
| 218 | isa => sub { croak "$_[0] is not an code-ref" unless ref($_[0]) eq 'CODE' }, | ||
| 219 | default => sub { \&_default_part }, | ||
| 220 | ); | ||
| 221 | |||
| 222 | |||
| 223 | has velo_max => ( | ||
| 224 | is => 'rw', | ||
| 225 | isa => sub { croak "$_[0] is not valid" unless $_[0] =~ /^\d+$/ }, | ||
| 226 | default => sub { 10 }, | ||
| 227 | ); | ||
| 228 | |||
| 229 | |||
| 230 | has velo_min => ( | ||
| 231 | is => 'rw', | ||
| 232 | isa => sub { croak "$_[0] is not valid" unless $_[0] =~ /^-?\d+$/ }, | ||
| 233 | default => sub { -10 }, | ||
| 234 | ); | ||
| 235 | |||
| 236 | |||
| 237 | has velo_off => ( | ||
| 238 | is => 'rw', | ||
| 239 | isa => sub { croak "$_[0] is not valid" unless $_[0] =~ /^\d+$/ }, | ||
| 240 | default => sub { 110 }, | ||
| 241 | ); | ||
| 242 | |||
| 243 | |||
| 244 | has verbose => ( | ||
| 245 | is => 'ro', | ||
| 246 | isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ }, | ||
| 247 | default => sub { 0 }, | ||
| 248 | ); | ||
| 249 | |||
| 250 | has _queue => ( | ||
| 251 | is => 'rw', | ||
| 252 | default => sub { [] }, | ||
| 253 | ); | ||
| 254 | |||
| 255 | has _midi_out => ( | ||
| 256 | is => 'lazy', | ||
| 257 | builder => '_build__midi_out', | ||
| 258 | ); | ||
| 259 | sub _build__midi_out { | ||
| 260 | my ($self) = @_; | ||
| 261 | my $midi_out = RtMidiOut->new; | ||
| 262 | try { # this will die on windows but is needed on the mac | ||
| 263 | $midi_out->open_virtual_port('RtMidiOut'); | ||
| 264 | } | ||
| 265 | catch ($e) {} | ||
| 266 | my $name = $self->port_name; | ||
| 267 | try { | ||
| 268 | $midi_out->open_port_by_name(qr/\Q$name/i); | ||
| 269 | } | ||
| 270 | catch ($e) { | ||
| 271 | die "Can't open MIDI port: $name"; | ||
| 272 | } | ||
| 273 | return $midi_out; | ||
| 274 | } | ||
| 275 | |||
| 276 | has _interval => ( | ||
| 277 | is => 'lazy', | ||
| 278 | builder => '_build__interval', | ||
| 279 | ); | ||
| 280 | sub _build__interval { | ||
| 281 | my ($self) = @_; | ||
| 282 | return 60 / $self->bpm / $self->ppqn; | ||
| 283 | } | ||
| 284 | |||
| 285 | has _nth => ( # clocks per 16th-note | ||
| 286 | is => 'lazy', | ||
| 287 | builder => '_build__nth', | ||
| 288 | ); | ||
| 289 | sub _build__nth { | ||
| 290 | my ($self) = @_; | ||
| 291 | return $self->ppqn / $self->divisions; | ||
| 292 | } | ||
| 293 | |||
| 294 | # keep track of things | ||
| 295 | my %attrs = ( | ||
| 296 | rw => { | ||
| 297 | _ticks => 0, # how many clock ticks? | ||
| 298 | _beat_count => 0, # how many beats? | ||
| 299 | _bar_count => 0, # how many measures? | ||
| 300 | _hats => 0, # 1st hihat beat bit | ||
| 301 | _part_inc => 0, # number of next_part | ||
| 302 | _filled => 0, # we just filled | ||
| 303 | }, | ||
| 304 | ); | ||
| 305 | for my $is (keys %attrs) { | ||
| 306 | for my $attr (keys $attrs{$is}->%*) { | ||
| 307 | has $attr => ( | ||
| 308 | is => $is, | ||
| 309 | default => sub { $attrs{$is}{$attr} }, | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | has _loop => ( | ||
| 315 | is => 'ro', | ||
| 316 | default => sub { IO::Async::Loop->new }, | ||
| 317 | ); | ||
| 318 | |||
| 319 | |||
| 320 | sub BUILD { | ||
| 321 | my ($self, $args) = @_; | ||
| 322 | |||
| 323 | $SIG{INT} = sub { | ||
| 324 | say "\nStop" if $self->verbose; | ||
| 325 | try { | ||
| 326 | $self->_midi_out->stop; | ||
| 327 | $self->_midi_out->panic; | ||
| 328 | } | ||
| 329 | catch ($e) { | ||
| 330 | warn "Can't halt the MIDI out device: $e\n"; | ||
| 331 | } | ||
| 332 | if ($self->save) { | ||
| 333 | $self->score->write_score($self->save); | ||
| 334 | say 'Saved score to ' . $self->save if $self->verbose; | ||
| 335 | } | ||
| 336 | exit; | ||
| 337 | }; | ||
| 338 | |||
| 339 | # any drums to add? | ||
| 340 | my $n = keys $self->drums->%*; | ||
| 341 | for my $drum ($args->{add_drums}->@*) { | ||
| 342 | my $chan = defined $drum->{chan} ? $drum->{chan} : $self->chan < 0 ? $n++ : $self->chan; | ||
| 343 | $self->drums->{ $drum->{drum} } = { num => $drum->{num}, chan => $chan, pat => [] }; | ||
| 344 | } | ||
| 345 | |||
| 346 | my $timer = IO::Async::Timer::Periodic->new( | ||
| 347 | interval => $self->_interval, | ||
| 348 | on_tick => sub { | ||
| 349 | $self->_midi_out->clock; # send a clock tick | ||
| 350 | $self->_ticks($self->_ticks + 1); | ||
| 351 | |||
| 352 | if ($self->_ticks % $self->_nth == 0) { | ||
| 353 | my $phrase = $self->beats * $self->bars; # steps in a phrase | ||
| 354 | my $pos = $self->_beat_count % $phrase; | ||
| 355 | # ugly gymnastics - ugh | ||
| 356 | if (($self->filling || (ref($self->next_part) eq 'ARRAY' && $self->next_part->[ $self->_part_inc % $self->next_part->@* ] =~ /fill/)) | ||
| 357 | && $self->bars > 1 | ||
| 358 | && $pos == $phrase - $self->beats # the last bar of the phrase | ||
| 359 | ) { | ||
| 360 | $self->_adjust_drums(1); # fill! | ||
| 361 | $self->_filled($self->_filled + 1); | ||
| 362 | } | ||
| 363 | if ($pos == 0) { | ||
| 364 | $self->_adjust_drums(0); # normal part | ||
| 365 | } | ||
| 366 | my @hits; # for the score, if saving | ||
| 367 | for my $drum (keys $self->drums->%*) { # fill the queue | ||
| 368 | if ($self->drums->{$drum}{pat}[ $self->_beat_count % scalar($self->drums->{$drum}{pat}->@*) ]) { | ||
| 369 | push $self->_queue->@*, { drum => $drum, velocity => $self->velocity }; | ||
| 370 | push @hits, $self->drums->{$drum}{num} if $self->save; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | if ($self->save) { | ||
| 374 | $self->score->Volume($self->velocity); | ||
| 375 | if (@hits) { | ||
| 376 | $self->score->n('sn', @hits); # chord = simultaneous drum hits | ||
| 377 | } | ||
| 378 | else { | ||
| 379 | $self->score->r('sn'); | ||
| 380 | } | ||
| 381 | } | ||
| 382 | for my $drum ($self->_queue->@*) { # play the queue | ||
| 383 | $self->_midi_out->note_on( | ||
| 384 | $self->drums->{ $drum->{drum} }{chan}, | ||
| 385 | $self->drums->{ $drum->{drum} }{num}, | ||
| 386 | $drum->{velocity} | ||
| 387 | ); | ||
| 388 | } | ||
| 389 | $self->_beat_count($self->_beat_count + 1); | ||
| 390 | } | ||
| 391 | else { # drain the queue | ||
| 392 | while (my $drum = pop $self->_queue->@*) { | ||
| 393 | $self->_midi_out->note_off( | ||
| 394 | $self->drums->{ $drum->{drum} }{chan}, | ||
| 395 | $self->drums->{ $drum->{drum} }{num}, | ||
| 396 | 0 | ||
| 397 | ); | ||
| 398 | } | ||
| 399 | } | ||
| 400 | if ($self->_ticks % ($self->ppqn * $self->divisions) == 0) { | ||
| 401 | $self->_bar_count($self->_bar_count + 1); | ||
| 402 | } | ||
| 403 | }, | ||
| 404 | ); | ||
| 405 | $timer->start; | ||
| 406 | |||
| 407 | $self->_loop->add($timer); | ||
| 408 | $self->_loop->run; | ||
| 409 | } | ||
| 410 | |||
| 411 | sub _adjust_cymbals($self) { | ||
| 412 | if ($self->fill_crash) { | ||
| 413 | if ($self->_filled) { | ||
| 414 | $self->drums->{fillcrash}{pat}[0] = 1; # crash on one | ||
| 415 | $self->drums->{closed}{pat}[0] = 0; # mutually exclusive | ||
| 416 | } | ||
| 417 | else { | ||
| 418 | $self->drums->{fillcrash}{pat}[0] = 0; # not crashing | ||
| 419 | $self->drums->{closed}{pat}[0] = $self->_hats; # restore hihat bit | ||
| 420 | } | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | # resolve a part spec to a single part name | ||
| 425 | sub _choose_part($self, $spec) { | ||
| 426 | return $spec unless ref $spec eq 'HASH'; | ||
| 427 | my @names = sort { $a cmp $b } grep { $spec->{$_} > 0 } keys %$spec; | ||
| 428 | croak 'No positive weighted part to choose' unless @names; | ||
| 429 | return choose_weighted(\@names, [ $spec->@{@names} ]); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _adjust_drums($self, $fill_flag) { | ||
| 433 | say 'Beats: ' . $self->_beat_count if $self->verbose; | ||
| 434 | my ($next, $patterns, $part); | ||
| 435 | my $name = ''; | ||
| 436 | if (ref $self->next_part eq 'ARRAY') { | ||
| 437 | $name = $self->next_part->[ $self->_part_inc % $self->next_part->@* ]; | ||
| 438 | $part = $self->parts->{$name}; | ||
| 439 | } | ||
| 440 | # play a fill or a part | ||
| 441 | if (($self->filling || $name =~ /fill/) && $fill_flag) { | ||
| 442 | $part ||= $self->fills->{ $self->next_fill }; | ||
| 443 | ($next, $patterns) = $part->(); | ||
| 444 | if ($next) { | ||
| 445 | $self->next_fill($next); | ||
| 446 | } | ||
| 447 | else { | ||
| 448 | $self->_part_inc($self->_part_inc + 1); | ||
| 449 | } | ||
| 450 | } | ||
| 451 | else { | ||
| 452 | $part ||= do { | ||
| 453 | my $part_name = $self->_choose_part($self->next_part); | ||
| 454 | $self->parts->{$part_name} // croak "Unknown part: $part_name"; | ||
| 455 | }; | ||
| 456 | ($next, $patterns) = $part->(); | ||
| 457 | if ($next) { | ||
| 458 | $self->next_part($next); | ||
| 459 | } | ||
| 460 | else { | ||
| 461 | $self->_part_inc($self->_part_inc + 1); | ||
| 462 | } | ||
| 463 | } | ||
| 464 | # add the patterns to the drums | ||
| 465 | for my $drum (keys %$patterns) { | ||
| 466 | $self->drums->{$drum}{pat} = $patterns->{$drum}; | ||
| 467 | } | ||
| 468 | # add zero-patterns to the unused drums | ||
| 469 | for my $drum (keys $self->drums->%*) { | ||
| 470 | unless (exists $patterns->{$drum}) { | ||
| 471 | $self->drums->{$drum}{pat} = [ (0) x $self->beats ]; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | if ($self->filling) { | ||
| 475 | $self->_hats($self->drums->{closed}{pat}[0]); # save bit | ||
| 476 | $self->drums->{fillcrash}{pat} = [ (0) x ($self->beats * $self->bars) ]; | ||
| 477 | $self->_adjust_cymbals; | ||
| 478 | $self->_filled(0); | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | sub _default_part($self) { | ||
| 483 | say '_default_part' if $self->verbose; | ||
| 484 | my %patterns = ( | ||
| 485 | closed => [qw(1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0)], | ||
| 486 | kick => [qw(1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0)], | ||
| 487 | snare => [qw(0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0)], | ||
| 488 | ); | ||
| 489 | my $next = '_default_part'; | ||
| 490 | return $next, \%patterns; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub _default_fill($self) { | ||
| 494 | say '_default_fill' if $self->verbose; | ||
| 495 | my $size = rand() < 0.5 ? $self->divisions / 2 : $self->divisions; | ||
| 496 | say "size: $size" if $self->verbose; | ||
| 497 | my %durations = ( | ||
| 498 | sn => [1], | ||
| 499 | en => [1,0], | ||
| 500 | qn => [1,0,0,0], | ||
| 501 | ); | ||
| 502 | my $mdp = Music::Duration::Partition->new( | ||
| 503 | size => $self->divisions, | ||
| 504 | pool => [qw(qn en sn)], | ||
| 505 | weights => [1, 2, 1], | ||
| 506 | groups => [0, 0, 2], | ||
| 507 | ); | ||
| 508 | my $motif = $mdp->motif; | ||
| 509 | my @converted = map { $durations{$_}->@* } @$motif; | ||
| 510 | my %patterns; | ||
| 511 | if ($size < $self->divisions) { | ||
| 512 | my $div = $self->beats / $size; | ||
| 513 | my ($next, $pats) = $self->prefill_part->($self); | ||
| 514 | for my $drum (keys $self->drums->%*) { | ||
| 515 | if ($drum eq 'snare') { | ||
| 516 | $patterns{$drum} = [ $pats->{$drum}->@[0 .. $div - 1], @converted[0 .. $div - 1] ] | ||
| 517 | } | ||
| 518 | else { | ||
| 519 | $patterns{$drum} = [ $pats->{$drum}->@[0 .. $div - 1], (0) x $div ]; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | } | ||
| 523 | else { | ||
| 524 | for my $drum (keys $self->drums->%*) { | ||
| 525 | if ($drum eq 'snare') { | ||
| 526 | $patterns{$drum} = \@converted; | ||
| 527 | } | ||
| 528 | else { | ||
| 529 | $patterns{$drum} = [ (0) x $self->beats ]; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | } | ||
| 533 | my $next = '_default_fill'; | ||
| 534 | return $next, \%patterns; | ||
| 535 | } | ||
| 536 | |||
| 537 | |||
| 538 | sub write_score { | ||
| 539 | my ($self, $filename) = @_; | ||
| 540 | $filename //= $self->save; | ||
| 541 | croak 'No filename given and no save attribute set' unless $filename; | ||
| 542 | $self->score->write_score($filename); | ||
| 543 | } | ||
| 544 | |||
| 545 | |||
| 546 | sub velocity($self) { | ||
| 547 | my $random = $self->velo_off + int(rand($self->velo_max - $self->velo_min + 1)) + $self->velo_min; | ||
| 548 | return $random; | ||
| 549 | } | ||
| 550 | |||
| 551 | 1; | ||
| 552 | |||
| 553 | __END__ | ||