Skip to content

Instantly share code, notes, and snippets.

@taptapdan
Created November 7, 2013 06:40
Show Gist options
  • Save taptapdan/7350091 to your computer and use it in GitHub Desktop.
Save taptapdan/7350091 to your computer and use it in GitHub Desktop.
First attempt at rendering a waveform visualization over a background image.
import ddf.minim.*;
Minim minim;
AudioPlayer song;
PImage bg;
int one_third;
int two_third;
/// VARIABLES TO CHANGE
String songtitle = "Stephen Swartz - Bullet Train (feat. Joni Fatora)";
int force_width = 1158; // canvas width
int force_height = 648; // canvas height
color c_hi = color(166, 98, 161); // high waveform color
color c_mid = color(235, 245, 252); // mid waveform color
color c_lo = color(187, 250, 250); // low waveform color
void setup() {
// Set canvas size
size(force_width, force_height, P2D);
// Load background image
bg = loadImage("miku-train2.jpg");
// resize, in case the bg image doesn't match canvas size
bg.resize(force_width, force_height);
minim = new Minim(this);
// Load song
song = minim.loadFile("bullet.mp3", force_width);
}
void draw() {
// Display background image
background(bg);
// Draw the waveform
for ( int i = 0; i < song.bufferSize() - 1; i++ )
{
one_third = height/3;
two_third = height/3*2;
if (song.left.get(i) < -.1) { stroke(c_hi); } else if (song.left.get(i) > .1) { stroke(c_lo); } else { stroke(c_mid); }
line(i, one_third + song.left.get(i)*75, i+1, one_third + song.left.get(i+1)*50);
if (song.right.get(i) < -.1) { stroke(c_hi); } else if (song.right.get(i) > .1) { stroke(c_lo); } else { stroke(c_mid); }
line(i, two_third + song.right.get(i)*75, i+1, two_third + song.right.get(i+1)*50);
}
// Song title display
textSize(24);
// ... title shadow in black
fill(0);
text(songtitle, width-25, height-25);
textAlign(RIGHT);
// ... title text in white
fill(255);
text(songtitle, width-27, height-27);
textAlign(RIGHT);
}
void keyPressed() {
// Start song when "s" key is pressed.
// This is to allow me to start video recording before song begins :)
if (key == 's') {
song.play();
}
}
void stop() {
song.close();
minim.stop();
super.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment