Here's a very basic sequencer, using the MidiBus library to send midi events to the Java Sound Synthesizer. You'll have to install the MidiBus library in order for this to work, and depending on your system, you may need to use a different sound generator than the Java Sound Synth.
Because this uses MidiBus, you won't be able to get it work on a webpage. That requires using a different method of producing sound.
This is based on code originally written by James McLain - I was helping him earlier today and rewrote much of this.
- // Basic Sequencer Demo - James Mclain, Jim Bumgardner
- import themidibus.*;
- MidiBus myBus;
- Cell[][] grid;
- int kWidth = 1000;
- int kHeight = 833;
- int cols = 24;
- int rows = 20;
- int gridTop = 20;
- int gridLeft = 20;
- int gridWidth = kWidth - gridLeft*2;
- int gridHeight = kHeight - gridTop*2;
- float colWidth = gridWidth/cols;
- float rowHeight = gridHeight/rows;
- int kMidiChannel = 0; // Use 9 for the drum channel :)
- // jbum added this variable to track current playing row
- int curColumn = -1;
- void setup() {
- size(kWidth,kHeight);
- MidiBus.list();
-
- // jbum changed this not care about input device - this is an output only script
- myBus = new MidiBus(this, "", "Java Sound Synthesizer"); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device.
- // myBus = new MidiBus(this, "Microsoft MIDI Mapper", "Java Sound Synthesizer"); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device.
- smooth();
- frameRate(8); // jbum increased your framerate from .1
-
- grid = new Cell[cols][rows];
- for (int x = 0; x < cols; x++) {
- for (int y = 0; y < rows; y++) {
- grid[x][y] = new Cell(x,y);
- }
- }
- }
- void draw() {
- background(0);
- // Added this code to keep track of where we are, and where we've been
- curColumn = (curColumn + 1) % cols;
- int lastColumn = (curColumn + cols-1) % cols;
- for (int x = 0; x < cols; x++) {
- for (int y = 0; y < rows; y++) {
- Cell cell = grid[x][y];
- if (cell.stat && cell.x == curColumn)
- cell.noteOn();
- else if (cell.stat && cell.x == lastColumn)
- cell.noteOff();
- else
- cell.display();
- }
- }
- }
- boolean setState = false;
- void mousePressed()
- {
- int gx = (int) map(mouseX-gridLeft, 0, gridWidth, 0, cols);
- int gy = (int) map(mouseY-gridTop,0, gridHeight, 0, rows);
- if (gx >= 0 && gx < cols && gy >= 0 && gy < rows) {
- setState = !grid[gx][gy].stat;
- grid[gx][gy].stat = setState;
- }
- }
- void mouseDragged()
- {
- int gx = (int) map(mouseX-gridLeft, 0, gridWidth, 0, cols);
- int gy = (int) map(mouseY-gridTop,0, gridHeight, 0, rows);
- if (gx >= 0 && gx < cols && gy >= 0 && gy < rows) {
- grid[gx][gy].stat = setState;
- }
- }
- // A Cell object
- class Cell {
- int x, y;
- boolean stat;
- int noteNumber;
-
- // Cell Constructor
- Cell(int x, int y) {
- this.x = x;
- this.y = y;
- noteNumber = (int) map(y, 0, rows, 64-rows/2, 64+rows/2);
- stat = false;
- }
-
- void dodraw() {
- rect(x*colWidth+gridLeft,y*rowHeight+gridTop, colWidth, rowHeight);
- }
-
- void display() {
- stroke(255);
-
- int fillShade = stat? 80 : 20;
-
- fill(fillShade,fillShade,fillShade);
- dodraw();
- }
-
- void noteOn()
- {
- myBus.sendNoteOn(kMidiChannel, noteNumber, 40);
- fill(255,255,0);
- dodraw();
- }
-
- void noteOff()
- {
- myBus.sendNoteOff(kMidiChannel, noteNumber, 0);
- display();
- }
- } // end of Cell class
-