control p5 errors
in
Contributed Library Questions
•
3 years ago
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.
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; //
void setup(){
size(640,480 + traySize); // size capture + room for sliders
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);
addToRadioButton(r,"r=r",1);
addToRadioButton(r,"r=g",2);
addToRadioButton(r,"r=b",3);
g = controlP5.addRadioButton("radioButtonG",width-105,height-55);
g.setColorForeground(color(120));
g.setColorActive(color(255));
g.setColorLabel(color(255));
g.setItemsPerRow(3);
g.setSpacingColumn(20);
addToRadioButton(g,"g=r",4);
addToRadioButton(g,"g=g",5);
addToRadioButton(g,"g=b",6);
b = controlP5.addRadioButton("radioButtonB",width-105,height-40);
b.setColorForeground(color(120));
b.setColorActive(color(255));
b.setColorLabel(color(255));
b.setItemsPerRow(3);
b.setSpacingColumn(20);
addToRadioButton(b,"b=r",7);
addToRadioButton(b,"b=g",8);
addToRadioButton(b,"b=b",9);
slider = controlP5.addSlider("attackLength" ,0,800,50 ,10 ,yTray ,sliderLength ,sliderHeight);
slider2 = controlP5.addSlider("highPass" ,0,1 ,.5 ,10 ,yTray+offset ,sliderLength ,sliderHeight);
slider3 = controlP5.addSlider("colourDif" ,0,250,100,10 ,yTray+offset*2,sliderLength ,sliderHeight);
slider4 = controlP5.addSlider("rFactor" ,0,2 ,1 ,350,yTray ,sliderLength/2,sliderHeight);
slider5 = controlP5.addSlider("bFactor" ,0,2 ,1 ,350,yTray+offset ,sliderLength/2,sliderHeight);
slider6 = controlP5.addSlider("gFactor" ,0,2 ,1 ,350,yTray+offset*2,sliderLength/2,sliderHeight);
slider7 = controlP5.addSlider("maxi" ,0,200,100,10 ,yTray+offset*3,sliderLength ,sliderHeight);
slider8 = controlP5.addSlider("alphaInvert" ,0,255,50 ,10 ,yTray+offset*4,sliderLength ,sliderHeight);
slider9 = controlP5.addSlider("alphaInNormal" ,0,255,50 ,10 ,yTray+offset*5,sliderLength ,sliderHeight);
slider10 = controlP5.addSlider("alphaNormal" ,0,255,255,10 ,yTray+offset*6,sliderLength ,sliderHeight);
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;
}
}
/*
----------------------CLEAN-UP-------------------
*/
public void stop(){
m.stop();
in.close();
minim.stop();
super.stop();
}
1