Skip to content

Commit

Permalink
(#23) Fixed unit tests, fixed issue with StreamedAudioPlayer resettin…
Browse files Browse the repository at this point in the history
…g, added missing documentation

Breaking Changes:
- `PlaybackState` has been moved to `systems.audio.state`
  • Loading branch information
lucasstarsz committed Jul 10, 2021
1 parent fd26107 commit 583ae92
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
exports tech.fastj.input.mouse;

exports tech.fastj.systems.audio;
exports tech.fastj.systems.audio.state;
exports tech.fastj.systems.behaviors;
exports tech.fastj.systems.control;
exports tech.fastj.systems.fio;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/systems/audio/Audio.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.fastj.systems.audio;

import tech.fastj.systems.audio.state.PlaybackState;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.DataLine;
import java.nio.file.Path;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/tech/fastj/systems/audio/AudioManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tech.fastj.engine.CrashMessages;
import tech.fastj.engine.FastJEngine;

import tech.fastj.systems.audio.state.PlaybackState;
import tech.fastj.systems.fio.FileUtil;

import javax.sound.sampled.*;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/systems/audio/MemoryAudio.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.fastj.systems.audio;

import tech.fastj.systems.audio.state.PlaybackState;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import java.nio.file.Path;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/systems/audio/MemoryAudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import tech.fastj.engine.FastJEngine;
import tech.fastj.math.Maths;

import tech.fastj.systems.audio.state.PlaybackState;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/tech/fastj/systems/audio/PlaybackState.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/systems/audio/StreamedAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import tech.fastj.engine.CrashMessages;
import tech.fastj.engine.FastJEngine;

import tech.fastj.systems.audio.state.PlaybackState;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.FloatControl;
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/tech/fastj/systems/audio/StreamedAudioPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import tech.fastj.engine.CrashMessages;
import tech.fastj.engine.FastJEngine;

import javax.sound.sampled.AudioFormat;
import tech.fastj.systems.audio.state.PlaybackState;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.io.IOException;
Expand All @@ -17,15 +16,17 @@
public class StreamedAudioPlayer {

public static final int BufferSize = 4096;
private static final ExecutorService LineWriter = Executors.newWorkStealingPool();
private static ExecutorService lineWriter = Executors.newWorkStealingPool();

/** Resets the {@code StreamedAudioPlayer}, removing all of its loaded audio files. */
public static void reset() {
LineWriter.shutdownNow();
lineWriter.shutdownNow();
lineWriter = Executors.newWorkStealingPool();
}

/** Sets up the audio streaming process for the specified {@link StreamedAudio} object. */
static void streamAudio(StreamedAudio audio) {
LineWriter.submit(() -> {
lineWriter.submit(() -> {
SourceDataLine sourceDataLine = audio.getAudioSource();
AudioInputStream audioInputStream = audio.getAudioInputStream();

Expand All @@ -49,6 +50,7 @@ static void streamAudio(StreamedAudio audio) {
});
}

/** See {@link StreamedAudio#play()}. */
static void playAudio(StreamedAudio audio) {
SourceDataLine sourceDataLine = audio.getAudioSource();
AudioInputStream audioInputStream = audio.getAudioInputStream();
Expand All @@ -69,6 +71,7 @@ static void playAudio(StreamedAudio audio) {
}
}

/** See {@link StreamedAudio#pause()}. */
static void pauseAudio(StreamedAudio audio) {
SourceDataLine sourceDataLine = audio.getAudioSource();

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/tech/fastj/systems/audio/state/PlaybackState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.fastj.systems.audio.state;

import tech.fastj.systems.audio.Audio;

/** Enum containing the different states an {@link Audio} instance can be in. */
public enum PlaybackState {
/** {@link Audio} state when the audio is emitting sound. */
Playing,
/** {@link Audio} state when the audio is held from emitting sound, but still ready to resume playing. */
Paused,
/** {@link Audio} state when the audio is not emitting sound. */
Stopped
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ void tryMemoryAudioLoading_withUnsupportedAudioFormat() {
Throwable exception = assertThrows(IllegalStateException.class, () -> AudioManager.loadMemoryAudioInstance(testAudioPath));
Throwable underlyingException = exception.getCause();
assertEquals(UnsupportedAudioFileException.class, underlyingException.getClass(), "The underlying exception's class should match the expected exception's class.");
assertEquals(underlyingException.getMessage(), testAudioPath.toAbsolutePath() + " seems to be of an unsupported file format.", "Upon reading an unsupported audio file format, an error should be thrown.");
assertEquals(underlyingException.getMessage(), testAudioPath.toAbsolutePath() + " is of an unsupported file format \"flac\".", "Upon reading an unsupported audio file format, an error should be thrown.");
}

@Test
void checkStreamedAudioLoading_withWAVFormatAudio() {
StreamedAudio streamedAudio = AudioManager.loadStreamedAudioInstance(TestAudioPath);
assertNotNull(AudioManager.getMemoryAudio(streamedAudio.getID()), "Loading the audio file into memory should cause it to be stored in the audio player.");
assertNotNull(AudioManager.getStreamedAudio(streamedAudio.getID()), "Loading the audio file into memory should cause it to be stored in the audio player.");
}

@Test
Expand All @@ -90,6 +90,6 @@ void tryStreamedAudioLoading_withUnsupportedAudioFormat() {
Throwable exception = assertThrows(IllegalStateException.class, () -> AudioManager.loadStreamedAudioInstance(testAudioPath));
Throwable underlyingException = exception.getCause();
assertEquals(UnsupportedAudioFileException.class, underlyingException.getClass(), "The underlying exception's class should match the expected exception's class.");
assertEquals(underlyingException.getMessage(), testAudioPath.toAbsolutePath() + " seems to be of an unsupported file format.", "Upon reading an unsupported audio file format, an error should be thrown.");
assertEquals(underlyingException.getMessage(), testAudioPath.toAbsolutePath() + " is of an unsupported file format \"flac\".", "Upon reading an unsupported audio file format, an error should be thrown.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import tech.fastj.systems.audio.AudioManager;
import tech.fastj.systems.audio.MemoryAudio;
import tech.fastj.systems.audio.PlaybackState;
import tech.fastj.systems.audio.state.PlaybackState;

import java.nio.file.Path;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package unittest.testcases.systems.audio;

import tech.fastj.systems.audio.AudioManager;
import tech.fastj.systems.audio.PlaybackState;
import tech.fastj.systems.audio.state.PlaybackState;
import tech.fastj.systems.audio.StreamedAudio;

import java.nio.file.Path;
Expand Down

0 comments on commit 583ae92

Please sign in to comment.