Setting all drum machine buttons to 0 with Minim Library

Hello everyone!
I am using Processing Audio Library Minim and the example -> Drum Machine
I can post the code here, but it is an example of the library so I dont think its needed :stuck_out_tongue:
Well, I am trying to set all the buttons of the drum machine to 0 like in the beginning when we run the code first time, to dont be always closing and opening. Is there a way to acess it from the outside? Like the press of a button.
Here is the class for the drum machine:

public class Rect {
  
  public int x, y, w, h;
  public boolean[] steps;
  public int stepId;

  public Rect(int _x, int _y, boolean[] _steps, int _id){
    x = _x;
    y = _y;
    w = 35;
    h = 30;
    steps = _steps;
    stepId = _id;
  }
  
  public void draw(){
    
    if (steps[stepId]){
      fill(157, 30, 139);
      
    } else {
      fill(38, 160, 180);
    }
    
    rect(x, y, w, h);
    
  }
  
  public void mousePressed(){
    if (mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
      steps[stepId] = !steps[stepId];
    
    }
  }
  public void print(){
    println(_steps[]);
  }
  
}

Best regards and thank you in advance!
(The last function is not working by the way)

So what are you trying to do with that last function?

1 Like

Hello!
Well, the thing is I am creating a button and then that button is pressed I want to bring all the steps of the drum machine to 0 like when you turn it on. But I canā€™t access the class property, or I donā€™t know how to do it. This last function I just created a print() that would print the steps[] or something for me to know the ones triggered (didnā€™t work btw). I really donā€™t know how to solve this problem.
Thanks in advance.

Hello :smiley:

So, Iā€™m taking it that steps is an array of booleans? First off, I donā€™t think you can print out arrayā€™s using println. I would do something like:

    for(int i = 0; i<steps.length; i++)
    {
     println(steps[i]);  
    }

In that print function. Also, the array variable for the class is steps, whereas _steps is what you passed in while creating the class, so that is why I put steps[i] .

Secondly, what exactly do you want to set to zero in terms of the class? The stepId? If so, you could make a set function, and using a loop you can set all of the stepId variables to zero. Iā€™m not sure if that is what you would want to do, but if so we could provide assistance on how that would be done?

Hopefully this helps,

EnhancedLoop7

1 Like

Hello again!
Yes I think that is accurate!
The thing is that in this class it only says what type of color for it to change. I might need to set them to 0 (if mousepressed puts them on 1). Like a reset to the whole drum machine (currently 6 rows of arrays).
Here I post another part of the code that regards to this!
Main page here

// -------------------------------------------------
// -------------------------------------------------
import g4p_controls.*;
import processing.opengl.*;
import ddf.minim.*;
import ddf.minim.ugens.*;
import ddf.minim.signals.*;
import controlP5.*;

Minim minim;
AudioOutput out;


Rect rect;
Tick tick;
Sampler kick;
Sampler snare;
Sampler hat;

ControlP5 cp5;

boolean[] row1 = new boolean[16];
boolean[] row2 = new boolean[16];
boolean[] row3 = new boolean[16];
boolean[] row4 = new boolean[16];
boolean[] row5 = new boolean[16];
boolean[] row6 = new boolean[16];

public boolean TOGGLE_VALUE = true;
public int BPM = 120;
public int BEAT;

ArrayList<Rect> buttons = new ArrayList<Rect>();


// -------------------------------------------------
// -------------------------------------------------

public void setup() {
  
  size(1100, 650, JAVA2D);
  minim = new Minim(this);
  cp5 = new ControlP5(this);
  out = minim.getLineOut();
  Power();
  Interface();



  kick = new Sampler("BD.wav", 4, minim); // 4 nr de vozes
  snare = new Sampler("SD.wav", 4, minim);
  hat = new Sampler("CHH.wav", 4, minim);
  
  
  kick.patch(out);
  snare.patch(out);
  hat.patch(out);
  
  
  for (int i = 0; i < 16; i++){
    buttons.add(new Rect(233+i*40, 155, row1, i));
    buttons.add(new Rect(233+i*40, 190, row2, i));
    buttons.add(new Rect(233+i*40, 225, row3, i));
    buttons.add(new Rect(233+i*40, 260, row4, i));
    buttons.add(new Rect(233+i*40, 295, row5, i));
    buttons.add(new Rect(233+i*40, 330, row6, i));
  }
  
  BEAT = 0;
  out.setTempo(BPM);
  out.playNote(0, 0.25f, new Tick());
}

public void draw() {
  
  background(20, 80, 89);
  stroke(0, 0, 0, 0);
  fill(24, 100, 110);
  rect(226, 150, 649, 215); //Grande
  fill(24, 100, 110);
  rect(226, 370, 649, 110); //Grande2
  fill(24, 100, 110);
  rect(226, 120, 649, 25); // Pequeno
  fill(24, 100, 110);
  rect(146, 120, 75, 25); // Pequeno2
  fill(24, 100, 110);
  rect(146, 150, 75, 215); // Pequeno3
  fill(24, 100, 110);
  rect(880, 120, 75, 245); // Pequeno4
  
  for (int i = 0; i < buttons.size(); i++){
  buttons.get(i).draw();
  }
  
  
  if (TOGGLE_VALUE == true) {
    fill(38, 160, 180);
    rect(233 + BEAT * 40, 125, 35, 15);
    out.unmute();

  }  
  
  if (TOGGLE_VALUE == false) {

    fill(0, 0, 0, 0);
    out.mute();
  }
}

  

