Skip to content

Commit

Permalink
(#23) Began work on streaming audio from files
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jun 6, 2021
1 parent 6fed164 commit f2d1ac1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/tech/fastj/engine/FastJEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tech.fastj.graphics.util.DisplayUtil;

import tech.fastj.systems.audio.AudioManager;
import tech.fastj.systems.audio.AudioPlayer;
import tech.fastj.systems.behaviors.BehaviorManager;
import tech.fastj.systems.control.LogicManager;
import tech.fastj.systems.input.keyboard.Keyboard;
Expand Down Expand Up @@ -486,6 +487,7 @@ private static void exit() {
Mouse.stop();
Keyboard.stop();
AudioManager.reset();
AudioPlayer.reset();
BehaviorManager.reset();
TagManager.reset();

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

import tech.fastj.engine.CrashMessages;
import tech.fastj.engine.FastJEngine;

import javax.sound.sampled.AudioFormat;
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;
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AudioPlayer {

public static final int BufferSize = 4096;

private static final ExecutorService LineWriter = Executors.newWorkStealingPool();

public static void playSoundEffect(Path soundPath) {
AudioInputStream audioInputStream = AudioManager.newAudioStream(soundPath);
if (audioInputStream == null) {
return;
}

SourceDataLine sourceDataLine = getAudioOutputLine(audioInputStream.getFormat());
if (sourceDataLine == null) {
return;
}

LineWriter.submit(() -> {
try {
sourceDataLine.open(audioInputStream.getFormat());
sourceDataLine.start();
} catch (LineUnavailableException exception) {
FastJEngine.error(CrashMessages.theGameCrashed("an error while trying to play sound."), exception);
return;
}
// System.out.println("float control time yay");
// System.out.println(Arrays.toString(sourceDataLine.getControls()));
// FloatControl gainControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
// gainControl.setValue(gainControl.getMaximum());
// FloatControl panControl = (FloatControl) sourceDataLine.getControl(FloatControl.Type.PAN);
// panControl.shift(-1.0f, 1.0f, 100000000);
// System.out.println(floatControl);
// System.out.println("float control time over?");
// System.out.println(sourceDataLine.getClass());

int sourceLength;
byte[] soundSamples = new byte[BufferSize];
try {
while ((sourceLength = audioInputStream.read(soundSamples, 0, BufferSize)) != -1) {
sourceDataLine.write(soundSamples, 0, sourceLength);
}
} catch (IOException exception) {
FastJEngine.error(CrashMessages.theGameCrashed("an error while trying to play sound."), exception);
} finally {
sourceDataLine.drain();
sourceDataLine.close();
}
});
}


private static SourceDataLine getAudioOutputLine(AudioFormat audioFormat) {
DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);

if (!AudioSystem.isLineSupported(lineInfo)) {
FastJEngine.warning("No audio output lines supported.");
return null;
}

try {
return (SourceDataLine) AudioSystem.getLine(lineInfo);
} catch (LineUnavailableException exception) {
FastJEngine.error(
CrashMessages.theGameCrashed("an audio error while trying to open an audio line."),
exception
);
return null;
}
}

public static void reset() {
LineWriter.shutdownNow();
}
}
Binary file added src/test/resources/Don't_Get_Shot,_Guys!.wav
Binary file not shown.

0 comments on commit f2d1ac1

Please sign in to comment.