Skip to content

Commit

Permalink
(#23) Changed AudioEventListener actions to consumers (consume LineEv…
Browse files Browse the repository at this point in the history
…ent)
  • Loading branch information
lucasstarsz committed Jul 10, 2021
1 parent bc50ef6 commit f093b1d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
57 changes: 29 additions & 28 deletions src/main/java/tech/fastj/systems/audio/AudioEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,46 @@
import javax.sound.sampled.LineEvent;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/** An event listener for the {@link MemoryAudio} class. */
public class AudioEventListener {

private Runnable audioOpenAction;
private Runnable audioCloseAction;
private Runnable audioStartAction;
private Runnable audioStopAction;
private Runnable audioPauseAction;
private Runnable audioResumeAction;
private Consumer<LineEvent> audioOpenAction;
private Consumer<LineEvent> audioCloseAction;
private Consumer<LineEvent> audioStartAction;
private Consumer<LineEvent> audioStopAction;
private Consumer<LineEvent> audioPauseAction;
private Consumer<LineEvent> audioResumeAction;

private final Audio audio;

private static final Map<LineEvent.Type, Consumer<AudioEventListener>> AudioEventProcessor = Map.of(
LineEvent.Type.OPEN, audioEventListener -> audioEventListener.audioOpenAction.run(),
LineEvent.Type.START, audioEventListener -> {
private static final Map<LineEvent.Type, BiConsumer<LineEvent, AudioEventListener>> AudioEventProcessor = Map.of(
LineEvent.Type.OPEN, (audioEvent, audioEventListener) -> audioEventListener.audioOpenAction.accept(audioEvent),
LineEvent.Type.START, (audioEvent, audioEventListener) -> {
switch (audioEventListener.audio.getPreviousPlaybackState()) {
case Paused: {
audioEventListener.audioResumeAction.run();
audioEventListener.audioResumeAction.accept(audioEvent);
}
case Stopped: {
audioEventListener.audioStartAction.run();
audioEventListener.audioStartAction.accept(audioEvent);
break;
}
}
},
LineEvent.Type.STOP, audioEventListener -> {
LineEvent.Type.STOP, (audioEvent, audioEventListener) -> {
switch (audioEventListener.audio.getCurrentPlaybackState()) {
case Paused: {
audioEventListener.audioPauseAction.run();
audioEventListener.audioPauseAction.accept(audioEvent);
}
case Stopped: {
audioEventListener.audioStopAction.run();
audioEventListener.audioStopAction.accept(audioEvent);
break;
}
}
},
LineEvent.Type.CLOSE, audioEventListener -> audioEventListener.audioCloseAction.run()
LineEvent.Type.CLOSE, (audioEvent, audioEventListener) -> audioEventListener.audioCloseAction.accept(audioEvent)
);

/**
Expand All @@ -59,7 +60,7 @@ public class AudioEventListener {
*
* @return The "audio open" event action.
*/
public Runnable getAudioOpenAction() {
public Consumer<LineEvent> getAudioOpenAction() {
return audioOpenAction;
}

Expand All @@ -68,7 +69,7 @@ public Runnable getAudioOpenAction() {
*
* @return The "audio close" event action.
*/
public Runnable getAudioCloseAction() {
public Consumer<LineEvent> getAudioCloseAction() {
return audioCloseAction;
}

Expand All @@ -77,7 +78,7 @@ public Runnable getAudioCloseAction() {
*
* @return The "audio start" event action.
*/
public Runnable getAudioStartAction() {
public Consumer<LineEvent> getAudioStartAction() {
return audioStartAction;
}

Expand All @@ -86,7 +87,7 @@ public Runnable getAudioStartAction() {
*
* @return The "audio stop" event action.
*/
public Runnable getAudioStopAction() {
public Consumer<LineEvent> getAudioStopAction() {
return audioStopAction;
}

Expand All @@ -95,7 +96,7 @@ public Runnable getAudioStopAction() {
*
* @return The "audio pause" event action.
*/
public Runnable getAudioPauseAction() {
public Consumer<LineEvent> getAudioPauseAction() {
return audioPauseAction;
}

Expand All @@ -104,7 +105,7 @@ public Runnable getAudioPauseAction() {
*
* @return The "audio resume" event action.
*/
public Runnable getAudioResumeAction() {
public Consumer<LineEvent> getAudioResumeAction() {
return audioResumeAction;
}

Expand All @@ -113,7 +114,7 @@ public Runnable getAudioResumeAction() {
*
* @param audioOpenAction The action to set.
*/
public void setAudioOpenAction(Runnable audioOpenAction) {
public void setAudioOpenAction(Consumer<LineEvent> audioOpenAction) {
this.audioOpenAction = audioOpenAction;
}

Expand All @@ -122,7 +123,7 @@ public void setAudioOpenAction(Runnable audioOpenAction) {
*
* @param audioCloseAction The action to set.
*/
public void setAudioCloseAction(Runnable audioCloseAction) {
public void setAudioCloseAction(Consumer<LineEvent> audioCloseAction) {
this.audioCloseAction = audioCloseAction;
}

Expand All @@ -131,7 +132,7 @@ public void setAudioCloseAction(Runnable audioCloseAction) {
*
* @param audioStartAction The action to set.
*/
public void setAudioStartAction(Runnable audioStartAction) {
public void setAudioStartAction(Consumer<LineEvent> audioStartAction) {
this.audioStartAction = audioStartAction;
}

Expand All @@ -140,7 +141,7 @@ public void setAudioStartAction(Runnable audioStartAction) {
*
* @param audioStopAction The action to set.
*/
public void setAudioStopAction(Runnable audioStopAction) {
public void setAudioStopAction(Consumer<LineEvent> audioStopAction) {
this.audioStopAction = audioStopAction;
}

Expand All @@ -149,7 +150,7 @@ public void setAudioStopAction(Runnable audioStopAction) {
*
* @param audioPauseAction The action to set.
*/
public void setAudioPauseAction(Runnable audioPauseAction) {
public void setAudioPauseAction(Consumer<LineEvent> audioPauseAction) {
this.audioPauseAction = audioPauseAction;
}

Expand All @@ -158,7 +159,7 @@ public void setAudioPauseAction(Runnable audioPauseAction) {
*
* @param audioResumeAction The action to set.
*/
public void setAudioResumeAction(Runnable audioResumeAction) {
public void setAudioResumeAction(Consumer<LineEvent> audioResumeAction) {
this.audioResumeAction = audioResumeAction;
}

Expand All @@ -168,6 +169,6 @@ public void setAudioResumeAction(Runnable audioResumeAction) {
* @param audioEvent The event fired.
*/
public void fireEvent(LineEvent audioEvent) {
AudioEventProcessor.get(audioEvent.getType()).accept(this);
AudioEventProcessor.get(audioEvent.getType()).accept(audioEvent, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void checkPlayMemoryAudio_shouldTriggerOpenAndStartEvents() throws InterruptedEx
MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath);
AtomicBoolean audioOpenEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStartEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioOpenAction(() -> audioOpenEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(true));
audio.getAudioEventListener().setAudioOpenAction(audioEvent -> audioOpenEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -137,8 +137,8 @@ void checkPauseMemoryAudio_shouldTriggerPauseAndStopEvents() throws InterruptedE
MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath);
AtomicBoolean audioPauseEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioPauseAction(() -> audioPauseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true));
audio.getAudioEventListener().setAudioPauseAction(audioEvent -> audioPauseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -154,8 +154,8 @@ void checkResumeMemoryAudio_shouldTriggerStartAndResumeEvents() throws Interrupt
MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath);
AtomicBoolean audioStartEventBoolean = new AtomicBoolean(true);
AtomicBoolean audioResumeEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(!audioStartEventBoolean.get()));
audio.getAudioEventListener().setAudioResumeAction(() -> audioResumeEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(!audioStartEventBoolean.get()));
audio.getAudioEventListener().setAudioResumeAction(audioEvent -> audioResumeEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -173,8 +173,8 @@ void checkStopMemoryAudio_shouldTriggerStopAndCloseEvents() throws InterruptedEx
MemoryAudio audio = AudioManager.loadMemoryAudioInstance(TestAudioPath);
AtomicBoolean audioCloseEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioCloseAction(() -> audioCloseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true));
audio.getAudioEventListener().setAudioCloseAction(audioEvent -> audioCloseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ void checkPlayStreamedAudio_shouldTriggerOpenAndStartEvents() throws Interrupted
StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath);
AtomicBoolean audioOpenEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStartEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioOpenAction(() -> audioOpenEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(true));
audio.getAudioEventListener().setAudioOpenAction(audioEvent -> audioOpenEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -57,8 +57,8 @@ void checkPauseStreamedAudio_shouldTriggerPauseAndStopEvents() throws Interrupte
StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath);
AtomicBoolean audioPauseEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioPauseAction(() -> audioPauseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true));
audio.getAudioEventListener().setAudioPauseAction(audioEvent -> audioPauseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -74,8 +74,8 @@ void checkResumeStreamedAudio_shouldTriggerStartAndResumeEvents() throws Interru
StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath);
AtomicBoolean audioStartEventBoolean = new AtomicBoolean(true);
AtomicBoolean audioResumeEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioStartAction(() -> audioStartEventBoolean.set(!audioStartEventBoolean.get()));
audio.getAudioEventListener().setAudioResumeAction(() -> audioResumeEventBoolean.set(true));
audio.getAudioEventListener().setAudioStartAction(audioEvent -> audioStartEventBoolean.set(!audioStartEventBoolean.get()));
audio.getAudioEventListener().setAudioResumeAction(audioEvent -> audioResumeEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand All @@ -93,8 +93,8 @@ void checkStopStreamedAudio_shouldTriggerStopAndCloseEvents() throws Interrupted
StreamedAudio audio = AudioManager.loadStreamedAudioInstance(TestAudioPath);
AtomicBoolean audioCloseEventBoolean = new AtomicBoolean(false);
AtomicBoolean audioStopEventBoolean = new AtomicBoolean(false);
audio.getAudioEventListener().setAudioCloseAction(() -> audioCloseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(() -> audioStopEventBoolean.set(true));
audio.getAudioEventListener().setAudioCloseAction(audioEvent -> audioCloseEventBoolean.set(true));
audio.getAudioEventListener().setAudioStopAction(audioEvent -> audioStopEventBoolean.set(true));

audio.play();
TimeUnit.MILLISECONDS.sleep(3);
Expand Down

0 comments on commit f093b1d

Please sign in to comment.