Skip to content

Commit 1186d18

Browse files
committed
Song.isStereo(), StereoMode fallback ChannelMode #45
1 parent 34a72f3 commit 1186d18

File tree

7 files changed

+129
-9
lines changed

7 files changed

+129
-9
lines changed

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

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Song implements Cloneable {
2222
private float delay;
2323
private CustomInstrument[] customInstruments;
2424
private int firstCustomInstrumentIndex;
25+
private boolean isStereo = false;
2526

2627
/**
2728
* Create Song instance by copying other Song parameters
@@ -30,11 +31,11 @@ public class Song implements Cloneable {
3031
public Song(Song other) {
3132
this(other.getSpeed(), other.getLayerHashMap(), other.getSongHeight(),
3233
other.getLength(), other.getTitle(), other.getAuthor(),
33-
other.getDescription(), other.getPath(), other.getFirstCustomInstrumentIndex(), other.getCustomInstruments());
34+
other.getDescription(), other.getPath(), other.getFirstCustomInstrumentIndex(), other.getCustomInstruments(), other.isStereo);
3435
}
3536

3637
/**
37-
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int)}
38+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, boolean)}
3839
* @param speed
3940
* @param layerHashMap
4041
* @param songHeight
@@ -44,14 +45,15 @@ public Song(Song other) {
4445
* @param description
4546
* @param path
4647
*/
48+
@Deprecated
4749
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
4850
short songHeight, final short length, String title, String author,
4951
String description, File path) {
50-
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), new CustomInstrument[0]);
52+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), new CustomInstrument[0], false);
5153
}
5254

5355
/**
54-
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, CustomInstrument[])}
56+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, CustomInstrument[], boolean)}
5557
* @param speed
5658
* @param layerHashMap
5759
* @param songHeight
@@ -62,21 +64,61 @@ public Song(float speed, HashMap<Integer, Layer> layerHashMap,
6264
* @param path
6365
* @param customInstruments
6466
*/
67+
@Deprecated
6568
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
6669
short songHeight, final short length, String title, String author,
6770
String description, File path, CustomInstrument[] customInstruments) {
68-
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), customInstruments);
71+
this(speed, layerHashMap, songHeight, length, title, author, description, path, InstrumentUtils.getCustomInstrumentFirstIndex(), customInstruments, false);
6972
}
7073

74+
/**
75+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, boolean)}
76+
* @param speed
77+
* @param layerHashMap
78+
* @param songHeight
79+
* @param length
80+
* @param title
81+
* @param author
82+
* @param description
83+
* @param path
84+
* @param firstCustomInstrumentIndex
85+
*/
86+
@Deprecated
7187
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
7288
short songHeight, final short length, String title, String author,
7389
String description, File path, int firstCustomInstrumentIndex) {
74-
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, new CustomInstrument[0]);
90+
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, new CustomInstrument[0], false);
91+
}
92+
93+
/**
94+
* @deprecated Use {@link #Song(float, HashMap, short, short, String, String, String, File, int, CustomInstrument[], boolean)}
95+
* @param speed
96+
* @param layerHashMap
97+
* @param songHeight
98+
* @param length
99+
* @param title
100+
* @param author
101+
* @param description
102+
* @param path
103+
* @param firstCustomInstrumentIndex
104+
* @param customInstruments
105+
*/
106+
@Deprecated
107+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
108+
short songHeight, final short length, String title, String author,
109+
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments) {
110+
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, customInstruments, false);
111+
}
112+
113+
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
114+
short songHeight, final short length, String title, String author,
115+
String description, File path, int firstCustomInstrumentIndex, boolean isStereo) {
116+
this(speed, layerHashMap, songHeight, length, title, author, description, path, firstCustomInstrumentIndex, new CustomInstrument[0], isStereo);
75117
}
76118

