Showing posts with label probabilities. Show all posts
Showing posts with label probabilities. Show all posts

Sunday, 16 May 2010

Algorithmic Composition: Markov Chains in Max MSP

We've looked at using Markov Chains for Algorithmic Composition in PureData, Markov Chains in Keykit and Markov Chains in OpenMusic in previous posts.

In today's Algorithmic Composition tutorial we'll look at using Markov Chains in Max MSP to analysis existing music and generate new algorithmic music.

We'll start off by recreating the Markov Chains in PureData example and then extend it with some new features. Creating the Markov Chain in Max is similar to composing in PureData, so if you haven't read through the Markov Chains in PureData post yet it's worth doing that now.

Saturday, 8 May 2010

Algorithmic Composition: Markov Chains in PureData

We've looked at Markov Chains in a few previous Algorithmic Composer tutorials including Markov Chains in Keykit and Markov Chains in OpenMusic. Today we'll be creating some algorithmic music by composing with Markov Chains in PureData.


Markov Chains choose the next state based on the current state and a set of probabilities. Mapped to pitch this would involve choosing our next note based on our current note and a list of possible next notes and their probabilities.

In today's tutorial we'll look at examples of Markov Chains and use them to generate pitches for a simple algorithmic composition.

Friday, 23 April 2010

OpenMusic Markov Chains and omlea

We've looked at using OpenMusic for algorithmic composition in a number of posts before, we've also looked at using the OpenMusic OMalea library and random walks in OpenMusic. Today we're going to use OpenMusic and OMalea to work with Markov Chains.

Markov Chains are a very useful tool for the algorithmic composer, we've looked at Markov Chains in keykit here. If you're not familiar with how Markov Chains work it's definitely worth reading this algorithmic composition post.

In brief, Markov Chains chose the next note using a probability. For example if the current note is E, there might be a 70% chance of the next note being a A and a 30% chance of it being an C#. These probabilities are stored in something called a transition table or transition matrix.

We can either create the transition matrix by hand or we can create it by analysing an existing piece of music (or any other piece of data).


This is the approach we'll take in today's post. We'll load up a MIDI file, create a transition matrix from this using the ana-mark function, and then generate new melodies based upon this.

1. First of all we need to load up a MIDI file. Create a new class MIDIfile, either by choosing from the menu or CMD clicking (CTRL click on PC) and typing midifile.



2. Evaluate the midifile class by hitting the 'v' key or CMD clicking on it's output (CTRL click on PC). This will allow you to load up a MIDI file, once it's loaded make sure the midifile class is selected and press b to lock it. This will ensure that you don't have to load the MIDIfile every time you evaluate the patch.

3. We next have to extract the pitch data from the MIDI file. We can also use markov chains to generate rhythm, dynamics and other musical information, but for now we'll stick with pitch. Add a mf-info function, this allows to extract each note from the MIDI file.

4. If you evaluate the mf-info function, you can see a list of lists that describe each note, something like this, where the values are pitch, onset, duration, velocity and MIDI channel
((64 1000 500 52 1) (73 1500 385 58 1) (74 1875 125 43 1) (62 2000 500 32 1))

5. We need to access just the first element of each set of parentheses: the pitch. There are a number of ways we can do this, but the simplest is to use the mat-trans function followed by the first function. Add these in one a time and evaluate them to see what they do. You patch should now look like this:


