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 intAn immutable number of PCM frames held in array buffers, set during instantiation.final intAn immutable number describing the size of an internal array buffer, set during instantiation.final intAn immutable number describing the size of an internal array buffer, set during instantiation.final intA 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 TypeMethodDescriptionvoidaddTrack(AudioMixerTrack track) Designates anAudioMixerTrackto be staged for addition into the collection of tracks actively being mixed.voidclose()intReturns the number of tracks being mixed.voidremoveTrack(AudioMixerTrack track) Designates anAudioMixerTrackto be staged for removal from the collection of tracks actively being mixed.voidstart()Starts the operation of theAudioMixer.voidstop()Sets a flag that will signal theAudioMixerto stop media writes and release resources.voidSignals the internal media writer to load an updatedAudiomixerTrackcollection 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. ThereadBufferSizehas two PCM values per each frame being handled, corresponding to the left and right stereo channels, and is calculated by multiplyingbufferFramesby 2. -
sdlByteBufferSize
public final int sdlByteBufferSizeAn immutable number describing the size of an internal array buffer, set during instantiation. ThesdlBufferSizehas four bytes per frame, as each of the two PCM values per frame is encoded into two constituent bytes. ThesdlByteBufferSizeis calculated by multiplyingbufferFramesby 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_PRIORITYtojava.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 singlewhileloop 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 singlewhileloop 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 anAudioMixerTrackto be staged for addition into the collection of tracks actively being mixed. If theAudioMixeris running, actual addition occurs when theupdateTracksmethod is executed. If theAudioMixeris not running, the addition will occur automatically when thestartmethod is called.- Parameters:
track- - anAudioMixerTrackto be added to the mix- See Also:
-
removeTrack
Designates anAudioMixerTrackto be staged for removal from the collection of tracks actively being mixed. If theAudioMixeris running, actual removal occurs when theupdateTracksmethod is executed. If theAudioMixeris not running, the removal will occur automatically when thestartmethod is called.- Parameters:
track- - anAudioMixerTrackto be removed from the mix- See Also:
-
updateTracks
public void updateTracks()Signals the internal media writer to load an updatedAudiomixerTrackcollection at the next opportunity. Tracks to be added or removed are first staged using the methodsaddTrackandremoveTrack. -
start
Starts the operation of theAudioMixer. A runningAudioMixeriteratively sums a buffer's worth of frames of sound data from a collection ofAudioMixerTracks, and writes the resulting array to aSourceDataLine.- Throws:
IllegalStateException- is thrown if theAudioMixeris already running.LineUnavailableException- is thrown if there is a problem securing aSourceDataLine
-
stop
public void stop()Sets a flag that will signal theAudioMixerto stop media writes and release resources.- Throws:
IllegalStateException- if theAudioMixeris already in a stopped state.
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-