Tuesday, 30 September 2014

Simple sketch

I wanted the visualization to have more of a shape, so I tried drawing a circle connecting the wave values with the radius and vertexes around it...? I include the basic tempo code thing from minim library to compare the two visualizations created.

It is still rough and not very interesting yet (I think). Doesn't really create a sort of unique feel of the song as I wanted. However I like how the shape changes dramatically when the song picks up its pace.


Code:

import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioPlayer player;
int
  samples = 1024;
float
  angleStep  = TWO_PI/samples,
  maxRadius = 300.,
  scaleF = 125.,
  radiusStep;
PVector
  shapeCenter,
  currentVertex;
PVector[]
  lookupTable;
 
 
void setup()
{
  size(1000, 1000, P3D);
   smooth();
    noStroke();
    background(-1);
    noCursor();
     shapeCenter  = new PVector(width/2, height/2);
    currentVertex= new PVector();
    lookupTable= new PVector[samples];
    for (int i = 0; i < samples; i++)
    {
         float angle= i*angleStep + HALF_PI;
         lookupTable[i] = new PVector(cos(angle), sin(angle));
    }
  minim = new Minim(this);
  player = minim.loadFile("Warriors.mp3");
  radiusStep = maxRadius/(player.length()*6/100);
    player.loop();
   
}

void draw()
{
  background(0);
  stroke(255);
   for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
    line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }
 
   if (player.isPlaying())
    {
         visualizeWaves(player.right.toArray(), 0x50ff0000, maxRadius, scaleF);
         visualizeWaves(player.left.toArray(), 0x50000000, maxRadius, scaleF);
         visualizeWaves(player.mix.toArray(), 0xffffffff, maxRadius -= radiusStep, scaleF);
    }
}

void visualizeWaves(float [] wavesToVisualize, color fillColor, float radius, float scale_factor)
{
     fill(fillColor);
     stroke(0x10ffffff);
     beginShape();
     for (int i = 0; i < wavesToVisualize.length; i++)
     {
          float  r = radius + wavesToVisualize[i]*scale_factor;
          currentVertex = PVector.add(shapeCenter, PVector.mult(lookupTable[i], r));
          vertex (currentVertex.x, currentVertex.y);
      }
      endShape();
}

2nd sketch using 2 audios

This sketch attempts to import 2 mp3 files at the same time by using an array list and display 2 audio players instead of 1

    import ddf.minim.*;
    AudioPlayer [] players = new AudioPlayer[2];
    Minim minim;

    void setup() {
      size(512, 250, P2D);
      minim = new Minim(this);
      players[0] = minim.loadFile("itstime.mp3");
      players[1] = minim.loadFile("Warriors.mp3");
      for (int i=0; i<players.length; i++) {
        players[i].play();
      }
    }

    void draw() {
      background(0);
      displayAudio(players[0], 50, color(0,0,255));
      displayAudio(players[1], 150, color(255,0,0));
    }

    void displayAudio(AudioPlayer player, int playerHeight, color c) {
      stroke(c);
      for(int i=0; i<player.left.size()-1; i++) {
        line(i, playerHeight + player.left.get(i)*50, i+1, playerHeight + player.left.get(i+1)*50);
        line(i, playerHeight + 25 + player.right.get(i)*50, i+1, playerHeight + 25 + player.right.get(i+1)*50);
      }
    }

    void stop() {
      for (int i=0; i<players.length; i++) {
        players[i].close();
      }
      minim.stop();
      super.stop();
    }

Sunday, 28 September 2014

How to save audio file into text file

Found this tutorial on vimeo. Might be useful for turning my sketch into graphic ie visualization?

http://vimeo.com/27642353

The code:

openprocessing.org/visuals/?visualID=33855

It also includes play/pause, mute/unmute button, which may also be of use later...

Friday, 26 September 2014

1st sketch

I found a sketch that will probably be useful as a base for my project. It plays a song inserted into the sketch folder, and let you control some effects on the sound by interacting with the mouse.

I also learned how to use the minim library from various sources on the web. Quick note is to always include a code to close the sound after you turn off Processing.

Which is something like this
void stop() {
  song.close();
  minim.stop();

  super.stop();
}

Here is the whole sketch though:

/*
 * Playback_BPF
 *
 * Plays a soundfile, sound.mp3.
 * Applies a BPF, which is controlled interactively by the mouse
 * (left-right is frequency, up-down is Q/bandwidth).
 * Plots the instantaneous spectrum, showing the effect of the filter.
 *
 * 2010-01-22 Dan Ellis dpwe@ee.columbia.edu
 */

import ddf.minim.analysis.*;
import ddf.minim.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer sound;

BandPass bpf;
FFT fft;
float centerFreq;
float bandwidth;

float gain = 60; // in dB

int spectrumScale = 2; // pixels per FFT bin

void setup()
{
  size(512, 200);
  textFont(createFont("SanSerif", 12));
 
  minim = new Minim(this);
  minim.debugOn();
 
  sound = minim.loadFile("itstime.mp3");
  // make it repeat
  sound.loop();

  // add in the bpf to the input
  centerFreq = 440;
  bandwidth = 20;
  bpf = new BandPass(centerFreq, bandwidth, sound.sampleRate());
  sound.addEffect(bpf);

  // create an FFT object that has a time-domain buffer
  // the same size as line-in's sample buffer
  fft = new FFT(sound.bufferSize(), sound.sampleRate());
  // Tapered window important for log-domain display
  fft.window(FFT.HAMMING);

}

void mouseMoved()
{
  // map the mouse position to the range [100, 10000], an arbitrary range of passBand frequencies
  centerFreq = map(mouseX, 0, width, 0, sound.sampleRate()/(2*spectrumScale));
  bpf.setFreq(centerFreq);
  float bpfQ = 100 - map(mouseY, 0, height, 0, 98);  // lowest Q is 100-98 = 2;
  bandwidth = centerFreq/bpfQ;
  bpf.setBandWidth(bandwidth);
}


void draw()
{
  background(0);
  fft.forward(sound.mix);
  fill(64,192,255);
  noStroke();
  for(int i = 0; i < fft.specSize(); i++)
  {
    // draw the line for frequency band i using dB scale
    float val = 2*(20*((float)Math.log10(fft.getBand(i))) + gain);
    rect(i*spectrumScale, height, spectrumScale, -Math.round(val));
  }
  // add band showing BPF
  fill(255, 0, 0, 80);
  rect(mouseX - bpf.getBandWidth()/20, 0, bpf.getBandWidth()/10, height);
  // report center frequency
  fill(255);
  text("Center frequency="+Math.round(centerFreq)+" Hz bw="+Math.round(bandwidth), 5, 20);
}

void keyReleased()
{
  // +/- used to adjust gain on the fly
  if (key == '+' || key == '=') {
    gain = gain + 3.0;
  } else if (key == '-' || key == '_') {
    gain = gain - 3.0;
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  sound.close();

  minim.stop();

  super.stop();
}

Monday, 22 September 2014

Second assignment

Turn code into real life object (print, audio, etc)

I haven't decided yet on my variable of the three to work on this project, as I don't actually know what they represent, or what they turn into yet...

Functions
Conditionals
Loops

For now I have decided to experiment with audio, meaning testing out the minim library.