markov2seq
markov2seq(
@matrix ## llll (required)
@maxlength 25
@start null
@autoclear 1
@useseed 0
@prepad '^'
@postpad '$'
) -> llll
Generates a sequence using a given Markov transition matrix. If a starting state is specified, the function attempts to find transitions from it. Otherwise, it selects a random state from the transition matrix. The function supports both 0th-order Markov chains (where each state is chosen independently with a probability distribution) and higher-order Markov chains (where state transitions depend on previous elements). If the sequence reaches the maximum length or no valid transitions are available, generation stops. See seq2markov
.
Arguments
@matrix
[llll]: Transition probability matrix. (required)@maxlength
[int]: Maximum length of the generated sequence. (default:25
).@start
[int]: Initial state for the sequence. Ifnull
, a random state is chosen. (default:null
).@autoclear
[int]: Allow sequence to automatically reset when no valid transitions exist. (default:1
).0
: Off1
: On
@useseed
[int]: Use seeded random function for persistent output (default:0
).0
: Off1
: On
@prepad
[symbol]: Padding symbol representing beginning of the sequence. (default:'^'
).@postpad
[symbol]: Padding symbol representing end of a sequence. (default:'$'
).
Output
Generated sequence of elements based on the given Markov transition matrix [llll]
Usage
$events = importmidi('bach.mid'); ## import MIDI file
## get pitch from each event
$pitches = for $e in $events collect $e.getkey('pitch');
## build 2nd-order Markov model from pitch sequence
$matrix = seq2markov(@sequence $pitches @order 2);
## generate pitch sequence of size 50 from Markov model
$pitchseq = markov2seq(@matrix $matrix @maxlength 50);
$t = 0;
## Transcribe pitches
for $pitch in $pitchseq do (
ezsampler(@pitch $pitch).transcribe($t);
$t += 120
);
render(@play 1)