-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDanKey.java
64 lines (53 loc) · 1.8 KB
/
DanKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//http://stackoverflow.com/questions/1485307/java-midi-getting-data-from-piano
import javax.sound.midi.*;
import java.util.Scanner;
public class DanKey{
public static void main(String[] args){
}
public MidiDevice inputDevice;
public MidiDevice outputDevice;
public MidiDevice.Info[] infos;
public Synthesizer synthesizer;
public MidiDevice.Info[] getDevices(){
return MidiSystem.getMidiDeviceInfo();
}
public void setInputOutputDevices(MidiDevice.Info input,
MidiDevice.Info output){
try {
inputDevice = MidiSystem.getMidiDevice(input);
outputDevice = MidiSystem.getMidiDevice(output);
} catch (MidiUnavailableException e) {
System.out.println("A MidiUnavailableException occured");
e.printStackTrace();
}
}
public void play(){
try{
synthesizer = MidiSystem.getSynthesizer();
Transmitter transmitter;
Receiver receiver;
// Open a connection to the input device
inputDevice.open();
synthesizer.open();
final MidiChannel[] mc = synthesizer.getChannels();
Instrument[] instr = synthesizer.getDefaultSoundbank().getInstruments();
synthesizer.loadInstrument(instr[90]);
//get transmitter and receiver and connect them
transmitter = inputDevice.getTransmitter();
receiver = synthesizer.getReceiver();
transmitter.setReceiver(receiver);
//then wait for an interrupt (do nothing)
try {
Thread.sleep(100000000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
} catch (MidiUnavailableException e) {
System.out.println("A MidiUnavailableException occured");
e.printStackTrace();
} finally {
inputDevice.close();
outputDevice.close();
}
}//method
}