Actualise data automaticaly (with a beat detection)

Hi there,

I would like my program actualise itself the data to compute each time it detected a threshold of Beat in a song.

For the moment I actualise my self the program by tapping on the key: 7.

I would like my program do the case 7, automatically when k (the data of the variable BEAT) is upper than 1.5.

k is at the end of void draw
k = map (kickSize, 16, 32, 1,2);

In others words

if k>1.5
do as if I tap on the key 7

I guess I have to write a “string” 7 in the draw loop in a manner the function void keyPressed() can recognize it.
How could I do?

I put a part of the program below

Thanks for helping.

 //*****************BEAT DETECT 
   // draw a green rectangle for every detect band
  // that had an onset this frame
  float rectW = width / beat.detectSize();
  for(int i = 0; i < beat.detectSize(); ++i)
  {
    // test one frequency band for an onset
    if ( beat.isOnset(i) )
    {
      fill(0,200,0);
      rect( i*rectW, 0, rectW, height);
    }
  }
  
  // draw an orange rectangle over the bands in 
  // the range we are querying
  int lowBand = 5;
  int highBand = 15;
  // at least this many bands must have an onset 
  // for isRange to return true
  int numberOfOnsetsThreshold = 4;
  if ( beat.isRange(lowBand, highBand, numberOfOnsetsThreshold) )
  {
    fill(232,179,2,200);
    rect(rectW*lowBand, 0, (highBand-lowBand)*rectW, height);
  }
  
  if ( beat.isKick() ) kickSize = 32;
  if ( beat.isSnare() ) snareSize = 32;
  if ( beat.isHat() ) hatSize = 32;
  
  fill(255);
    
  textSize(kickSize);
  text("KICK", width/4, height/2);
  
  textSize(snareSize);
  text("SNARE", width/2, height/2);
  
  textSize(hatSize);
  text("HAT", 3*width/4, height/2);
  
  kickSize = constrain(kickSize * 0.95, 16, 32);
  snareSize = constrain(snareSize * 0.95, 16, 32);
  hatSize = constrain(hatSize * 0.95, 16, 32);
  
    k = map (kickSize, 16, 32, 1,2);
  
    s = map (snareSize, 16, 32, 1,2);
  
    h = map (hatSize, 16, 32, 1,2);
  
/*  
  println (k);
  println (s);
  println (h);
  println();
*/
}

void keyPressed() {
  switch (key) {
  
      case '7':
   
       changeNaturalF= 2.0* (mouseX/600.0*1.0)+0.5;
       changeNaturalF1= 2.0* (mouseY/600.0*1.0)+0.5;
       noiseDetail(mouseX/30, mouseY/300);
       
         println (k);
          println (s);
          println (h);
        println();
    
   //   print ( changeNaturalF); println ( changeNaturalF1);
      
     
      
     
      net.naturalFrequency[0] = 1.0*k*TWO_PI * noise(t);
      t += dt; 
      net.naturalFrequency[1] = 1.0*s*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[2] = 1.0*h*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[3] = 0.9*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[4] = 1.1*TWO_PI * noise(t);
      t += dt;
   
      break;
   
  }
}

try a other thinking:

// in draw:
if ( k > 1.5 ) doit();

// for keyPressed
 
void keyPressed() {
  switch (key) {
      case '7':
        doit();
      break;  
  }
}

// in both case:
void doit() {
// what to do here?

}

Thanks but, It doesn’t work, my data are not actualized automatically. Maybe, I have not well explained my problem. So, I try to explain better

For the moment, my program when i switch by tapping 7 use the variable k,s and h only one time, with values of the beginning of the song.

Those values change continuesly while the song advance.

I would like to have those values k, s and h, each time k >1.5.

For the moment I can actualise my datas (print k,s h for example) each time I tap on the keyboard 7.

So, How could i do my datas actualise itself automaticcaly when the KICK is beatting louder than 1.5 (k>1.5) ?

In void doit() I should order
do as if I tap on the keyboard the 7
But I don’t know how to do it.

Thanks you

i can not see that from your code, but
if that means that k is not updated, well yes, you can not do a

  if ( k > x ) {}

