Massively improve audio stability on Mac

pull/67/head
James Ball 2022-05-14 15:31:26 +01:00 zatwierdzone przez James H Ball
rodzic 9fdab5a4f1
commit 3089cd2b15
3 zmienionych plików z 24 dodań i 27 usunięć

1
.gitignore vendored
Wyświetl plik

@ -2,6 +2,7 @@
/.idea /.idea
osci-render.iml osci-render.iml
*.osci *.osci
*.log
# ignore temporary files # ignore temporary files
*~ *~

Wyświetl plik

@ -47,51 +47,48 @@ public class JavaAudioEngine implements AudioEngine {
// connects to a device that can support the format above (i.e. default audio device) // connects to a device that can support the format above (i.e. default audio device)
this.source = AudioSystem.getSourceDataLine(format); this.source = AudioSystem.getSourceDataLine(format);
source.open(format);
int bufferSize = calculateBufferSize(device, UNSTABLE_LATENCY_MS); int bufferSize = calculateBufferSize(device, UNSTABLE_LATENCY_MS);
int remainingBufferSpace = source.getBufferSize() - bufferSize;
byte[] buffer = new byte[bufferSize * 2]; byte[] buffer = new byte[bufferSize * 2];
source.open(format, buffer.length);
source.start(); source.start();
while (!stopped) { while (!stopped) {
if (makeMoreStable || makeLessStable) { if (makeMoreStable || makeLessStable) {
int newLatency = makeMoreStable ? STABLE_LATENCY_MS : UNSTABLE_LATENCY_MS; int newLatency = makeMoreStable ? STABLE_LATENCY_MS : UNSTABLE_LATENCY_MS;
bufferSize = calculateBufferSize(device, newLatency); bufferSize = calculateBufferSize(device, newLatency);
remainingBufferSpace = source.getBufferSize() - bufferSize;
buffer = new byte[bufferSize * 2]; buffer = new byte[bufferSize * 2];
isStable = makeMoreStable; isStable = makeMoreStable;
makeMoreStable = false; makeMoreStable = false;
makeLessStable = false; makeLessStable = false;
} }
int delta = source.available() - remainingBufferSpace;
if (delta > 0) {
int requiredSamples = (delta + bufferSize) / FRAME_SIZE;
if (requiredSamples * NUM_CHANNELS > buffer.length / 2) { int requiredSamples = bufferSize / FRAME_SIZE;
buffer = new byte[requiredSamples * NUM_CHANNELS * 2];
}
for (int i = 0; i < requiredSamples; i++) { if (requiredSamples * NUM_CHANNELS > buffer.length / 2) {
try { buffer = new byte[requiredSamples * NUM_CHANNELS * 2];
Vector2 channels = channelGenerator.call();
// converting doubles from Vector2 into shorts and then bytes so
// that the byte buffer supports them
short left = (short) (channels.getX() * Short.MAX_VALUE);
short right = (short) (channels.getY() * Short.MAX_VALUE);
buffer[i * 4] = (byte) left;
buffer[i * 4 + 1] = (byte) (left >> 8);
buffer[i * 4 + 2] = (byte) right;
buffer[i * 4 + 3] = (byte) (right >> 8);
} catch (Exception e) {
e.printStackTrace();
}
}
source.write(buffer, 0, requiredSamples * FRAME_SIZE);
} }
for (int i = 0; i < requiredSamples; i++) {
try {
Vector2 channels = channelGenerator.call();
// converting doubles from Vector2 into shorts and then bytes so
// that the byte buffer supports them
short left = (short) (channels.getX() * Short.MAX_VALUE);
short right = (short) (channels.getY() * Short.MAX_VALUE);
buffer[i * 4] = (byte) left;
buffer[i * 4 + 1] = (byte) (left >> 8);
buffer[i * 4 + 2] = (byte) right;
buffer[i * 4 + 3] = (byte) (right >> 8);
} catch (Exception e) {
e.printStackTrace();
}
}
source.write(buffer, 0, requiredSamples * FRAME_SIZE);
} }
source.stop(); source.stop();
this.device = null; this.device = null;

Wyświetl plik

@ -39,7 +39,6 @@ public class Gui extends Application {
@Override @Override
public void start(Stage stage) throws Exception { public void start(Stage stage) throws Exception {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
System.setProperty("prism.lcdtext", "false"); System.setProperty("prism.lcdtext", "false");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"));