Skip to content

Commit 84abe95

Browse files
committed
Rewrite of fade effect #10
Version changed to 1.2.2
1 parent 3ac9c88 commit 84abe95

File tree

4 files changed

+190
-37
lines changed

4 files changed

+190
-37
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.xxmicloxx</groupId>
88
<artifactId>NoteBlockAPI</artifactId>
9-
<version>1.2.1</version>
9+
<version>1.2.2</version>
1010
<name>NoteBlockAPI</name>
1111

1212
<properties>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public enum FadeType {
44

5+
NONE,
56
LINEAR
67

78
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.xxmicloxx.NoteBlockAPI.songplayer;
2+
3+
import com.xxmicloxx.NoteBlockAPI.model.FadeType;
4+
import com.xxmicloxx.NoteBlockAPI.utils.Interpolator;
5+
6+
public class Fade {
7+
8+
private FadeType type;
9+
private byte fadeStart;
10+
private byte fadeTarget;
11+
private int fadeDuration;
12+
private int fadeDone = 0;
13+
14+
/**
15+
* Create new fade effect
16+
* @param type Type of fade effect
17+
* @param fadeDuration - duration of fade effect in ticks
18+
*/
19+
public Fade(FadeType type, int fadeDuration){
20+
this.type = type;
21+
this.fadeDuration = fadeDuration;
22+
}
23+
24+
protected byte calculateFade() {
25+
switch (type){
26+
case LINEAR:
27+
if (fadeDone == fadeDuration) {
28+
return -1; // no fade today
29+
}
30+
double targetVolume = Interpolator.interpLinear(
31+
new double[]{0, fadeStart, fadeDuration, fadeTarget}, fadeDone);
32+
fadeDone++;
33+
return (byte) targetVolume;
34+
default:
35+
fadeDone++;
36+
return -1;
37+
}
38+
}
39+
40+
protected int getFadeDone() {
41+
return fadeDone;
42+
}
43+
44+
protected void setFadeStart(byte fadeStart) {
45+
this.fadeStart = fadeStart;
46+
}
47+
48+
protected void setFadeTarget(byte fadeTarget) {
49+
this.fadeTarget = fadeTarget;
50+
}
51+
52+
/**
53+
* Returns fade effect type
54+
* @return {@link FadeType}
55+
*/
56+
public FadeType getType() {
57+
return type;
58+
}
59+
60+
/**
61+
* Set fade effect type
62+
* @param type FadeType
63+
*/
64+
public void setType(FadeType type) {
65+
this.type = type;
66+
}
67+
68+
/**
69+
* Returns duration of fade effect
70+
* @return duration in ticks
71+
*/
72+
public int getFadeDuration() {
73+
return fadeDuration;
74+
}
75+
76+
/**
77+
* Set fade effect duration
78+
* @param fadeDuration duration in ticks
79+
*/
80+
public void setFadeDuration(int fadeDuration) {
81+
this.fadeDuration = fadeDuration;
82+
}
83+
84+
protected byte getFadeStart() {
85+
return fadeStart;
86+
}
87+
88+
protected byte getFadeTarget() {
89+
return fadeTarget;
90+
}
91+
92+
protected void setFadeDone(int fadeDone){
93+
this.fadeDone = fadeDone;
94+
}
95+
96+
}

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

Lines changed: 92 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.xxmicloxx.NoteBlockAPI.model.Note;
2828
import com.xxmicloxx.NoteBlockAPI.model.Song;
2929
import com.xxmicloxx.NoteBlockAPI.model.SoundCategory;
30-
import com.xxmicloxx.NoteBlockAPI.utils.Interpolator;
3130

3231

3332
/**
@@ -48,11 +47,13 @@ public abstract class SongPlayer {
4847
protected Thread playerThread;
4948

5049
protected byte volume = 100;
51-
protected byte fadeStart = volume;
50+
/*protected byte fadeStart = volume;
5251
protected byte fadeTarget = 100;
5352
protected int fadeDuration = 60;
5453
protected int fadeDone = 0;
55-
protected FadeType fadeType = FadeType.LINEAR;
54+
protected FadeType fadeType = FadeType.LINEAR;*/
55+
protected Fade fadeIn;
56+
protected Fade fadeOut;
5657
protected boolean loop = false;
5758

5859
private final Lock lock = new ReentrantLock();
@@ -71,6 +72,15 @@ public SongPlayer(Song song, SoundCategory soundCategory) {
7172
this.song = song;
7273
this.soundCategory = soundCategory;
7374
plugin = NoteBlockAPI.getAPI();
75+
76+
fadeIn = new Fade(FadeType.NONE, 60);
77+
fadeIn.setFadeStart((byte) 0);
78+
fadeIn.setFadeTarget(volume);
79+
80+
fadeOut = new Fade(FadeType.NONE, 60);
81+
fadeOut.setFadeStart(volume);
82+
fadeOut.setFadeTarget((byte) 0);
83+
7484
start();
7585
}
7686

@@ -96,6 +106,14 @@ public SongPlayer(Song song, SoundCategory soundCategory) {
96106
instruments[i] = new CustomInstrument(ci.getIndex(), ci.getName(), ci.getSoundfile());
97107
}
98108
song = new Song(s.getSpeed(), layerHashMap, s.getSongHeight(), s.getLength(), s.getTitle(), s.getAuthor(), s.getDescription(), s.getPath(), instruments);
109+
110+
fadeIn = new Fade(FadeType.NONE, 60);
111+
fadeIn.setFadeStart((byte) 0);
112+
fadeIn.setFadeTarget(volume);
113+
114+
fadeOut = new Fade(FadeType.NONE, 60);
115+
fadeOut.setFadeStart(volume);
116+
fadeOut.setFadeTarget((byte) 0);
99117
}
100118