you posted a code part what has no correct beginning?
is it inside draw() ?
or called from there?

I made few modifications. Now when I push the shift key, my program actualise data of k,s h (kick, snare, hat).
The program actualise datas in the case ‘default’

But data are actualised only one time at each time that i tap the shift key and not synchronisly with the “kick” detection (k>1.7).

I tried to automatise this detection by calling key’7’ when k >1.7.

Now, If I push the shift key at the very same moment of a big kick, the program switch on case 7

But I thought that if I maintain the shift key, the program would switch on case 7 automatically while a big kick appears.

How could I do that?

More basically, I would like the program simulate me tapping on the shift key at the same moment of a big kick (kick upper than 1.7) .

I put the entire program below

/*
 * Parameter Example.
 *
 * Initialize a small network of coupled oscillators and vary the natural frequency of
 * each oscillator based on user input.
 * 
 * Visualization idea borrowed from the Wikipedia article for the Kuramoto model.
 * https://en.wikipedia.org/wiki/Kuramoto_model
 */
import sync.*;

PNetwork net;
float radius;
int t;
int dt;
float changeNaturalF, changeNaturalF1;

float noiseVal;
float noiseScale=0.02;

//***********************FrequencyEnergy BeatDetection
import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer song;
BeatDetect beat;
BeatListener bl;

float kickSize, snareSize, hatSize;
float v,k,s,h;

class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioPlayer source;
  
  BeatListener(BeatDetect beat, AudioPlayer source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }
  
  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }
  
  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}




public void settings() {
  
   size(600, 600);
 
  }
  
void setup() {
  
  
 
  int networkSize = 5;
  float coupling = 1.5;
  float stepSize = 0.05;
  float noiseLevel = 0;
  net = new PNetwork(this, networkSize, coupling, stepSize, noiseLevel);
  radius = 75;
  t = 0;
  dt = 1;
  frameRate(12);
  
    //********* BEAT DETECT SETUP
 //  size(600,600,P3D);
  
  minim = new Minim(this);
  
  song = minim.loadFile("marcus_kellis_theme.mp3", 1024);
  song.play();
  // a beat detection object that is FREQ_ENERGY mode that 
  // expects buffers the length of song's buffer size
  // and samples captured at songs's sample rate
  beat = new BeatDetect(song.bufferSize(), song.sampleRate());
  // set the sensitivity to 300 milliseconds
  // After a beat has been detected, the algorithm will wait for 300 milliseconds 
  // before allowing another beat to be reported. You can use this to dampen the 
  // algorithm if it is giving too many false-positives. The default value is 10, 
  // which is essentially no damping. If you try to set the sensitivity to a negative value, 
  // an error will be reported and it will be set to 10 instead. 
  // note that what sensitivity you choose will depend a lot on what kind of audio 
  // you are analyzing. in this example, we use the same BeatDetect object for 
  // detecting kick, snare, and hat, but that this sensitivity is not especially great
  // for detecting snare reliably (though it's also possible that the range of frequencies
  // used by the isSnare method are not appropriate for the song).
  beat.setSensitivity(300);  
  kickSize = snareSize = hatSize = 16;
  // make a new beat listener, so that we won't miss any buffers for the analysis
  bl = new BeatListener(beat, song);  
  textFont(createFont("Helvetica", 16));
  textAlign(CENTER);
  
  
}

