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

NAME

Music::RhythmSet::Util - pattern generation and classification functions

DESCRIPTION

Various functions related to the generation and classification of patterns of beats, and so forth. A pattern of beats is assumed to be an array reference of zeros and ones, e.g. for 4/4 time in 16th notes

  [ 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0 ]

Nothing is exported by default; the functions must be used fully qualified or by importing them on the use line.

FUNCTIONS

beatstring pattern

Converts a pattern such as [qw/1 0 1 0/] to a string such as x.x.. Opposite of pattern_from.

compare_onsets pattern1 pattern2

What percentage of onsets between two patterns are identical? Makes assumptions about the patterns which may not be true; ideally feed it patterns of the same length.

  compare_onsets([1,0,0,0],[1,0,1,0])
duration replay-log

Returns a list consisting of the number of measures and the total number of beats in those measures given a replay-log.

filter_pattern onsets total trials ...

Generates trials number of patterns via rand_onsets and selects for the "best" pattern by the lowest combined score of score_stddev and score_fourfour. This routine will need to be profiled and tuned for the need at hand; see the eg/variance script under this module's distribution for one way to study how the function behaves.

flatten replay-log

Flattens the given replay-log into a single array reference of beats.

ocvec pattern

Converts a pattern into "onset-coordinate vector" notation. This format is suitable to be fed to Music::AtonalUtil.

onset_count pattern

Returns a count of how many onsets there are in the pattern.

pattern_from string

Since version 0.02.

Converts a beat string such as x.x. into an array reference such as [qw/1 0 1 0/]. Opposite of beatstring.

It may be more sensible to use from_string in Music::RhythmSet::Voice or Music::RhythmSet especially if there are multiple patterns and TTL being parsed.

rand_onsets onsets total

Randomly turns on onsets in total beats and returns that as an array reference of zeros and ones. Will likely need to be filtered somehow to select for more usable results.

score_fourfour pattern

Fiddled with by hand so that a lower score is something closer to one opinion of 4/4 time in 16th notes. A (probably poor) attempt to select for patterns such as

  [ 1,0,0,0, 0,0,1,0, 1,0,0,0, 0,0,1,0 ]

and not the identical but rotated off-beat

  [ 0,0,0,0, 0,1,0,1, 0,0,0,0, 0,1,0,1 ]

Assumes the pattern is 16 beats in length.

score_stddev pattern

Standard deviation of the distances to the following onset; lower scores indicate higher regularity (non-clumping) of the onsets. However, you probably want a rhythm somewhere between the zero score

  [ 1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0 ]

and

  [ 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0 ]

(or the various rotations of the above) as the first is probably too regular and the second probably too irregular.

This method should work on patterns of any length (CPU and memory and user patience permitting).

upsize pattern new-length

Increases the size of the pattern to new-length which ideally should be a positive integer multiple of the current pattern length, possibly the "least common multiple" with some other pattern length:

  $ perl -MMath::BigInt -E 'say Math::BigInt->new(8)->blcm(6,7)'
  168

At some point it may be more useful to convert the onsets into "close enough" slots of at most 32 or 64 beats depending on the resolution desired, or to simply use measures of different lengths. I have not experimented with measures of different lengths over multiple voices so do not know what the problems will be.

Returns a new pattern.

write_midi filename track [ params ]

A small wrapper around MIDI::Opus that writes a MIDI track (or tracks) to a file. The optional params may include format and ticks (see the MIDI specification).

write_tracks template track [ params ]

As write_midi except that the individual track(s) are written to individual files. The template should contain a sprintf format that among other things contains a %d for the track number. The i parameter can be used to set the starting track number, 1 by default.

BUGS

None known.

SEE ALSO

Music::AtonalUtil has various relevant routines, especially for beat patterns of length 12.

"The Geometry of Musical Rhythm" by Godfried T. Toussaint.

Balanced parentheses (there are code problems for this) are somewhat musical and may be converted to rhythm via

  tr/()/10/;
  # and maybe also
  s/(?<=1)(1+)/"0" x length $1/eg;

COPYRIGHT AND LICENSE

Copyright 2021 Jeremy Mates

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