/
NoteBlockReceiver.java
200 lines (181 loc) · 6.81 KB
/
NoteBlockReceiver.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.denizenscript.denizen.utilities.midi;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.google.common.collect.Maps;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import javax.sound.midi.*;
import java.util.List;
import java.util.Map;
/**
* Midi Receiver for processing note events.
*
* @author authorblues, patched by mcmonkey
*/
public class NoteBlockReceiver implements Receiver, MetaEventListener {
public float VOLUME_RANGE = 10.0f;
private List<EntityTag> entities;
private LocationTag location;
private Map<Integer, Integer> channelPatches;
public String key;
public Sequencer sequencer;
public boolean closing = false;
public NoteBlockReceiver(List<EntityTag> entities, String _Key) {
this.entities = entities;
this.location = null;
this.channelPatches = Maps.newHashMap();
this.key = _Key;
}
public NoteBlockReceiver(LocationTag location, String _Key) {
this.entities = null;
this.location = location;
this.channelPatches = Maps.newHashMap();
this.key = _Key;
}
public void setSequencer(Sequencer sequencer) {
this.sequencer = sequencer;
}
@Override
public void meta(MetaMessage meta) {
if (meta.getType() == 47) { // Track completion
close();
}
}
@Override
public void send(MidiMessage m, long time) {
if (closing) {
return;
}
if (m instanceof ShortMessage) {
ShortMessage smessage = (ShortMessage) m;
int chan = smessage.getChannel();
switch (smessage.getCommand()) {
case ShortMessage.PROGRAM_CHANGE:
int patch = smessage.getData1();
channelPatches.put(chan, patch);
break;
case ShortMessage.NOTE_ON:
this.playNote(smessage);
break;
case ShortMessage.NOTE_OFF:
break;
case ShortMessage.STOP:
close();
break;
}
}
}
public void playNote(ShortMessage message) {
int channel = message.getChannel();
// If this is a percussion channel, return
if (channel == 9) {
return;
}
if (channelPatches == null) {
Debug.echoError("Trying to play notes on closed midi NoteBlockReceiver!");
return;
}
// get the correct instrument
Integer patch = channelPatches.get(channel);
// get pitch and volume from the midi message
float pitch = (float) ToneUtil.midiToPitch(message);
float volume = VOLUME_RANGE * (message.getData2() / 127.0f);
Sound instrument = patch == null ? defaultMidiInstrument : getMidiInstrumentFromPatch(patch);
Runnable actualPlay = () -> {
if (location != null) {
location.getWorld().playSound(location, instrument, volume, pitch);
}
else if (entities != null && !entities.isEmpty()) {
for (int i = 0; i < entities.size(); i++) {
EntityTag entity = entities.get(i);
if (entity.isSpawned()) {
if (entity.isPlayer()) {
entity.getPlayer().playSound(entity.getPlayer(), instrument, SoundCategory.RECORDS, volume, pitch);
}
else {
entity.getLocation().getWorld().playSound(entity.getLocation(), instrument, SoundCategory.RECORDS, volume, pitch);
}
}
else {
entities.remove(i);
i--;
}
}
}
else {
this.close();
}
};
if (Bukkit.isPrimaryThread()) {
actualPlay.run();
}
else {
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), actualPlay);
}
}
public Runnable onFinish = null;
@Override
public void close() {
if (closing) {
return;
}
closing = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
try {
MidiUtil.receivers.remove(key);
if (sequencer != null) {
sequencer.close();
sequencer = null;
}
channelPatches.clear();
channelPatches = null;
entities = null;
location = null;
if (onFinish != null) {
onFinish.run();
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}, 1);
}
private static final int[] instruments_1_12 = {
0, 0, 0, 0, 0, 0, 0, 5, // 8
9, 9, 9, 9, 9, 6, 0, 9, // 16
9, 0, 0, 0, 0, 0, 0, 5, // 24
5, 5, 5, 5, 5, 5, 5, 1, // 32
1, 1, 1, 1, 1, 1, 1, 5, // 40
1, 5, 5, 5, 5, 5, 5, 5, // 48
5, 5, 5, 8, 8, 8, 8, 8, // 56
8, 8, 8, 8, 8, 8, 8, 8, // 64
8, 8, 8, 8, 8, 8, 8, 8, // 72
8, 8, 8, 8, 8, 8, 8, 8, // 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, 5, // 104
5, 5, 5, 9, 8, 5, 8, 6, // 112
6, 3, 3, 2, 2, 2, 6, 5, // 120
1, 1, 1, 6, 1, 2, 4, 7, // 128
};
public static Sound getMidiInstrumentFromPatch(int patch) {
switch (instruments_1_12[patch]) {
case 0: return Sound.BLOCK_NOTE_BLOCK_HARP;
case 1: return Sound.BLOCK_NOTE_BLOCK_BASS;
case 2: return Sound.BLOCK_NOTE_BLOCK_SNARE;
case 3: return Sound.BLOCK_NOTE_BLOCK_HAT;
case 4: return Sound.BLOCK_NOTE_BLOCK_BASEDRUM;
case 5: return Sound.BLOCK_NOTE_BLOCK_GUITAR;
case 6: return Sound.BLOCK_NOTE_BLOCK_BELL;
case 7: return Sound.BLOCK_NOTE_BLOCK_CHIME;
case 8: return Sound.BLOCK_NOTE_BLOCK_FLUTE;
case 9: return Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
case 10: return Sound.BLOCK_NOTE_BLOCK_PLING;
default: return defaultMidiInstrument;
}
}
public static final Sound defaultMidiInstrument = Sound.BLOCK_NOTE_BLOCK_HARP;
}