Reset my sketch

edited April 2014 in Library Questions

Hi I'm working on a final project where by audio input generates a visualization. The visualization is a line that plots ellipses along the from left to right similar to an oscilloscope. I've run into a small problem however as i'm trying to reset the visualization once it gets to the end of the threshold which is a value of 15 float flux = random(15);

Is anyone good with loops that can help me out as soon as possible?

here the code

import javax.media.opengl.*;
import processing.opengl.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;

import com.getflourish.stt.*;
import ddf.minim.*;
import javax.sound.sampled.*;

STT stt;

Minim minim;
AudioInput in;


FFT fft;

int index;

float[]x = new float[width];
float[]y = new float[width];


float[] spectrum;
float[] lastspectrum;


void setup()
{
  size(displayWidth, displayHeight, OPENGL);

  // Init STT automatically starts listening
  stt = new STT(this);
  stt.enableDebug();
  stt.setLanguage("en");



  background (0) ;
  smooth();
  noStroke();

  x = new float[width];
  y = new float[width];


  for (int i = 0; i < x.length ; i++)
    x[i] = (float)i ;

  //  minim = new Minim(this);
  //in = minim.getLineIn(Minim.STEREO, 512);

  // Display available Inputs with id and name
  Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
  for (int i = 0; i < mixerInfo.length; i++)
  {
    println(i + ": " + mixerInfo[i].getName());
  }
  // Set input (e.g. 0)
  Mixer mixer = AudioSystem.getMixer(mixerInfo[1]);
  // Update the Minim instance that STT is using
  Minim minim = stt.getMinimInstance();
  //minim.setInputMixer(mixer);
  in = minim.getLineIn(Minim.MONO, 512);



  //in.enableMonitoring();
  fft = new FFT(in.bufferSize(), in.sampleRate());
  //exact values 
  fft.noAverages();

  //first and last values on spectrum 
  spectrum = new float[fft.specSize()];
  lastspectrum = new float[fft.specSize()];
}

void draw()
{

  fft.forward(in.mix);

  ///------------------------
  // SPECTRAL FLUX (CONTINUOUS CHANGE)
  ///------------------------

  float flux = random(15);//added a random float value(5) is the max value random can be


  for ( int i = 0 ; i < fft.specSize() ; i++)
  {
    spectrum[i] = fft.getBand(i);
    //for every int flux takes the current spectrum subtracted by the last
    flux += spectrum[i] - lastspectrum[i];

   }

  System.arraycopy( spectrum, 0, lastspectrum, 0, spectrum.length );

  background(0);

  fill(0, 255, 0);
  translate(0, height/2);
  for ( int i = 5 ; i < x.length ; i++)
    ellipse(x[i], y[i], abs(flux), abs(flux));

  y[index++%width] = flux;


}

void stop()
{
  in.close();
  minim.stop();
  super.stop();
}


void mouseClicked()
{
  saveFrame();
}

Answers

  • Answer ✓

    Some remarks:

    float[]x = new float[width];
    

    It is declared at the global level. At this level, setup() isn't run yet, so size() hasn't be called, so width is zero there. You re-assign the variable in setup(), so you can drop the init part of these lines, just declare the variable.

    Note: I prefer to use plural for collections and array names...

    System.arraycopy()
    

    Processing has an arrayCopy() function, too... :-)

    "i'm trying to reset the visualization once it gets to the end of the threshold"
    I don't understand the question, actually. But most "reset this sketch" questions are answered with "assign again the initial values to your variables".

  • what i meant is everytime the spectrum gets to the end, i wanted it to reset so that there's no loop

Sign In or Register to comment.