6. Now when we evaluate our patch we get a list of pitches. This can be fed into the ana-mark function (remember you need the omalea library enabled to be able to use this function - this can be turned on in preferences if it's not already).

The ana-mark function in OpenMusic automatically generates a Markov transition matrix for us of the form:
((list-of-pitches ((first-pitch-probabilities) (second-pitch-probabilities... ... (last-pitch-probabilities)))

Something like this:
((1 2 3) ((0.0 1.0 0.0) (0.333 0.0 0.667) (0.0 1.0 0.0)))

7. We need to be able to separate our list of pitches, the first element in the list [ (1 2 3) in the above example ] from the list of probabilities [the second element in the list]. For this we can use the Lisp functions first and second. You can type first and second directly into a new box to create them, or create them from the drop down menu. So far our patch looks like this:

8. We can now connect a Markov function. There are two Markov functions in the OpenMusic omalea library, markov1 generates a single next pitch and can be used with repeat-n to generate a series of pitches. Markov2 generates a series of pitches. Here we've added a markov2 function and asked it to create a series of 50 notes starting with the first note.

9. When you evaluate markov2 you'll notice that outputs the index rather than the actual pitch we entered. So if we were using notes 60, 64, 65 and 67 the markov2 function thinks of them as 1, 2, 3 and 4. We can easily map this index to our original list of pitches by using the posn-match function (position match). Your finished patch should look like this:
Now when you evaluate the patch, we generate a list of pitches based upon the original set of pitches in out MIDI file. To get a better understanding of what's happening at each stage, evaluate each function in turn by selecting and pressing v and looking at the output in the listener window. To get a better understanding of Markov Chains in general do have a read through a previous on Markov chains and algorithmic composition.

You can also try using the Markov chains to generate elements of your CSound scores as looked at in the post on algorithmic composition, OpenMusic and CSound - using the om2csound library.

Check back soon for more algorithmic composition techniques and tutorials.

Saturday, 17 April 2010

The OpenMusic OMalea library - Random Walk

In our last post we had a look at some of the functions in the omalea OpenMusic library.

In the last post we looked at mapping some different probability distributions to pitch. Using other omalea functions, we can explore a number of random walk functions to generate pitch sequences:

Random walks
Imagine a drunk walking home. When he reaches a junction he can turn left, right or carry straight on, being quite drunk he randomly chooses a direction, then he reaches another junction and repeats the process.

Using a random walk process for deciding pitches with algorithmic composition, we choose a start note and then randomly choose between the same note, a note above or a note below, we can usually choose how big a steps our 'drunk' can take.

These two random walks brownian1 and randwalk1 set a start point, upper and lower boundaries, a sequence length and a bandwidth or maximum step.

brownian1:

rand-walk1:

The layout of both examples if fairly straightforward with the random walk function and a rhythm-tree, feeding into a voice object.

In a future post we'll look at some of the remaining omalea functions, including using markov chains in OpenMusic.

Friday, 16 April 2010

Probabilities in OpenMusic using OMalea

We've introduced OpenMusic the computer-aided composition environment in a couple of previous algorithmic composition posts.

Today we'll have a look at using the OpenMusic library omalea to discuss the concept of probabilities and music. OpenMusic has a number of libraries that give additional functionality. Omalea (OM stands for OpenMusic and alea is Latin for dice. Aleatoric music uses chance as part of the compositional process. Both Pierre Boulez and John Cage have made use of aleatoric processes).

To enable the omalea library, go to preferences:

Check the omalea checkbox to load and enable the library:

In your workspace, create a new patch.
The omalea library offers four categories of functions: distributions, alea-seq, random-walk and tools.

Distributions
The distributions functions offer a range of ways of making aleatoric choices with different probability distributions.
The range of distributions includes linear (each possibility has the same chance of being selected e.g. RAN and RAN01), through choosing between 2 or a set a number of possibilities where you define their probability of being selected (CHOIX and CHOIXMULTIPLE), through to various triangle and various bell curve like distributions.

Choices (choix) - a coin flip' between two choices where you can adjust the probability. Here it's set to 50/50 chance of being note 5900 (B) and 6200 (D) - OpenMusic using MIDIcents to represent pitch so middle C (MIDI note 60) is 6000.

Poisson - a bell curve of normal distribution based around MIDI note 60.

A linear distribution.
When using the distribution functions to create pitches we had to perform a number of other operations to scale the numbers up and get appropriate values. OpenMusic uses MIDIcents to represent pitch so 6000 = MIDI note 60 or middle C.

If we're creating pitches, it's probably easy to use the alea-seq functions:


Not-centr chooses a random pitch, in this example around note 6000 +/- 12 semitones. It chooses only one value, here the repeat-n function has been used to generate a list of 8 randomly chosen notes:
Alea-seq works in a similar way, however as you can see in this example, it can generate lists of notes. Here we generate 8 notes, there's no need for the repeat-n function if using this object:
Linea-seq also produces a sequence of notes with a linear distribution. Using linea-seq you specify the number of notes and the upper and lower boundaries:


Triang-seq works in the same way to linea-seq, however it uses a triangle distribution. Notice how the notes are more centred around middle C:


Randomness, probabilities and chance are very useful tools for algorithmic composers. We'll look at these more in future posts in both OpenMusic and other software. We'll also have a look at some of the remaining omalea functions in future http://www.algorithmiccomposer.com/ posts and start to pull the tools together to create full compositions.