Renamed Main to MoppySequencer to support UI.

This commit is contained in:
Sam Archer 2011-10-09 03:17:17 -07:00
parent 6cf73b70d0
commit ff335bbc81
3 changed files with 101 additions and 108 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/Java/MoppyDesk/nbproject/private/ /Java/MoppyDesk/nbproject/private/
/Java/MoppyDesk/build/ /Java/MoppyDesk/build/
/Java/MoppyDesk/dist/

View File

@ -1,107 +0,0 @@
package moppydesk;
/**
*
* @author Sammy1Am
*/
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.UnsupportedCommOperationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MoppyBridge mb = null;
MoppyPlayer mp = null;
try {
mb = new MoppyBridge("COM6"); //Create MoppyBridge on the COM port with the Arduino
mp = new MoppyPlayer(mb);
mb.resetDrives();
//Load a MIDI file (TODO: Make this an argument, or a GUI window)
Sequence sequence = MidiSystem.getSequence(new File("samplesongs/KirbysTheme.mid"));
final Sequencer sequencer = MidiSystem.getSequencer(false);
//Start a new thread to listen on the command-line to exit the program early
new Thread(){
@Override
public void run(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true){
try {
if (br.readLine().equalsIgnoreCase("exit")) {
sequencer.stop();
sequencer.close();
System.exit(0);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}.start();
//Start the sequencer, and set the tempo (not being read from file!)
sequencer.open();
sequencer.setSequence(sequence);
sequencer.setTempoInBPM(160);
System.out.println(sequence.getTracks().length);
sequencer.getTransmitter().setReceiver(mp); // Set MoppyPlayer as a receiver.
sequencer.start(); //GO!
//Wait til the sequence is done...
while (sequencer.isRunning()){
Thread.sleep(3000);
}
//Close the sequencer
sequencer.close();
} catch (MidiUnavailableException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidMidiDataException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPortException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (PortInUseException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedCommOperationException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Reset everything and close down cleanly (hopefully)
if (mb != null){
mb.resetDrives();
mb.close();
}
if (mp != null){
mp.close();
}
}
System.exit(0);
}
}

View File

@ -0,0 +1,99 @@
package moppydesk;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.UnsupportedCommOperationException;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MetaEventListener;
import javax.sound.midi.MetaMessage;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
/**
*
* @author Sammy1Am
*/
public class MoppySequencer implements MetaEventListener{
MoppyBridge mb;
MoppyPlayer mp;
Sequencer sequencer;
ArrayList<MoppyStatusConsumer> listeners = new ArrayList<MoppyStatusConsumer>(1);
public MoppySequencer(String comPort) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException, MidiUnavailableException {
mb = new MoppyBridge("COM6"); //Create MoppyBridge on the COM port with the Arduino
mp = new MoppyPlayer(mb);
mb.resetDrives();
sequencer = MidiSystem.getSequencer(false);
sequencer.open();
sequencer.getTransmitter().setReceiver(mp); // Set MoppyPlayer as a receiver.
sequencer.addMetaEventListener(this);
}
public void loadFile(String filePath) throws InvalidMidiDataException, IOException, MidiUnavailableException {
sequencer.stop();
Sequence sequence = MidiSystem.getSequence(new File(filePath));
sequencer.setSequence(sequence);
System.out.println("Loaded sequence with "+sequence.getTracks().length+" tracks.");
}
public void startSequencer(){
sequencer.start();
}
public void stopSequencer(){
sequencer.stop();
mb.resetDrives();
}
public void setTempo(float newTempo){
sequencer.setTempoInBPM(newTempo);
}
public void addListener(MoppyStatusConsumer newListener){
listeners.add(newListener);
}
public void removeListener(MoppyStatusConsumer oldListener){
listeners.remove(oldListener);
}
public void closeSequencer(){
stopSequencer();
sequencer.close();
mp.close();
}
public void meta(MetaMessage meta) {
if (meta.getType() == 81){
int uSecondsPerQN = 0;
uSecondsPerQN |= meta.getData()[0] & 0xFF;
uSecondsPerQN <<= 8;
uSecondsPerQN |= meta.getData()[1] & 0xFF;
uSecondsPerQN <<= 8;
uSecondsPerQN |= meta.getData()[2] & 0xFF;
int newTempo = 60000000/uSecondsPerQN;
sequencer.setTempoInBPM(newTempo);
for (MoppyStatusConsumer c : listeners){
c.tempoChanged(newTempo);
}
System.out.println("Tempo changed to: " + newTempo);
}
}
}