Random Synthesizer



Random Synthesizer


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.

Libraries Examples

Comments

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

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!