Skip to content

Commit 0e992be

Browse files
author
koca2000
committed
New ONBS note pitch support #38
1 parent 53f7067 commit 0e992be

File tree

8 files changed

+67
-17
lines changed

8 files changed

+67
-17
lines changed

src/main/java/com/xxmicloxx/NoteBlockAPI/model/Note.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ public class Note {
99

1010
private byte instrument;
1111
private byte key;
12+
13+
private short pitch;
1214

13-
public Note(byte instrument, byte key) {
15+
public Note(byte instrument, byte key, byte velocity, byte panning, short pitch) {
1416
this.instrument = instrument;
1517
this.key = key;
18+
this.pitch = pitch;
1619
}
1720

1821
/**
@@ -36,4 +39,12 @@ public byte getKey() {
3639
public void setKey(byte key) {
3740
this.key = key;
3841
}
42+
43+
public short getPitch() {
44+
return pitch;
45+
}
46+
47+
public void setPitch(short pitch) {
48+
this.pitch = pitch;
49+
}
3950
}

src/main/java/com/xxmicloxx/NoteBlockAPI/model/NotePitch.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
/**
77
* Represents pitches of every noteblock note pre- & post-1.9
8-
*
8+
* @deprecated Use NoteUtils
99
*/
10+
@Deprecated()
1011
public enum NotePitch {
1112

1213
NOTE_0(0, 0.5F, 0.50000F),
@@ -52,6 +53,7 @@ public static float getPitch(int note) {
5253
return pre1_9 ? notePitch.pitchPre1_9 : notePitch.pitchPost1_9;
5354
}
5455
}
56+
5557
return 0.0F;
5658
}
5759

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/EntitySongPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.xxmicloxx.NoteBlockAPI.model.*;
66
import com.xxmicloxx.NoteBlockAPI.utils.CompatibilityUtils;
77
import com.xxmicloxx.NoteBlockAPI.utils.InstrumentUtils;
8+
import com.xxmicloxx.NoteBlockAPI.utils.NoteUtils;
89
import org.bukkit.Bukkit;
910
import org.bukkit.entity.Entity;
1011
import org.bukkit.entity.Player;
@@ -76,7 +77,7 @@ public void playTick(Player player, int tick) {
7677

7778
float volume = ((layer.getVolume() * (int) this.volume * (int) playerVolume) / 1000000F)
7879
* ((1F / 16F) * getDistance());
79-
float pitch = NotePitch.getPitch(note.getKey() - 33);
80+
float pitch = NoteUtils.getPitch(note);
8081

8182
if (InstrumentUtils.isCustomInstrument(note.getInstrument())) {
8283
CustomInstrument instrument = song.getCustomInstruments()

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/NoteBlockSongPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI.songplayer;
22

3+
import com.xxmicloxx.NoteBlockAPI.utils.NoteUtils;
34
import org.bukkit.Bukkit;
45
import org.bukkit.Location;
56
import org.bukkit.Material;
@@ -101,7 +102,7 @@ public void playTick(Player player, int tick) {
101102

102103
float volume = ((layer.getVolume() * (int) this.volume * (int) playerVolume) / 1000000F)
103104
* ((1F / 16F) * getDistance());
104-
float pitch = NotePitch.getPitch(note.getKey() - 33);
105+
float pitch = NoteUtils.getPitch(note);
105106

106107
if (InstrumentUtils.isCustomInstrument(note.getInstrument())) {
107108
CustomInstrument instrument = song.getCustomInstruments()

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/PositionSongPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI.songplayer;
22

3+
import com.xxmicloxx.NoteBlockAPI.utils.NoteUtils;
34
import org.bukkit.Bukkit;
45
import org.bukkit.Location;
56
import org.bukkit.entity.Player;
@@ -90,7 +91,7 @@ public void playTick(Player player, int tick) {
9091

9192
float volume = ((layer.getVolume() * (int) this.volume * (int) playerVolume) / 1000000F)
9293
* ((1F / 16F) * getDistance());
93-
float pitch = NotePitch.getPitch(note.getKey() - 33);
94+
float pitch = NoteUtils.getPitch(note);
9495

9596
if (InstrumentUtils.isCustomInstrument(note.getInstrument())) {
9697
CustomInstrument instrument = song.getCustomInstruments()

src/main/java/com/xxmicloxx/NoteBlockAPI/songplayer/RadioSongPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI.songplayer;
22

3+
import com.xxmicloxx.NoteBlockAPI.utils.NoteUtils;
34
import org.bukkit.entity.Player;
45

56
import com.xxmicloxx.NoteBlockAPI.NoteBlockAPI;
@@ -56,7 +57,7 @@ public void playTick(Player player, int tick) {
5657
}
5758

5859
float volume = (layer.getVolume() * (int) this.volume * (int) playerVolume) / 1000000F;
59-
float pitch = NotePitch.getPitch(note.getKey() - 33);
60+
float pitch = NoteUtils.getPitch(note);
6061

6162
if (InstrumentUtils.isCustomInstrument(note.getInstrument())) {
6263
CustomInstrument instrument = song.getCustomInstruments()

src/main/java/com/xxmicloxx/NoteBlockAPI/utils/NBSDecoder.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,18 @@ private static Song parse(InputStream inputStream, File songFile) {
118118
}
119119

120120
byte key = dataInputStream.readByte();
121-
121+
byte velocity = 100;
122+
byte panning = 100;
123+
short pitch = 0;
122124
if (nbsversion >= 4) {
123-
dataInputStream.readByte(); // note block velocity
124-
dataInputStream.readByte(); // note block panning
125-
readShort(dataInputStream); // note block pitch
125+
velocity = dataInputStream.readByte(); // note block velocity
126+
panning = dataInputStream.readByte(); // note block panning
127+
pitch = readShort(dataInputStream); // note block pitch
126128
}
127129

128-
setNote(layer, tick, instrument /* instrument */,
129-
key/* note */, layerHashMap);
130+
setNote(layer, tick,
131+
new Note(instrument /* instrument */, key/* note */, velocity, panning, pitch),
132+
layerHashMap);
130133
}
131134
}
132135

@@ -190,18 +193,16 @@ private static Song parse(InputStream inputStream, File songFile) {
190193
* Sets a note at a tick in a song
191194
* @param layerIndex
192195
* @param ticks
193-
* @param instrument
194-
* @param key
196+
* @param note
195197
* @param layerHashMap
196198
*/
197-
private static void setNote(int layerIndex, int ticks, byte instrument,
198-
byte key, HashMap<Integer, Layer> layerHashMap) {
199+
private static void setNote(int layerIndex, int ticks, Note note, HashMap<Integer, Layer> layerHashMap) {
199200
Layer layer = layerHashMap.get(layerIndex);
200201
if (layer == null) {
201202
layer = new Layer();
202203
layerHashMap.put(layerIndex, layer);
203204
}
204-
layer.setNote(ticks, new Note(instrument, key));
205+
layer.setNote(ticks, note);
205206
}
206207

207208
private static short readShort(DataInputStream dataInputStream) throws IOException {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.xxmicloxx.NoteBlockAPI.utils;
2+
3+
import com.xxmicloxx.NoteBlockAPI.model.Note;
4+
5+
public class NoteUtils {
6+
7+
private static float[] pitches = null;
8+
9+
public static void generatePitchValues(){
10+
pitches = new float[2400];
11+
12+
for (int i = 0; i < 2400; i++){
13+
pitches[i] = (float) Math.pow(2, (i - 1200d) / 1200d);
14+
}
15+
}
16+
17+
public static float getPitch(Note note){
18+
return getPitch(note.getKey(), note.getPitch());
19+
}
20+
21+
public static float getPitch(byte key, short pitch){
22+
pitch += (key - 33) * 100;
23+
if (pitch < 0) pitch = 0;
24+
25+
if (pitch > 2400) pitch = 2400;
26+
27+
if (pitches == null) generatePitchValues();
28+
29+
return pitches[pitch];
30+
}
31+
32+
}

0 commit comments

Comments
 (0)