import javax.sound.midi.*; import themidibus.*; // Set var int bpm = 100; String file; PFont font; Sequencer sequencer; MidiBus myBus; // The MidiBus // setup void setup() { size(500, 200); smooth(); background(0); // load font and set as default font = loadFont("HelveticaNeue-Bold-40.vlw"); textFont(font); textSize(16); fill(200); text ("Press keys between 1 and 9 to generate midi tones.", 20,20, width, 200); // MIDI SEQUENCER try { // Load and open default sequencer sequencer = MidiSystem.getSequencer(); // Open Midi file file = selectInput(); if (file == null) { System.out.println("No file was selected ..."); } else { System.out.println("Selected file: " + file); textFont(font); textSize(16); fill(200); text ("Current Midi-File:\n" + file, 20,60, width, 200); } File myMidiFile = new File(file); // open sequencer if (sequencer == null) { } else { sequencer.open(); } // Load selected midi file Sequence mySeq = MidiSystem.getSequence(myMidiFile); // Allocate sequence to sequencer sequencer.setSequence(mySeq); sequencer.setTempoInBPM(bpm); } catch (Exception e) { System.out.println ("Error: " + e.toString()); } // MIDI SYNTHESIZER // initialize midibus myBus = new MidiBus(this,0,1); } void keyPressed() { // KEY TO PLAY A MIDI FILE if (key == 'p') { if (sequencer.isRunning()) { sequencer.stop(); textFont(font); textSize(16); fill(200); background(0); text ("Press keys between 1 and 9 to generate midi tones.", 20,20, width, 200); text ("Current Midi-File:\n" + file, 20,60, width, 200); } else { sequencer.start(); background(0); textFont(font); textSize(16); fill(200); text ("Press keys between 1 and 9 to generate midi tones.", 20,20, width, 200); text ("Playing:\n" + file, 20,60, width, 200); } } // KEYS TO PRODUCE MIDI SOUND if (key == '1') { // (channel, pitch, velocity) myBus.sendNoteOn(0, 64, 100); // Send a Midi } else if (key == '2'){ myBus.sendNoteOn(1, 66, 100); // Send a Midi } else if (key == '3'){ myBus.sendNoteOn(2, 68, 100); // Send a Midi } else if (key == '4'){ myBus.sendNoteOn(3, 70, 100); // aSend a Midi } else if (key == '5'){ myBus.sendNoteOn(4, 72, 100); // Send a Midi } else if (key == '6'){ myBus.sendNoteOn(5, 74, 100); // Send a Midi } else if (key == '7'){ myBus.sendNoteOn(6, 76, 200); // Send a Midi } else if (key == '8'){ myBus.sendNoteOn(7, 78, 200); // Send a Midi } else if (key == '9'){ myBus.sendNoteOn(8, 80, 200); // Send a Midi } } void draw() { } void stop() { sequencer.stop(); super.stop(); }