Examples / Processing Examples / Processing Libraries / Random Synthesizer

Random Synthesizer

example processing library

This code uses the Minim library, which makes it possible to play sounds from Processing.

Looking at Minim’s documentation, I found the AudioPlayer#playNote() function, which allows playing of a note at a specific frequency.

Now that we have that function, we can feed it random values to create a random song.

import ddf.minim.Minim;
import ddf.minim.AudioOutput;

AudioOutput out;

void setup() {
  size(300, 100);

  Minim minim = new Minim(this);
  out = minim.getLineOut();

  frameRate(4);
}

void draw() {

  float note = random(1000);

  out.playNote(note);

  stroke(random(256), random(256), random(256));
  line(width*note/1000, 0, width*note/1000, height);
}

The result is a random “song” that sounds like this:

Oh and then just for fun, I’m drawing a randomly colored line with an X value based on the frequency of the note.

lines

Tweak Ideas

  • Make it so the notes speed up over time.
  • Move the random note playing into the keyPressed() function to turn your keyboard into a synthesizer. Or play a note when the user clicks, based on the mouse position.
  • Instead of playing random notes, create an “audio random walker” that plays notes based on a value that you change over time.
  • Use the noise() function instead of playing random notes.

Comments and Questions

Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!