-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
MidiUtil.java
128 lines (112 loc) · 4.35 KB
/
MidiUtil.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package net.aufdemrand.denizen.utilities.midi;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Sound;
import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Utility for playing midi files for players to hear.
*
* @author authorblues, patched by mcmonkey
*/
public class MidiUtil {
public static Map<String, Receiver> receivers = new HashMap<String, Receiver>();
public static void startSequencer(File file, float tempo, NoteBlockReceiver receiver)
throws InvalidMidiDataException, IOException, MidiUnavailableException {
Sequencer sequencer = MidiSystem.getSequencer(false);
sequencer.setSequence(MidiSystem.getSequence(file));
sequencer.open();
receiver.setSequencer(sequencer);
// Set desired tempo
sequencer.setTempoFactor(tempo);
sequencer.getTransmitter().setReceiver(receiver);
sequencer.start();
}
public static NoteBlockReceiver playMidi(File file, float tempo, float volume, List<dEntity> entities) {
try {
NoteBlockReceiver receiver = new NoteBlockReceiver(entities, entities.get(0).getUUID().toString());
receiver.VOLUME_RANGE = volume;
// If there is already a midi file being played for one of the entities,
// stop playing it
for (dEntity entity : entities) {
stopMidi(entity.getUUID().toString());
}
receivers.put(entities.get(0).getUUID().toString(), receiver);
startSequencer(file, tempo, receiver);
return receiver;
}
catch (Exception e) {
dB.echoError(e);
return null;
}
}
public static NoteBlockReceiver playMidi(File file, float tempo, float volume, dLocation location) {
try {
NoteBlockReceiver receiver = new NoteBlockReceiver(location, location.identify());
receiver.VOLUME_RANGE = volume;
// If there is already a midi file being played for this location,
// stop playing it
stopMidi(location.identify());
receivers.put(location.identify(), receiver);
startSequencer(file, tempo, receiver);
return receiver;
}
catch (Exception e) {
dB.echoError(e);
return null;
}
}
public static void stopMidi(String object) {
if (receivers.containsKey(object)) {
receivers.get(object).close();
}
}
public static void stopMidi(List<dEntity> entities) {
for (dEntity entity : entities) {
stopMidi(entity.getUUID().toString());
}
}
// provided by github.com/sk89q/craftbook
private static final int[] instruments = {
0, 0, 0, 0, 0, 0, 0, 5, // 8
6, 0, 0, 0, 0, 0, 0, 0, // 16
0, 0, 0, 0, 0, 0, 0, 5, // 24
5, 5, 5, 5, 5, 5, 5, 5, // 32
6, 6, 6, 6, 6, 6, 6, 6, // 40
5, 5, 5, 5, 5, 5, 5, 2, // 48
5, 5, 5, 5, 0, 0, 0, 0, // 56
0, 0, 0, 0, 0, 0, 0, 0, // 64
0, 0, 0, 0, 0, 0, 0, 0, // 72
0, 0, 0, 0, 0, 0, 0, 0, // 80
0, 0, 0, 0, 0, 0, 0, 0, // 88
0, 0, 0, 0, 0, 0, 0, 0, // 96
0, 0, 0, 0, 0, 0, 0, 0, // 104
0, 0, 0, 0, 0, 0, 0, 0, // 112
1, 1, 1, 3, 1, 1, 1, 5, // 120
1, 1, 1, 1, 1, 2, 4, 3, // 128
};
public static Sound patchToInstrument(int patch) {
// look up the instrument matching the patch
switch (instruments[patch]) {
case 1:
return Sound.BLOCK_NOTE_BASS;
case 2:
return Sound.BLOCK_NOTE_SNARE;
case 3:
return Sound.BLOCK_NOTE_HARP;
case 4:
return Sound.BLOCK_NOTE_HAT;
case 5:
return Sound.BLOCK_NOTE_PLING;
case 6:
return Sound.BLOCK_NOTE_BASEDRUM;
}
// if no instrument match is found, use piano
return Sound.BLOCK_NOTE_HAT;
}
}