sample.trigger() problem drum sequencer
in
Contributed Library Questions
•
1 year ago
Hi guys,
I've already posted about this and phi.lho was kind enough to impart some of his knowledge in answering some of my questions. One of the unanswered problems though was that of my drum samples being triggered multiple times in quick succession, making them sound distorted. I think I now understand why this is happening, but still cant come up with a solution. Here's my code so far:
- Grid [][] grid;
- import controlP5.*;
- import ddf.minim.*;
- ControlP5 gui;
- Minim minim;
- AudioSample kick, snare, hihat, crash;
- Slider bpmSlider;
- int columns = 20, rows = 9;
- float speed;//total time across grid
- float bpm;
- float initial;
- float current;
- float next;
- float gridd;
- int step;//column along grid
- static int clock;
- boolean on = false;
- void setup(){
- size(735, 350);
- background(0);
- gui = new ControlP5(this);
- bpmSlider = gui.addSlider("bpm", 80, 200, 228, 265, 160, 15);
- minim = new Minim(this);
- kick = minim.loadSample("BD.mp3", 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) * 4);//60 seconds divided by bpm times by a thousand (to make it milli seconds) times by 4 for the 4 beats on the grid
- 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 beat
- }else{
- grid[c][r].display(255);
- }
- }
- if(on == true){
- int r;
- clock = millis();
- current = clock - initial;
- next = speed / current;
- println(next);
- gridd = (4 + (16 / next));
- if(current < speed){
- step = floor(gridd);
- for(r = 4; r < rows; r++){
- grid[step][r].display(0);
- grid[step][r].trigger(step, r);
- }
- }else{
- initial = clock;
- }
- }
- }
- }
- void mousePressed() {
- if(mouseX > 100 && mouseX < 500 && mouseY > 100 && mouseY < 225){// 'if mouse is clicked within grid'
- 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 'm':
- 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(float x, float y){
- if (active == true){
- if (y == 4){
- hihat.trigger();
- }
- if (y == 5){
- kick.trigger();
- }
- if (y == 6){
- snare.trigger();
- }
- if (y == 7){
- crash.trigger();
- }
- }
- }
- }
Any help or suggestions on this would be greatly appreciated!
Thanks for your time
1