/
NoteBlockReceiver.java
167 lines (144 loc) · 5.25 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
package com.denizenscript.denizen.utilities.midi;
import com.google.common.collect.Maps;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.SoundHelper;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import javax.sound.midi.*;
import java.io.IOException;
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 = null;
public Sequencer sequencer;
public boolean closing = false;
public NoteBlockReceiver(List<EntityTag> entities, String _Key) throws InvalidMidiDataException, IOException {
this.entities = entities;
this.location = null;
this.channelPatches = Maps.newHashMap();
this.key = _Key;
}
public NoteBlockReceiver(LocationTag location, String _Key) throws InvalidMidiDataException, IOException {
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;
}
}
}
// Note that this may run async
public void playNote(ShortMessage message) {
// if this isn't a NOTE_ON message, we can't play it
if (ShortMessage.NOTE_ON != message.getCommand()) {
return;
}
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);
SoundHelper soundHelper = NMSHandler.getSoundHelper();
Sound instrument = soundHelper.getDefaultMidiInstrument();
if (patch != null) {
instrument = soundHelper.getMidiInstrumentFromPatch(patch);
}
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()) {
NMSHandler.getSoundHelper().playSound(entity.getPlayer(), entity.getLocation(), instrument, volume, pitch, "RECORDS");
}
else {
NMSHandler.getSoundHelper().playSound(null, entity.getLocation(), instrument, volume, pitch, "RECORDS");
}
}
else {
entities.remove(i);
i--;
}
}
}
else {
this.close();
}
}
public Runnable onFinish = null;
@Override
public void close() {
if (closing) {
return;
}
closing = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), () -> {
MidiUtil.receivers.remove(key);
if (sequencer != null) {
sequencer.close();
sequencer = null;
}
channelPatches.clear();
channelPatches = null;
entities = null;
location = null;
if (onFinish != null) {
onFinish.run();
}
}, 1);
}
}