77119
public Song(float speed, HashMap<Integer, Layer> layerHashMap,
78120
short songHeight, final short length, String title, String author,
79-
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments) {
121+
String description, File path, int firstCustomInstrumentIndex, CustomInstrument[] customInstruments, boolean isStereo) {
80122
this.speed = speed;
81123
delay = 20 / speed;
82124
this.layerHashMap = layerHashMap;
@@ -88,6 +130,7 @@ public Song(float speed, HashMap<Integer, Layer> layerHashMap,
88130
this.path = path;
89131
this.firstCustomInstrumentIndex = firstCustomInstrumentIndex;
90132
this.customInstruments = customInstruments;
133+
this.isStereo = isStereo;
91134
}
92135

93136
/**
@@ -179,4 +222,12 @@ public Song clone() {
179222
public int getFirstCustomInstrumentIndex() {
180223
return firstCustomInstrumentIndex;
181224
}
225+
226+
/**
227+
* Returns true if song has at least one stereo {@link Note} or {@link Layer} in nbs file
228+
* @return
229+
*/
230+
public boolean isStereo() {
231+
return isStereo;
232+
}
182233
}

src/main/java/com/xxmicloxx/NoteBlockAPI/model/playmode/ChannelMode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.bukkit.Location;
88
import org.bukkit.entity.Player;
99

