Random Piano

Random Piano

October 30, 2016

example processing library

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

First I downloaded a bunch of piano notes from freesound.org, and I added them to my sketch as files named 0.mp3, 1.mp3, 2.mp3, etc.

Then I looked at Minim’s documentation to find the code that would load and play the sound files.

import ddf.minim.Minim;
import ddf.minim.AudioPlayer;

Minim minim;
AudioPlayer[] notes = new AudioPlayer[73];

void setup()
  minim = new Minim(this);

  for (int i = 0; i < notes.length; i++) {
    notes[i] = minim.loadFile("notes/" + i + ".mp3");


void draw(){


  int i = int(random(notes.length));

I load the sound files into an array, and then I play a random note two times per second. The result is a random “song” that sounds like this:

The sketch, including the sound files, can be downloaded here.

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 piano.
  • Rearrange the sound files from low pitched to high pitched.

Libraries Examples


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!