From 4fffef2ca42494526254029279b56016efe176f6 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 20 May 2023 07:53:29 +0200 Subject: Synth02AL: Use ALAudioSink's default latency instead of fixed frameDuration, using actual OpenAL frame processing refresh value Also pre-allocate sampleBuffer to 30ms actual format, using AudioFormat.getDurationsByteSize(..) --- .../com/jogamp/openal/test/manual/Synth02AL.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/test/com/jogamp/openal/test/manual/Synth02AL.java b/src/test/com/jogamp/openal/test/manual/Synth02AL.java index bc7cb43..b7c7136 100644 --- a/src/test/com/jogamp/openal/test/manual/Synth02AL.java +++ b/src/test/com/jogamp/openal/test/manual/Synth02AL.java @@ -72,10 +72,6 @@ public final class Synth02AL { private static final float SHORT_MAX = 32767.0f; // == Short.MAX_VALUE - public static final int frameDuration = 12; // AudioSink.DefaultFrameDuration; // [ms] - - public static final int audioQueueLimit = 3 * frameDuration; - public static final float MIDDLE_C = 261.625f; private final Object stateLock = new Object(); @@ -98,6 +94,9 @@ public final class Synth02AL { } public float getAmplitude() { return audioAmplitude; } + /** Returns latency or frame-duration in milliseconds */ + public int getLatency() { return null != streamWorker ? streamWorker.frameDuration : 2*AudioSink.DefaultFrameDuration; } + public void play() { synchronized( stateLock ) { if( null == streamWorker ) { @@ -152,7 +151,7 @@ public final class Synth02AL { final String as = null != streamWorker ? streamWorker.audioSink.toString() : "ALAudioSink[null]"; final int pts = getPTS(); final int lag = getGenPTS() - pts; - return getClass().getSimpleName()+"[f "+audioFreq+", a "+audioAmplitude+", state[running "+isRunning()+", playing "+isPlaying()+"], pts[gen "+getGenPTS()+", play "+pts+", lag "+lag+"], "+as+"]"; + return getClass().getSimpleName()+"[f "+audioFreq+", a "+audioAmplitude+", latency "+getLatency()+", state[running "+isRunning()+", playing "+isPlaying()+"], pts[gen "+getGenPTS()+", play "+pts+", lag "+lag+"], "+as+"]"; } } @@ -173,7 +172,10 @@ public final class Synth02AL { private final boolean useFloat32SampleType; private final int bytesPerSample; private final AudioFormat audioFormat; - private ByteBuffer sampleBuffer = allocate(2*1000); + private ByteBuffer sampleBuffer; + private int frameDuration; + private int audioQueueLimit; + private float lastFreq; private float nextSin; private boolean upSin; @@ -206,7 +208,12 @@ public final class Synth02AL { } System.err.println("OpenAL float32 supported: "+useFloat32SampleType); - sampleBuffer = allocate(bytesPerSample*1000); + sampleBuffer = allocate( audioFormat.getDurationsByteSize(30) ); // pre-allocate buffer for 30ms + + // clip [16 .. 2*AudioSink.DefaultFrameDuration] + frameDuration = Math.max( 16, Math.min(2*AudioSink.DefaultFrameDuration, Math.round( 1000f*audioSink.getDefaultLatency() ) ) ); // ms + audioQueueLimit = 3 * frameDuration; + audioSink.init(audioFormat, frameDuration, audioQueueLimit, 0, audioQueueLimit); lastFreq = 0; nextSin = 0; @@ -470,11 +477,11 @@ public final class Synth02AL { for(float f=min; f