From 6a74d16a805a4204093972bb91361b2aa633065c Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 23 May 2023 01:37:01 +0200 Subject: AudioSink: Drop getMaxSupportedChannels(), use getNativeFormat(), getPreferredFormat() and isSupported(); Add setChannelLimit() impacting Add setChannelLimit() impacting getPreferredFormat() and isSupported(), i.e. to limit channels for e.g. JOAL/OpenAL spatial 3D sound usage. getNativeFormat() shall be unaffected. getMaxSupportedChannels() is redudandant -> getPreferredFormat() --- src/java/jogamp/common/av/JavaSoundAudioSink.java | 43 +++++++++++++++++++---- src/java/jogamp/common/av/NullAudioSink.java | 14 ++++---- 2 files changed, 42 insertions(+), 15 deletions(-) (limited to 'src/java/jogamp/common') diff --git a/src/java/jogamp/common/av/JavaSoundAudioSink.java b/src/java/jogamp/common/av/JavaSoundAudioSink.java index d3ddda9..58f0fec 100644 --- a/src/java/jogamp/common/av/JavaSoundAudioSink.java +++ b/src/java/jogamp/common/av/JavaSoundAudioSink.java @@ -61,6 +61,9 @@ public final class JavaSoundAudioSink implements AudioSink { private int bufferCount; private final byte [] sampleData = new byte[BUFFER_SIZE]; private boolean available = false; + private final AudioFormat nativeFormat; + private int userMaxChannels = 8; + private AudioFormat preferredFormat = null; private AudioFormat chosenFormat = null; private volatile boolean playRequested = false; @@ -79,8 +82,12 @@ public final class JavaSoundAudioSink implements AudioSink { public JavaSoundAudioSink() { available = false; if( !staticAvailable ) { + nativeFormat = DefaultFormat; return; } + nativeFormat = new AudioFormat(DefaultFormat.sampleRate, DefaultFormat.sampleSize, getMaxSupportedChannels(), + DefaultFormat.signed, DefaultFormat.fixedP, DefaultFormat.planar, DefaultFormat.littleEndian); + preferredFormat = nativeFormat; available = true; } @@ -117,11 +124,6 @@ public final class JavaSoundAudioSink implements AudioSink { return true; } - @Override - public int getPreferredSampleRate() { - return DefaultFormat.sampleRate; - } - @Override public int getSourceCount() { return -1; @@ -137,18 +139,42 @@ public final class JavaSoundAudioSink implements AudioSink { return 20f/1000f; // fake 20ms } + @Override + public final AudioFormat getNativeFormat() { + return nativeFormat; + } + @Override public AudioFormat getPreferredFormat() { return DefaultFormat; } @Override - public final int getMaxSupportedChannels() { - return 2; + public final void setChannelLimit(final int cc) { + userMaxChannels = Math.min(8, Math.max(1, cc)); + preferredFormat = new AudioFormat(nativeFormat.sampleRate, + nativeFormat.sampleSize, getMaxSupportedChannels(), + nativeFormat.signed, nativeFormat.fixedP, + nativeFormat.planar, nativeFormat.littleEndian); + if( DEBUG ) { + System.out.println("ALAudioSink: channelLimit "+userMaxChannels+", preferredFormat "+preferredFormat); + } + } + + private final int getMaxSupportedChannels() { + final int cc = 2; + return Math.min(userMaxChannels, cc); } @Override public final boolean isSupported(final AudioFormat format) { + if( format.planar != preferredFormat.planar || + format.fixedP != preferredFormat.fixedP || + format.sampleRate > preferredFormat.sampleRate || + format.channelCount > preferredFormat.channelCount ) + { + return false; + } return true; } @@ -157,6 +183,9 @@ public final class JavaSoundAudioSink implements AudioSink { if( !staticAvailable ) { return false; } + if( !isSupported(requestedFormat) ) { + return false; + } // Create the audio format we wish to use format = new javax.sound.sampled.AudioFormat(requestedFormat.sampleRate, requestedFormat.sampleSize, requestedFormat.channelCount, requestedFormat.signed, !requestedFormat.littleEndian); diff --git a/src/java/jogamp/common/av/NullAudioSink.java b/src/java/jogamp/common/av/NullAudioSink.java index 19b0acb..904b863 100644 --- a/src/java/jogamp/common/av/NullAudioSink.java +++ b/src/java/jogamp/common/av/NullAudioSink.java @@ -83,11 +83,6 @@ public final class NullAudioSink implements AudioSink { return true; } - @Override - public int getPreferredSampleRate() { - return DefaultFormat.sampleRate; - } - @Override public int getSourceCount() { return -1; @@ -104,15 +99,18 @@ public final class NullAudioSink implements AudioSink { } @Override - public AudioFormat getPreferredFormat() { + public AudioFormat getNativeFormat() { return DefaultFormat; } @Override - public final int getMaxSupportedChannels() { - return 8; + public AudioFormat getPreferredFormat() { + return DefaultFormat; } + @Override + public void setChannelLimit(final int cc) { } + @Override public final boolean isSupported(final AudioFormat format) { /** -- cgit v1.2.3