From c908ca694912508ac3d937d66a2316d6402cc379 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 10 Oct 2011 09:32:58 +0100 Subject: Added the midi seq. --- .../open/punchingbag/examples/MidiSequencer.java | 166 +++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 PunchingBag/src/uk/ac/open/punchingbag/examples/MidiSequencer.java 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(); + } + } + } + +} -- cgit v1.2.3