Sound and text error in Processing

Hello everyone :smiley:
I have a problem with this drummachine. I can not add another row of sound that is from my disktop and not from Minim. The 4. row just plays the previous soundpiece? What can I do to change it?
When I set the fill() for the text, the screen get this color! I do not know if it is because of the blinking square background.
Hope you see the error! thank you

import processing.sound.*;

import cc.arduino.;
import processing.serial.
;
import processing.opengl.*;

Serial myPort; //Creating a object from serial classes
SoundFile file;

import ddf.minim.;
import ddf.minim.ugens.
;

Minim minim;
AudioOutput out;

Sampler kick;
Sampler snare;
Sampler hat;
Sampler coin;

boolean[] hatRow = new boolean[16];
boolean[] snrRow = new boolean[16];
boolean[] kikRow = new boolean[16];
boolean[] coiRow = new boolean[16];

ArrayList buttons = new ArrayList();

int bpm = 200; // Beats pr. minute

int beat; // Indicateing which beat we are on

class Tick implements Instrument
{
void noteOn( float dur ) //The instrument implementation
{
if ( hatRow[beat] ) hat.trigger();
if ( snrRow[beat] ) snare.trigger();
if ( kikRow[beat] ) kick.trigger();
if ( coiRow[beat] ) kick.trigger();

}

void noteOff()
{
// next beat
beat = (beat+1)%16;
// set the new tempo
out.setTempo( bpm );
// play this again right now, with a sixteenth note duration
out.playNote( 0, 0.25f, this );
}
}

// simple class for drawing the gui
class Rect
{
int x, y, w, h;
boolean[] steps;
int stepId;

public Rect(int _x, int _y, boolean[] _steps, int _id)
{
x = _x;
y = _y;
w = 20;
h = 35;
steps = _steps;
stepId = _id;
}

void draw()
{
if ( steps[stepId] )
{
fill(255);
stroke(0);
}
else
{
fill(253,69,255);
}

rect(x,y,w,h);

}

public void mousePressed()
{
if ( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h )
{
steps[stepId] = !steps[stepId];
}
}
}

void setup()
{
size(500, 400);
minim = new Minim(this);
out = minim.getLineOut();
file = new SoundFile(this,“coin.wav”);

// loading all the samples, using 4 voices for each.
kick = new Sampler( “BD.wav”, 4, minim );
snare = new Sampler( “SD.wav”, 4, minim );
hat = new Sampler( “CHH.wav”, 4, minim );
coin = new Sampler( “coin.wav”, 4, minim);

// patch samplers to the output
kick.patch( out );
snare.patch( out );
hat.patch( out );
coin.patch( out );

for (int i = 0; i < 16; i++)
{
buttons.add( new Rect(10+i24, 50, hatRow, i ) );
buttons.add( new Rect(10+i
24, 100, snrRow, i ) );
buttons.add( new Rect(10+i24, 150, kikRow, i ) );
buttons.add( new Rect(10+i
24, 200, coiRow, i ) );

}

beat = 0;

// start the sequencer
out.setTempo( bpm );
out.playNote( 0, 0.25f, new Tick() );
}

void draw()
{
background(255);
textSize(25);
text(“Beatmaker”, 10, 30);
square(0, 0, 500); //square(x, y, extent) x and y is the cordinats of the placement and the extend is the size.
fill(255,292,103);

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

stroke(255);
if ( beat % 4 == 0 )
{
fill(255, 235, 242); // Light pink
}
else
{
fill(255, 217, 251); //Light purple
}

// beat marker
rect(10+beat*24, 25, 20, 9, 2);

}

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

Please format your code. To do so, you can paste the code in the forum, then highlight the code block and press the button </> and it will wrap your code with three back ticks. Doing this ensures the code is not changed and the format of your code is preserved.

Your original post (OP) there are two questions. It is better to keep a question per post.

Can you explain what do you mean when you add an extra row? What part of your code are you referring to?

For the color, which fill instruction are you referring to?

Kf

It is a drummachine, that has 3 rows of buttons. I want to add a 4. row and a new sound. As you see in the code there is 4 rows, but the last one plays the same sound as the 3. row.

After I have put in the text;
background(255);
textSize(25);
text(“Beatmaker”, 10, 30);

i could then continue with fill(0);, but the color I choose becomes the background.

thank you!

please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.


please REPAIR the code posting in your first post


what has your question to do with Arduino and serial library
what has your question to do with Minim sound library

in case it is about your arrays of buttons? the operation ? the button text ?
make a short code version what we can run without any libraries.

! and i expect that you do ( for test ) exactly what we would have to do:
copy paste your code posting back to PDE and press RUN

