Final Project: Visualizing Sound

 

Guysssss I’m gonna miss y’all. Thank you for a great semester. Friend me on the ‘book, and/or check out ma website to stay in touch!

tuneshot-1338

 

 

Step 1 of ‘the dream’ is complete. The program plays a select set of songs and visualizes the music using a forward fourier transformation (fft). The “.getBand()” command is used to quantify the various aspects of the audio waves.

 

tuneshot-876

As a user, hold down the ‘P’ key to view the menu. It will tell you that you have the option to change the song (with the number keys), the colors of the visualization (keys a through g). You can increase and decrease the intensity of the visualization with the ‘=’ and ‘-‘ keys, respectively. Finally, you can pause the movie with the ‘I’ key and take a screenshot by clicking on the screen. downloadable .zip file – including .mp3’s – here.

 

Worldstar-quality video clip of the in-class demo

 

Code:

import ddf.minim.*;
import ddf.minim.ugens.*; 
import ddf.minim.analysis.*;
import processing.video.*;

int spectrum;
float videoHeight;
float videoWidth;
float threshold;
float multiplier;
PFont font;
StringList playlist;
color ringColor;
color antiColor;

Minim       minim;
AudioPlayer jingle;
FFT         fft;
Capture     video;

void setup()
{
  //video presets
  font = loadFont("MicrosoftTaiLe-Bold-12.vlw");
  video = new Capture(this, 640, 480);
  video.start();
  threshold = 20;

  //sound presets
  minim = new Minim(this);
  jingle = minim.loadFile("bt.mp3", 1024);
  fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
  spectrum = fft.specSize();
  multiplier = 20;

  //sound-dependant video settings
  videoHeight = ((spectrum - 480)/2)+1;
  videoWidth = ((spectrum - 640)/2)+1;

  //sound-dependant background
  size(spectrum, spectrum);
}

void draw() {

//tracklist, key commands
if (keyPressed) {
  if (key == 'p') {background(255);
                  fill(0);
                  textFont(font, 12);
                  textAlign(CENTER);
                  text("Press 'P' at any time for playlist options, press number keys to change the song:",(width/2), 25);
                  text("1: One Direction - Happily",(width/2),50);
                  text("2: No Doubt - Sunday Morning",(width/2),75);
                  text("3: Beethoven - 5th Symphony",(width/2),100);
                  text("4: Andrew McMahon - Catching Cold",(width/2),125);
                  text("5: DJ Naim - Crispy Hunneds",(width/2),150);
                  text("6: Arctic Monkeys - Do I Wanna Know?",(width/2),175);
                  text("7: Grimes - Oblivion",(width/2),200);
                  text("8: Frank Ocean (feat. Diplo) - Hero",(width/2),225);
                  text("9: Srkrillex - Bangarang",(width/2),250);
                  text("0: Grimes - Genesis",(width/2),275);
                  text("Press a, b, c, d, e, f, and g keys to change the color profile", (width/2), 325);
                  text("Press = to increase the intensity, - to decrease the intensity of the waves", (width/2), 350);
                  text("Press 'I' to pause the current song", (width/2), 375);
                  text("Click on the screen to snap a screenshot!", (width/2), 400);
                  }

//color changer
  if (key == 'a') {ringColor = color(255,0,0); antiColor = color(0,255,0);};
  if (key == 'b') {ringColor = color(0,255,0); antiColor = color(255,0,0);};
  if (key == 'c') {ringColor = color(255,0,0); antiColor = color(0,0,255);};  
  if (key == 'd') {ringColor = color(0,255,0); antiColor = color(0,0,255);};
  if (key == 'e') {ringColor = color(0,0,255); antiColor = color(255,0,0);};
  if (key == 'f') {ringColor = color(0,0,255); antiColor = color(0,255,0);};
  if (key == 'g') {ringColor = color(random(255),random(255),random(255)); antiColor = color(random(255),random(255),random(255));};

//intensity changer
  if (key == '=') {multiplier = multiplier + 5;};
  if (key == '-') {multiplier = multiplier - 5;};

//pause button
  if (key == 'i') {jingle.pause();};

//track changer
  if (key == '1') {jingle.pause();jingle = minim.loadFile("oned.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '2') {jingle.pause();jingle = minim.loadFile("nod.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '3') {jingle.pause();jingle = minim.loadFile("bt.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '4') {jingle.pause();jingle = minim.loadFile("catch.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '5') {jingle.pause();jingle = minim.loadFile("crispy.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '6') {jingle.pause();jingle = minim.loadFile("doi.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '7') {jingle.pause();jingle = minim.loadFile("oblivion.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '8') {jingle.pause();jingle = minim.loadFile("hero.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '9') {jingle.pause();jingle = minim.loadFile("skr.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
  if (key == '0') {jingle.pause();jingle = minim.loadFile("genesis.mp3", 1024);jingle.loop();fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );};
    }

else {
  //launch music analysis
  fft.forward(jingle.mix);

  //video section
  if (video.available()) {
   video.read();
  }
    video.loadPixels();
    image(video,videoWidth, videoHeight);

  //for loop for EQs
  for (int i=0; i < fft.specSize(); i++) {
    float level = fft.getBand(i)*multiplier;
    noFill();
    strokeWeight(3);
    stroke(ringColor,random(100,200));
    //top left & right
    line(i, 0, i, level);line(width - i, 0, width - i, level);
    //bottom left & right
    line( i, height, i, height - level);line( width - i, height, width - i, height - level);
    stroke(antiColor,random(100,200));
    //left top & bottom
    line(0, i, level, i);line(0, height - i, level, height - i);
    //right top & bottom
    line(width, i, width - level, i);line(width, height - i, width - level, height - i);
    //oval
    if (level > 350 && level < 450) {
       strokeWeight(15);
       stroke(ringColor,random(0,150));
       ellipse((width/2),(height/2), level, level*1.2);
    }
  }
  };};

//  screenshot
void mousePressed() {
    saveFrame("tuneshot-###.png");
  }

One thought on “Final Project: Visualizing Sound

Leave a Reply