aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2011-09-16 13:05:26 +0100
committerChristopher Baines <cbaines8@gmail.com>2011-09-16 13:05:26 +0100
commitd0569a3346faf63f973b804c4ab55f52c58c0df7 (patch)
tree6e61dcf841eb3679f5bf88d96f2c48bb6239466b
parent201f4564f236ee1a15ebfe1efc16929375f9947c (diff)
downloadpunchingbag-d0569a3346faf63f973b804c4ab55f52c58c0df7.tar
punchingbag-d0569a3346faf63f973b804c4ab55f52c58c0df7.tar.gz
Beginings of midi stuff.
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java2
-rw-r--r--PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java163
2 files changed, 68 insertions, 97 deletions
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
index eee961c..0c33499 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/PunchingBagGUI.java
@@ -104,7 +104,7 @@ public class PunchingBagGUI implements MouseListener, MouseMotionListener,
// Mixer mix = AudioSystem.getMixer(AudioSystem.getMixerInfo()[0]);
try {
- // bag.connectToArduinos();
+ bag.connectToArduinos();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
diff --git a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
index 7c6c655..9c05254 100644
--- a/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
+++ b/PunchingBag/src/uk/ac/open/punchingbag/examples/SimpleKeyboard.java
@@ -28,7 +28,7 @@ import uk.ac.open.punchingbag.Contact;
import uk.ac.open.punchingbag.PunchingBag;
public class SimpleKeyboard implements ButtonListener, Runnable {
- String soundDir = System.getProperty("user.dir")
+ String soundDir = System.getProperty("user.dir")
+ System.getProperty("file.separator");
File[] keys = { new File(soundDir + "G4.wav"),
@@ -40,9 +40,6 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
long lastActionTime = System.currentTimeMillis();
boolean noising = false;
-
-
-
public static final int DAMPER_PEDAL = 64;
public static final int DAMPER_ON = 127;
@@ -55,7 +52,8 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
// A B C D E F G
-4, -2, 0, 1, 3, 5, 7 };
- public static void main(String[] args) throws InvalidMidiDataException, MidiUnavailableException, IOException {
+ public static void main(String[] args) throws InvalidMidiDataException,
+ MidiUnavailableException, IOException {
try {
bag.connectToArduinos();
@@ -64,78 +62,14 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
e.printStackTrace();
}
-
new SimpleKeyboard();
- /*int instrument = 0;
- int tempo = 120;
- String filename = null;
-
- // Parse the options
- // -i <instrument number> default 0, a piano. Allowed values: 0-127
- // -t <beats per minute> default tempo is 120 quarter notes per minute
- // -o <filename> save to a midi file instead of playing
- int a = 0;
- while (a < args.length) {
- if (args[a].equals("-i")) {
- instrument = Integer.parseInt(args[a + 1]);
- a += 2;
- } else if (args[a].equals("-t")) {
- tempo = Integer.parseInt(args[a + 1]);
- a += 2;
- } else if (args[a].equals("-o")) {
- filename = args[a + 1];
- a += 2;
- } else
- break;
- }
-
- char[] notes = args[a].toCharArray();
-
- // 16 ticks per quarter note.
- Sequence sequence = new Sequence(Sequence.PPQ, 16);
-
- // Add the specified notes to the track
- addTrack(sequence, instrument, tempo, notes);
-
- if (filename == null) { // no filename, so play the notes
- // Set up the Sequencer and Synthesizer objects
- Sequencer sequencer = MidiSystem.getSequencer();
- sequencer.open();
- Synthesizer synthesizer = MidiSystem.getSynthesizer();
- synthesizer.open();
- sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
-
- // Specify the sequence to play, and the tempo to play it at
- sequencer.setSequence(sequence);
- sequencer.setTempoInBPM(tempo);
-
- // Let us know when it is done playing
- sequencer.addMetaEventListener(new MetaEventListener() {
- public void meta(MetaMessage m) {
- // A message of this type is automatically sent
- // when we reach the end of the track
- if (m.getType() == END_OF_TRACK)
- System.exit(0);
- }
- });
- // And start playing now.
- sequencer.start();
- } else { // A file name was specified, so save the notes
- int[] allowedTypes = MidiSystem.getMidiFileTypes(sequence);
- if (allowedTypes.length == 0) {
- System.err.println("No supported MIDI file types.");
- } else {
- MidiSystem.write(sequence, allowedTypes[0], new File(filename));
- System.exit(0);
- }
- }*/
}
public SimpleKeyboard() {
bag.addButtonListener(this);
- new Thread (this).start();
+ new Thread(this).start();
}
public void buttonPressed(int x, int y) {
@@ -145,28 +79,38 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
}
lastActionTime = System.currentTimeMillis();
System.out.println("Button Pressed: " + x + " " + y);
- if (y < 3) {
- bag.fillRect(0, 0, 9, 4, Color.red, 500);
- playKey(5);
- } else if (y < 6) {
- bag.fillRect(0, 3, 9, 4, Color.red, 500);
- playKey(4);
- } else if (y < 9) {
- bag.fillRect(0, 6, 9, 4, Color.red, 500);
- playKey(3);
- } else if (y < 12) {
- bag.fillRect(0, 9, 9, 4, Color.red, 500);
- playKey(2);
- } else if (y < 15) {
- bag.fillRect(0, 12, 9, 4, Color.red, 500);
- playKey(1);
- } else {
- bag.fillRect(0, 15, 9, 4, Color.red, 500);
- playKey(0);
+ try {
+ if (y < 3) {
+ bag.fillRect(0, 0, 9, 4, Color.red, 500);
+ playKey(5);
+ } else if (y < 6) {
+ bag.fillRect(0, 3, 9, 4, Color.red, 500);
+ playKey(4);
+ } else if (y < 9) {
+ bag.fillRect(0, 6, 9, 4, Color.red, 500);
+
+ playKey(3);
+
+ } else if (y < 12) {
+ bag.fillRect(0, 9, 9, 4, Color.red, 500);
+ playKey(2);
+ } else if (y < 15) {
+ bag.fillRect(0, 12, 9, 4, Color.red, 500);
+ playKey(1);
+ } else {
+ bag.fillRect(0, 15, 9, 4, Color.red, 500);
+ playKey(0);
+ }
+ } catch (InvalidMidiDataException | MidiUnavailableException
+ | IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
}
- void playKey(int key) {
+ void playKey(int key) throws InvalidMidiDataException,
+ MidiUnavailableException, IOException {
+
try {
AudioInputStream sound = AudioSystem.getAudioInputStream(keys[key]);
DataLine.Info dataLine = new DataLine.Info(Clip.class,
@@ -174,16 +118,42 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
Clip clip = (Clip) AudioSystem.getLine(dataLine);
clip.open(sound);
clip.start();
- } catch (LineUnavailableException e) {
- // TODO Auto-generated catch block
+ } catch (LineUnavailableException e) { // TODO Auto-generated catch
+ // block
e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
+ } catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
- } catch (UnsupportedAudioFileException e) {
- // TODO Auto-generated catch block
+ } catch (UnsupportedAudioFileException e) { // TODO Auto-generated catch
+ // block
e.printStackTrace();
}
+
+ /*char[] notes = { 'C', 'C' };
+
+ String filename = null;
+ int instrument = 0;
+ int tempo = 120;
+
+ // 16 ticks per quarter note.
+ Sequence sequence = new Sequence(Sequence.PPQ, 16);
+
+ // Add the specified notes to the track
+ addTrack(sequence, instrument, tempo, notes);
+
+ // Set up the Sequencer and Synthesizer objects
+ Sequencer sequencer = MidiSystem.getSequencer();
+ sequencer.open();
+ Synthesizer synthesizer = MidiSystem.getSynthesizer();
+ synthesizer.open();
+ sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());
+
+ // Specify the sequence to play, and the tempo to play it at
+ sequencer.setSequence(sequence);
+ sequencer.setTempoInBPM(tempo);
+
+ // And start playing now.
+ sequencer.start();*/
+
}
@Override
@@ -302,6 +272,7 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
track.add(new MidiEvent(on, startTick));
track.add(new MidiEvent(off, startTick + tickLength));
}
+
@Override
public void run() {
while (true) {
@@ -323,7 +294,7 @@ public class SimpleKeyboard implements ButtonListener, Runnable {
}
}
-
+
}
}