I am focusing on this code in your draw():

background(255);
textSize(25);
text(“Beatmaker”, 10, 30);
square(0, 0, 500); //square(x, y, extent) x and y is the cordinats of the placement and the extend is the size.
fill(255,292,103);

... //Rest of your code

Because square is draw after the text, the square will be draw on top of your text. What color do you want your square and your text? Say you want your square to be filled with yellow and your text to be red, then I will do this:

background(255);

fill(255,255,0);  //Yellow
square(0, 0, 500); //square(x, y, extent) x and y is the cordinats of the placement and the extend is the size.

textSize(25);
fill(255,0,0);  //Red
text(“Beatmaker”, 10, 30);
fill(255,292,103);

... //Rest of your code

So I draw the square BEFORE I draw the text.
You have control of the colors at all times. You also have the control of the placements on your sketch. The order you place your objects in your sketch matters. This should resolve one of your questions.

Kf

I copied formatted and fixed your original code. I made some cosmetic changes so I could see things properly. The main issue was in line 38 in the Tick class where you had kick.trigger() instead of coin.trigger(). Hope this helps

import processing.sound.*;
import cc.arduino.*;
import processing.serial.*;
import processing.opengl.*;

Serial myPort; //Creating a object from serial classes
SoundFile file;

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;

Sampler kick;
Sampler snare;
Sampler hat;
Sampler coin;

boolean[] hatRow = new boolean[16];
boolean[] snrRow = new boolean[16];
boolean[] kikRow = new boolean[16];
boolean[] coiRow = new boolean[16];

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

int bpm = 200; // Beats pr. minute

int beat; // Indicateing which beat we are on

class Tick implements Instrument
{
  void noteOn( float dur ) //The instrument implementation
  {
    if ( hatRow[beat] ) hat.trigger();
    if ( snrRow[beat] ) snare.trigger();
    if ( kikRow[beat] ) kick.trigger();
    if ( coiRow[beat] ) coin.trigger();
  }

  void noteOff()
  {
    // next beat
    beat = (beat+1)%16;
    // set the new tempo
    out.setTempo( bpm );
    // play this again right now, with a sixteenth note duration
    out.playNote( 0, 0.25f, this );
  }
}

// simple class for drawing the gui
class Rect
{
  int x, y, w, h;
  boolean[] steps;
  int stepId;

  public Rect(int _x, int _y, boolean[] _steps, int _id)
  {
    x = _x;
    y = _y;
    w = 20;
    h = 35;
    steps = _steps;
    stepId = _id;
  }

  void draw()
  {
    if ( steps[stepId] )
    {
      fill(255);
      stroke(0);
    } else
    {
      fill(253, 69, 255);
    }

    rect(x, y, w, h);
  }

  public void mousePressed()
  {
    if ( mouseX >= x && mouseX <= x+w && mouseY >= y && mouseY <= y+h )
    {
      steps[stepId] = !steps[stepId];
    }
  }
}

void setup()
{
  size(500, 400);
  minim = new Minim(this);
  out = minim.getLineOut();
  file = new SoundFile(this, "coin.wav");

  // loading all the samples, using 4 voices for each.
  kick = new Sampler( "BD.wav", 4, minim );
  snare = new Sampler( "SD.wav", 4, minim );
  hat = new Sampler( "CHH.wav", 4, minim );
  coin = new Sampler( "coin.wav", 4, minim);

  // patch samplers to the output
  kick.patch( out );
  snare.patch( out );
  hat.patch( out );
  coin.patch( out );

  for (int i = 0; i < 16; i++)
  {
    buttons.add( new Rect(10+i*24, 50, hatRow, i ) );
    buttons.add( new Rect(10+i*24, 100, snrRow, i ) );
    buttons.add( new Rect(10+i*24, 150, kikRow, i ) );
    buttons.add( new Rect(10+i*24, 200, coiRow, i ) );
  }

  beat = 0;

  // start the sequencer
  out.setTempo( bpm );
  out.playNote( 0, 0.25f, new Tick() );
}

void draw()
{
  background(255);
  square(0, 0, 500); //square(x, y, extent) x and y is the cordinats of the placement and the extend is the size.
  fill(0);
  textSize(20);
  text("Beatmaker", 10, 30);

  fill(255, 292, 103);

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

  stroke(255);

  // beat marker
  fill(0);
  rect(10+beat*24, 35, 20, 9, 2);


  if ( beat % 4 == 0 )
  {
    fill(255, 235, 242); // Light pink
  } else
  {
    fill(255, 217, 251); //Light purple
  }
}

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

2 Likes