void draw() {
  background(220);
  translate(width/2, height/2);
  
  // Draw a track
  stroke(100);
  noFill();
  ellipse(0, 0, 2*radius, 2*radius);
  
  // Draw a circle corresponding to the phase of each oscillator
  for (int i = 0; i < net.networkSize; i++) {
    pushMatrix();
    float x = radius*cos(net.phase[i]);
    float y = radius*sin(net.phase[i]);
    translate(x, y);
    // Fill circles based on velocity
    if (net.velocity[i] > 0) {
      fill(0, 0, 255, 50);
    } else {
      fill(255, 0, 0, 50);
    }
    // Outline circles based on acceleration
    if (net.acceleration[i] > 0) {
      stroke(0, 0, 255);
    } else {
      stroke(255, 0, 0);
    }
    
    ellipse(0, 0, 10, 10);
    popMatrix();
  }
  
  // Draw a line pointing to the average phase of the network
  pushMatrix();
  scale(net.orderParameter);
  float x = radius*cos(net.averagePhase);
  float y = radius*sin(net.averagePhase);
  stroke(100);
  line(0, 0, x, y);
  popMatrix();

  net.step();
  
 //*****************BEAT DETECT 
   // draw a green rectangle for every detect band
  // that had an onset this frame
  float rectW = width / beat.detectSize();
  for(int i = 0; i < beat.detectSize(); ++i)
  {
    // test one frequency band for an onset
    if ( beat.isOnset(i) )
    {
      fill(0,200,0);
      rect( i*rectW, 0, rectW, height);
    }
  }
  
  // draw an orange rectangle over the bands in 
  // the range we are querying
  int lowBand = 5;
  int highBand = 15;
  // at least this many bands must have an onset 
  // for isRange to return true
  int numberOfOnsetsThreshold = 4;
  if ( beat.isRange(lowBand, highBand, numberOfOnsetsThreshold) )
  {
    fill(232,179,2,200);
    rect(rectW*lowBand, 0, (highBand-lowBand)*rectW, height);
  }
  
  if ( beat.isKick() ) kickSize = 32;
  if ( beat.isSnare() ) snareSize = 32;
  if ( beat.isHat() ) hatSize = 32;
  
  fill(255);
    
  textSize(kickSize);
  text("KICK", width/4, height/2);
  
  textSize(snareSize);
  text("SNARE", width/2, height/2);
  
  textSize(hatSize);
  text("HAT", 3*width/4, height/2);
  
  kickSize = constrain(kickSize * 0.95, 16, 32);
  snareSize = constrain(snareSize * 0.95, 16, 32);
  hatSize = constrain(hatSize * 0.95, 16, 32);
  
    k = map (kickSize, 16, 32, 1,2); //beat
  
    s = map (snareSize, 16, 32, 1,2); // snare
  
    h = map (hatSize, 16, 32, 1,2);// hat
  
/*  
  println (k);
  println (s);
  println (h);
  println();
*/
    if ( k > 1.5 ) { // if the beat is louder 1.5 switch on case 7
   
   doit();    
  float v= frequencyEnergy(k);
    }
   
}

float frequencyEnergy (float dVal) { // useful to return value constantly???
  dVal= dVal *1;
  return dVal;
  
}

void  doit()  {
       
//  if (frequencyEnergy(k)>1.7 ) {
    if (k>1.7){
  
      
  print ("Frequency_ENERGY"); println (k); println(frequencyEnergy(k));
      key='7'; // tap on key 7 to simulate me tapping on case 7
        
      }
  }