10+
/**
11+
* Decides how is {@link Note} played to {@link Player}
12+
*/
1013
public abstract class ChannelMode {
1114

1215
public abstract void play(Player player, Location location, Song song, Layer layer, Note note,

src/main/java/com/xxmicloxx/NoteBlockAPI/model/playmode/MonoMode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.bukkit.Location;
77
import org.bukkit.entity.Player;
88

9+
/**
10+
* {@link Note} is played inside of {@link Player}'s head.
11+
*/
912
public class MonoMode extends ChannelMode {
1013

1114
@Override

src/main/java/com/xxmicloxx/NoteBlockAPI/model/playmode/MonoStereoMode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.bukkit.Location;
77
import org.bukkit.entity.Player;
88

9+
/**
10+
* Ignores panning of {@link Note} and {@link Layer} from nbs format and plays mono {@link Note} as fake stereo at fixed offset from {@link Player} head.
11+
*/
912
public class MonoStereoMode extends ChannelMode{
1013

1114
private float distance = 2;
@@ -28,10 +31,18 @@ public void play(Player player, Location location, Song song, Layer layer, Note
2831
}
2932
}
3033

34+
/**
35+
* Returns distance of {@link Note} from {@link Player}'s head.
36+
* @return
37+
*/
3138
public float getDistance() {
3239
return distance;
3340
}
3441

42+
/**
43+
* Sets distance of {@link Note} from {@link Player}'s head.
44+
* @param distance
45+
*/
3546
public void setDistance(float distance) {
3647
this.distance = distance;
3748
}

src/main/java/com/xxmicloxx/NoteBlockAPI/model/playmode/StereoMode.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
import org.bukkit.Location;
77
import org.bukkit.entity.Player;
88

9+
/**
10+
* Uses panning for individual {@link Note} and {@link Layer} based on data from nbs file.
11+
*/
912
public class StereoMode extends ChannelMode {
1013

11-
float maxDistance = 2;
14+
private float maxDistance = 2;
15+
private ChannelMode fallbackChannelMode = null;
1216

1317
@Override
1418
public void play(Player player, Location location, Song song, Layer layer, Note note, SoundCategory soundCategory, float volume, float pitch) {
19+
if (!song.isStereo() && fallbackChannelMode != null){
20+
fallbackChannelMode.play(player, location, song, layer, note, soundCategory, volume, pitch);
21+
return;
22+
}
23+
1524
float distance = 0;
1625
if (layer.getPanning() == 100){
1726
distance = (note.getPanning() - 100) * maxDistance;
@@ -31,11 +40,38 @@ public void play(Player player, Location location, Song song, Layer layer, Note
3140
}
3241
}
3342

43+
/**
44+
* Returns scale of panning in blocks. {@link Note} with maximum left panning will be played this distance from {@link Player}'s head on left side.
45+
* @return
46+
*/
3447
public float getMaxDistance() {
3548
return maxDistance;
3649
}
3750

51+
/**
52+
* Sets scale of panning in blocks. {@link Note} with maximum left panning will be played this distance from {@link Player}'s head on left side.
53+
* @param maxDistance
54+
*/
3855
public void setMaxDistance(float maxDistance) {
3956
this.maxDistance = maxDistance;
4057
}
58+
59+
/**
60+
* Returns fallback {@link ChannelMode} used when song is not stereo.
61+
* @return ChannelMode or null when fallback ChannelMode is disabled
62+
*/
63+
public ChannelMode getFallbackChannelMode() {
64+
return fallbackChannelMode;
65+
}
66+
67+
/**
68+
* Sets fallback {@link ChannelMode} which is used when song is not stereo. Set to null to disable.
69+
* @param fallbackChannelMode
70+
* @throws IllegalArgumentException if parameter is instance of StereoMode
71+
*/
72+
public void setFallbackChannelMode(ChannelMode fallbackChannelMode) {
73+
if (fallbackChannelMode instanceof StereoMode) throw new IllegalArgumentException("Fallback ChannelMode can't be instance of StereoMode!");
74+
75+
this.fallbackChannelMode = fallbackChannelMode;
76+
}
4177
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void playTick(Player player, int tick) {
7171
* @return if is played stereo
7272
* @deprecated
7373
*/
74+
@Deprecated
7475
public boolean isStereo(){
7576
return !(channelMode instanceof MonoMode);
7677
}
@@ -80,10 +81,15 @@ public boolean isStereo(){
8081
* @param stereo
8182
* @deprecated
8283
*/
84+
@Deprecated
8385
public void setStereo(boolean stereo){
8486
channelMode = stereo ? new MonoMode() : new MonoStereoMode();
8587
}
8688

89+
/**
90+
* Sets how will be {@link Note} played to {@link Player} (eg. mono or stereo). Default is {@link MonoMode}.
91+
* @param mode
92+
*/
8793
public void setChannelMode(ChannelMode mode){
8894
channelMode = mode;
8995
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public static Song parse(InputStream inputStream) {
6060
private static Song parse(InputStream inputStream, File songFile) {
6161
HashMap<Integer, Layer> layerHashMap = new HashMap<Integer, Layer>();
6262
byte biggestInstrumentIndex = -1;
63+
boolean isStereo = false;
6364
try {
6465
DataInputStream dataInputStream = new DataInputStream(inputStream);
6566
short length = readShort(dataInputStream);
@@ -127,6 +128,10 @@ private static Song parse(InputStream inputStream, File songFile) {
127128
pitch = readShort(dataInputStream); // note block pitch
128129
}
129130

131+
if (panning != 100){
132+
isStereo = true;
133+
}
134+
130135
setNote(layer, tick,
131136
new Note(instrument /* instrument */, key/* note */, velocity, panning, pitch),
132137
layerHashMap);
@@ -150,6 +155,11 @@ private static Song parse(InputStream inputStream, File songFile) {
150155
if (nbsversion >= 2){
151156
panning = 200 - dataInputStream.readUnsignedByte(); // layer stereo, 0 is right in nbs format
152157
}
158+
159+
if (panning != 100){
160+
isStereo = true;
161+
}
162+
153163
if (layer != null) {
154164
layer.setName(name);
155165
layer.setVolume(volume);
@@ -176,7 +186,7 @@ private static Song parse(InputStream inputStream, File songFile) {
176186
}
177187

178188
return new Song(speed, layerHashMap, songHeight, length, title,
179-
author, description, songFile, firstcustominstrument, customInstrumentsArray);
189+
author, description, songFile, firstcustominstrument, customInstrumentsArray, isStereo);
180190
} catch (FileNotFoundException e) {
181191
e.printStackTrace();
182192
} catch (EOFException e) {

0 commit comments

Comments
 (0)