Interface AudioSink
-
public interface AudioSink
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
AudioSink.AudioDataFrame
Audio data frame example ofAudioSink.AudioFrame
with actual audio data being attached.static class
AudioSink.AudioFrame
Abstract audio frame containing multiple audio samples per channel, trackingTimeFrameI
pts and size in bytes.
-
Field Summary
Fields Modifier and Type Field Description static boolean
DEBUG
static AudioFormat
DefaultFormat
DefaultAudioFormat
, [type PCM, sampleRate 44100, sampleSize 16, channelCount 2, signed, fixedP, !planar, littleEndian].static int
DefaultFrameDuration
Default frame duration in millisecond, i.e.static int
DefaultInitialQueueSize
Initial audio queue size in milliseconds.static int
DefaultQueueGrowAmount
Audio queue grow size in milliseconds.static int
DefaultQueueLimitAudioOnly
Audio queue limit w/o video in milliseconds.static int
DefaultQueueLimitWithVideo
Audio queue limit w/ video in milliseconds.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
destroy()
Destroys this instance, i.e.AudioSink.AudioFrame
enqueueData(int pts, ByteBuffer bytes, int byteCount)
EnqueuebyteCount
bytes as a newAudioSink.AudioFrame
to this sink.void
flush()
Flush all queued buffers, impliespause()
.float
getAvgFrameDuration()
Returns average frame duration last assessed @enqueueData(int, ByteBuffer, int)
when queue was full.AudioFormat
getChosenFormat()
Returns theAudioFormat
as chosen by#init(AudioFormat, float, int, int, int)
, i.e.float
getDefaultLatency()
Returns the default (minimum) latency in secondsint
getEnqueuedFrameCount()
Returns the current enqueued frames count since#init(AudioFormat, float, int, int, int)
.int
getFrameCount()
Returns the number of allocated buffers as requested by#init(AudioFormat, float, int, int, int)
.int
getFreeFrameCount()
Returns the current number of frames in the sink available for writing.float
getLatency()
Returns the (minimum) latency in seconds of this sink as set by#init(AudioFormat, float, int, int, int)
, seegetDefaultLatency()
.AudioFormat
getNativeFormat()
Returns the nativeAudioFormat
by this sink.float
getPlaySpeed()
Returns the playback speed.AudioFormat
getPreferredFormat()
Returns the preferredAudioFormat
by this sink.int
getPTS()
Return the current audio presentation timestamp (PTS) in milliseconds.int
getQueuedByteCount()
Returns the current number of bytes queued for playing.int
getQueuedFrameCount()
Returns the current number of frames queued for playing.float
getQueuedTime()
Returns the current queued frame time in seconds for playing.int
getSourceCount()
Returns the number of sources the used device is capable to mix.float
getVolume()
Returns the volume.boolean
init(AudioFormat requestedFormat, int frameDurationHint, int initialQueueSize, int queueGrowAmount, int queueLimit)
Initializes the sink.boolean
isAvailable()
Returns theavailable state
of this instance.boolean
isPlaying()
Returns true, ifplay()
has been requested and the sink is still playing, otherwise false.boolean
isSupported(AudioFormat format)
Returns true if the given format is supported by the sink, otherwise false.boolean
makeCurrent(boolean throwException)
Makes the audio context current on the calling thread, if implementation utilizes context locking.void
pause()
Pause playing buffers while keeping enqueued data incl.void
play()
Play buffers queued via#enqueueData(AudioFrame)
from current internal position.boolean
release(boolean throwException)
Releases control of this audio context from the current thread, if implementation utilizes context locking.void
setChannelLimit(int cc)
Limit maximum supported audio channels by user.boolean
setPlaySpeed(float s)
Sets the playback speed.boolean
setVolume(float v)
Sets the volume [0f..1f].
-
-
-
Field Detail
-
DEBUG
static final boolean DEBUG
-
DefaultFrameDuration
static final int DefaultFrameDuration
Default frame duration in millisecond, i.e. 1AudioSink.AudioFrame
per 32 ms.- See Also:
- Constant Field Values
-
DefaultInitialQueueSize
static final int DefaultInitialQueueSize
Initial audio queue size in milliseconds. 512 ms, i.e. 16AudioSink.AudioFrame
s per 32 ms. See#init(AudioFormat, float, int, int, int)
.- See Also:
- Constant Field Values
-
DefaultQueueGrowAmount
static final int DefaultQueueGrowAmount
Audio queue grow size in milliseconds. 512 ms, i.e. 16AudioSink.AudioFrame
s per 32 ms. See#init(AudioFormat, float, int, int, int)
.- See Also:
- Constant Field Values
-
DefaultQueueLimitWithVideo
static final int DefaultQueueLimitWithVideo
Audio queue limit w/ video in milliseconds. 3072 ms, i.e. 96AudioSink.AudioFrame
s per 32 ms. See#init(AudioFormat, float, int, int, int)
.- See Also:
- Constant Field Values
-
DefaultQueueLimitAudioOnly
static final int DefaultQueueLimitAudioOnly
Audio queue limit w/o video in milliseconds. 1024 ms, i.e. 32AudioSink.AudioFrame
s per 32 ms. See#init(AudioFormat, float, int, int, int)
.- See Also:
- Constant Field Values
-
DefaultFormat
static final AudioFormat DefaultFormat
DefaultAudioFormat
, [type PCM, sampleRate 44100, sampleSize 16, channelCount 2, signed, fixedP, !planar, littleEndian].
-
-
Method Detail
-
makeCurrent
boolean makeCurrent(boolean throwException)
Makes the audio context current on the calling thread, if implementation utilizes context locking.If implementation doesn't utilizes context locking, method always returns true.
Recursive call to
#makeCurrent()
and hence#release()
are supported.At any point in time one context can only be current by one thread, and one thread can only have one context current.
- Parameters:
throwException
- if true, throws ALException if context is null, current thread holds another context or failed to natively make current- Returns:
- true if current thread holds no other context and context successfully made current, otherwise false
- See Also:
#release()
-
release
boolean release(boolean throwException)
Releases control of this audio context from the current thread, if implementation utilizes context locking.If implementation doesn't utilizes context locking, method always returns true.
Recursive call to
#makeCurrent()
and hence#release()
are supported.- Parameters:
throwException
- if true, throws ALException if context has not been previously made current on current thread or native release failed.- Returns:
- true if context has previously been made current on the current thread and successfully released, otherwise false
- See Also:
#makeCurrent()
-
isAvailable
boolean isAvailable()
Returns theavailable state
of this instance.The
available state
is affected by this instance overall availability, i.e. after instantiation, as well as bydestroy()
.
-
getPlaySpeed
float getPlaySpeed()
Returns the playback speed.
-
setPlaySpeed
boolean setPlaySpeed(float s)
Sets the playback speed.To simplify test, play speed is normalized, i.e.
1.0f
: ifMath.abs(1.0f - rate) < 0.01f
- Returns:
- true if successful, otherwise false, i.e. due to unsupported value range of implementation.
-
getVolume
float getVolume()
Returns the volume.
-
setVolume
boolean setVolume(float v)
Sets the volume [0f..1f].To simplify test, volume is normalized, i.e.
0.0f
: ifMath.abs(v) < 0.01f
1.0f
: ifMath.abs(1.0f - v) < 0.01f
- Returns:
- true if successful, otherwise false, i.e. due to unsupported value range of implementation.
-
getSourceCount
int getSourceCount()
Returns the number of sources the used device is capable to mix.This device attribute is only formally exposed and not used, since an audio sink is only utilizing one source.
May return
-1
if undefined.- Returns:
-
getDefaultLatency
float getDefaultLatency()
Returns the default (minimum) latency in secondsLatency might be the reciprocal mixer-refresh-interval [Hz], e.g. 50 Hz refresh-rate = 20ms minimum latency.
May return 20ms for a 50 Hz refresh rate if undefined.
-
getNativeFormat
AudioFormat getNativeFormat()
Returns the nativeAudioFormat
by this sink.The native format is guaranteed to be supported and shall reflect this sinks most native format, i.e. best performance w/o data conversion.
The native format is not impacted by
setChannelLimit(int)
.May return
DefaultFormat
if undefined.- See Also:
#init(AudioFormat, float, int, int, int)
-
getPreferredFormat
AudioFormat getPreferredFormat()
Returns the preferredAudioFormat
by this sink.The preferred format is a subset of
getNativeFormat()
, impacted bysetChannelLimit(int)
.Known
#AudioFormat
attributes considered by implementations:- ALAudioSink:
AudioFormat.sampleRate
. - ALAudioSink:
AudioFormat.channelCount
- See Also:
getNativeFormat()
,#init(AudioFormat, float, int, int, int)
,setChannelLimit(int)
,isSupported(AudioFormat)
- ALAudioSink:
-
setChannelLimit
void setChannelLimit(int cc)
Limit maximum supported audio channels by user.Must be set before
getPreferredFormat()
,isSupported(AudioFormat)
and naturallyinit(AudioFormat, int, int, int, int)
.May be utilized to enforce 1 channel (mono) downsampling in combination with JOAL/OpenAL to experience spatial 3D position effects.
- Parameters:
cc
- maximum supported audio channels, will be clipped [1..getNativeFormat()
.channelCount
]- See Also:
getNativeFormat()
,getPreferredFormat()
,isSupported(AudioFormat)
,init(AudioFormat, int, int, int, int)
-
isSupported
boolean isSupported(AudioFormat format)
Returns true if the given format is supported by the sink, otherwise false.The
getPreferredFormat()
is used to validate compatibility with the given format.- See Also:
#init(AudioFormat, float, int, int, int)
,getPreferredFormat()
-
init
boolean init(AudioFormat requestedFormat, int frameDurationHint, int initialQueueSize, int queueGrowAmount, int queueLimit)
Initializes the sink.Implementation must match the given
requestedFormat
AudioFormat
.Caller shall validate
requestedFormat
viaisSupported(AudioFormat)
beforehand and try to find a suitable supported one.getPreferredFormat()
may help.- Parameters:
requestedFormat
- the requestedAudioFormat
.frameDurationHint
- averageAudioSink.AudioFrame
duration hint in milliseconds. May assist to shape theAudioSink.AudioFrame
initial queue size using `initialQueueSize`. May assist to adjust latency of the backend, as currently used for JOAL's ALAudioSink. A value below 30ms orDefaultFrameDuration
may increase the audio processing load. Assumed asDefaultFrameDuration
, ifframeDuration < 1 ms
.initialQueueSize
- initial queue size in milliseconds, seeDefaultInitialQueueSize
. May use `frameDurationHint` to determine initialAudioSink.AudioFrame
queue size.queueGrowAmount
- queue grow size in milliseconds if queue is full, seeDefaultQueueGrowAmount
. May usegetAvgFrameDuration()
to determineAudioSink.AudioFrame
queue growth amount.queueLimit
- maximum time in milliseconds the queue can hold (and grow), seeDefaultQueueLimitWithVideo
andDefaultQueueLimitAudioOnly
. May usegetAvgFrameDuration()
to determineAudioSink.AudioFrame
queue limit.- Returns:
- true if successful, otherwise false
- See Also:
enqueueData(int, ByteBuffer, int)
,getAvgFrameDuration()
-
getChosenFormat
AudioFormat getChosenFormat()
Returns theAudioFormat
as chosen by#init(AudioFormat, float, int, int, int)
, i.e. it shall match the requestedFormat.
-
getLatency
float getLatency()
Returns the (minimum) latency in seconds of this sink as set by#init(AudioFormat, float, int, int, int)
, seegetDefaultLatency()
.Latency might be the reciprocal mixer-refresh-interval [Hz], e.g. 50 Hz refresh-rate = 20ms minimum latency.
- See Also:
#init(AudioFormat, float, int, int, int)
-
isPlaying
boolean isPlaying()
Returns true, ifplay()
has been requested and the sink is still playing, otherwise false.
-
play
void play()
Play buffers queued via#enqueueData(AudioFrame)
from current internal position. If no buffers are yet queued or the queue runs empty, playback is being continued when buffers are enqueued later on.- See Also:
#enqueueData(AudioFrame)
,pause()
-
pause
void pause()
Pause playing buffers while keeping enqueued data incl. it's internal position.
-
flush
void flush()
Flush all queued buffers, impliespause()
.#init(AudioFormat, float, int, int, int)
must be called first.
-
destroy
void destroy()
Destroys this instance, i.e. closes all streams and devices allocated.
-
getFrameCount
int getFrameCount()
Returns the number of allocated buffers as requested by#init(AudioFormat, float, int, int, int)
.- See Also:
#init(AudioFormat, float, int, int, int)
-
getEnqueuedFrameCount
int getEnqueuedFrameCount()
Returns the current enqueued frames count since#init(AudioFormat, float, int, int, int)
.- See Also:
#init(AudioFormat, float, int, int, int)
-
getQueuedFrameCount
int getQueuedFrameCount()
Returns the current number of frames queued for playing.#init(AudioFormat, float, int, int, int)
must be called first.- See Also:
#init(AudioFormat, float, int, int, int)
-
getQueuedByteCount
int getQueuedByteCount()
Returns the current number of bytes queued for playing.#init(AudioFormat, float, int, int, int)
must be called first.- See Also:
#init(AudioFormat, float, int, int, int)
-
getQueuedTime
float getQueuedTime()
Returns the current queued frame time in seconds for playing.#init(AudioFormat, float, int, int, int)
must be called first.- See Also:
#init(AudioFormat, float, int, int, int)
-
getAvgFrameDuration
float getAvgFrameDuration()
Returns average frame duration last assessed @enqueueData(int, ByteBuffer, int)
when queue was full.avgFrameDuration =
getQueuedTime()
/getQueuedFrameCount()
-
getPTS
int getPTS()
Return the current audio presentation timestamp (PTS) in milliseconds.
-
getFreeFrameCount
int getFreeFrameCount()
Returns the current number of frames in the sink available for writing.#init(AudioFormat, float, int, int, int)
must be called first.- See Also:
#init(AudioFormat, float, int, int, int)
-
enqueueData
AudioSink.AudioFrame enqueueData(int pts, ByteBuffer bytes, int byteCount)
EnqueuebyteCount
bytes as a newAudioSink.AudioFrame
to this sink.The data must comply with the chosen
AudioFormat
as set via#init(AudioFormat, float, int, int, int)
.#init(AudioFormat, float, int, int, int)
must be called first.- Parameters:
pts
- presentation time stamp in milliseconds for the newly enqueuedAudioSink.AudioFrame
bytes
- audio data for the newly enqueuedAudioSink.AudioFrame
- See Also:
#init(AudioFormat, float, int, int, int)
-
-