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

NAME

Music::Canon - routines for musical canon construction

SYNOPSIS

  use Music::Canon;
  my $mc = Music::Canon->new;

  # options affecting all the *_map routines
  $mc->set_contrary(1);
  $mc->set_retrograde(1);
  $mc->set_transpose(12);     # by semitones
  $mc->set_transpose(q{c'});  # or "to" a lilypond note

  # 1:1 semitone mapping
  my @phrase = $mc->exact_map(qw/0 7 4 0 -1 0/);
  $mc->exact_map_reset;

  # trickier is the so-called modal mapping
  # default is Major to Major (or call set_scale_intervals first)
  @phrase = $mc->modal_map(qw/0 7 4 0 -1 0/);
  $mc->modal_map_reset;

  # or instead modal mapping by scale name (via Music::Scales)
  $mc->set_scale_intervals( 'input',  'minor'  );
  $mc->set_scale_intervals( 'output', 'dorian' );

And more!

DESCRIPTION

Musical canons involve horizontal lines of music (often called voices) that are combined with other canon or free counterpoint voices to produce harmony. This module assists with the creation of new voices. Whether or not the output is usable is left to the composer. Harmony can be created by careful selection of the input material and the mapping settings, or perhaps by adding a free counterpoint voice to support the canon voices. Analyzing the results with Music::Tension may help search for suitable material.

The methods of this module suit crab canon, as those lines are relatively easy to calculate. Other forms of canon would ideally require a counterpoint module, which has not yet been written.

Knowledge of canon will doubtless help any user of this module; the "SEE ALSO" section lists resources for learning these.

METHODS

Methods may die or croak under various conditions. new would be a good one to start with, then one of the *_map functions to transform the list of pitches into new material.

exact_map phrase

One-to-one semitone mapping from the input phrase to the returned list. phrase may be a list or an array reference, and may contain raw pitch numbers, objects that support a pitch method, or other data that will be passed through unchanged.

Affected by various settings, notably set_contrary, set_retrograde, and set_transpose.

Be sure to call exact_map_reset when done converting a phrase, or disable the keep_state option of new and then pass the phrase in a single call to exact_map.

exact_map_reset

Resets current state of the exact_map method. Not necessary if keep_state option of new disabled, and entire phrases passed in one go to exact_map.

Returns the Music::Canon object, so can be chained with other method calls.

get_contrary

Returns the current contrary setting (boolean).

get_modal_pitches

Returns the current modal input and output layer starting pitches (these will be undefined if unset).

get_retrograde

Returns the current retrograde setting (boolean). Retrograde is a fancy way to indicate that the output list be reversed.

get_scale_intervals layer

Returns the current scale intervals for the indicated layer (input or output), or throws an exception if these are unset. The intervals are returned as a list of two array references, the first for the scale ascending, the second for the scale descending.

Note that descending scale intervals are noted from the lowest note up, not highest note down.

get_transpose

Returns the current transpose setting (integer of semitones or lilypond note name, depending). Lilypond note names are preserved until necessary, as to transpose to a lilypond note name, one must have the pitch being transposed from, so that the delta between those two pitches can be derived. Otherwise, given an integer, the transpose is by that amount.

Modal mapping of the pitches in phrase from an arbitrary input mode to an arbitrary output mode, as set by set_scale_intervals. Returns a list, or throws an exception if a pitch cannot be converted. phrase may be a list or an array reference, and may contain raw pitch numbers, objects that support a pitch method, or other data that will be passed through unchanged.

The algorithm operates by converting the intervals between the notes into a number of diatonic steps in the input mode (and chromatic adjustment, if necessary), then replaying that many steps in the output (and chromatic adjustment, if necessary) mode, using the starting pitches of the input phrase (first note) and output phrase (first note of input phrase as affected by the transpose setting) as the linkage between the two modes.

An example may help illustrate this function. By default, the Major scale to the Major scale conversion is used, which, with other defaults set by this module, uses something like the following chart to convert notes:

  In  | C  | c# | D | d# | E | F | f# | G | g# | A | a# | B | C' |
  Out | C' | x  | B | a# | A | G | f# | F | x  | E | d# | D | C  |

Assuming an input phrase of C G c#, the output phrase would be C' F and then an exception would be thrown, as there is no way to convert c# using this mapping and transposition. Other mappings and transpositions will have between zero to several notes that cannot be converted.

modal_map is affected by various settings, notably set_contrary, set_retrograde, set_scale_intervals, and set_transpose.

Be sure to call modal_map_reset when done converting a phrase.

Resets the state variables associated with modal_map.

Returns the Music::Canon object, so can be chained with other method calls.

new

Constructor. Accepts a number of options, the useful or safe of which are listed here.

  • contrary - sets the contrary option. On by default.

  • input - scale or Forte Number or interval set for the modal_map input mode. Defaults to the Major scale if unset. See set_scale_intervals and modal_map for details.

  • keep_state - configures whether state is maintained through different calls to the various *_map methods. On by default, which will require the use of the corresponding *_map_reset methods when a phrase is complete. There are two possible workflows; with state enabled, multiple calls can be made to the mapping function, which suits modal_map and the need to handle individual exceptions should a pitch produce an undefined conversion:

      my $mc_state = Music::Canon->new;
      for my $e (@input) {
        my $result;
        eval { $result = $mc_state->modal_map($e) };
        if ($@ and $@ =~ m/undefined chromatic conversion/) {
          $result = 'r';   # make it a lilypond rest
        }
        push @output, $result;
      }
      $mc_state->modal_map_reset;

    The other workflow is to disable state, and pass entire phrases for conversion in one go. This better suits exact_map, which unlike the modal transformation will not have pitches it cannot convert:

      my $mc_no_state = Music::Canon->new(keep_state => 0);
      my @output = $mc_no_state->exact_map(\@input);
  • non_octave_scales - configures whether scales should be bounded at an octave (12 semitones) or not. The default is to complete interval sets that sum up to less than 12 to include an additional element such that the sum of the intervals is 12. Interval sets greater than 12 will cause an exception to be thrown.

    Enable this option only if dealing with a maqam or similar scale that is not bounded by the Western notion of octave.

  • output - scale or Forte Number or interval set for the modal_map output mode. Defaults to the Major scale if unset. See set_scale_intervals and modal_map for details.

  • retrograde - sets whether phrases are reversed. On by default.

  • transpose - value to transpose by, in semitones, or "to" a lilypond note name.

set_contrary boolean

Sets the contrary boolean (on by default). With this set, phrases from the *_map routines will be set in contrary motion to the input phrase.

Returns the Music::Canon object, so can be chained with other method calls.

set_modal_pitches input_start_pitch, output_start_pitch

Sets the starting pitches used for the modal_map conversion. These by default are derived from the first pitch passed to modal_map and the transpose value; this method allows these pitches to be customized to some other value.

  $mc->set_modal_pitches(60, 62);
  $mc->set_modal_pitches(undef, 64);  # just output start pitch
  $mc->set_modal_pitches(q{c'});      # by lilypond note

Returns the Music::Canon object, so can be chained with other method calls.

set_retrograde boolean

Sets the retrograde boolean (on by default). If set, phrases from the *_map routines will be reversed.

Returns the Music::Canon object, so can be chained with other method calls.

set_scale_intervals layer, asc, [dsc]

Sets the scale intervals for the indicated layer (input or output). The asc (and optional dsc) can be a number of different things:

  $mc->set_scale_intervals('input', 'minor');  # Music::Scales
  $mc->set_scale_intervals('input', '7-23');   # Forte Number
  # arbitrary interval sequence
  $mc->set_scale_intervals('input', [qw/2 1 3 2 1 3 1/]);

If the dsc is undefined, the corresponding asc intervals will be used, except for Music::Scales, for which the descending intervals associated with the ascending scale will be used.

NOTE that the descending intervals must be ordered from the lowest pitch up. That is, melodic minor can be stated manually via:

  $mc->set_scale_intervals( 'output',
    [2,1,2,2,2,2],  # asc - c d ees f g a   b
    [2,1,2,2,1,2]   # dsc - c d ees f g aes bes
  );

Though this particular case would be much more easily stated via Music::Scales via:

  $mc->set_scale_intervals('output', 'mm');

set_scale_intervals returns the Music::Canon object, so can be chained with other method calls.

set_transpose integer or lilypond note

Sets the value to transpose to or by in *_map methods, either in semitones, or to a particular lilypond note:

  $mc->set_transpose(-12)    # down by an octave
  $mc->set_transpose(q{c'})  # to the lilypond note

Returns the Music::Canon object, so can be chained with other method calls.

SEE ALSO

"Counterpoint in Composition" by Felix Salzer and Carl Schachter.

"The Technique of Canon" by Hugo Norden

"Counterpointer" by Ars Nova (training software).

http://en.wikipedia.org/wiki/Forte_number

Music::AtonalUtil, Music::LilyPondUtil, Music::Scales, Music::Tension

The scalemogrifier utility of App::MusicTools may also be of interest.

AUTHOR

Jeremy Mates, <jmates@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2013 by Jeremy Mates

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16 or, at your option, any later version of Perl 5 you may have available.