The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Music::Duration::Partition::Tutorial::Quickstart

VERSION

version 0.0812

Setup

Import Modules

  use MIDI::Util qw(setup_score);
  use Music::Duration::Partition ();
  use Music::Scales qw(get_scale_MIDI);

Get a Score

  my $score = setup_score();

Instantiate an object

  my $mdp = Music::Duration::Partition->new(
      size      => 8,
      pool      => [qw(hn dqn qn en)],
      weights   => [   1, 1,  2, 1 ],
      groups    => [   1, 1,  1, 2 ],
      remainder => 1,
      verbose   => 1,
  );

Motifs

motif()

  my $a_motif = $mdp->motif;
  # e.g. ['qn','en','en','hn','qn','qn','dqn','en']

A "motif" is (usually) a short phrase that is used in the development or progression of a piece of music.

For this module, a motif is an ordered set of rhythmic durations.

motifs()

  my @motifs = $mdp->motifs(4);

Return a set of motifs.

Pitches == Notes == Voices

Get Some Pitches

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

Use the scale method to get a set of pitches in the key of C major, in the fourth octave.

Gather Voices at Random

  my @voices;

  for my $motif (@motifs) {
      my @notes;

      for my $i (@$motif) {
          push @notes, $pitches[ int rand @pitches ];
      }

      push @voices, \@notes;
  }

Here the voices to be played, are collected and correspond directly to motif entries.

Finish

add_to_score()

  for my $i (1 .. 4) {
      for my $n (0 .. $#motifs) {
          $mdp->add_to_score($score, $motifs[$n], $voices[$n]);
      }
  }

Loop over the indices of the motifs and voices, adding each to the score, repeated four times.

And Write

  $score->write_score('duration-partition.mid');

Write the score out to a MIDI file.

MIDI Playback

Use the venerable command-line program, timidity++, which is likely available through your package manager (e.g. apt, homebrew). You can also use the excellent, cross platform program, VLC.

  > timidity duration-partition.mid

Easy!

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019-2023 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.