diff options
author | Christopher Baines <cbaines8@gmail.com> | 2011-10-10 09:32:58 +0100 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2011-10-10 09:32:58 +0100 |
commit | c908ca694912508ac3d937d66a2316d6402cc379 (patch) | |
tree | 519ca27329bf750ac0fe4f07c66a45d7963dcad4 /PunchingBag | |
parent | 235298571505c669b0e7c976478e74ddbf804700 (diff) | |
download | punchingbag-c908ca694912508ac3d937d66a2316d6402cc379.tar punchingbag-c908ca694912508ac3d937d66a2316d6402cc379.tar.gz |
Added the midi seq.
Diffstat (limited to 'PunchingBag')
-rw-r--r-- | PunchingBag/src/uk/ac/open/punchingbag/examples/MidiSequencer.java | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/MidiSequencer.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/MidiSequencer.java new file mode 100644 index 0000000..6947778 --- /dev/null +++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/MidiSequencer.java @@ -0,0 +1,166 @@ +package uk.ac.open.punchingbag.examples; + +import java.awt.Color; + +import javax.sound.midi.MidiEvent; +import javax.sound.midi.MidiSystem; +import javax.sound.midi.Sequence; +import javax.sound.midi.Sequencer; +import javax.sound.midi.ShortMessage; +import javax.sound.midi.Track; +import javax.swing.JCheckBox; + +import uk.ac.open.punchingbag.ButtonListener; +import uk.ac.open.punchingbag.Contact; +import uk.ac.open.punchingbag.PunchingBag; + +public class MidiSequencer implements ButtonListener, Runnable { + + Sequencer sequencer; + Sequence sequence; + Track track; + int tempo = 120; + + PunchingBag bag = PunchingBag.getBag(); + + boolean[][] buttons = new boolean[8][20]; + + String[] instrumentNames = { "Bass Drum", "Closed Hi-Hat", "Open Hi-Hat", "Acoustic Snare", "Crash Cymbal", "Hand Clap", "High Tom", "Hi Bongo", "Maracas", + "Whistle", "Low Conga", "Cowbell", "Vibraslap", "Low-mid Tom", "High Agogo", "Open Hi Conga" }; + + int[] instruments = { 35, 42, 46, 38, 49, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63 }; + + public MidiSequencer() { + bag.addButtonListener(this); + setUpMidi(); + new Thread(this).start(); + } + + public void setUpMidi() { + try { + sequencer = MidiSystem.getSequencer(); + sequencer.open(); + sequence = new Sequence(Sequence.PPQ, 4); + track = sequence.createTrack(); + sequencer.setTempoInBPM(120); + + } catch (Exception e) { + e.printStackTrace(); + } + } // close method + + public void buildTrackAndStart() { + int[] trackList = null; + + sequence.deleteTrack(track); + track = sequence.createTrack(); + + for (int y = 0; y < bag.buttonHeight; y++) { + trackList = new int[bag.buttonWidth]; + + int key =0; + if (y < instruments.length) { + key = instruments[y]; + } else { + // key = instruments[y - instruments.length]; + } + + for (int x = 0; x < bag.buttonWidth; x++) { + if (buttons[x][y]) { + trackList[x] = key; + } else { + trackList[x] = 0; + } + } // close inner loop + + makeTracks(trackList); + track.add(makeEvent(176, 1, 127, 0, 16)); + } // close outer + + track.add(makeEvent(192, 9, 1, 0, 15)); + try { + sequencer.setSequence(sequence); + //sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY); + sequencer.setLoopCount(1); + sequencer.start(); + sequencer.setTempoInBPM(tempo); + } catch (Exception e) { + e.printStackTrace(); + } + } // close buildTrackAndStart method + + public void makeTracks(int[] list) { + + for (int i = 0; i < bag.buttonWidth; i++) { + int key = list[i]; + + if (key != 0) { + track.add(makeEvent(144, 9, key, 100, i)); + track.add(makeEvent(128, 9, key, 100, i + 1)); + } + } + } + + public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) { + MidiEvent event = null; + try { + ShortMessage a = new ShortMessage(); + a.setMessage(comd, chan, one, two); + event = new MidiEvent(a, tick); + + } catch (Exception e) { + e.printStackTrace(); + } + return event; + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + + @Override + public void buttonPressed(int x, int y) { + System.out.println("Button pressed, drawing"); + buttons[x][y] = !buttons[x][y]; + if (buttons[x][y]) { + bag.fillRect(x, y, 2, 2, Color.red); + // bag.drawRectCenter(x, y, 1, 1, Color.red); + } else { + bag.fillRect(x, y, 2, 2, Color.white); + // bag.drawRectCenter(x, y, 1, 1, Color.white); + } + } + + @Override + public void contact(Contact c) { + // TODO Auto-generated method stub + + } + + @Override + public void run() { + while (true) { + buildTrackAndStart(); + try { + for (int x = 0; x < 8; x++) { + bag.fillRect(x, 0, 2, 8, Color.green); + for (int y = 0; y < bag.buttonHeight; y++) { + if (buttons[x][y]) + bag.fillRect(x, y, 2, 2, Color.red); + } + Thread.sleep(60000 / 120 / 4); + bag.fillRect(x, 0, 2, 8, Color.white); + for (int y = 0; y < bag.buttonHeight; y++) { + if (buttons[x][y]) + bag.fillRect(x, y, 2, 2, Color.red); + } + } + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + +} |