help with a midi sequencer (drum machine type)
in
Programming Questions
•
2 years ago
Hey All,
If anyone is interested in munching on some code, I'd greatly appreciate a push over this hump. I'm trying to implement a simple MIDI drum sequencer. Basically click on a square on one of the rows of squares to get that note to sound every time the time indicator comes around in the loop.
I'm having trouble with my logic for clicking on a square and dragging across multiple squares to turn them on/off. Basically, if a square is "off", I want to be able to click it to turn it on, and then drag across other "off" squares to turn them on, and vice versa if they a square is initially "on".
I'm a super noob to OO programming, so my logic (if you even want to call it that after looking at my code) may be pretty out there. Any pushes in the right direction will be GREATLY appreciated!
If anyone is interested in munching on some code, I'd greatly appreciate a push over this hump. I'm trying to implement a simple MIDI drum sequencer. Basically click on a square on one of the rows of squares to get that note to sound every time the time indicator comes around in the loop.
I'm having trouble with my logic for clicking on a square and dragging across multiple squares to turn them on/off. Basically, if a square is "off", I want to be able to click it to turn it on, and then drag across other "off" squares to turn them on, and vice versa if they a square is initially "on".
I'm a super noob to OO programming, so my logic (if you even want to call it that after looking at my code) may be pretty out there. Any pushes in the right direction will be GREATLY appreciated!
//import themidibus.*; // Import the library
//MidiBus myBus; // The MidiBus
int TALLNESS = 8;
int WIDENESS = 16;
int x_position = 100;
int y_position = 100;
int r_def = 128;
int g_def = 255;
int b_def = 0;
int tile_size_x = 30;
int tile_size_y = 30;
Sequencer berry = new Sequencer(x_position, y_position, WIDENESS, TALLNESS, tile_size_x, tile_size_y, r_def, g_def, b_def);
void setup()
{
// MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
size(800, 600);
noStroke();
smooth();
//MIDI STUFF
// myBus = new MidiBus(this, 2, 2);
int i, j;
}
void draw (){
background(0);
berry.display();
}
public class Tile extends MIDI_Parts {
int x_position; // starting x position of tile
int y_position; // starting y position of tile
int x_dim; // how wide the tile is
int y_dim; // how tall the tile is
// Color components
int r_on;
int g_on;
int b_on;
int r_off = 100;
int g_off = 100;
int b_off = 100;
boolean mouseHeldTile = false; // used in isTouched() to keep the method from toggling the on/off state while the mouse is held
boolean on = false; // tile's on/off state
public Tile(int x_position_in, int y_position_in, int x_dim_in, int y_dim_in, int r_in, int g_in, int b_in) {
x_position = x_position_in;
y_position = y_position_in;
x_dim = x_dim_in;
y_dim = y_dim_in;
r_on = r_in;
g_on = g_in;
b_on = b_in;
}
boolean isTouched() {
/*
THIS IS THE METHOD I NEED HELP WITH... WHENEVER I DRAG ACROSS TILES, IT JUST TOGGLES THE STATE. I WANT EACH TILE DRAGGED ACROSS TO TAKE THE STATE
OPPOSITE OF THE FIRST TILE THAT IS TOUCHED... I.E. IF I CLICK ON ONE TILE TO TURN IT OFF, I CAN THEN DRAG ACROSS OTHER TILES THAT ARE ON AND TURN THEM OFF
WITHOUT TURNING ON TILES DRAGGED ACCROSS THAT WERE OFF
*/
if ((mousePressed && (mouseButton == LEFT))&&(((mouseX>=x_position)&&(mouseX<=(x_position+x_dim-1)))&&((mouseY>=y_position)&&(mouseY<=(y_position+y_dim-1))))){
if (!mouseHeldTile){
mouseHeldTile = true;
if (on){
on = false;
}else{
on = true;
}
}
return true;
}else{
mouseHeldTile = false;
return false;
}
}
void display() {
stroke(0);
strokeWeight(1);
if (on){
fill (r_on, g_on, b_on); // if tile is "on", make it the color of an "on" tile
}else{
fill(r_off, g_off, b_off); // else make it the color of an "off" tile
}
rect(x_position, y_position, x_dim, y_dim); // draw the tile
}
}
// this is just a class that creates rows of tiles. I use a sepearte class for each track in case I want to create track-specific MIDI control attributes.
class Track{
int x_position;
int y_position;
int tile_size_x;
int tile_size_y;
int r, g, b;
int wide;
int swing;
Tile tile[];
Track(int x_position_in, int y_position_in, int wide_in, int tile_size_xin, int tile_size_yin, int r_in, int g_in, int b_in){
tile = new Tile[wide_in];
x_position = x_position_in;
y_position = y_position_in;
wide = wide_in;
r = r_in;
g = g_in;
b = b_in;
tile_size_x = tile_size_xin;
tile_size_y = tile_size_yin;
int j;
for (j = 0; j < wide_in; j++){
tile[j] = new Tile(x_position + (j*tile_size_x), y_position, tile_size_x, tile_size_y, r, g, b);
tile[j].r_on = r;
tile[j].g_on = g;
tile[j].b_on = b;
tile[j].x_position = x_position + (j*tile_size_x);
tile[j].y_position = y_position;
tile[j].x_dim = tile_size_x;
tile[j].y_dim = tile_size_y;
}
}
void display(){
int j;
for (j = 0; j < wide; j++){
tile[j].isTouched(); // calls the method in the Tile class that changes the "on/off" state of the tiles if they are clicked on
tile[j].display();
}
}
}
// Class that creates rows of Track objects and one beat indicator row at the bottom. Will contain other elements of the sequencer once I get past the stupid isTouched issue
class Sequencer{
int x_position;
int y_position;
int wide;
int tall;
int tile_size_x;
int tile_size_y;
int r_def;
int g_def;
int b_def;
Track track[];
Tile beat[];
Sequencer (int x_position_in, int y_position_in, int wide_in, int tall_in, int tile_size_xin, int tile_size_yin, int r_in, int g_in, int b_in){
x_position = x_position_in;
y_position = y_position_in;
wide = wide_in;
tall = tall_in;
tile_size_x = tile_size_xin;
tile_size_y = tile_size_yin;
r_def = r_in;
g_def = g_in;
b_def = b_in;
// tiles = new Tile[tall][wide];
track = new Track[wide];
beat = new Tile[wide];
int i, j;
for (j = 0; j < wide; j++){
beat[j] = new Tile(x_position + (j*tile_size_x), y_position + (tall*tile_size_y), tile_size_x, tile_size_y/4, 110, 110, 110);
for (i = 0; i < tall; i++){
track[i] = new Track(x_position, y_position + (i*tile_size_y), wide, tile_size_x, tile_size_y, r_def, g_def, b_def);
}
}
}
void display(){
int i, j;
for (j = 0; j < wide; j++){
beat[j].display(); // display the beat indicator row at the bottom
}
for (i = 0; i < tall; i++){
// tiles[i][j].display();
track[i].display();
}
}
}
// class that contains all the MIDI attributes of a given object... not completed
class MIDI_Parts {
int channel_TX;
int noteOn_TX;
int noteOff_TX;
int noteOn_RX;
int noteOff_RX;
int cc_TX;
void MIDI_Parts (int chan_TX_in, int noteOn_TX_in, int noteOff_TX_in, int noteOn_RX_in, int noteOff_RX_in, int cc_TX_in){
channel_TX = chan_TX_in;
noteOn_TX = noteOn_TX_in;
noteOff_TX = noteOff_TX_in;
noteOn_RX = noteOn_RX_in;
noteOff_RX = noteOff_RX_in;
cc_TX = cc_TX_in;
}
}
1