markov applied to music? (was Re: markov.py)

Alex Martelli aleaxit at yahoo.com
Tue Jan 23 15:44:22 EST 2001


"Terry Hancock" <hancock at earthlink.net> wrote in message
news:mailman.980270667.10267.python-list at python.org...
    [snip]
> I know almost nothing about Markov chains (though I've seen the
> output before), but they were recommended to me as a possible
> way to generate more complex game music based on a static score:
    [snip]
> It does however, have different moods, and it seems that it might
> make sense to take a score with a gradually changing mood and use
> a sliding window to determine what portion of the score is used,
> thus allowing a "mood" parameter to control the output.  Whenever

This might work; it's worth trying to code it up and check if
it sounds decent.

> the mood changes, the markov chain generator would have to
> re-analyze the new window and start generating output.

Better to run the analysis 'off-line', earlier.  A transition will
be modeled as a gradual interpolation in time between one
and another transition-probability-matrix, with a time-varying
parameter T between 0 and 1; pseudocode might be:

def pickFrom(choice_probs):
    r = random.random()
    for i in range(len(choice_probs)-1):
        r -= choice_probs[i]
        if r<=0: break
    return outputAt(i)

def outputSteadyState(state, matrix):
    if matrix.has_key(state):
        choice_probs = matrix[state]
    else:
        choice_probs = defaultProbs(state)
    return pickFrom(choice_probs)

def outputTransition(state, matrix1, matrix2, lambda):
    if matrix1.has_key(state):
        if matrix2.has_key(state):
            c1 = matrix1[state]
            c2 = matrix2[state]
            choice_probs = [a*lambda+b*(1-lambda)
                for a, b in zip(c1,c2)]
            return pickFrom(choice_probs)
        else: return outputSteadyState(state, matrix1)
    else: return outputSteadyState(state, matrix2)

This assumes sparse matrices for the state->probabilities
correspondences and dense ones for the probability
(density) vectors themselves (rather than cumulative);
much optimization may of course be possible.

> I presume the Markov algorithm would take care of the transition
> problem, so that it doesn't just sound like random clips all
> hashed together.

That's a reasonable hope, which needs to be tested.

> Would it be feasible to use the markov.py module to do this sort
> of thing?

I don't think it explicitly stores/fetches transition probability
tables, which would be advisable here (hard to interpolate
during transitions otherwise); so, while it will surely repay
some study, you'll probably be better off redoing it from
scratch according to your specific needs.  It's not rocket
science anyway, since you only need to simulate output
rather than any of the hard Markov stuff (recognizing &c
input sources by working backwards from observed output).


Alex






More information about the Python-list mailing list