- Fix for the Alternate Sound System by kevlahnota

This commit is contained in:
Michael Kamensky
2021-02-13 16:49:09 +03:00
parent 02297189ad
commit deea26d985

View File

@@ -1,6 +1,10 @@
package forge.sound; package forge.sound;
import com.google.common.io.Files;
import com.sipgate.mp3wav.Converter;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
@@ -40,7 +44,7 @@ class AsyncSoundRegistry {
} }
} }
public class AltSoundSystem extends Thread { public class AltSoundSystem extends Thread {
private String filename; private String filename;
private boolean isSync; private boolean isSync;
@@ -48,13 +52,13 @@ public class AltSoundSystem extends Thread {
private final int EXTERNAL_BUFFER_SIZE = 524288; private final int EXTERNAL_BUFFER_SIZE = 524288;
private final int MAX_SOUND_ITERATIONS = 5; private final int MAX_SOUND_ITERATIONS = 5;
public AltSoundSystem(String wavfile, boolean synced) { public AltSoundSystem(String wavfile, boolean synced) {
filename = wavfile; filename = wavfile;
isSync = synced; isSync = synced;
} }
@Override @Override
public void run() { public void run() {
if (isSync && AsyncSoundRegistry.getNumIterations(filename) >= 1) { if (isSync && AsyncSoundRegistry.getNumIterations(filename) >= 1) {
return; return;
} }
@@ -63,31 +67,45 @@ public class AltSoundSystem extends Thread {
} }
File soundFile = new File(filename); File soundFile = new File(filename);
if (!soundFile.exists()) { if (!soundFile.exists()) {
return; return;
} }
AudioInputStream audioInputStream = null; AudioInputStream audioInputStream = null;
try { try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile); ByteArrayInputStream bis = new ByteArrayInputStream(Converter.convertFrom(Files.asByteSource(soundFile).openStream()).toByteArray());
} catch (UnsupportedAudioFileException e) { audioInputStream = AudioSystem.getAudioInputStream(bis);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
} }
AudioFormat format = audioInputStream.getFormat(); AudioFormat format = audioInputStream.getFormat();
SourceDataLine audioLine = null; SourceDataLine audioLine = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try { Mixer.Info selectedMixer = null;
audioLine = (SourceDataLine) AudioSystem.getLine(info);
audioLine.open(format); for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
} catch (Exception e) { Mixer mixer = AudioSystem.getMixer(mixerInfo);
if (mixer.isLineSupported(info)) {
selectedMixer = mixerInfo;
break;
}
}
if (selectedMixer == null)
return; return;
}
try {
audioLine = AudioSystem.getSourceDataLine(format, selectedMixer);
audioLine.open(format);
} catch (Exception e) {
return;
}
audioLine.start(); audioLine.start();
AsyncSoundRegistry.registerSound(filename); AsyncSoundRegistry.registerSound(filename);
@@ -95,16 +113,16 @@ public class AltSoundSystem extends Thread {
int nBytesRead = 0; int nBytesRead = 0;
byte[] audioBufData = new byte[EXTERNAL_BUFFER_SIZE]; byte[] audioBufData = new byte[EXTERNAL_BUFFER_SIZE];
try { try {
while (nBytesRead != -1) { while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(audioBufData, 0, audioBufData.length); nBytesRead = audioInputStream.read(audioBufData, 0, audioBufData.length);
if (nBytesRead >= 0) if (nBytesRead >= 0)
audioLine.write(audioBufData, 0, nBytesRead); audioLine.write(audioBufData, 0, nBytesRead);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return; return;
} finally { } finally {
audioLine.drain(); audioLine.drain();
audioLine.close(); audioLine.close();
try { try {
@@ -113,6 +131,6 @@ public class AltSoundSystem extends Thread {
// Can't do much if closing it fails. // Can't do much if closing it fails.
} }
AsyncSoundRegistry.unregisterSound(filename); AsyncSoundRegistry.unregisterSound(filename);
} }
} }
} }