sheadernanigans/src/2023-06-21.festl_is/processing_sketches/audio_processing_with_touchosc/audio.pde

55 lines
1.2 KiB
Plaintext

import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioInput myAudio;
// beat detection with sound energy
BeatDetect myBeat;
final int BEAT_MAX = 60;
int myBeatMax = 20; // sync with gui
int myBeatCurrent = 0;
FFT myFFT;
void setupAudioAnalysis() {
minim = new Minim(this);
myAudio = minim.getLineIn(); // Minim.STEREO, bufferSize=1024, sampleRate=44100, int bitDepth=16
// beat detection setup
myBeat = new BeatDetect();
// fft setup
myFFT = new FFT(myAudio.bufferSize(), myAudio.sampleRate());
myFFT.logAverages(22, 12); // (22,12) -> 120 averages
myFFT.window(FFT.HANN); // good for "random" signals
// debug
println(".");
println("[debug][audio]: number of fft bins: "+myFFT.avgSize());
println(".");
}
void drawAudioAnalysis() {
// beat detection processing
if(myBeatCurrent > 0) {
myBeatCurrent--; // linear decay
}
myBeat.detect(myAudio.mix);
if(myBeat.isOnset()) {
myBeatCurrent = myBeatMax;
}
// draw beat detection
fill(0);noStroke();
rect(20,20,40,40);
fill(255);
circle(20+20,20+20,getBeatDetectNormalized()*36);
// fft processing
}
float getBeatDetectNormalized() {
return constrain((myBeatCurrent / float(myBeatMax)), 0.0f, 1.0f);
}