void setup() { minim = new Minim(this); in = minim.getLineIn(); bpf = new BandPass(55, 12, in.sampleRate()); in.addEffect(bpf); beat = new BeatDetect(in.bufferSize(), in.sampleRate()); beat.setSensitivity(300); bl = new BeatListener(beat, in); //beat.detect(in.mix); //bpf filter helps beatDetection algo when bass beat detection is only //wanted } void draw(){
if ( beat.isOnset() ) { println("beat!"); //what you want it to do } }
void stop() { // always close Minim audio classes when you are finished with them in.close(); // always stop Minim before exiting minim.stop(); // this closes the sketch super.stop(); } class BeatListener implements AudioListener { private BeatDetect beat; private AudioInput source;
I'm working on a live filter for camera input, and I have been tinkering a little with this still somewhat scruffy code. It works but it gives me a general console error every time I move the P5 sliders:
ERROR. an error occured while forwarding a Controller value
to a method in your program. please check your code for any
possible errors that might occur in this method .
e.g. check for casting errors, possible nullpointers, array overflows ... .
method: controlEvent
exception: java.lang.reflect.InvocationTargetException
Additionally the radio buttons seem not always be responsive. I'm really new to this library I'm not sure where it went wrong.
/* ----------------------INITIAL SETUP----------------------------- */ import controlP5.*; // import slider, sound and video library import ddf.minim.*; import JMyron.*;
Minim minim; ControlP5 controlP5;
RadioButton r; // creates three radio buttons and 10 sliders for p5 RadioButton g; RadioButton b; Slider slider; Slider slider2; Slider slider3; Slider slider4; Slider slider5; Slider slider6; Slider slider7; Slider slider8; Slider slider9; Slider slider10;
AudioInput in; // the mic Timer attackTimer; // timer for fading images over time JMyron m;// camera object
/* ---------------------VARIABLES----------------------------------- */ int traySize = 120; // extension amount of the video window for sliders etc. int yTray = 490; // y coordinate for first slider int sliderLength = 250; // x size slider int sliderHeight = 10; // y size slider int offset = sliderHeight+5;// y space between sliders
float maxi = 80; // threshold for brightness filter float highPass = 0.50; // threshold for mic to trigger filter float attackLength = 50.0; // fade duration for filter effect float alphaInvert = 50.0; // opacity for above threshold part of filtered image float alphaInNormal = 50.0; // opacity for below threshold part of filterd image float alphaNormal = 255; // opacity for normal camera image
boolean attackOn = false; // stores whether the filter is still triggered float endTime; // stores in millis the end of the attack time of the filter float colourFadeFactor; // stores how fast the the filter should fade base on attack time and colour difference float colourFade; // stores the colour of the fading effect
float colourDif = 150.0; // stores how much colour the above threshold part of filtered image has float rFactor = 1; // float gFactor = 1; // RGB modifiers for the above threshold part of filtered image float bFactor = 1; // int rChannel = 1; // int gChannel = 2; // reference values RGB channels for radio buttons int bChannel = 3; //
minim = new Minim(this); attackTimer = new Timer(); controlP5 = new ControlP5(this);
/* ------------------BUTTONS & SLIDERS-------------------------------- */ // these buttons are for switching channels in the filter so red // can be mapped to green et cetera et cetera.
r = controlP5.addRadioButton("radioButtonR",width-105,height-70); r.setColorForeground(color(120)); r.setColorActive(color(255)); r.setColorLabel(color(255)); r.setItemsPerRow(3); r.setSpacingColumn(20);
in = minim.getLineIn(Minim.STEREO, 512); m = new JMyron(); m.start(width,height - traySize);//start a capture at width height minus slider area m.findGlobs(0);//disable the glob tracking to speed up frame rate }
void draw(){ m.update();//update the camera view background(125); // bg for the sliders and buttons int[] img = m.image(); //get the normal image of the camera float r,g,b,a; loadPixels();
if (attackOn == true) // if the filter gets triggered the timer contantly updates the fading colour { attackTimer.update(); }
for(int i = 0; i < in.bufferSize() - 1; i++) // triggers the filter effect if the mic threshold is reached { if (in.left.get(i) > highPass) { attackTimer.timer(attackLength); } }
/* The drawing part. If the values of R,G,B are all higher than the threshold value it will fill in those pixels with colour over time based on the attackLength with colourDif intensity modified by channel swapping and the r g b modifiers all controllable via buttons */
if (attackOn == true){ for(int i = 0; i < width * (height - traySize); i++){ //loop through all the pixels if (red(img[i]) > maxi && green(img[i]) > maxi && blue(img[i]) > maxi) { if (rChannel == 3) { r = colourFade * rFactor; } else if (rChannel == 2) { r = colourFade * rFactor; } else { r = colourFade * rFactor; } if (gChannel == 3) { g = colourFade * gFactor; } else if (gChannel == 1) { g = colourFade * gFactor; } else { g = colourFade * gFactor; } if (bChannel == 1) { b = colourFade * bFactor; } else if (bChannel == 2) { b = colourFade * bFactor; } else { b = colourFade * bFactor; } a = alphaInvert; } else { r = red(img[i]); g = green(img[i]); b = blue(img[i]); a = alphaInNormal; } pixels[i] = color(r,g,b,a); //draw each pixel to the screen } } else { for(int i = 0 ; i < width * (height - traySize); i++){ r = red(img[i]); g = green(img[i]); b = blue(img[i]); a = alphaNormal; pixels[i] = color(r,g,b,a); } } updatePixels(); }
// Colour fading class, sets the fade rate and calculates // what colour at any given time class Timer{ void timer(float interval){ attackOn = true; endTime = millis() + interval; colourFadeFactor = colourDif/interval; } void update(){ colourFade = colourFadeFactor * (endTime - millis()); if (millis() > endTime){ attackOn = false; } } }
// function for creating radio buttons void addToRadioButton(RadioButton theRadioButton, String theName, int theValue ) { Toggle t = theRadioButton.addItem(theName,theValue); t.captionLabel().setColorBackground(color(80)); t.captionLabel().style().movePadding(2,0,-1,2); t.captionLabel().style().moveMargin(-2,0,0,-3); t.captionLabel().style().backgroundWidth = 15; }
// RGB swapping function for the radio buttons void controlEvent(ControlEvent theEvent) { int buttonIndex = int(theEvent.group().value()); switch(buttonIndex) { case 1: rChannel = 1; break; case 2: rChannel = 2; break; case 3: rChannel = 3; break; case 4: gChannel = 1; break; case 5: gChannel = 2; break; case 6: gChannel = 3; break; case 7: bChannel = 1; break; case 8: bChannel = 2; break; case 9: bChannel = 3; break; default: break; } }
I am trying to upscale a webcam stream using JMyron. I have noticed that processing copes badly with higher resolution input, so Instead I want the input scaled up twice the size so it fills up a little more of the screen. Is there an easy way I am missing here, or do I work on the more manual version down below, currently it does not work.
I though a 2x scale
algorithm could be interesting to see on live footage, and it doesn't add many lines of code. But alas I am just getting a grey rectangle it is the right size (twice the capture resolution) but no video. I think I am making some error on the display side of things, the math seems right. Here is the code:
import JMyron.*; JMyron m; int camWidth = 320; //capture resolution int camHeight = 240;
void setup(){ size(640,480); // size is twice the capture res m = new JMyron(); m.start(camWidth,camHeight); m.findGlobs(0); println("Myron " + m.version()); }
void draw(){ m.update();//update the camera int[] img = m.image(); //get frame of the camera int B,D,E,F,H,E0,E1,E2,E3; loadPixels(); /* this is my implementation of super2x it uses a 9x9 grid around the pixel A | B | C | D | E | F | G | H | I | where E is expanded to four pixels E0 | E1 E2 | E2 based on the relation between different diagonals: B-F, F-H, H-D, D-B see: http://scale2x.sourceforge.net/algorithm.html for full detials */ for(int i=0;i<camWidth*camHeight;i++){ //loop through all the captured pixels E = color(img[i]); // checks if given pixel is at an edge and if so fills the of edge pixel with E's value. B = i < camWidth ? E : color(img[i-camWidth]);// pixel above E D = i < 1 ? E : color(img[i-1]);// pixel to the left of E F = i > camWidth * camHeight - 1 ? E : color(img[i+1]);// pixel to the right of E H = i > camWidth * camHeight - camWidth ? E : color(img[i+camWidth]);// pixel below E // checks for diagonal relations if (B != H && D != F){ E0 = D == B ? D : E; E1 = B == F ? F : E; E2 = D == H ? D : E; E3 = H == F ? F : E; // if there are none regular scaling occurs } else { E0 = E; E1 = E; E2 = E; E3 = E; } for (int j=0;1<width*height;j++){ // expands the read pixels in to 2 x scale pixels[(i*2)] = E0; pixels[(i*2+1)] = E1; pixels[(i*2+width)] = E2; pixels[(i*2+width+1)] = E3; } } updatePixels(); }
public void stop(){ m.stop();//clean up super.stop(); }