CCRMA

Music 220b: Lecture 4 Slides


State Machines

A state machine remembers a "state" (a memory of where it is in its internal process) and depending on whar state it is in, it calculates outputs that are determined by the current inputs and then transitions to a different (or the same) state. We could specify a state machine by a connected graph. States are represented by circles. Directed arrows specify the transitions between states. Each transition is determined by the "current state" (that is, the circle it originates in) and by the input conditions. As a side effect of the state transition the state machine defines new output values.

A very simple state machine could be defined by the following lisp code:

(defparameter h-state t)

(defun harmonize (input)
  (if h-state
      ;; up by a fifth
      (progn
	(setf h-state nil)
	(+ input 7))
    ;; up by a fourth
    (progn
      (setf h-state t)
      (+ input 5))))

h-state is a global variable that remembers the current state of the state machine. It can have two values, t or nil (ie: this state machine has only two state). The function definition is simple, depending on h-state is executes two branches of code, each one calculates and returns an output value based on the input to the state machine and the current state. It then transitions to the other state.

(with-sound()
  (loop
    repeat 10
    for time from 0 by 0.2
    for note = 60 then (harmonize note)
    do
    (fm-violin time 0.16 (pitch (if (> note 128) 128 note)) 0.1)))

See statemachine.lisp for ready to run examples...

Each state within the state machine has to define what to do with the outputs and how to transition to the next state. So far we have only seen a completely deterministic behavior in our state machine.

What if some of the decisions were taken by stochastic (random) processes? The output of our state machine is no longer completely predictable. Those stochastic decisions could influence the transition between states of the generation of outputs from the current inputs to the state machine.

Probability distributions

A probability distribution is a table that dhows the likehood of occurrence of one of more events, where the probability of occurrence of a given event is expressed as a value between 0 and 1.

Random numbers... as normally generated in a computer are not really really random. They are the result of executing an algorithm, the output values can be considered statistically random if the algorithm is good enough. Normally the random number generator is seeded by a number, after that the numbers generated are predictable (each time we seed the generator with the same number it produces the same sequence of "random" numbers).

In common lisp we can generate random floating point values using the (random xxx) function, where xxx is a floating point number. If we need integers (for example to generate midi note numbers) then the argument to random should be an integer.

The example file probability.lisp has some examples of probability distributions (Note: you'll have to first compile and load plot.lisp to be able to plot the distributions)


©2000 Fernando Lopez-Lezcano. All Rights Reserved.
nando@ccrma.stanford.edu