Class AudioMixer
- All Implemented Interfaces:
AutoCloseable
AudioMixer
mixes the media content of all the members
of a AudioMixerTrack
collection into a single output
line. Classes implementing AudioMixerTrack
can be added
and removed from the mix asynchronously, with the operation
occurring at the next iteration of the read buffer. Unlike a
mixer used in sound studios, the AudioMixer
does not
provide functions such as panning or volume controls.
An SourceDataLine
can be in one of two states: (1) running,
or (2) not running. When running, audio data is read from the
constituent tracks, mixed and written as a single stream using a
javax.sound.sampled.SourceDataLine
. The mixer imposes a
simple floor/ceiling of -1, 1, to guard against volume overflows.
When not running, the SourceDataLine
is allowed to drain
and close. A new SourceDataLine
is instantiated if/when
this AudioMixer
is reopened.
Values used to configure the media output are provided in the
constructor, and are held as immutable. These include a
javax.sound.sampled.Mixer
used to provide the
SourceDataLine
, the size of the buffer used for iterative
reads of the PCM data, and the thread priority. Multiple constructors
are provided to facilitate the use of default values. These
configuration values override those associated with the constituent
tracks.
- Since:
- 2.0.0
- Version:
- 2.0.0
- Author:
- Philip Freihofner
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int
An immutable number of PCM frames held in array buffers, set during instantiation.final int
An immutable number describing the size of an internal array buffer, set during instantiation.final int
An immutable number describing the size of an internal array buffer, set during instantiation.final int
A value that holds the priority level of the thread that that handles the media output, set upon instantiation of the class. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor forAudioMixer
, using default settings: Mixer = system default Buffer size = 8192 frames Thread priority = 10.AudioMixer
(Mixer mixer, int bufferFrames, int threadPriority) Constructor forAudioMixer
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addTrack
(AudioMixerTrack track) Designates anAudioMixerTrack
to be staged for addition into the collection of tracks actively being mixed.void
close()
int
Returns the number of tracks being mixed.void
removeTrack
(AudioMixerTrack track) Designates anAudioMixerTrack
to be staged for removal from the collection of tracks actively being mixed.void
start()
Starts the operation of theAudioMixer
.void
stop()
Sets a flag that will signal theAudioMixer
to stop media writes and release resources.void
Signals the internal media writer to load an updatedAudiomixerTrack
collection at the next opportunity.
-
Field Details
-
bufferFrames
public final int bufferFramesAn immutable number of PCM frames held in array buffers, set during instantiation. -
readBufferSize
public final int readBufferSizeAn immutable number describing the size of an internal array buffer, set during instantiation. ThereadBufferSize
has two PCM values per each frame being handled, corresponding to the left and right stereo channels, and is calculated by multiplyingbufferFrames
by 2. -
sdlByteBufferSize
public final int sdlByteBufferSizeAn immutable number describing the size of an internal array buffer, set during instantiation. ThesdlBufferSize
has four bytes per frame, as each of the two PCM values per frame is encoded into two constituent bytes. ThesdlByteBufferSize
is calculated by multiplyingbufferFrames
by 4. -
threadPriority
public final int threadPriorityA value that holds the priority level of the thread that that handles the media output, set upon instantiation of the class. The value is clamped to the rangejava.lang.Thread.MIN_PRIORITY
tojava.lang.Thread.MAX_PRIORITY
.
-
-
Constructor Details
-
AudioMixer
public AudioMixer()Constructor forAudioMixer
, using default settings: Mixer = system default Buffer size = 8192 frames Thread priority = 10. The buffer size pertains to the frames collected in a singlewhile
loop iteration. A buffer that corresponds to the same number of frames converted to bytes is assigned to theSourceDataLine
. -
AudioMixer
Constructor forAudioMixer
. The buffer size is the number of frames collected in a singlewhile
loop iteration. A buffer with a corresponding frame count, calculated in bytes, is assigned to theSourceDataLine
. A thread priority of 10 is recommended, in order to help prevent sound drop outs. Note that a well designed and properly running sound thread should spend the vast majority of its time in a blocked state, and thus have a minimal impact in terms of usurping cpu cycles from other threads.- Parameters:
mixer
- javax.sound.sampled.Mixer to be usedbufferFrames
- int specifying the number of frames to process with each iterationthreadPriority
- int ranging from 1 to 10 specifying the priority of the sound thread
-
-
Method Details
-
getTracksCount
public int getTracksCount()Returns the number of tracks being mixed.- Returns:
- integer number of tracks being mixed.
-
addTrack
Designates anAudioMixerTrack
to be staged for addition into the collection of tracks actively being mixed. If theAudioMixer
is running, actual addition occurs when theupdateTracks
method is executed. If theAudioMixer
is not running, the addition will occur automatically when thestart
method is called.- Parameters:
track
- - anAudioMixerTrack
to be added to the mix- See Also:
-
removeTrack
Designates anAudioMixerTrack
to be staged for removal from the collection of tracks actively being mixed. If theAudioMixer
is running, actual removal occurs when theupdateTracks
method is executed. If theAudioMixer
is not running, the removal will occur automatically when thestart
method is called.- Parameters:
track
- - anAudioMixerTrack
to be removed from the mix- See Also:
-
updateTracks
public void updateTracks()Signals the internal media writer to load an updatedAudiomixerTrack
collection at the next opportunity. Tracks to be added or removed are first staged using the methodsaddTrack
andremoveTrack
. -
start
Starts the operation of theAudioMixer
. A runningAudioMixer
iteratively sums a buffer's worth of frames of sound data from a collection ofAudioMixerTrack
s, and writes the resulting array to aSourceDataLine
.- Throws:
IllegalStateException
- is thrown if theAudioMixer
is already running.LineUnavailableException
- is thrown if there is a problem securing aSourceDataLine
-
stop
public void stop()Sets a flag that will signal theAudioMixer
to stop media writes and release resources.- Throws:
IllegalStateException
- if theAudioMixer
is already in a stopped state.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-