From 583ae92ea032b989d62f91787f77bf5e6cc3b8b5 Mon Sep 17 00:00:00 2001 From: lucasstarsz Date: Sat, 10 Jul 2021 11:59:16 -0400 Subject: [PATCH] (#23) Fixed unit tests, fixed issue with StreamedAudioPlayer resetting, added missing documentation Breaking Changes: - `PlaybackState` has been moved to `systems.audio.state` --- src/main/java/module-info.java | 1 + src/main/java/tech/fastj/systems/audio/Audio.java | 2 ++ .../tech/fastj/systems/audio/AudioManager.java | 1 + .../tech/fastj/systems/audio/MemoryAudio.java | 2 ++ .../fastj/systems/audio/MemoryAudioPlayer.java | 2 ++ .../tech/fastj/systems/audio/PlaybackState.java | 7 ------- .../tech/fastj/systems/audio/StreamedAudio.java | 2 ++ .../fastj/systems/audio/StreamedAudioPlayer.java | 15 +++++++++------ .../fastj/systems/audio/state/PlaybackState.java | 13 +++++++++++++ .../systems/audio/AudioManagerTests.java | 6 +++--- .../testcases/systems/audio/MemoryAudioTests.java | 2 +- .../systems/audio/StreamedAudioTests.java | 2 +- 12 files changed, 37 insertions(+), 18 deletions(-) delete mode 100644 src/main/java/tech/fastj/systems/audio/PlaybackState.java create mode 100644 src/main/java/tech/fastj/systems/audio/state/PlaybackState.java diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 244429fc..00be9d62 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -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; diff --git a/src/main/java/tech/fastj/systems/audio/Audio.java b/src/main/java/tech/fastj/systems/audio/Audio.java index c1f99aa7..a0fbe189 100644 --- a/src/main/java/tech/fastj/systems/audio/Audio.java +++ b/src/main/java/tech/fastj/systems/audio/Audio.java @@ -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; diff --git a/src/main/java/tech/fastj/systems/audio/AudioManager.java b/src/main/java/tech/fastj/systems/audio/AudioManager.java index 297b81c9..579a968e 100644 --- a/src/main/java/tech/fastj/systems/audio/AudioManager.java +++ b/src/main/java/tech/fastj/systems/audio/AudioManager.java @@ -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.*; diff --git a/src/main/java/tech/fastj/systems/audio/MemoryAudio.java b/src/main/java/tech/fastj/systems/audio/MemoryAudio.java index 33c43835..dd652f4b 100644 --- a/src/main/java/tech/fastj/systems/audio/MemoryAudio.java +++ b/src/main/java/tech/fastj/systems/audio/MemoryAudio.java @@ -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; diff --git a/src/main/java/tech/fastj/systems/audio/MemoryAudioPlayer.java b/src/main/java/tech/fastj/systems/audio/MemoryAudioPlayer.java index 3952a3b3..a52c3b00 100644 --- a/src/main/java/tech/fastj/systems/audio/MemoryAudioPlayer.java +++ b/src/main/java/tech/fastj/systems/audio/MemoryAudioPlayer.java @@ -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; diff --git a/src/main/java/tech/fastj/systems/audio/PlaybackState.java b/src/main/java/tech/fastj/systems/audio/PlaybackState.java deleted file mode 100644 index 5ee38513..00000000 --- a/src/main/java/tech/fastj/systems/audio/PlaybackState.java +++ /dev/null @@ -1,7 +0,0 @@ -package tech.fastj.systems.audio; - -public enum PlaybackState { - Playing, - Paused, - Stopped -} \ No newline at end of file diff --git a/src/main/java/tech/fastj/systems/audio/StreamedAudio.java b/src/main/java/tech/fastj/systems/audio/StreamedAudio.java index b803accd..0beca4ab 100644 --- a/src/main/java/tech/fastj/systems/audio/StreamedAudio.java +++ b/src/main/java/tech/fastj/systems/audio/StreamedAudio.java @@ -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; diff --git a/src/main/java/tech/fastj/systems/audio/StreamedAudioPlayer.java b/src/main/java/tech/fastj/systems/audio/StreamedAudioPlayer.java index 81a8de0a..d5e89b29 100644 --- a/src/main/java/tech/fastj/systems/audio/StreamedAudioPlayer.java +++ b/src/main/java/tech/fastj/systems/audio/StreamedAudioPlayer.java @@ -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; @@ -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(); @@ -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(); @@ -69,6 +71,7 @@ static void playAudio(StreamedAudio audio) { } } + /** See {@link StreamedAudio#pause()}. */ static void pauseAudio(StreamedAudio audio) { SourceDataLine sourceDataLine = audio.getAudioSource(); diff --git a/src/main/java/tech/fastj/systems/audio/state/PlaybackState.java b/src/main/java/tech/fastj/systems/audio/state/PlaybackState.java new file mode 100644 index 00000000..bd64cb88 --- /dev/null +++ b/src/main/java/tech/fastj/systems/audio/state/PlaybackState.java @@ -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 +} \ No newline at end of file diff --git a/src/test/java/unittest/testcases/systems/audio/AudioManagerTests.java b/src/test/java/unittest/testcases/systems/audio/AudioManagerTests.java index ea154986..7f0b0b63 100644 --- a/src/test/java/unittest/testcases/systems/audio/AudioManagerTests.java +++ b/src/test/java/unittest/testcases/systems/audio/AudioManagerTests.java @@ -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 @@ -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."); } } diff --git a/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java b/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java index e53865a7..c9caae67 100644 --- a/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java +++ b/src/test/java/unittest/testcases/systems/audio/MemoryAudioTests.java @@ -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; diff --git a/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java b/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java index e3438282..25fea9ec 100644 --- a/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java +++ b/src/test/java/unittest/testcases/systems/audio/StreamedAudioTests.java @@ -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;