Using the recorder and the beat detect simultaneously with minim library


Maybe I should ask this question the creator of minim library but maybe some can help me.

I have mix both example of the minim library, the record and playback and the FrequencyEnergyBeatDetection.

The last example can detect only sound from an mp3 whereas I would like to make the beat detection directly from the output of the micro recorded.

How could I do?

I put my program below:

 * This sketch demonstrates how to use the BeatDetect object in FREQ_ENERGY mode.<br />
 * You can use <code>isKick</code>, <code>isSnare</code>, </code>isHat</code>, <code>isRange</code>, 
 * and <code>isOnset(int)</code> to track whatever kind of beats you are looking to track, they will report 
 * true or false based on the state of the analysis. To "tick" the analysis you must call <code>detect</code> 
 * with successive buffers of audio. You can do this inside of <code>draw</code>, but you are likely to miss some 
 * audio buffers if you do this. The sketch implements an <code>AudioListener</code> called <code>BeatListener</code> 
 * so that it can call <code>detect</code> on every buffer of audio processed by the system without repeating a buffer 
 * or missing one.
 * <p>
 * This sketch plays an entire song so it may be a little slow to load.
 * <p>
 * For more information about Minim and additional features, 
 * visit

import ddf.minim.*;

import ddf.minim.analysis.*;

import ddf.minim.ugens.*;

Minim minim;

// for recording
AudioInput in;
AudioRecorder recorder;
boolean recorded;

// for playing back
AudioOutput out;
FilePlayer player;

// Player of the song and beat detecter
AudioPlayer song;
BeatDetect beat;
BeatListener bl;

float kickSize, snareSize, hatSize;

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

void setup()
 size(512, 600, P3D);
 minim = new Minim(this);
  // get a stereo line-in: sample buffer length of 2048
 // default sample rate is 44100, default bit depth is 16
 in = minim.getLineIn(Minim.STEREO, 2048);
   // create an AudioRecorder that will record from in to the filename specified.
 // the file will be located in the sketch's main folder.
 recorder = minim.createRecorder(in, "myrecording.wav");
 // get an output we can playback the recording on
 out = minim.getLineOut( Minim.STEREO );
   //************AT THE ORIGIN
//  song = minim.loadFile("marcus_kellis_theme.mp3", 1024);
//  beat = new BeatDetect(song.bufferSize(), song.sampleRate());

// // 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
 //******************** WHAT I HAVE CHANGED
//  song = minim.getLineOut( Minim.STEREO );
 beat = new BeatDetect(out .bufferSize(), out.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).
 kickSize = snareSize = hatSize = 16;
 // make a new beat listener, so that we won't miss any buffers for the analysis
 //************AT THE ORIGIN  
 //  bl = new BeatListener(beat,  );
 textFont(createFont("Helvetica", 16));

void draw()
 // draw the waveforms
 // the values returned by left.get() and right.get() will be between -1 and 1,
 // so we need to scale them up to see the waveform
 for(int i = 0; i < in.left.size()-1; i++)
   line(i, 50 + in.left.get(i)*50, i+1, 50 + in.left.get(i+1)*50);
   line(i, 150 + in.right.get(i)*50, i+1, 150 + in.right.get(i+1)*50);
 // manage the recording
  if ( recorder.isRecording() )
   text("Now recording, press the r key to stop recording.", 5, 15);
   print ("DoWhat youwant");
 else if ( !recorded )
   text("Press the r key to start recording.", 5, 15);
   text("Press the s key to save the recording to disk and play it back in the sketch.", 5, 15);
 // 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) )
     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) )
   rect(rectW*lowBand, 0, (highBand-lowBand)*rectW, height);
 if ( beat.isKick() ) kickSize = 32;
 if ( beat.isSnare() ) snareSize = 32;
 if ( beat.isHat() ) hatSize = 32;
 text("KICK", width/4, height/2);
 text("SNARE", width/2, height/2);
 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);

void keyReleased()
 if ( !recorded && key == 'r' ) 
   // to indicate that you want to start or stop capturing audio data, 
   // you must callstartRecording() and stopRecording() on the AudioRecorder object. 
   // You can start and stop as many times as you like, the audio data will 
   // be appended to the end of to the end of the file. 
   if ( recorder.isRecording() ) 
     println ("Endrecord");
     recorded = true;
       println ("beginRecord");
 if ( recorded && key == 's' )
   // we've filled the file out buffer, 
   // now write it to a file of the type we specified in setup
   // in the case of buffered recording, 
   // this will appear to freeze the sketch for sometime, if the buffer is large
   // in the case of streamed recording, 
   // it will not freeze as the data is already in the file and all that is being done
   // is closing the file.
   // save returns the recorded audio in an AudioRecordingStream, 
   // which we can then play with a FilePlayer
   if ( player != null )
       println ("close");
       player.unpatch( out );
   player = new FilePlayer( );
   player.patch( out );;