Skip to content
Snippets Groups Projects
Commit 4bcd5a30 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Add abstract fade controller, rename existing logarithmic fade class

parent a4517fb9
No related branches found
No related tags found
No related merge requests found
...@@ -7,9 +7,9 @@ import de.tobias.playpad.audio.AudioRegistry; ...@@ -7,9 +7,9 @@ import de.tobias.playpad.audio.AudioRegistry;
import de.tobias.playpad.pad.Pad; import de.tobias.playpad.pad.Pad;
import de.tobias.playpad.pad.PadStatus; import de.tobias.playpad.pad.PadStatus;
import de.tobias.playpad.pad.content.play.*; import de.tobias.playpad.pad.content.play.*;
import de.tobias.playpad.pad.fade.Fade; import de.tobias.playpad.pad.fade.FadeControllerDelegate;
import de.tobias.playpad.pad.fade.FadeDelegate;
import de.tobias.playpad.pad.fade.Fadeable; import de.tobias.playpad.pad.fade.Fadeable;
import de.tobias.playpad.pad.fade.LogarithmicFadeController;
import de.tobias.playpad.pad.mediapath.MediaPath; import de.tobias.playpad.pad.mediapath.MediaPath;
import de.tobias.playpad.volume.VolumeManager; import de.tobias.playpad.volume.VolumeManager;
import javafx.application.Platform; import javafx.application.Platform;
...@@ -24,7 +24,7 @@ import java.nio.file.Files; ...@@ -24,7 +24,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
public class AudioContent extends PadContent implements Pauseable, Durationable, Fadeable, public class AudioContent extends PadContent implements Pauseable, Durationable, Fadeable,
Equalizeable, FadeDelegate, Seekable, SpeedAdjustable { Equalizeable, FadeControllerDelegate, Seekable, SpeedAdjustable {
private final String type; private final String type;
...@@ -36,12 +36,12 @@ public class AudioContent extends PadContent implements Pauseable, Durationable, ...@@ -36,12 +36,12 @@ public class AudioContent extends PadContent implements Pauseable, Durationable,
private final ChangeListener<Number> volumeListener; private final ChangeListener<Number> volumeListener;
private final ChangeListener<Number> rateListener; private final ChangeListener<Number> rateListener;
private final Fade fade; private final LogarithmicFadeController fade;
AudioContent(String type, Pad pad) { AudioContent(String type, Pad pad) {
super(pad); super(pad);
this.type = type; this.type = type;
fade = new Fade(this); fade = new LogarithmicFadeController(this);
// Pad Volume Listener // Pad Volume Listener
volumeListener = (a, oldValue, newValue) -> updateVolume(); volumeListener = (a, oldValue, newValue) -> updateVolume();
......
...@@ -4,19 +4,14 @@ import javafx.animation.Transition; ...@@ -4,19 +4,14 @@ import javafx.animation.Transition;
import javafx.util.Duration; import javafx.util.Duration;
/** /**
* Fade utils. * @since 7.1.0
*
* @author tobias
* @since 6.0.0
*/ */
public class Fade { public abstract class AbstractFadeController {
private static final double VELOCITY = 1;
private final FadeDelegate fadeDelegate;
private Transition currentFadeTransition; private Transition currentFadeTransition;
protected final FadeControllerDelegate fadeDelegate;
public Fade(FadeDelegate fadeDelegate) { public AbstractFadeController(FadeControllerDelegate fadeDelegate) {
this.fadeDelegate = fadeDelegate; this.fadeDelegate = fadeDelegate;
} }
...@@ -58,15 +53,7 @@ public class Fade { ...@@ -58,15 +53,7 @@ public class Fade {
@Override @Override
protected void interpolate(double frac) { protected void interpolate(double frac) {
double diff = Math.abs(to - from); AbstractFadeController.this.interpolate(this, frac, from, to);
if (from < to) { // Fade In
double fade = fadeInVolumeMultiplier(frac);
fadeDelegate.onFadeLevelChange(from + fade * diff);
} else { // Fade Out
double fade = fadeOutVolumeMultiplier(frac);
double newValue = to + fade * diff;
fadeDelegate.onFadeLevelChange(newValue);
}
} }
}; };
currentFadeTransition.setOnFinished(e -> currentFadeTransition.setOnFinished(e ->
...@@ -79,12 +66,5 @@ public class Fade { ...@@ -79,12 +66,5 @@ public class Fade {
currentFadeTransition.play(); currentFadeTransition.play();
} }
private double fadeInVolumeMultiplier(double time) { protected abstract void interpolate(Transition transition, double frac, double from, double to);
return Math.pow(Math.E, VELOCITY * (time - 1)) * time;
}
private double fadeOutVolumeMultiplier(double time) {
return Math.pow(Math.E, -VELOCITY * time) * (1 - time);
}
} }
...@@ -6,7 +6,7 @@ package de.tobias.playpad.pad.fade; ...@@ -6,7 +6,7 @@ package de.tobias.playpad.pad.fade;
* @author tobias * @author tobias
* @since 6.0.0 * @since 6.0.0
*/ */
public interface FadeDelegate { public interface FadeControllerDelegate {
/** /**
* If the fade level is changed, the delegate performs this method. The faded object should update. * If the fade level is changed, the delegate performs this method. The faded object should update.
......
package de.tobias.playpad.pad.fade;
import javafx.animation.Transition;
/**
* A fade controller implementation, that handles fade scala logarithmic for dB.
*
* @author tobias
* @since 6.0.0
*/
public class LogarithmicFadeController extends AbstractFadeController {
private static final double VELOCITY = 1;
public LogarithmicFadeController(FadeControllerDelegate fadeDelegate) {
super(fadeDelegate);
}
@Override
protected void interpolate(Transition transition, double frac, double from, double to) {
double diff = Math.abs(to - from);
if (from < to) { // Fade In
double fade = computeFadeInMultiplier(frac);
fadeDelegate.onFadeLevelChange(from + fade * diff);
} else { // Fade Out
double fade = computeFadeOutMultiplier(frac);
double newValue = to + fade * diff;
fadeDelegate.onFadeLevelChange(newValue);
}
}
protected double computeFadeInMultiplier(double frac) {
return Math.pow(Math.E, VELOCITY * (frac - 1)) * frac;
}
protected double computeFadeOutMultiplier(double frac) {
return Math.pow(Math.E, -VELOCITY * frac) * (1 - frac);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment