change strokeWeight with frequency

edited March 2016 in Library Questions

Hi, i was wondering if someone could help me change the strokeWeight to adjust when sound hits a certain frequency, I managed to get it to work on one sketch however, i cant figure out how to apply it to my new code.

The new code works through the microphone and the old code has imported sound file

old code that increases stroke around main ellipse ( which i want to apply to new code) based on frequency:

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

Minim minim;
AudioPlayer song;
FFT fft;
AudioInput in;

int sampleRate = 44100;
int bufferSize = 512;
int fft_base_freq = 86;
int  fft_band_per_oct = 1;
int numZones = 0;
float loudestFreq = 0;
int timerCounter = 0;

void setup() {
  size(1280, 720);  
  smooth();

  minim = new Minim(this);
  song = minim.loadFile("KXA - Gladius.mp3", bufferSize);
  song.loop();


  fft = new FFT( bufferSize, sampleRate );
  fft.logAverages(fft_base_freq, fft_band_per_oct);
  fft.window(FFT.HAMMING);
  numZones = fft.avgSize();
  smooth();
  background(0);
}

void draw() {


  fft.forward(song.mix);
  int highZone = numZones - 1;

  for (int i = 0; i < numZones; i++) { // 9 bands / zones / averages

    float average = fft.getAvg(i);
    println("Averages " + i + " : " + average);
    loudestFreq = i * 4;

    fill(126,242,255);
     if(loudestFreq > 30)
     {
       float x1 = random(0,width);
       float y1 = random(0,height);
       noStroke();
       ellipse(x1, y1, loudestFreq, loudestFreq);
       stroke(126,242,255);
       strokeWeight(2);
       line(width/2,height/2,x1,y1);
     }
      fill(255, 102, 0);
     if(loudestFreq > 31)
     {
       float x1 = random(0,width);
       float y1 = random(0,height);
       noStroke();
       rect(x1-10, y1-10, loudestFreq, loudestFreq);
       stroke(255, 102, 0);
       strokeWeight(2);
       line(width/2,height/2,x1,y1);
     }

    float avg = 0;
    int lowFreq;

    if ( i == 0 ) {
      lowFreq = 0;
    } else {
      lowFreq = (int)((sampleRate/2) / (float)Math.pow(2, numZones - i));
    }
    int hiFreq = (int)((sampleRate/2) / (float)Math.pow(2, highZone - i));
    int lowBound = fft.freqToIndex(lowFreq);
    int hiBound = fft.freqToIndex(hiFreq);


    ellipse();
    zigzag(average);

  }
  timerCounter = 0;
   timerCounter++;
 if(timerCounter >= 90)
 {
   //println("Clr screen");
   fill(0, 0, 0, (timerCounter - 90) * 2);
   rect(0, 0, width, height);
 }
}


void zigzag(float avg) {  
  float xPosition=525;
  float yPosition=350;

  float newXPosition=0;
  float newYPosition=30;

  float theDistance=50;
  float theRange=100; 
  delay(10);

  for (int i=0; i<4; i++) {
    stroke(avg+126, avg-213, avg-242);
    strokeWeight(avg+3);
    newXPosition = xPosition + theDistance;
    newYPosition = yPosition + theRange / 2 - (i % 2) * theRange;
    line(xPosition, yPosition, newXPosition, newYPosition);
    xPosition = newXPosition;
    yPosition = newYPosition;
  }
}

void ellipse() {  
  fill(0);
  ellipse(625, 375, 250, 250);
}

new code ( where i want to apply the change in the strokeWeight) :

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

Minim minim;
AudioInput in;
FFT fft;

float loudestFreqAmp = 0;
float loudestFreq = 0;
int timerCounter = 0;
int numZones = 0;

void setup()
{
 size(displayWidth, displayHeight);
 frameRate(30);
 background(0);
 smooth();

 minim = new Minim(this);
 minim.debugOn();

 in = minim.getLineIn(Minim.STEREO, 1024);
 fft = new FFT(in.bufferSize(), in.sampleRate());

}


void draw(){

 fft.window(FFT.HAMMING);
 for(int i = 0; i < fft.specSize(); i++)

 {
   float average = fft.getAvg(i);
   if (fft.getBand(i) > loudestFreqAmp && fft.getBand(i) > 10)
   {
     loudestFreqAmp = fft.getBand(i);
     loudestFreq = i * 4;

     fill(loudestFreq * 10, 255 - loudestFreq, loudestFreq * 100, 128 );
     float avg = 0;
     if(loudestFreq < 1800)
     {
       float x1 = random(0,width);
       float y1 = random(0,height);
       noStroke();
       fill(126,242,255);
       ellipse(x1, y1, loudestFreq, loudestFreq);  
       stroke(126,242,255);
       strokeWeight(2);
       line(width/2,height/2,x1,y1);
     }
     if(loudestFreq < 2000){
       {
       float x1 = random(0,width);
       float y1 = random(0,height);
       noStroke();
       fill(255, 102, 0);
       rect(x1-10, y1-10, 20, 20);
       stroke(255, 102, 0);
       strokeWeight(2);
       line(width/2,height/2,x1,y1);
      }
     }
      if(loudestFreq < 1500){
       {
       float x1 = random(0,width);
       float y1 = random(0,height);
       noStroke();
       fill(150, 0, 0);
       ellipse(x1, y1, loudestFreq, loudestFreq);
       stroke(150, 0, 0);
       strokeWeight(2);
       line(width/2,height/2,x1,y1);
      }
     }
     timerCounter = 0;
   }
   ellipse(average);
   zigzag(average);
 }
 loudestFreqAmp = 0;

 fft.forward(in.mix);
 timerCounter++;
 if(timerCounter >= 90)
 {
   //println("Clr screen");
   fill(0, 0, 0, (timerCounter - 5));
   rect(0, 0, width, height);
 }
}

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

 super.stop();
}

void zigzag(float avg) {  
  float xPosition=532;
  float yPosition=380;

  float newXPosition=0;
  float newYPosition=30;

  float theDistance=50;
  float theRange=100; 


  for (int i=0; i<4; i++) {
    stroke(avg+126, avg-213, avg-242);
    strokeWeight(avg+3);
    newXPosition = xPosition + theDistance;
    newYPosition = yPosition + theRange / 2 - (i % 2) * theRange;
    line(xPosition, yPosition, newXPosition, newYPosition);
    xPosition = newXPosition;
    yPosition = newYPosition;
  }
}
void ellipse(float avg) { 
  fill(0);
  ellipse(630, 400, 250, 250);
}

Thanks in advance

Sign In or Register to comment.