mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-15 21:31:12 +00:00
Only use the audio buffer hack on OS X
This commit is contained in:
@@ -16,8 +16,10 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
private byte[] lineBuffer;
|
private byte[] lineBuffer;
|
||||||
private int channelCount;
|
private int channelCount;
|
||||||
private int sampleRate;
|
private int sampleRate;
|
||||||
|
private boolean reallocateLines;
|
||||||
|
|
||||||
public static final int DEFAULT_BUFFER_SIZE = 4096;
|
public static final int DEFAULT_BUFFER_SIZE = 0;
|
||||||
|
public static final int STARING_BUFFER_SIZE = 4096;
|
||||||
public static final int STAGING_BUFFERS = 3; // 3 complete frames of audio
|
public static final int STAGING_BUFFERS = 3; // 3 complete frames of audio
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -26,10 +28,8 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
// Queue the decoded samples into the staging sound buffer
|
// Queue the decoded samples into the staging sound buffer
|
||||||
soundBuffer.queue(new ShortBufferDescriptor(pcmData, offset, length));
|
soundBuffer.queue(new ShortBufferDescriptor(pcmData, offset, length));
|
||||||
|
|
||||||
// If there's space available in the sound line, pull some data out
|
|
||||||
// of the staging buffer and write it to the sound line
|
|
||||||
int available = soundLine.available();
|
int available = soundLine.available();
|
||||||
|
if (reallocateLines) {
|
||||||
// Kinda jank. If the queued is larger than available, we are going to have a delay
|
// Kinda jank. If the queued is larger than available, we are going to have a delay
|
||||||
// so we increase the buffer size
|
// so we increase the buffer size
|
||||||
if (available < soundBuffer.size()) {
|
if (available < soundBuffer.size()) {
|
||||||
@@ -37,10 +37,19 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
int currentBuffer = soundLine.getBufferSize();
|
int currentBuffer = soundLine.getBufferSize();
|
||||||
soundLine.close();
|
soundLine.close();
|
||||||
createSoundLine(currentBuffer*2);
|
createSoundLine(currentBuffer*2);
|
||||||
|
if (soundLine != null) {
|
||||||
available = soundLine.available();
|
available = soundLine.available();
|
||||||
System.out.println("creating new line with buffer size: " + soundLine.getBufferSize());
|
System.out.println("creating new line with buffer size: " + soundLine.getBufferSize());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
available = 0;
|
||||||
|
System.out.println("failed to create sound line");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's space available in the sound line, pull some data out
|
||||||
|
// of the staging buffer and write it to the sound line
|
||||||
if (available > 0) {
|
if (available > 0) {
|
||||||
int written = soundBuffer.fill(lineBuffer, 0, available);
|
int written = soundBuffer.fill(lineBuffer, 0, available);
|
||||||
if (written > 0) {
|
if (written > 0) {
|
||||||
@@ -59,10 +68,26 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
|
|
||||||
private void createSoundLine(int bufferSize) {
|
private void createSoundLine(int bufferSize) {
|
||||||
AudioFormat audioFormat = new AudioFormat(sampleRate, 16, channelCount, true, true);
|
AudioFormat audioFormat = new AudioFormat(sampleRate, 16, channelCount, true, true);
|
||||||
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
|
|
||||||
|
DataLine.Info info;
|
||||||
|
|
||||||
|
if (bufferSize == DEFAULT_BUFFER_SIZE) {
|
||||||
|
info = new DataLine.Info(SourceDataLine.class, audioFormat);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
info = new DataLine.Info(SourceDataLine.class, audioFormat, bufferSize);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
soundLine = (SourceDataLine) AudioSystem.getLine(info);
|
soundLine = (SourceDataLine) AudioSystem.getLine(info);
|
||||||
|
|
||||||
|
if (bufferSize == DEFAULT_BUFFER_SIZE) {
|
||||||
|
soundLine.open(audioFormat);
|
||||||
|
}
|
||||||
|
else {
|
||||||
soundLine.open(audioFormat, bufferSize);
|
soundLine.open(audioFormat, bufferSize);
|
||||||
|
}
|
||||||
|
|
||||||
soundLine.start();
|
soundLine.start();
|
||||||
lineBuffer = new byte[soundLine.getBufferSize()];
|
lineBuffer = new byte[soundLine.getBufferSize()];
|
||||||
soundBuffer = new SoundBuffer(STAGING_BUFFERS);
|
soundBuffer = new SoundBuffer(STAGING_BUFFERS);
|
||||||
@@ -75,7 +100,16 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
public void streamInitialized(int channelCount, int sampleRate) {
|
public void streamInitialized(int channelCount, int sampleRate) {
|
||||||
this.channelCount = channelCount;
|
this.channelCount = channelCount;
|
||||||
this.sampleRate = sampleRate;
|
this.sampleRate = sampleRate;
|
||||||
|
|
||||||
|
// Workaround OS X's bad Java mixer
|
||||||
|
if (System.getProperty("os.name").contains("Mac OS X")) {
|
||||||
|
createSoundLine(STARING_BUFFER_SIZE);
|
||||||
|
reallocateLines = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
createSoundLine(DEFAULT_BUFFER_SIZE);
|
createSoundLine(DEFAULT_BUFFER_SIZE);
|
||||||
|
reallocateLines = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user