import ddf.minim.*; import ddf.minim.analysis.*; import processing.serial.*; Serial port; Minim minim; 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.source.addListener(this); this.beat = beat; } void samples(float[] samps) { beat.detect(source.mix); } void samples(float[] sampsL, float[] sampsR) { beat.detect(source.mix); } } void setup() { size(512, 200, P3D); minim = new Minim(this); println("Available serial ports:"); println(Serial.list()); // check on the output monitor wich port is available on your machine port = new Serial(this, Serial.list()[0], 9600); song = minim.loadFile("1.mp3", 2048); // , ! *.mp3 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. beat.setSensitivity(300); kickSize = snareSize = hatSize = 0; // 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); } boolean k1,k2,k3; void draw() { float s=10; background(0); fill(255); if ( beat.isKick()) k1 = true; if ( beat.isSnare()) k2 = true; if ( beat.isHat()) k3 = true; // textSize(kickSize/8); // text("KICK", width/4, height/2); // textSize(snareSize/8); // text("SNARE", width/2, height/2); // textSize(hatSize/8); // text("HAT", 3*width/4, height/2); port.write('R'); port.write((int)kickSize); port.write('G'); port.write((int)snareSize); port.write('B'); port.write((int)hatSize); if (k1) kickSize = constrain(kickSize+5, 20, 255); else kickSize = constrain(kickSize -5, 20, 255); if (kickSize==255) k1=false; if (k2) snareSize = constrain(snareSize+15, 40, 255); else snareSize = constrain(snareSize-15, 40, 255); if (snareSize==255) k2=false; if (k3) hatSize = constrain(hatSize+s, 20, 255); else hatSize = constrain(hatSize-s, 20, 255); if (hatSize==255) k3=false; }
Source: https://habr.com/ru/post/214337/
All Articles