Drum-sequencer problem!
in
Contributed Library Questions
•
1 year ago
Hi guys,
I'm trying to make a drum-sequencing programme that plays drum samples with a control to change the bpm. I have 2 problems that I can' figure out. First, if I mouse-click outside of the grid I get an OutOfBoundsException and I don't know how to get around it. Secondly when a sample is triggered it sounds distorted (in fact, it sounds like the sample is being played three or four times in very quick succession). I've used sample.trigger() in another programme and it worked fine so I have no idea why its not working in this code. I've been scratching my head for days! I should point out I'm new to Processing and programming in general so apologies if my code is messy!
Any help or suggestions would be greatly appreciated!
Grid [][] grid;
import controlP5.*;
import ddf.minim.*;
ControlP5 gui;
Minim minim;
AudioSample kick, snare, hihat, crash;
Slider vSlider;
int columns = 20, rows = 9;
float speed;//total time across grid
float bpm;
float initial;
float next;//time elapsed
int step;//column along time line
static int clock;
boolean on = false;
void setup(){
size(600, 350);
background(0);
gui = new ControlP5(this);
vSlider = gui.addSlider("bpm", 80, 200, 510, 99, 15, 124);
minim = new Minim(this);
kick = minim.loadSample("BD2.wav", 128);
snare = minim.loadSample("SN.wav", 128);
hihat = minim.loadSample("HH.wav", 128);
crash = minim.loadSample("CR.wav", 128);
fill(255);
text("hihat", 55, 115);
fill(255);
text("kick", 55, 140);
fill(255);
text("snare", 55, 165);
fill(255);
text("crash", 55, 190);
grid = new Grid[columns][rows];
for (int c = 4; c < columns; c++) {
for (int r = 4; r < rows; r++) {
grid[c][r] = new Grid(c * 25, r * 25, 20, 20);
}
}
}
void draw() {
speed = (((60 / bpm) * 1000) * 16);//60 seconds divided by bpm times by a thousand (to make it milli seconds) times by 16 for the 16 columns of the grid
println(speed);
for (int c = 4; c < columns; c++) {
for (int r = 4; r < rows; r++) {
if((c == 4) || (c == 8) || (c == 12) || (c == 16)){
grid[c][r].display(210);//darker shade to signify 'first beat' of bar
}else{
grid[c][r].display(255);
}
}
if(on == true){
int r;
clock = millis();
if(clock - initial < speed){
next = (clock - initial);
step = floor(4 + (16 / (speed / next)));
for(r = 4; r < rows; r++){
grid[step][r].display(0);
grid[step][r].trigger(step, r);
}
}else{
initial = clock;
r=4;
}
}
}
}
void mousePressed() {
int colBox = int(mouseX)/25; //uses mouse coordinates to determine grid number
int rowBox = int(mouseY)/25;
grid[colBox][rowBox].checked();
}
void keyPressed(){
switch(key){
case 's':
initial = millis();
if(on == true){
on = false;
}else{
on = true;}
break;
}
}
void stop(){
kick.close();
hihat.close();
snare.close();
crash.close();
minim.stop();
super.stop();
}
class Grid {
float x, y, wd, ht;
boolean active = false;
Grid(float X, float Y, float WD, float HT) {
x = X;
y = Y;
wd = WD;
ht = HT;
}
void display(int col) {
stroke (255);
if(active == true){
fill(0);
}else{
fill(col);
}
rect(x, y, wd, ht);
}
void checked(){
if(active == true){
active = false;
}else{
active = true;
}
}
void trigger(int x, int y){
if (active == true){
if (y == 4){
hihat.trigger();
}
if (y == 5){
kick.trigger();
}
if (y == 6){
snare.trigger();
}
if (y == 7){
crash.trigger();
}
}
}
}
1