The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Music::Duration::Partition - Partition a musical duration into rhythmic phrases

VERSION

version 0.0512

SYNOPSIS

  use MIDI::Simple;
  use Music::Duration::Partition;
  use Music::Scales;

  my $mdp = Music::Duration::Partition->new(
    size    => 8,
    pool    => [qw/ qn en sn /],
    weights => [ 0.2, 0.3, 0.5 ], # Optional
  );

  $mdp->pool_select( sub { ... } ); # Optional

  my $motif = $mdp->motif;

  my @scale = get_scale_MIDI( 'C', 4, 'major' );

  my $score = MIDI::Simple->new_score;

  for my $n ( 0 .. 31 ) { # 4 loops over the motif
    $score->n( $motif->[$n % @$motif], $scale[int rand @scale] );
  }

  $score->write_score('motif.mid');

  # The size and pool may also be made of MIDI durations
  $mdp = Music::Duration::Partition->new(
    size => 100,
    pool => [qw/ d50 d25 /],
  );

DESCRIPTION

Music::Duration::Partition partitions a musical duration into rhythmic phrases, given by the size, into smaller durations drawn from the pool of possibly weighted durations.

ATTRIBUTES

durations

  $durations = $mdp->durations;

A hash reference of duration lengths (keyed by duration name).

Default: \%MIDI::Simple::Length

size

  $size = $mdp->size;

The value, in quarter notes, of the duration to partition.

Default: 4

pool

  $pool = $mdp->pool;

The list of possible note durations to use in constructing a rhythmic motif.

Default: [ keys %MIDI::Simple::Length ] (wn, hn, qn, ...)

This can be either a list of duration names, as in the default example, or duration values, specified with a preceding 'd'. A mixture of both is not well defined. YMMV

pool_select

  $code = $mdp->pool_select->();
  $mdp->pool_select( sub { ... } );

A code reference used to select an item from the given duration pool.

Default: Random item from pool

weights

  $weights = $mdp->weights;

Specification of the frequency of pool item selection.

The number of weights must equal the number of pool entries. The weights do not have to sum to 1 and can be any relative numbers.

Default: Equal probability for each pool entry

verbose

  $verbose = $mdp->verbose;

Show the progress of the motif method.

Default: 0

METHODS

new

  $mdp = Music::Duration::Partition->new(%arguments);

Create a new Music::Duration::Partition object.

motif

  $motif = $mdp->motif;

Generate a rhythmic phrase of the given size.

This method returns a possibly different rhythmic motif each time it is called.

The default pool_select used constructs this by selecting a pool duration at random, that fits into the size remaining after each application, in a loop until the size is met.

SEE ALSO

The eg/* and t/01-methods.t programs in this distribution.

List::Util

Math::Random::Discrete

MIDI::Simple

Moo

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by Gene Boggs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.