void keyPressed() {
  
  
  
     doit (); //each time I push the shift key AND kick is upper than 1.7--> go on case 7
                // (BUT I would like not to tape on shift key to go on case 7)
              // otherwise (k<1.7) if i push on the shift key, it actualise case 'default'
              // with new value of kick, snare, hat.
      
  switch (key) {
        default:
      
       
  
   //    noiseDetail(mouseX/30, mouseY/300);
     
     
      net.naturalFrequency[0] = frequencyEnergy(k)*TWO_PI * noise(t);
      t += dt; 
      net.naturalFrequency[1] = 1.0*s*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[2] = 1.0*h*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[3] = 0.9*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[4] = 1.1*TWO_PI * noise(t);
      t += dt;
     
       println ("DEFAULT");
       print ("Kick"); println (k);
       print ("snare"); println (s);
       print ("hat"); println (h);
       println ();
        
       print ("Freq Energy Kick"); println(frequencyEnergy(k));;
        
      break;
    
      case '0':
      changeNaturalF= (mouseX/600.0*1.0)+0.5;
      changeNaturalF1= (mouseY/600.0*1.0)+0.5;
      
      println ("CASE 0"); 
      println ( changeNaturalF); println ( changeNaturalF1);


  
      net.naturalFrequency[2] = PI*noise(t);
      net.naturalFrequency[0] = changeNaturalF*PI*noise(t);
      net.naturalFrequency[1] = changeNaturalF1*PI*noise(t);
      net.naturalFrequency[3] = PI*noise(t);
      net.naturalFrequency[4] = PI*noise(t);
      t += dt;
     
      break;
    case '1':
    changeNaturalF= (mouseX/600.0*1.0)+0.5;
       changeNaturalF1= (mouseY/600.0*1.0)+0.5;
      println ( changeNaturalF); println ( changeNaturalF1);
       net.naturalFrequency[0] =changeNaturalF*PI;
      net.naturalFrequency[1] = changeNaturalF1*PI;
      net.naturalFrequency[2] = 0.8f*PI;
      net.naturalFrequency[3] = 1.1f*PI;
      net.naturalFrequency[4] = 1.2f*PI;
      t += dt;
      break;
    case '2':
    
       net.naturalFrequency[0] = PI*noise(t);
      net.naturalFrequency[1] = 0.9f*PI;
      net.naturalFrequency[2] = 0.8f*PI;
      net.naturalFrequency[3] = 1.1f*PI;
      net.naturalFrequency[4] = 1.2f*PI;
      t += dt;
   
      break;
    case '3':
    for (int y = 0; y < height; y++) {
    for (int x = 0; x < width/2; x++) {
       noiseDetail(3,0.5);
      noiseVal = noise((mouseX+x) * noiseScale, (mouseY+y) * noiseScale);
      stroke(noiseVal*255);
      point(x,y);
      noiseDetail(8,0.65);
      noiseVal = noise((mouseX + x + width/2) * noiseScale, 
                       (mouseY + y) * noiseScale);
  
      net.naturalFrequency[3] = 1.5*PI*noise(t);
      net.naturalFrequency[0] = 1.2*PI*noise(t);
      net.naturalFrequency[1] = PI*noise(t);
      net.naturalFrequency[2] = 0.8*noise(t);
      net.naturalFrequency[4] = 0.6*noise(t);
      t += dt;
      }
      }
      break;
    case '4':
    
        net.naturalFrequency[3] = TWO_PI;
      net.naturalFrequency[0] = HALF_PI;
      net.naturalFrequency[1] = QUARTER_PI;
      net.naturalFrequency[2] = QUARTER_PI;
      net.naturalFrequency[4] = PI;
      t += dt;
      break;
   case '5':
   
       changeNaturalF= 2.0* (mouseX/600.0*1.0)+0.5;
       changeNaturalF1= 2.0* (mouseY/600.0*1.0)+0.5;
    
      print ( changeNaturalF); println ( changeNaturalF1);
      
      net.naturalFrequency[0] = changeNaturalF*TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[1] = changeNaturalF*TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[2] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[3] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[4] = TWO_PI*noise(t);
      t += dt;
      break;
      
    case '6':
   
       changeNaturalF= 2.0* (mouseX/600.0*1.0)+0.5;
       changeNaturalF1= 2.0* (mouseY/600.0*1.0)+0.5;
       noiseDetail(mouseX/30, mouseY/300);
    
      print ( changeNaturalF); println ( changeNaturalF1);
      
      net.naturalFrequency[0] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[1] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[2] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[3] = TWO_PI*noise(t);
      t += dt;
      net.naturalFrequency[4] = TWO_PI*noise(t);
      t += dt;
      break;
      
      case '7':
      
      
      doit();

    
   
       noiseDetail(mouseX/30, mouseY/300);
       
         
    
   
     
      net.naturalFrequency[0] = 1.0f*k*TWO_PI * noise(t);
      t += dt; 
      net.naturalFrequency[1] = 2.0f*s*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[2] = 3.0f*h*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[3] = 0.9f*TWO_PI * noise(t);
      t += dt;
      net.naturalFrequency[4] = 1.1f*TWO_PI * noise(t);
      t += dt;
     
      
     
      
       print ("Frequency-Energy inside case 7: "); println(frequencyEnergy(k));
       print ("Kick"); println (k);
        
          println(s);
          println (h);
          println ();
          println ();
          
      break;
      
     
   
  }
}
1 Like