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,28 +47,26 @@ 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 = bufferSize / FRAME_SIZE;
int requiredSamples = (delta + bufferSize) / FRAME_SIZE;
if (requiredSamples * NUM_CHANNELS > buffer.length / 2) { if (requiredSamples * NUM_CHANNELS > buffer.length / 2) {
buffer = new byte[requiredSamples * NUM_CHANNELS * 2]; buffer = new byte[requiredSamples * NUM_CHANNELS * 2];
@ -92,7 +90,6 @@ public class JavaAudioEngine implements AudioEngine {
source.write(buffer, 0, requiredSamples * FRAME_SIZE); 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"));