Displaying randomized song titles underneath fft with fade

edited November 2014 in Questions about Code

So I was following a tutorial to create some visuals using minim and fft and then wanted to build ontop of it. I managed to switch the sound it's using from an AudioInput to an AudioPlayer and to randomize what song is playing after a mouseclick. However, the song title at the bottom of the screen wont change along with the song. I've tried putting it in the void mouseClicked(){} section but the fade seems to get rid of it after a frame passes. Anyone know how to solve this?

The code:

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
FFT fft;
AudioPlayer player;

String [] words = {
  "song1.mp3", "song2.mp3", "song3.mp3"
};


float v;
int w;
PImage fade;
int hVal;
float rWidth, rHeight;
int index;


void setup () {
  size(2000, 1000, P3D);

  int index = int(random(words.length));

  minim = new Minim(this);

  player = minim.loadFile(words[index]);
  player.play();

  fft = new FFT(player.bufferSize(), player.sampleRate());
  fft.logAverages(60, 7);

  stroke(255);
  w= width/fft.avgSize();
  strokeWeight(w/3);
  strokeCap(SQUARE);

  background(0);
  fade = get(0, 0, width, height);

  rWidth = width * 0.99;
  rHeight = height * 0.99;
  hVal = 0;
}

void draw() {

  noStroke();
  fill(0);
  rect(0, 0, width, height);

  tint(255, 255, 255, 254);
  image(fade, (width - rWidth)/2, (height - rHeight)/2, rWidth, rHeight);
  noTint();

  fft.forward(player.mix);

  colorMode(HSB);
  stroke(hVal, 255, 255);
  colorMode (RGB);

  for (int i = 0; i<fft.avgSize (); i++) {
    line((i*w)+(w/2), (height-100), (i*w)+(w/2), (height-100) - fft.getAvg(i) *10);
  } 

  fade = get(0, 0, width, height);

  hVal += 2;
  if ( hVal >255) {
    hVal = 0;
  }

  stroke(255);
  for (int i = 0; i<fft.avgSize (); i++) {
    line((i*w)+(w/2), height-100, (i*w)+(w/2), height-100 - fft.getAvg(i) *10);
  }

  fill(255);
  textSize(48);
  text(words[index], 50, height-20);

}

void mouseClicked(){
  int index = int(random(words.length));

  player.pause();
  player = minim.loadFile(words[index]);
  player.play();

}
Tagged:

Answers

  • Answer ✓

    index on line 90 is a local variable, not the same thing as the global variable defined on line 22 and printed on line 85. Just remove the 'int'.

    But beware... You're printing the new track name over the old one so you might end up with a mess

  • Ah thanks I'll try that. I'm still pretty new to this so I was just copying and pasting things around without realizing I'm creating a new variable each time.

    Is there a less messy way of changing text other than rewriting on top of old text?

  • Thanks it worked! However, every time I start up the program the first song name it prints is song1.mp3. Only after that does it go on showing the correct one. The Audioplayer plays a song at random in the beginning not always song1.

Sign In or Register to comment.