From deea26d985494d7472995e6c06300f96f8d39a9f Mon Sep 17 00:00:00 2001 From: Michael Kamensky Date: Sat, 13 Feb 2021 16:49:09 +0300 Subject: [PATCH] - Fix for the Alternate Sound System by kevlahnota --- .../main/java/forge/sound/AltSoundSystem.java | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java b/forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java index 6aed9f95289..428f9b0fc1e 100644 --- a/forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java +++ b/forge-gui-desktop/src/main/java/forge/sound/AltSoundSystem.java @@ -1,6 +1,10 @@ package forge.sound; +import com.google.common.io.Files; +import com.sipgate.mp3wav.Converter; + import javax.sound.sampled.*; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -40,7 +44,7 @@ class AsyncSoundRegistry { } } -public class AltSoundSystem extends Thread { +public class AltSoundSystem extends Thread { private String filename; private boolean isSync; @@ -48,13 +52,13 @@ public class AltSoundSystem extends Thread { private final int EXTERNAL_BUFFER_SIZE = 524288; private final int MAX_SOUND_ITERATIONS = 5; - public AltSoundSystem(String wavfile, boolean synced) { + public AltSoundSystem(String wavfile, boolean synced) { filename = wavfile; isSync = synced; - } + } @Override - public void run() { + public void run() { if (isSync && AsyncSoundRegistry.getNumIterations(filename) >= 1) { return; } @@ -63,31 +67,45 @@ public class AltSoundSystem extends Thread { } File soundFile = new File(filename); - if (!soundFile.exists()) { + if (!soundFile.exists()) { return; - } + } AudioInputStream audioInputStream = null; - try { - audioInputStream = AudioSystem.getAudioInputStream(soundFile); - } catch (UnsupportedAudioFileException e) { + try { + ByteArrayInputStream bis = new ByteArrayInputStream(Converter.convertFrom(Files.asByteSource(soundFile).openStream()).toByteArray()); + audioInputStream = AudioSystem.getAudioInputStream(bis); + } catch (UnsupportedAudioFileException e) { e.printStackTrace(); return; - } catch (IOException e) { + } catch (IOException e) { e.printStackTrace(); return; - } + } AudioFormat format = audioInputStream.getFormat(); SourceDataLine audioLine = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); - try { - audioLine = (SourceDataLine) AudioSystem.getLine(info); - audioLine.open(format); - } catch (Exception e) { + Mixer.Info selectedMixer = null; + + for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) { + Mixer mixer = AudioSystem.getMixer(mixerInfo); + if (mixer.isLineSupported(info)) { + selectedMixer = mixerInfo; + break; + } + } + + if (selectedMixer == null) return; - } + + try { + audioLine = AudioSystem.getSourceDataLine(format, selectedMixer); + audioLine.open(format); + } catch (Exception e) { + return; + } audioLine.start(); AsyncSoundRegistry.registerSound(filename); @@ -95,16 +113,16 @@ public class AltSoundSystem extends Thread { int nBytesRead = 0; byte[] audioBufData = new byte[EXTERNAL_BUFFER_SIZE]; - try { - while (nBytesRead != -1) { + try { + while (nBytesRead != -1) { nBytesRead = audioInputStream.read(audioBufData, 0, audioBufData.length); - if (nBytesRead >= 0) + if (nBytesRead >= 0) audioLine.write(audioBufData, 0, nBytesRead); - } - } catch (IOException e) { + } + } catch (IOException e) { e.printStackTrace(); return; - } finally { + } finally { audioLine.drain(); audioLine.close(); try { @@ -113,6 +131,6 @@ public class AltSoundSystem extends Thread { // Can't do much if closing it fails. } AsyncSoundRegistry.unregisterSound(filename); - } - } -} + } + } +}