View Javadoc

1   package com.jamesbryangraves.bob.midi;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import javax.sound.midi.MidiDevice;
7   import javax.sound.midi.MidiMessage;
8   import javax.sound.midi.MidiSystem;
9   import javax.sound.midi.MidiUnavailableException;
10  
11  import org.apache.log4j.Logger;
12  
13  public class BobGenericReceiver extends BobMidiReceiver {
14  	
15  	static Logger log = Logger.getLogger(BobGenericReceiver.class);
16  	
17  	private MidiDevice device;
18  	
19  	public void send(MidiMessage message, long ts) {
20  		try {
21  			if(device == null)
22  				device = getDevice();
23  			if(!device.isOpen())
24  				device.open();
25  			device.getReceiver().send(message, ts);
26  		} catch(MidiUnavailableException mue) {
27  			mue.printStackTrace();
28  			System.exit(1);
29  		}
30  	}
31  	
32  	public void close() {
33  		device.close();
34  	}
35  	
36  	private MidiDevice getDevice() {
37  		try {
38  			return MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[getDeviceIndex()]);
39  		} catch(MidiUnavailableException mue) {
40  			mue.printStackTrace();
41  			System.exit(1);
42  		}
43  		return null;
44  	}
45  	
46  	public static Collection<String> getDeviceNames() {
47  		ArrayList<String> list = new ArrayList<String>();
48  		MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
49  		for(int i = 0; i < infos.length; i++)
50  			list.add(infos[i].getName());
51  		return list;
52  	}
53  }