void mousePressed(){
  for (int i = 0; i < buttons.size(); ++i){
    buttons.get(i).mousePressed();
  }
}

public void customGUI(){
}

[Then I have a button page that I call GUIā€™s from controlP5 library, so I can trigger things on click]
Here the rectangle class

public boolean TEST = false;

public class Rect {
  
  public int x, y, w, h;
  public boolean[] steps;
  public int stepId;

  public Rect(int _x, int _y, boolean[] _steps, int _id){
    x = _x;
    y = _y;
    w = 35;
    h = 30;
    steps = _steps;
    stepId = _id;
  }
  
  public void draw(){
    
    if (steps[stepId]){
      fill(157, 30, 139);
      
    } else {
      fill(38, 160, 180);
    }
    
    rect(x, y, w, h);
    
  }
  
  public void mousePressed(){
    if (mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
      steps[stepId] = !steps[stepId];
    
    }
  }
}

Then in here the Tick class that extends Instrument

public class Tick implements Instrument {
  
  public void noteOn(float dur){
    
    if (row1[BEAT]) hat.trigger();
    if (row2[BEAT]) snare.trigger();
    if (row3[BEAT]) kick.trigger();
  }
  
  public void noteOff() {
    
    BEAT = (BEAT+1)%16;
    out.setTempo(BPM);
    out.playNote(0, 0.25f, this);
  }  
}

Sorry for the spam lol.
So the thing would be. Imagine I make a mess out of this and I click a bunch of buttons, and I want them to return to 0 because I donā€™t want to close and open the software again.
Do you understand my point?
Thank you again
Best regards Luis!

Need some help here lol
Thanks in advance!
Best regards Luis

Hello and Iā€™m sorry for a delayed response,

But as I was saying, what exactly do you want to set to zero? You set when you click the buttons you want them to be to zero, but arenā€™t the buttons booleans? So Iā€™m just a little confused on what you mean by setting them to zero. Do you mean that you will set them to false? If so, you can do a for loop and set each index to false.

On your mousePressed() you could do a for loop and instead of what you have there, something like,

for(int i = 0; i<steps.length; i++)
{
 steps[i] = false; 
}

So maybe that would be good? Hopefully that helps,

EnhancedLoop7

Hello EnhancedLoop7!
No problem at all!
Thank you for your response, yes when I mean 0 I mean false lol like ā€œnot pressedā€ lol
But how can I access it from an outside function?
Everything is public but I canā€™t exact acess it. I am going to post here some pictures.
Here is the page of the ControlP5 buttons


So when I click the button the function toZero() is triggered.
In here next I have the main page with a function that is resetSampler() to get the setDown() function from the class because I canā€™t acess it. It gives me that error ->

here is the Rect class ->

With the for loop!
Best regards Luis
And thanks again EnhancedLoop7

No problem at all, can you please send your whole code now? For this kind of issue it would be easier for me to see where you are calling things and where they are coming from. Thank you!

EnhancedLoop7

Hi!
I am going to put here a wetransfer link to the code on a zipped folder!

https://we.tl/t-sA5HpJ8YeV

Best regards Luis

No problem, Iā€™ll take a look :smiley:

EnhancedLoop7

Thank you for your time and patience, really
I actually found a way, well but its like hardcoded :confused:
This question raised me other questions like communicating between classes, itā€™s this way we learn lol
Best regards Luis

Yes, Iā€™m actually still currently looking at your code. This could be done better with more communication between classes. I will let you know what I find and you let me know if you find anything too! :smiley:

EnhancedLoop7

Thank you again!
Best, Luis

What I basically did was also hard coded. I do not know if it is similar to what you had in mind, but, it works.

  public void mousePressed(){
    
//these are the coordinates for the reset button 
   if(mouseX >= 885 && mouseX <= 885+65 && mouseY >= 175 && mouseY< 175+45) 
     {   
      for(int i=0; i<steps.length; i++) 
       {
         steps[i] = false; //set everything to false in here 
       }  
     } //end of for loop
     
    else if (mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h){
         steps[stepId] = !steps[stepId]; //else, check if it's clicking something else
    } 
  } //end of mouse pressed

I personally do not know how else to approach this problem, because of the controlP5 aspect of it. You could do something similar without controlP5, which would be a two dimensional array of button classes that contains a pressed boolean and a row number. That, is what I would do. Hopefully this helps,

EnhancedLoop7

Hah, that was my hack! At least someone thinks like me ahah
Thank you very much EnhancedLoop7
Best regards Luis

1 Like

Haha yes :smiley: Of course there are plenty of ways, but to keep your code as it is this was the best way I could think of. By the way, your project looks really cool and I would love to see it when itā€™s done!

EnhancedLoop7

Thank you, really appreciated!
Yes of course, let me just have a solid version of it and iā€™ll host it online, can even add you as a collaborator on version control!
This project started as a bridge from pure data to processing using libpd (really not so much developed) and now I think iā€™ll take another path lol, perhaps make a version of Breaktweaker from iZotope but open source ahah
Best regards and thank you again!
Kindly, Luis

Hello again, I would love to be a collaborator on version control :smiley:

The project looks great. Hopefully we can work soon,

EnhancedLoop7

Absolutelly! Link me your github account and we make version control, I can add you to some other projects too :slight_smile:
Best, Luis

1 Like