Notes Recorder



Notes Recorder


This code uses the Minim library to play and record random notes.

I use this as the Happy Coding “theme song” that plays at the beginning of Happy Coding videos!

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

int bps = 6;
int maxNote = 500;
int deltaNote = 100;

int note = 0;
boolean playing = true;
int stoppedPlayingTime;

AudioOutput out;
AudioRecorder recorder;

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

  Minim minim = new Minim(this);
  out = minim.getLineOut();
  recorder = minim.createRecorder(out, "notes.wav");

  frameRate(bps);

  recorder.beginRecord();
}

void draw() {
  if (playing) {
    // Move the note
    note += random(-deltaNote * .5, deltaNote);
    note = constrain(note, 0, maxNote);
    out.playNote(note);

    // Draw a line just for fun
    stroke(random(256), random(256), random(256));
    line(width * note / maxNote, 0, width * note / maxNote, height);

    // If the note gets too high, stop playing
    if (note >= maxNote) {
      playing = false;
      stoppedPlayingTime = millis();
    }
  }
  // Wait 1 second after stopping to record,
  // so the notes have time to fade out
  else if(millis() > stoppedPlayingTime + 1000){
    recorder.endRecord();
    recorder.save();
    println("Done!");
    noLoop();
  }
}

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

This code builds on the random synthesizer example, as well as Minim’s AudioRecorder example.

Tweak Ideas

  • Change the variables at the top to make different kinds of songs.
  • Use Minim’s AudioInput class to record your microphone.
  • Make a theme song based on different emotions: what would a sad song sound like?

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!