Andy Hulstkamp

about creating online experiences

15. September 2008

From cacophony to music - generative music

In this post I was experimenting with waveforms. Playing sounds constantly on the same pitch gets a bit boring after a while, however simply introducing random pitch isn't too charming either. I started to twiddle a bit with evolving music.

goal:

  • create a player that plays a bearable evolving tune

constraints:

  • player <5k
  • no samples
  • 3 voices
  • stop experimenting after 3 hours before loosing myself

First, get some decent frequencies. Notes on a western chromatic scale are related, so introducing a frequency table is simple:

//BASE_FREQ = 65 or 110 or 220 or whatever
private function buildNotetable():void {
    for (var i:int = 0; i < noteTable.length; i++) {
        noteTable[i] = int (BASE_FREQ * Math.pow(1.05946, i));
    }
}

Having all the semitones in it doesn’t really help. A lookup table of ‘valid’ notes gives better results. I decided, that mainly thirds, fifths, sevenths etc. (notes that fit well to the keynote) should be played.

Sequence of notes should somehow sound ‘related’, thus I used perlin noise over randomly selecting notes. I used one channel of perlin noise for each voice. This gives an improvement to simply play a sequence of notes randomly but the outcome is still too nervous.

To calm things down I merged voices 2+3 and let voice 2 modulate voice 3, giving a more interesting sound. There’s now a clear lead and a lower line. Still, progression of the lower line was to aggressive. It's better when the lower line changes at only int*4 bars or so. Additionally, the whole scale is transposed by a few semitones once in a while.

Well then, did it work? Not really, although there is an improvement to random music and there are some transitions that sound promising, it still is cacophony. As expected, introducing perlin noise and constrain the selection of notes is not enough. Some other means need to be established.

If you want to listen to the result or compare random play to the more structured approach here’s a simple example. Turn your volume down first. Better A and Better B use different settings for the frequency of the perlin noise. The seed is set to a new value each time you toggle a button.

Flash Generative Sound

DEMO

I think Better B generates the most bearable tunes. Click here for the doodle tune. Needs the latest Flash-Player 10 (rc of 091508).

Here’s the source. UPDATE: Adobe changed the sound api. Use SampleDataEvent.SAMPLE_DATA instead of Event.SAMPLE_DATA for the listener.

further reading

Generating Sounds. Waveforms, Timbre Loundness and Pitch.

Introduction to generating sounds covering samplerates, waveform, timbres and pitch. In Flash but should be applicable to any technology.

Flash Pixelshifting Session

Using particles for some special effect. Type any word and blow it up in pixels.