diff options
author | Sven Gothel <[email protected]> | 2023-05-18 04:13:57 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-05-18 04:13:57 +0200 |
commit | d5daaaab3544d9af49056f57a1fcf53abef17deb (patch) | |
tree | 47d664d81f6262596d148e8dda4f8052aea2872d /src/java | |
parent | 5320233de825b5e3c2131c9303ef94990a40fcb4 (diff) |
ALAudioSink: Promote to public, be fully functional regarding AudioFormat and OpenAL paremeter. Can be 'plugged' into existing OpenAL logic.
Diffstat (limited to 'src/java')
-rw-r--r-- | src/java/com/jogamp/openal/util/ALAudioSink.java (renamed from src/java/jogamp/openal/util/ALAudioSink.java) | 247 | ||||
-rw-r--r-- | src/java/com/jogamp/openal/util/ALHelpers.java | 242 |
2 files changed, 387 insertions, 102 deletions
diff --git a/src/java/jogamp/openal/util/ALAudioSink.java b/src/java/com/jogamp/openal/util/ALAudioSink.java index 824c227..e763899 100644 --- a/src/java/jogamp/openal/util/ALAudioSink.java +++ b/src/java/com/jogamp/openal/util/ALAudioSink.java @@ -25,7 +25,7 @@ * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ -package jogamp.openal.util; +package com.jogamp.openal.util; import java.nio.ByteBuffer; @@ -35,7 +35,9 @@ import java.util.concurrent.TimeUnit; import jogamp.openal.Debug; import com.jogamp.common.ExceptionUtils; +import com.jogamp.common.av.AudioFormat; import com.jogamp.common.av.AudioSink; +import com.jogamp.common.av.AudioSink.AudioFrame; import com.jogamp.common.os.Clock; import com.jogamp.common.util.LFRingbuffer; import com.jogamp.common.util.PropertyAccess; @@ -50,24 +52,19 @@ import com.jogamp.openal.ALCdevice; import com.jogamp.openal.ALConstants; import com.jogamp.openal.ALExt; import com.jogamp.openal.ALFactory; -import com.jogamp.openal.util.ALHelpers; /*** - * OpenAL Audio Sink + * OpenAL {@link AudioSink} implementation. + * <p> + * Besides given {@link AudioSink} functionality, implementation is fully functional regarding {@link AudioFormat} and all OpenAL parameter.<br/> + * <ul> + * <li>All OpenAL parameter can be queried</li> + * <li>Instance can be constructed with an OpenAL device and context, see {@link #ALAudioSink(ALCdevice, ALCcontext)}</li> + * <li>Initialization can be performed with OpenAL paramters, see {@link #init(int, int, int, int, int, float, int, int, int)}</li> + * </ul> + * </p> */ public class ALAudioSink implements AudioSink { - - /** - * [openal-soft >= 1.18.0](https://github.com/kcat/openal-soft/blob/master/ChangeLog) - * - Removed support for the AL_SOFT_buffer_samples and AL_SOFT_buffer_sub_data - * extensions. Due to conflicts with AL_EXT_SOURCE_RADIUS. - */ - private static final String AL_SOFT_buffer_samples = "AL_SOFT_buffer_samples"; - private static final String AL_EXT_MCFORMATS = "AL_EXT_MCFORMATS"; - private static final String AL_EXT_FLOAT32 = "AL_EXT_FLOAT32"; - private static final String AL_EXT_DOUBLE = "AL_EXT_DOUBLE"; - - private static final String ALC_EXT_thread_local_context = "ALC_EXT_thread_local_context"; private static final boolean DEBUG_TRACE; private static final ALC alc; private static final AL al; @@ -111,7 +108,6 @@ public class ALAudioSink implements AudioSink { } } - // private ALAudioFrame[] alFrames = null; private int[] alBufferNames = null; private int avgFrameDuration = 0; // [ms] private int frameGrowAmount = 0; @@ -123,7 +119,7 @@ public class ALAudioSink implements AudioSink { private volatile int playingPTS = AudioFrame.INVALID_PTS; private volatile int enqueuedFrameCount; - private int[] alSource = null; + private final int[] alSource = { -1 }; // actually ALuint, but JOAL expects INT_MAX limit is ok! private AudioFormat chosenFormat; private int alChannelLayout; private int alSampleType; @@ -168,7 +164,20 @@ public class ALAudioSink implements AudioSink { return ok; } + /** + * Create a new instance with all new OpenAL objects, i.e. opened default {@link ALCdevice} and new {@link ALCcontext}. + */ public ALAudioSink() { + this(null, null); + } + + /** + * Create a new instance based on optional given OpenAL objects. + * + * @param alDevice optional OpenAL device, a default device is opened if null. + * @param alContext optional OpenAL context associated to the given device, a new context is created if null. + */ + public ALAudioSink(final ALCdevice alDevice, final ALCcontext alContext) { available = false; chosenFormat = null; @@ -177,27 +186,35 @@ public class ALAudioSink implements AudioSink { } synchronized(ALAudioSink.class) { try { - // Get handle to default device. - device = alc.alcOpenDevice(null); - if (device == null) { - throw new RuntimeException(getThreadName()+": ALAudioSink: Error opening default OpenAL device"); + if( null == alDevice ) { + // Get handle to default device. + device = alc.alcOpenDevice(null); + if (device == null) { + throw new RuntimeException(getThreadName()+": ALAudioSink: Error opening default OpenAL device"); + } + clearPreALError("init.dev"); + } else { + device = alDevice; } int checkErrIter = 1; - clearPreALError("init."+checkErrIter++); - // Get the device specifier. deviceSpecifier = alc.alcGetString(device, ALCConstants.ALC_DEVICE_SPECIFIER); if (deviceSpecifier == null) { throw new RuntimeException(getThreadName()+": ALAudioSink: Error getting specifier for default OpenAL device"); } + clearPreALError("init."+checkErrIter++); - // Create audio context. - // final int[] attrs = new int[] { ALC.ALC_FREQUENCY, DefaultFormat.sampleRate, 0 }; - // context = alc.alcCreateContext(device, attrs, 0); - context = alc.alcCreateContext(device, null); - if (context == null) { - throw new RuntimeException(getThreadName()+": ALAudioSink: Error creating OpenAL context for "+deviceSpecifier); + if( null == alContext ) { + // Create audio context. + // final int[] attrs = new int[] { ALC.ALC_FREQUENCY, DefaultFormat.sampleRate, 0 }; + // context = alc.alcCreateContext(device, attrs, 0); + context = alc.alcCreateContext(device, null); + if (context == null) { + throw new RuntimeException(getThreadName()+": ALAudioSink: Error creating OpenAL context for "+deviceSpecifier); + } + } else { + context = alContext; } lockContext(); @@ -206,12 +223,12 @@ public class ALAudioSink implements AudioSink { if ( alc.alcGetError(device) != ALCConstants.ALC_NO_ERROR ) { throw new RuntimeException(getThreadName()+": ALAudioSink: Error making OpenAL context current"); } - hasSOFTBufferSamples = al.alIsExtensionPresent(AL_SOFT_buffer_samples); - hasEXTMcFormats = al.alIsExtensionPresent(AL_EXT_MCFORMATS); - hasEXTFloat32 = al.alIsExtensionPresent(AL_EXT_FLOAT32); - hasEXTDouble = al.alIsExtensionPresent(AL_EXT_DOUBLE); - hasALC_thread_local_context = alc.alcIsExtensionPresent(null, ALC_EXT_thread_local_context) || - alc.alcIsExtensionPresent(device, ALC_EXT_thread_local_context) ; + hasSOFTBufferSamples = al.alIsExtensionPresent(ALHelpers.AL_SOFT_buffer_samples); + hasEXTMcFormats = al.alIsExtensionPresent(ALHelpers.AL_EXT_MCFORMATS); + hasEXTFloat32 = al.alIsExtensionPresent(ALHelpers.AL_EXT_FLOAT32); + hasEXTDouble = al.alIsExtensionPresent(ALHelpers.AL_EXT_DOUBLE); + hasALC_thread_local_context = alc.alcIsExtensionPresent(null, ALHelpers.ALC_EXT_thread_local_context) || + alc.alcIsExtensionPresent(device, ALHelpers.ALC_EXT_thread_local_context) ; clearPreALError("init."+checkErrIter++); preferredSampleRate = querySampleRate(); preferredAudioFormat = new AudioFormat(preferredSampleRate, DefaultFormat.sampleSize, DefaultFormat.channelCount, DefaultFormat.signed, DefaultFormat.fixedP, DefaultFormat.planar, DefaultFormat.littleEndian); @@ -243,11 +260,10 @@ public class ALAudioSink implements AudioSink { // Create source { - alSource = new int[1]; al.alGenSources(1, alSource, 0); final int err = al.alGetError(); if( ALConstants.AL_NO_ERROR != err ) { - alSource = null; + alSource[0] = -1; throw new RuntimeException(getThreadName()+": ALAudioSink: Error generating Source: 0x"+Integer.toHexString(err)); } } @@ -287,6 +303,44 @@ public class ALAudioSink implements AudioSink { return sampleRate; } + // Expose AudioSink OpenAL implementation specifics + + /** Return OpenAL global {@link AL}. */ + public static final AL getAL() { return al; } + /** Return OpenAL global {@link ALC}. */ + public static final ALC getALC() { return alc; } + /** Return OpenAL global {@link ALExt}. */ + public static final ALExt getALExt() { return alExt; } + + /** Return this instance's OpenAL {@link ALCdevice}. */ + public final ALCdevice getDevice() { return device; } + /** Return this instance's OpenAL {@link ALCdevice} specifier. */ + public final String getDeviceSpec() { return deviceSpecifier; } + /** Return this instance's OpenAL {@link ALCcontext}. */ + public final ALCcontext getALContext() { return context; } + /** Return this instance's OpenAL source ID. */ + public final int getALSource() { return alSource[0]; } + + /** Return whether OpenAL extension <code>AL_SOFT_buffer_samples</code> is available. */ + public final boolean hasSOFTBufferSamples() { return hasSOFTBufferSamples; } + /** Return whether OpenAL extension <code>AL_EXT_MCFORMATS</code> is available. */ + public final boolean hasEXTMcFormats() { return hasEXTMcFormats; } + /** Return whether OpenAL extension <code>AL_EXT_FLOAT32</code> is available. */ + public final boolean hasEXTFloat32() { return hasEXTFloat32; } + /** Return whether OpenAL extension <code>AL_EXT_DOUBLE</code> is available. */ + public final boolean hasEXTDouble() { return hasEXTDouble; } + /** Return whether OpenAL extension <code>ALC_EXT_thread_local_context</code> is available. */ + public final boolean hasALCThreadLocalContext() { return hasALC_thread_local_context; } + + /** Return this instance's OpenAL channel layout, set after {@link #init(AudioFormat, float, int, int, int)}. */ + public final int getALChannelLayout() { return alChannelLayout; } + /** Return this instance's OpenAL sample type, set after {@link #init(AudioFormat, float, int, int, int)}. */ + public final int getALSampleType() { return alSampleType; } + /** Return this instance's OpenAL format, set after {@link #init(AudioFormat, float, int, int, int)}. */ + public final int getALFormat() { return alFormat; } + + // AudioSink implementation ... + @Override public final void lockExclusive() { lockContext(); @@ -422,42 +476,25 @@ public class ALAudioSink implements AudioSink { return false; } if( format.planar || !format.littleEndian ) { - // FIXME big-endian supported w/ SOFT where it's native format! if( DEBUG ) { System.err.println(getThreadName()+": ALAudioSink.isSupported: NO.0 "+format); } return false; } - final int alChannelLayout = ALHelpers.getDefaultALChannelLayout(format.channelCount); - if( ALConstants.AL_NONE != alChannelLayout ) { - final int alSampleType = ALHelpers.getALSampleType(format.sampleSize, format.signed, format.fixedP); - if( ALConstants.AL_NONE != alSampleType ) { - lockContext(); - try { - final int alFormat = ALHelpers.getALFormat(alChannelLayout, alSampleType, - hasSOFTBufferSamples, hasEXTMcFormats, hasEXTFloat32, hasEXTDouble, - al, alExt); - if( ALConstants.AL_NONE != alFormat ) { - if( DEBUG ) { - System.err.println(getThreadName()+": ALAudioSink.isSupported: OK "+format+", alFormat "+toHexString(alFormat)); - } - return true; - } else { - if( DEBUG ) { - System.err.println(getThreadName()+": ALAudioSink.isSupported: NO.1 "+format); - } - return false; - } - - } finally { - unlockContext(); - } + final int alFormat = ALHelpers.getALFormat(format, al, alExt, + hasSOFTBufferSamples, hasEXTMcFormats, + hasEXTFloat32, hasEXTDouble); + if( ALConstants.AL_NONE != alFormat ) { + if( DEBUG ) { + System.err.println(getThreadName()+": ALAudioSink.isSupported: OK "+format+", alFormat "+toHexString(alFormat)); } + return true; + } else { + if( DEBUG ) { + System.err.println(getThreadName()+": ALAudioSink.isSupported: NO.1 "+format); + } + return false; } - if( DEBUG ) { - System.err.println(getThreadName()+": ALAudioSink.isSupported: NO.X "+format); - } - return false; } @Override @@ -465,24 +502,68 @@ public class ALAudioSink implements AudioSink { if( !staticAvailable ) { return false; } - alChannelLayout = ALHelpers.getDefaultALChannelLayout(requestedFormat.channelCount); - alSampleType = ALHelpers.getALSampleType(requestedFormat.sampleSize, requestedFormat.signed, requestedFormat.fixedP); - lockContext(); - try { - if( ALConstants.AL_NONE != alChannelLayout && ALConstants.AL_NONE != alSampleType ) { - alFormat = ALHelpers.getALFormat(alChannelLayout, alSampleType, - hasSOFTBufferSamples, hasEXTMcFormats, hasEXTFloat32, hasEXTDouble, - al, alExt); - } else { - alFormat = ALConstants.AL_NONE; + final int alChannelLayout = ALHelpers.getDefaultALChannelLayout(requestedFormat.channelCount); + final int alSampleType = ALHelpers.getALSampleType(requestedFormat.sampleSize, requestedFormat.signed, requestedFormat.fixedP); + final int alFormat; + if( ALConstants.AL_NONE != alChannelLayout && ALConstants.AL_NONE != alSampleType ) { + alFormat = ALHelpers.getALFormat(alChannelLayout, alSampleType, al, alExt, + hasSOFTBufferSamples, hasEXTMcFormats, + hasEXTFloat32, hasEXTDouble); + } else { + alFormat = ALConstants.AL_NONE; + } + if( ALConstants.AL_NONE == alFormat ) { + // not supported + if( DEBUG ) { + System.err.println(getThreadName()+": ALAudioSink.init1: Not supported: "+requestedFormat+", "+toString()); } - if( ALConstants.AL_NONE == alFormat ) { - // not supported - if( DEBUG ) { - System.err.println(getThreadName()+": ALAudioSink.init: Not supported: "+requestedFormat+", "+toString()); - } - return false; + return false; + } + return initImpl(requestedFormat, alChannelLayout, alSampleType, alFormat, + frameDuration, initialQueueSize, queueGrowAmount, queueLimit); + } + + /** + * Initializes the sink using the given OpenAL audio parameter and streaming details. + * @param alChannelLayout OpenAL channel layout + * @param alSampleType OpenAL sample type + * @param alFormat OpenAL format + * @param sampleRate sample rate, e.g. 44100 + * @param sampleSize sample size in bits, e.g. 16 + * @param frameDuration average or fixed frame duration in milliseconds + * helping a caching {@link AudioFrame} based implementation to determine the frame count in the queue. + * See {@link #DefaultFrameDuration}. + * @param initialQueueSize initial time in milliseconds to queue in this sink, see {@link #DefaultInitialQueueSize}. + * @param queueGrowAmount time in milliseconds to grow queue if full, see {@link #DefaultQueueGrowAmount}. + * @param queueLimit maximum time in milliseconds the queue can hold (and grow), see {@link #DefaultQueueLimitWithVideo} and {@link #DefaultQueueLimitAudioOnly}. + * @return true if successful, otherwise false + * @see ALHelpers#getAudioFormat(int, int, int, int, int) + * @see #init(AudioFormat, float, int, int, int) + */ + public final boolean init(final int alChannelLayout, final int alSampleType, final int alFormat, + final int sampleRate, final int sampleSize, + final float frameDuration, final int initialQueueSize, final int queueGrowAmount, final int queueLimit) { + final AudioFormat requestedFormat = ALHelpers.getAudioFormat(alChannelLayout, alSampleType, alFormat, sampleRate, sampleSize); + if( null == requestedFormat ) { + if( DEBUG ) { + System.err.println(getThreadName()+": ALAudioSink.init2: Invalid AL channelLayout "+toHexString(alChannelLayout)+ + ", sampleType "+toHexString(alSampleType)+", format "+toHexString(alFormat)+" or sample[rate "+sampleRate+", size "+sampleSize+"]; "+toString()); } + return false; + } + return initImpl(requestedFormat, alChannelLayout, alSampleType, alFormat, + frameDuration, initialQueueSize, queueGrowAmount, queueLimit); + } + + private final boolean initImpl(final AudioFormat requestedFormat, + final int alChannelLayout, final int alSampleType, final int alFormat, + final float frameDuration, final int initialQueueSize, final int queueGrowAmount, final int queueLimit) { + this.alChannelLayout = alChannelLayout; + this.alSampleType = alSampleType; + this.alFormat = alFormat; + + lockContext(); + try { // Allocate buffers destroyBuffers(); { @@ -628,7 +709,7 @@ public class ALAudioSink implements AudioSink { t.printStackTrace(); } } - alSource = null; + alSource[0] = -1; } destroyBuffers(); diff --git a/src/java/com/jogamp/openal/util/ALHelpers.java b/src/java/com/jogamp/openal/util/ALHelpers.java index a666049..65869cc 100644 --- a/src/java/com/jogamp/openal/util/ALHelpers.java +++ b/src/java/com/jogamp/openal/util/ALHelpers.java @@ -1,7 +1,8 @@ -/* +/** * OpenAL Helpers * * Copyright (c) 2011 by Chris Robinson <[email protected]> + * Copyright (c) 2013-2023 JogAmp Community * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -21,53 +22,201 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - -/* This file contains routines to help with some menial OpenAL-related tasks, - * such as opening a device and setting up a context, closing the device and - * destroying its context, converting between frame counts and byte lengths, - * finding an appropriate buffer format, and getting readable strings for - * channel configs and sample types. */ package com.jogamp.openal.util; import com.jogamp.openal.AL; import com.jogamp.openal.ALConstants; import static com.jogamp.openal.ALConstants.*; import com.jogamp.openal.ALExt; -// import com.jogamp.openal.ALExtConstants; import static com.jogamp.openal.ALExtConstants.*; +import com.jogamp.common.av.AudioFormat; -/* This file contains routines to help with some menial OpenAL-related tasks, - * such as converting between frame counts and byte lengths, - * finding an appropriate buffer format, and getting readable strings for + +/* This class contains routines to help with some menial OpenAL-related tasks, + * such as finding an audio format and getting readable strings for * channel configs and sample types. */ public class ALHelpers { + /** + * [openal-soft >= 1.18.0](https://github.com/kcat/openal-soft/blob/master/ChangeLog) + * - Removed support for the AL_SOFT_buffer_samples and AL_SOFT_buffer_sub_data + * extensions. Due to conflicts with AL_EXT_SOURCE_RADIUS. + */ + public static final String AL_SOFT_buffer_samples = "AL_SOFT_buffer_samples"; + public static final String AL_EXT_MCFORMATS = "AL_EXT_MCFORMATS"; + public static final String AL_EXT_FLOAT32 = "AL_EXT_FLOAT32"; + public static final String AL_EXT_DOUBLE = "AL_EXT_DOUBLE"; + + public static final String ALC_EXT_thread_local_context = "ALC_EXT_thread_local_context"; + + /** + * Returns a compatible {@link AudioFormat} based on given OpenAL channel-layout, sample-type and format, + * as well as the generic sample-rate and sample-size. + * <p> + * The resulting {@link AudioFormat} uses {@link AudioFormat#planar} = false and {@link AudioFormat#littleEndian} = true. + * </p> + * @param alChannelLayout OpenAL channel layout + * @param alSampleType OpenAL sample type + * @param alFormat OpenAL format + * @param sampleRate sample rate, e.g. 44100 + * @param sampleSize sample size in bits, e.g. 16 + * @return a new {@link AudioFormat} instance or null if parameter are not conclusive or invalid. + */ + public static AudioFormat getAudioFormat(final int alChannelLayout, final int alSampleType, final int alFormat, + final int sampleRate, final int sampleSize) { + if( ALConstants.AL_NONE == alChannelLayout || ALConstants.AL_NONE == alSampleType || + ALConstants.AL_NONE == alFormat || 0 == sampleRate || 0 == sampleSize ) { + return null; + } + final int channelCount = getALChannelLayoutChannelCount(alChannelLayout); + if( 0 == channelCount ) { + return null; + } + final boolean signed = isALSampleTypeSigned(alSampleType); + final boolean fixedP = isALSampleTypeFixed(alSampleType); + return new AudioFormat(sampleRate, sampleSize, channelCount, signed, fixedP, + false /* planar */, true /* littleEndian */); + } /** - * Returns a compatible AL buffer format given the AL channel layout and - * AL sample type. If <code>hasSOFTBufferSamples</code> is true, + * Returns a compatible AL buffer format given the {@link AudioFormat}, + * which determines the AL channel layout and AL sample type. + * </p> + * <p> + * If <code>hasEXTMcFormats</code> or <code>hasSOFTBufferSamples</code> is true, * it will be called to find the closest-matching format from - * <code>AL_SOFT_buffer_samples</code>. + * <code>AL_EXT_MCFORMATS</code> or <code>AL_SOFT_buffer_samples</code>. + * </p> * <p> * Returns {@link ALConstants#AL_NONE} if no supported format can be found. * </p> + * <p> + * Function uses {@link AL#alIsExtensionPresent(String)}, which might be context dependent, + * otherwise function is context independent. + * </p> * - * @param alChannelLayout AL channel layout, see {@link #getDefaultALChannelLayout(int)} - * @param alSampleType AL sample type, see {@link #getALSampleType(int, boolean, boolean)}. + * @param audioFormat used to derive AL channel layout {@link #getDefaultALChannelLayout(int)} + * and AL sample type {@link #getALSampleType(int, boolean, boolean)} + * @param al AL instance + * @param alExt ALExt instance + * @return AL buffer format + */ + public static int getALFormat(final AudioFormat audioFormat, + final AL al, final ALExt alExt) { + final int alChannelLayout = ALHelpers.getDefaultALChannelLayout(audioFormat.channelCount); + final int alSampleType = ALHelpers.getALSampleType(audioFormat.sampleSize, audioFormat.signed, audioFormat.fixedP); + if( ALConstants.AL_NONE != alChannelLayout && ALConstants.AL_NONE != alSampleType ) { + return ALHelpers.getALFormat(alChannelLayout, alSampleType, al, alExt); + } else { + return ALConstants.AL_NONE; + } + } + + /** + * Returns a compatible AL buffer format given the {@link AudioFormat}, + * which determines the AL channel layout and AL sample type. + * </p> + * <p> + * If <code>hasEXTMcFormats</code> or <code>hasSOFTBufferSamples</code> is true, + * it will be called to find the closest-matching format from + * <code>AL_EXT_MCFORMATS</code> or <code>AL_SOFT_buffer_samples</code>. + * </p> + * <p> + * Returns {@link ALConstants#AL_NONE} if no supported format can be found. + * </p> + * <p> + * Function is context independent. + * </p> + * + * @param audioFormat used to derive AL channel layout {@link #getDefaultALChannelLayout(int)} + * and AL sample type {@link #getALSampleType(int, boolean, boolean)} + * @param al AL instance + * @param alExt ALExt instance * @param hasSOFTBufferSamples true if having extension <code>AL_SOFT_buffer_samples</code>, otherwise false * @param hasEXTMcFormats true if having extension <code>AL_EXT_MCFORMATS</code>, otherwise false * @param hasEXTFloat32 true if having extension <code>AL_EXT_FLOAT32</code>, otherwise false * @param hasEXTDouble true if having extension <code>AL_EXT_DOUBLE</code>, otherwise false + * @return AL buffer format + */ + public static int getALFormat(final AudioFormat audioFormat, + final AL al, final ALExt alExt, + final boolean hasSOFTBufferSamples, + final boolean hasEXTMcFormats, + final boolean hasEXTFloat32, final boolean hasEXTDouble) { + final int alChannelLayout = ALHelpers.getDefaultALChannelLayout(audioFormat.channelCount); + final int alSampleType = ALHelpers.getALSampleType(audioFormat.sampleSize, audioFormat.signed, audioFormat.fixedP); + final int alFormat; + if( ALConstants.AL_NONE != alChannelLayout && ALConstants.AL_NONE != alSampleType ) { + alFormat = ALHelpers.getALFormat(alChannelLayout, alSampleType, al, alExt, + hasSOFTBufferSamples, hasEXTMcFormats, + hasEXTFloat32, hasEXTDouble); + } else { + alFormat = ALConstants.AL_NONE; + } + return alFormat; + } + + /** + * Returns a compatible AL buffer format given the AL channel layout and AL sample type. + * <p> + * If <code>hasEXTMcFormats</code> or <code>hasSOFTBufferSamples</code> is true, + * it will be called to find the closest-matching format from + * <code>AL_EXT_MCFORMATS</code> or <code>AL_SOFT_buffer_samples</code>. + * </p> + * <p> + * Returns {@link ALConstants#AL_NONE} if no supported format can be found. + * </p> + * <p> + * Function uses {@link AL#alIsExtensionPresent(String)}, which might be context dependent, + * otherwise function is context independent. + * </p> + * + * @param alChannelLayout AL channel layout, see {@link #getDefaultALChannelLayout(int)} + * @param alSampleType AL sample type, see {@link #getALSampleType(int, boolean, boolean)}. * @param al AL instance * @param alExt ALExt instance * @return AL buffer format */ public static final int getALFormat(final int alChannelLayout, final int alSampleType, + final AL al, final ALExt alExt) { + final boolean hasSOFTBufferSamples = al.alIsExtensionPresent(AL_SOFT_buffer_samples); + final boolean hasEXTMcFormats = al.alIsExtensionPresent(AL_EXT_MCFORMATS); + final boolean hasEXTFloat32 = al.alIsExtensionPresent(AL_EXT_FLOAT32); + final boolean hasEXTDouble = al.alIsExtensionPresent(AL_EXT_DOUBLE); + return ALHelpers.getALFormat(alChannelLayout, alSampleType, al, alExt, + hasSOFTBufferSamples, hasEXTMcFormats, + hasEXTFloat32, hasEXTDouble); + } + + /** + * Returns a compatible AL buffer format given the AL channel layout and AL sample type. + * <p> + * If <code>hasEXTMcFormats</code> or <code>hasSOFTBufferSamples</code> is true, + * it will be called to find the closest-matching format from + * <code>AL_EXT_MCFORMATS</code> or <code>AL_SOFT_buffer_samples</code>. + * </p> + * <p> + * Returns {@link ALConstants#AL_NONE} if no supported format can be found. + * </p> + * <p> + * Function is context independent. + * </p> + * + * @param alChannelLayout AL channel layout, see {@link #getDefaultALChannelLayout(int)} + * @param alSampleType AL sample type, see {@link #getALSampleType(int, boolean, boolean)}. + * @param al AL instance + * @param alExt ALExt instance + * @param hasSOFTBufferSamples true if having extension <code>AL_SOFT_buffer_samples</code>, otherwise false + * @param hasEXTMcFormats true if having extension <code>AL_EXT_MCFORMATS</code>, otherwise false + * @param hasEXTFloat32 true if having extension <code>AL_EXT_FLOAT32</code>, otherwise false + * @param hasEXTDouble true if having extension <code>AL_EXT_DOUBLE</code>, otherwise false + * @return AL buffer format + */ + public static final int getALFormat(final int alChannelLayout, final int alSampleType, + final AL al, final ALExt alExt, final boolean hasSOFTBufferSamples, final boolean hasEXTMcFormats, - final boolean hasEXTFloat32, - final boolean hasEXTDouble, - final AL al, final ALExt alExt) { + final boolean hasEXTFloat32, final boolean hasEXTDouble) { int format = AL_NONE; /* If using AL_SOFT_buffer_samples, try looking through its formats */ @@ -262,6 +411,22 @@ public class ALHelpers { } /** + * Returns the channel count of the given AL channel layout + */ + public static final int getALChannelLayoutChannelCount(final int alChannelLayout) { + switch(alChannelLayout) { + case AL_MONO_SOFT: return 1; + case AL_STEREO_SOFT: return 2; + case AL_REAR_SOFT: return 2; + case AL_QUAD_SOFT: return 4; + case AL_5POINT1_SOFT: return 6; + case AL_6POINT1_SOFT: return 7; + case AL_7POINT1_SOFT: return 8; + } + return 0; + } + + /** * Returns the AL sample type matching the given audio type attributes, or {@link ALConstants#AL_NONE}. * @param sampleSize sample size in bits * @param signed true if signed number, false for unsigned @@ -311,6 +476,45 @@ public class ALHelpers { } /** + * Returns whether the given AL sample type is signed + */ + public static final boolean isALSampleTypeSigned(final int alSampleType) { + switch(alSampleType) { + case AL_BYTE_SOFT: + case AL_SHORT_SOFT: + case AL_INT_SOFT: + case AL_FLOAT_SOFT: + case AL_DOUBLE_SOFT: + return true; + case AL_UNSIGNED_BYTE_SOFT: + case AL_UNSIGNED_SHORT_SOFT: + case AL_UNSIGNED_INT_SOFT: + default: + return false; + } + } + + /** + * Returns true if the given AL sample type is a fixed point (byte, short, int, ..) + * or false if a floating point type (float, double). + */ + public static final boolean isALSampleTypeFixed(final int alSampleType) { + switch(alSampleType) { + case AL_BYTE_SOFT: + case AL_SHORT_SOFT: + case AL_INT_SOFT: + case AL_UNSIGNED_BYTE_SOFT: + case AL_UNSIGNED_SHORT_SOFT: + case AL_UNSIGNED_INT_SOFT: + return true; + case AL_FLOAT_SOFT: + case AL_DOUBLE_SOFT: + default: + return false; + } + } + + /** * Returns the byte size of the given AL sample type * @throws IllegalArgumentException for unknown <code>alChannelLayout</code> or <code>alSampleType</code> values. */ |