We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have recently create GUI using the GUI builder, but cant seem to know how to make the buttons to work, for example: a play button that wont play, slider that cant control the volume, stop and play button. How do I make the buttons to work? I'm using g4p and minim libraries. Below is my code
import g4p_controls.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer groove;
AudioMetaData meta;
BeatDetect beat;
float yoff = 0.0;
float slowChange1;
float slowChange2;
int screen=0;
void setup()
{
createGUI();
customGUI();
selectInput("Select a file to process:", "fileSelected");
size(640, 360, P3D);
ellipseMode(CENTER);
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
screen = 1;
} else {
println("User selected " + selection.getAbsolutePath());
minim = new Minim(this);
groove = minim.loadFile(selection.getAbsolutePath(), 2048);
groove.play();
beat = new BeatDetect();
screen = 2;
}
}
void draw()
{
if (screen ==2)
{
background(#7A9EB9);
fill(#FFA00F);
ellipse(320, 180, 300, 300);
beat.detect(groove.mix);
stroke(#40FAFF);
strokeWeight(2);
if(beat.isOnset()==true)
{
fill(#FF0808);
ellipse(270, 120, 60, 60);
fill(#2CE823);
ellipse(370, 120, 90, 90);
}
else
{
fill(#3B0D37);
ellipse(270, 120, 60, 60);
fill(#2754CB);
ellipse(370, 120, 90, 90);
}
if(keyPressed)
{
//sad
noFill();
for(int i = 0; i < groove.bufferSize() - 2; i++){
slowChange1 = lerp(slowChange1, groove.left.get(i), 0.2);
slowChange2 = lerp(slowChange2, groove.right.get(i), 0.2);
//Left right, up down, (how high)
arc(310, 320 + (30 * slowChange1), 100, 300, PI+QUARTER_PI, TWO_PI, OPEN);
}
}
else //happy
{
noFill();
for(int i = 0; i < groove.bufferSize() - 2; i++){
slowChange1 = lerp(slowChange1, groove.left.get(i), 0.1);
slowChange2 = lerp(slowChange2, groove.right.get(i), 0.1);
arc(320, 180 + (50 * slowChange2), 300, 200, 0, PI, OPEN);
}
}
fill(#0220D8);
beginShape();
float xoff = 0;
for (float x = 0; x <= width; x += 10) {
float y = map(noise(xoff, yoff), 10, 1, 0, 300);
vertex(x, y);
xoff += 0.10;
}
yoff += 0.10;
vertex(width, height);
vertex(0, height);
endShape(CLOSE);
}
else
{
rect(10,50,60,20);
}
}
void customGUI() {
}
//////////////////////////
/* ========================================================= * ==== WARNING === * ========================================================= * The code in this tab has been generated from the GUI form * designer and care should be taken when editing this file. * Only add/edit code inside the event handlers i.e. only * use lines between the matching comment tags. e.g.
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
* Do not rename this tab!
* =========================================================
*/
public void slider1_change1(GSlider source, GEvent event) { //_CODE_:slider1:950990:
println("slider1 - GSlider >> GEvent." + event + " @ " + millis());
} //_CODE_:slider1:950990:
public void imgButton1_click1(GImageButton source, GEvent event) { //_CODE_:imgButton1:626084:
println("imgButton1 - GImageButton >> GEvent." + event + " @ " + millis());
} //_CODE_:imgButton1:626084:
// Create all the GUI controls.
// autogenerated do not edit
public void createGUI(){
G4P.messagesEnabled(false);
G4P.setGlobalColorScheme(GCScheme.CYAN_SCHEME);
G4P.setCursor(ARROW);
surface.setTitle("Sketch Window");
slider1 = new GSlider(this, 40, 10, 170, 40, 10.0);
slider1.setLimits(0.5, 0.0, 1.0);
slider1.setNumberFormat(G4P.DECIMAL, 2);
slider1.setLocalColorScheme(GCScheme.GOLD_SCHEME);
slider1.setOpaque(false);
slider1.addEventHandler(this, "slider1_change1");
imgButton1 = new GImageButton(this, 260, 170, 100, 60, new String[] { "playz.png", "play1.png", "playz.png" } );
imgButton1.addEventHandler(this, "imgButton1_click1");
}
// Variable declarations
// autogenerated do not edit
GSlider slider1;
GImageButton imgButton1;
Answers
In your code you should have the statement
size(640, 360, P3D);
as the first line inside setup. Also move the statementminim = new Minim(this);
to inside the setup method as it should only be created once, not every time you load a file.The code below is a modified version of the 'Play a File' example that comes with minim to work with G4P. Notice that I use
setGain
to control the volume becausesetVolume
does not work in minimMain sketch code tab
gui.pde tab
Thanks for your help! But it says getValueF()): does not exist?
That is strange because the code I posted was from a working sketch. :-? Can you post the actual error message here?
It doesnt display the error now, but the slider wont work I dunno why
Did you have to modify the code to fix it or did it magically start working?
It magically start working
In that case there is not much I can suggest. Perhaps the slider will start working of its own accord ;)
It is still not working until now..