Articles Archive
Articles Search
Director Wiki
 

Using Lingo to Create Brownian Noise

April 5, 2000
by Gary Rosenzweig

In 1827, English botanist Robert Brown discovered that very small pieces of pollen, when suspended in water, seem to move about randomly. It turns out that these pieces were being affected by the bombardment of molecules in the water. This later lead to lots of discoveries in chemistry and physics. The random motion of the particles also lead to the idea that music might be generated with random numbers. Computers then made this possible.

A Brownian Noise generator is a simple program that uses random numbers to make a musical composition. Instead of just picking a random number and playing a note that corresponds to it, the random number is used to determine the change in pitch of the next note. So, for instance, if we start with the note C, there might be a random chance of the next note being C sharp, or being B. There is even a chance it will stay the same.

Lingo programmers have had to look on while programmers in other languages create Brownian Noise generators. This is because we have never before had the ability to pitch shift sounds. In order to create a Brownian Noise generator, we have had to create an entire cast of music notes. With Director 8, we can finally use the undocumented rateShift property of a sound to create a variety of tones from one sound.

The generator itself starts with the note of rateShift 0. It then has a 25 percent chance of shifting this down one semitone, and a 25 percent chance of shifting it up one semitone. To make things interesting, there will also be a 10 percent chance of going up 2, a 10 percent chance of going down 2, a 5 percent chance of going up 3 and a 5 percent chance of going down 3. This leaves a 20 percent chance of not changing pitch at all. To keep things controlled, we will also put limits on how far the note can wander from the original note. We'll only let it go 12 semitones above or below.

To make sure that notes will never fight for control of the same sound channel, we loop through all eight sound channels, using one at a time. The timing of the playing of the notes will not be controlled by the behavior at all, but rather by the Score tempo. In the example movie, I have the Score tempo set to 2 fps, which means that notes will be played every half second. Here is the behavior that creates the noise:

property pNote, pChannel

on beginSprite me
  -- start with center note, first channel
  pNote = 0
  pLastChannel = 1
end

on playNewNote(me)
  
  -- pick a random number from 1 to 100
  r = random(100)
  
  -- determine from random number how much note should change
  if r <= 5 then
    d = -3
  else if r <= 15 then
    d = -2
  else if r <= 40 then
    d = -1
  else if r > 60 then
    d = 1
  else if r > 85 then
    d = 2
  else if r > 95 then
    d = 3
  end if
  
  -- change next note
  pNote = pNote + d
  
  -- keep note in 12 semitone range
  if pNote < -12 then pNote = -12
  else if pNote > 12 then pNote = 12
  
  -- use next channel
  channel = pChannel
  pChannel = pChannel + 1
  if pChannel > 8 then pChannel = 1
  
  -- play sound
  sound(pChannel).play([#member: member("tone"), #rateShift: pNote])
end

on exitFrame me
  playNewNote(me)
  go to the frame
end

No doubt you have already noticed the Brownian Noise generator in action while you have been reading this. Some people find it annoying, while others find it soothing. You can take the source file and play with the tempo as well as swap in a new sound to use for the tone. You can also play with the random number thresholds.

D8 download for Mac or Windows.

OK, so it's not the best music in the world. But it's not the worst either. And it has its advantages: it never repeats itself and it never asks for royalties.

Gary Rosenzweig's latest book is "Advanced Lingo for Games." In it, you can find the source code for more than 20 complete games. More information about the book can be found at http://clevermedia.com/resources/bookstore/book4.html. It can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.