-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidiSynth.java
More file actions
81 lines (52 loc) · 2.25 KB
/
Copy pathMidiSynth.java
File metadata and controls
81 lines (52 loc) · 2.25 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.*;
import javax.sound.midi.*;
class MidiSynth {
public static void main(String[] args) {
InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader bufReader = new BufferedReader(isReader);
int channel = 1; // 0 is a piano, 9 is percussion, other channels are for other instruments
int volume = 126; // between 0 et 127
try {
Synthesizer synth = MidiSystem.getSynthesizer();
// Soundbank soundbank = synth.getDefaultSoundbank();
// Instrument[] instruments = soundbank.getInstruments();
// Instrument instrument = instruments[1];
synth.open();
MidiChannel[] channels = synth.getChannels();
MidiChannel mc = channels[channel];
// BufferedInputStream soundBankStream = new BufferedInputStream(
// MidiSynth.class.getClassLoader().getResourceAsStream("/data/projects/guitar_pro/soundbank-deluxe.gm"));
// synth.loadAllInstruments(MidiSystem.getSoundbank(soundBankStream));
// soundBankStream.close()
Soundbank soundbank = synth.getDefaultSoundbank();
Instrument[] instruments = soundbank.getInstruments();
Instrument instrument = instruments[17];
synth.loadInstrument(instrument);
mc.programChange(instrument.getPatch().getProgram());
while (true) {
try {
String inputStr = null;
if ((inputStr = bufReader.readLine()) != null) {
String[] notes = inputStr.split(", ");
int duration = 0;
for(int i = 0; i < notes.length; i++) {
String[] nd = notes[i].split(":");
duration = Integer.parseInt(nd[1]);
mc.noteOn(Integer.parseInt(nd[0]), volume);
}
Thread.sleep(duration);
for(int i = 0; i < notes.length; i++) {
String[] nd = notes[i].split(":");
mc.noteOff(Integer.parseInt(nd[0]), volume);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
// movie of dream sequences one after another from a person in VR. Cuts back to reality in very quick intervals where the person
} catch (Exception e) {
e.printStackTrace();
}
}
}