101119
void update(String key, Object value){
@@ -104,19 +122,19 @@ void update(String key, Object value){
104122
playing = (boolean) value;
105123
break;
106124
case "fadeType":
107-
fadeType = FadeType.valueOf(((String) value).replace("FADE_", ""));
125+
fadeIn.setType(FadeType.valueOf(((String) value).replace("FADE_", "")));
108126
break;
109127
case "fadeTarget":
110-
fadeTarget = (byte) value;
128+
fadeIn.setFadeTarget((byte) value);
111129
break;
112130
case "fadeStart":
113-
fadeStart = (byte) value;
131+
fadeIn.setFadeStart((byte) value);
114132
break;
115133
case "fadeDuration":
116-
fadeDuration = (int) value;
134+
fadeIn.setFadeDuration((int) value);
117135
break;
118136
case "fadeDone":
119-
fadeDone = (int) value;
137+
fadeIn.setFadeDone((int) value);
120138
break;
121139
case "tick":
122140
tick = (short) value;
@@ -143,99 +161,105 @@ void update(String key, Object value){
143161
/**
144162
* Gets the FadeType for this SongPlayer (unused)
145163
* @return FadeType
164+
* @deprecated returns fadeIn value
146165
*/
166+
@Deprecated
147167
public FadeType getFadeType() {
148-
return fadeType;
168+
return fadeIn.getType();
149169
}
150170

151171
/**
152172
* Sets the FadeType for this SongPlayer
153173
* @param fadeType
174+
* @deprecated set fadeIn value
154175
*/
176+
@Deprecated
155177
public void setFadeType(FadeType fadeType) {
156-
this.fadeType = fadeType;
178+
fadeIn.setType(fadeType);
157179
CallUpdate("fadetype", "FADE_" + fadeType.name());
158180
}
159181

160182
/**
161183
* Target volume for fade
162184
* @return byte representing fade target
185+
* @deprecated returns fadeIn value
163186
*/
187+
@Deprecated
164188
public byte getFadeTarget() {
165-
return fadeTarget;
189+
return fadeIn.getFadeTarget();
166190
}
167191

168192
/**
169193
* Set target volume for fade
170194
* @param fadeTarget
195+
* @deprecated set fadeIn value
171196
*/
197+
@Deprecated
172198
public void setFadeTarget(byte fadeTarget) {
173-
this.fadeTarget = fadeTarget;
199+
fadeIn.setFadeTarget(fadeTarget);
174200
CallUpdate("fadeTarget", fadeTarget);
175201
}
176202

177203
/**
178-
* Gets the starting time for the fade
204+
* Gets the starting volume for the fade
179205
* @return
206+
* @deprecated returns fadeIn value
180207
*/
208+
@Deprecated
181209
public byte getFadeStart() {
182-
return fadeStart;
210+
return fadeIn.getFadeStart();
183211
}
184212

185213
/**
186-
* Sets the starting time for the fade
214+
* Sets the starting volume for the fade
187215
* @param fadeStart
216+
* @deprecated set fadeIn value
188217
*/
218+
@Deprecated
189219
public void setFadeStart(byte fadeStart) {
190-
this.fadeStart = fadeStart;
220+
fadeIn.setFadeStart(fadeStart);
191221
CallUpdate("fadeStart", fadeStart);
192222
}
193223

194224
/**
195225
* Gets the duration of the fade
196226
* @return duration of the fade
227+
* @deprecated returns fadeIn value
197228
*/
229+
@Deprecated
198230
public int getFadeDuration() {
199-
return fadeDuration;
231+
return fadeIn.getFadeDuration();
200232
}
201233

202234
/**
203235
* Sets the duration of the fade
204236
* @param fadeDuration
237+
* @deprecated set fadeIn value
205238
*/
239+
@Deprecated
206240
public void setFadeDuration(int fadeDuration) {
207-
this.fadeDuration = fadeDuration;
241+
fadeIn.setFadeDuration(fadeDuration);
208242
CallUpdate("fadeDuration", fadeDuration);
209243
}
210244

211245
/**
212246
* Gets the tick when fade will be finished
213247
* @return tick
248+
* @deprecated returns fadeIn value
214249
*/
250+
@Deprecated
215251
public int getFadeDone() {
216-
return fadeDone;
252+
return fadeIn.getFadeDone();
217253
}
218254

219255
/**
220256
* Sets the tick when fade will be finished
221257
* @param fadeDone
258+
* @deprecated set fadeIn value
222259
*/
260+
@Deprecated
223261
public void setFadeDone(int fadeDone) {
224-
this.fadeDone = fadeDone;
225-
CallUpdate("fadeDone", fadeDone);
226-
}
227-
228-
/**
229-
* Calculates the fade at the given time and sets the current volume
230-
*/
231-
protected void calculateFade() {
232-
if (fadeDone == fadeDuration) {
233-
return; // no fade today
234-
}
235-
double targetVolume = Interpolator.interpLinear(
236-
new double[]{0, fadeStart, fadeDuration, fadeTarget}, fadeDone);
237-
setVolume((byte) targetVolume);
238-
fadeDone++;
262+
fadeIn.setFadeDone(fadeDone);
239263
CallUpdate("fadeDone", fadeDone);
240264
}
241265

@@ -253,11 +277,24 @@ private void start() {
253277
}
254278

255279
if (playing) {
256-
calculateFade();
280+
if (tick < fadeIn.getFadeDuration()){
281+
int fade = fadeIn.calculateFade();
282+
if (fade != -1){
283+
volume = (byte) fade;
284+
}
285+
CallUpdate("fadeDone", fadeIn.getFadeDone());
286+
} else if (tick >= song.getLength() - fadeOut.getFadeDuration()){
287+
int fade = fadeOut.calculateFade();
288+
if (fade != -1){
289+
volume = (byte) fade;
290+
}
291+
}
292+
257293
tick++;
258294
if (tick > song.getLength()) {
259295
tick = -1;
260-
fadeDone = 0;
296+
fadeIn.setFadeDone(0);
297+
fadeOut.setFadeDone(0);
261298
if (loop){
262299
SongLoopEvent event = new SongLoopEvent(this);
263300
plugin.doSync(() -> Bukkit.getPluginManager().callEvent(event));
@@ -309,6 +346,22 @@ private void start() {
309346
});
310347
}
311348

349+
/**
350+
* Returns {@link Fade} for Fade in effect
351+
* @return Fade
352+
*/
353+
public Fade getFadeIn(){
354+
return fadeIn;
355+
}
356+
357+
/**
358+
* Returns {@link Fade} for Fade out effect
359+
* @return Fade
360+
*/
361+
public Fade getFadeOut(){
362+
return fadeOut;
363+
}
364+
312365
/**
313366
* Gets list of current Player UUIDs listening to this SongPlayer
314367
* @return list of Player UUIDs
@@ -495,6 +548,9 @@ public void setVolume(byte volume) {
495548
}
496549
this.volume = volume;
497550

551+
fadeIn.setFadeTarget(volume);
552+
fadeOut.setFadeStart(volume);
553+
498554
CallUpdate("volume", volume);
499555
}
500556

0 commit comments

Comments
 (0)