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::Factory - some factory class for your music factory class

SYNOPSIS

  use Data::Dumper;
  use Music::Factory;

  # this could be put into a MIDI::Track at the end
  my $events = [];

  my $silence = Music::Factory::AssemblyLine->new(
      events => $events,
      gen    => Music::Factory::Rest->new,
      maxlen => 48,
  );

  my $short = Music::Factory::AssemblyLine->new(
      events => $events,
      gen    => Music::Factory::Note->new(
          duration => 96,
          pitch    => sub { 60 },
          velo     => sub { 96 },
      ),
      maxlen => 96,
  );

  my $long = Music::Factory::AssemblyLine->new(
      events => $events,
      gen    => Music::Factory::Note->new(
          duration => 192,
          pitch    => sub { 60 },
          velo     => sub { 96 },
      ),
      maxlen => 192,
  );

  sub dit { $_->update for $short, $silence }
  sub dah { $_->update for $long, $silence }

  dit; dit; dit;
  dah; dah; dah;
  dit; dit; dit;

  print Dumper $events;

See also the eg/ directory of this module's distribution, as well as the t/10-factory.t test script.

DESCRIPTION

This module offers various classes that may assist with music composition, notably an AssemblyLine that calls various generator classes repeatedly until a section is filled to a particular length. The generators might return MIDI events, or anything that fits into an array reference: lilypond notation, or who knows what.

Generators are assumed to be well behaved; a misbehaved generator could return a negative duration, or invalid MIDI events, or even MIDI events whose durations do not match the overall duration returned. This may be a feature in music composition, or a source of annoying bugs. Buyer beware!

Generators need not honor the length of the section; a section could be truncated (or overflow) the maximum duration. Whether this makes sense or needs to be guarded against will depend on the composition.

The caller will likely need to write generators of their own: the ones included with this module are not very interesting. How the generators are mixed together is also left as an exercise to the reader. See again the eg/ directory.

BUGS

None known.

However, the code isn't too complicated (or is complicated in a wrong way) and may need to be redone? The shape seems mostly okay (to me).

SEE ALSO

MIDI, Music::RhythmSet

COPYRIGHT AND LICENSE

Copyright 2023 Jeremy Mates

This program is distributed under the (Revised) BSD License: https://opensource.org/licenses/BSD-3-Clause