Package net.paulhertz.pixelaudio.sampler
Class PASamplerInstrumentPool
java.lang.Object
net.paulhertz.pixelaudio.sampler.PASamplerInstrumentPool
- All Implemented Interfaces:
PAPlayable,PASamplerPlayable
Manages a group of PASamplerInstruments sharing the same source buffer
and audio output. Provides polyphony management, buffer swapping, and
per-instrument voice recycling.
Features:
- Sample-based instruments with ADSR envelopes
- Configurable pool size (number of instruments)
- Configurable max voices per instrument
- Safe buffer and sample rate propagation
- Voice stealing via release or stop()
- Looping detection
- Thread-safe access
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ddf.minim.MultiChannelBufferShared source buffer for pooled instruments.private floatSample rate of the source from which the buffer was loaded.private ADSRParamsDefault ADSR envelope passed to pooled instruments.private floatPool-wide stereo pan offset, from -1.0 left to 1.0 right.private floatPool-wide pitch multiplier.private booleanprivate intMaximum voices allocated per instrument.private PASharedBufferSampler.MixProfilePool-wide sampler mix profile propagated to pooled instruments.private final ddf.minim.AudioOutputAudio output shared by all instruments in this pool.private intOutput buffer size in frames per audio callback.private floatSample rate of the AudioOutput.private final List<PASamplerInstrument>Instruments managed by this pool.private floatPool-wide linear gain scalar.private intRequested number of instruments in the pool. -
Constructor Summary
ConstructorsConstructorDescriptionPASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, float sampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams defaultEnv) Full backward-compatible constructor with a MultiChannelBuffer argument.PASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, int poolSize) Convenience constructor: default env, bufferSampleRate = out.sampleRate().PASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, int poolSize, float bufferSampleRate, ADSRParams env) Full constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate static floatclampPan(float pan) voidclose()Close all instruments and free audio resources.Convenience: advance to the next mix profile and apply it pool-wide.voidSmoothly release all active voices on all instruments.private PASamplerInstrumentFind a free instrument; if no free instrument find least-busy; otherwise smooth-steal one.floatfloatgetGain()floatfloatfloatintintfloatintprivate voidinitPool()booleanisClosed()Check whether the pool has been closed.booleanintplay(float amplitude, float pitch, float pan) The most basic play command, plays the entire buffer.intplay(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) The primary play command, with all common arguments in standard order.intplaySample(int samplePos, int sampleLen, float amplitude) Play subrange with amplitude only.intplaySample(int samplePos, int sampleLen, float amplitude, float pitch) Play subrange with amplitude and pitch (no envelope).intplaySample(int samplePos, int sampleLen, float amplitude, float pitch, float pan) Plays a sample range with pitch and pan.intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env) intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) Plays a sample range with envelope and pitch.intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Play a subrange of the buffer with full parameters.intplaySample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) Plays a range from a temporary buffer.intplaySample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Plays a range from a temporary buffer.private voidprivate voidResize the pool gracefully without disrupting existing instruments unnecessarily.voidAlias for fadeOutAll(), if you prefer the same naming style as instruments.intCounts all active or releasing sampler voices across the instrument pool.voidschedulePlayAtFrame(long frameIndex, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Hook: schedule by audio-frame index (frame = one AudioOutput callback).voidschedulePlayAtMillis(long triggerTimeMillis, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Hook: schedule by future wallclock milliseconds; will be wired to TimedLocation later.voidsetBuffer(float[] newBuffer, float newBufferSampleRate) Propagate a new float[] buffer to all instruments in this pool.voidsetBuffer(ddf.minim.MultiChannelBuffer newBuffer) Swap the pool's shared buffer; keeps existing bufferSampleRate.voidsetBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newBufferSampleRate) Swap the pool's shared buffer and update its buffer sample rate.voidsetDefaultEnv(ADSRParams env) Sets the default ADSR envelope for pooled instruments.voidsetGain(float linear) Sets pool output gain.voidsetGainDb(float db) Sets pool output gain in decibels.voidsetGlobalPan(float pan) Sets global pan used by overloads without explicit pan.voidsetGlobalPitch(float pitch) Sets global pitch multiplier applied to subsequent playback.voidsetMaxVoices(int maxVoices) Sets maximum voices per instrument and reapplies pool configuration.voidSet the sampler bus mix behavior for every instrument in the pool.voidsetPoolSize(int newSize) Sets the number of instruments in the pool.voidstop()Stop playback immediately (implementation-defined).voidstopAll()Stops playback on all pooled instruments.voidRe-sync instruments to current AudioOutput sample rate (if output device changes).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.paulhertz.pixelaudio.sampler.PAPlayable
play, play, play, play, playMethods inherited from interface net.paulhertz.pixelaudio.sampler.PASamplerPlayable
playSample, playSample, playSample, playSample, playSample
-
Field Details
-
out
private final ddf.minim.AudioOutput outAudio output shared by all instruments in this pool. -
buffer
private ddf.minim.MultiChannelBuffer bufferShared source buffer for pooled instruments. -
pool
Instruments managed by this pool. -
poolSize
private int poolSizeRequested number of instruments in the pool. -
maxVoices
private int maxVoicesMaximum voices allocated per instrument. -
defaultEnv
Default ADSR envelope passed to pooled instruments. -
globalPitch
private float globalPitchPool-wide pitch multiplier. -
globalPan
private float globalPanPool-wide stereo pan offset, from -1.0 left to 1.0 right. -
outputBufferSize
private int outputBufferSizeOutput buffer size in frames per audio callback. -
bufferSampleRate
private float bufferSampleRateSample rate of the source from which the buffer was loaded. -
outputSampleRate
private float outputSampleRateSample rate of the AudioOutput. -
poolGain
private volatile float poolGainPool-wide linear gain scalar. -
mixProfile
Pool-wide sampler mix profile propagated to pooled instruments. -
isClosed
private boolean isClosed
-
-
Constructor Details
-
PASamplerInstrumentPool
public PASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, float sampleRate, int poolSize, int perInstrumentVoices, ddf.minim.AudioOutput out, ADSRParams defaultEnv) Full backward-compatible constructor with a MultiChannelBuffer argument. Envelope is supplied, pitch and pan default to globalPitch and globalPan. Note that the MultiChannelBuffer we supply is not copied, we use it directly for storage. Calling applications should not modify the buffer.- Parameters:
buffer- shared MultiChannelBuffersampleRate- nominal sample rate of the buffer (Hz)poolSize- number of instruments in the poolperInstrumentVoices- number of voices per instrumentout- AudioOutput to patch instruments intodefaultEnv- default ADSR envelope for all instruments
-
PASamplerInstrumentPool
public PASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, int poolSize, float bufferSampleRate, ADSRParams env) Full constructor.- Parameters:
buffer- shared MultiChannelBufferout- target AudioOutputpoolSize- number of instruments to preallocatebufferSampleRate- nominal sample rate of buffer (Hz)env- default ADSR (nullable)
-
PASamplerInstrumentPool
public PASamplerInstrumentPool(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, int poolSize) Convenience constructor: default env, bufferSampleRate = out.sampleRate().- Parameters:
buffer- shared MultiChannelBufferout- target AudioOutputpoolSize- number of instruments to preallocate
-
-
Method Details
-
initPool
private void initPool() -
getAvailableInstrument
Find a free instrument; if no free instrument find least-busy; otherwise smooth-steal one. -
play
public int play(float amplitude, float pitch, float pan) The most basic play command, plays the entire buffer.- Specified by:
playin interfacePAPlayable- Parameters:
amplitude- gain multiplier (0..1+)pitch- playback rate or pitch factor (implementation-defined)pan- stereo pan (-1 left .. +1 right)- Returns:
- non-zero if a voice/event was triggered
-
stop
public void stop()Description copied from interface:PAPlayableStop playback immediately (implementation-defined).- Specified by:
stopin interfacePAPlayable
-
play
public int play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) The primary play command, with all common arguments in standard order. The playSample(...) methods provide the greatest flexibility in method signatures and will call this method.- Specified by:
playin interfacePASamplerPlayable- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplierpan- stereo pan- Returns:
- actual event duration in samples
-
isLooping
public boolean isLooping()- Returns:
- true if any pooled instrument has a looping sampler
-
samplerActiveVoiceCount
public int samplerActiveVoiceCount()Counts all active or releasing sampler voices across the instrument pool.- Returns:
- total active or releasing voice count
-
stopAll
public void stopAll()Stops playback on all pooled instruments. -
fadeOutAll
public void fadeOutAll()Smoothly release all active voices on all instruments. Preferred for live transitions and endings. -
releaseAll
public void releaseAll()Alias for fadeOutAll(), if you prefer the same naming style as instruments. -
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Play a subrange of the buffer with full parameters.- Specified by:
playSamplein interfacePASamplerPlayable- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplierpan- stereo pan- Returns:
- actual event duration in samples
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, float pitch, float pan) Plays a sample range with pitch and pan.- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierpitch- pitch or playback-rate multiplierpan- stereo pan- Returns:
- actual event duration in samples
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude) Play subrange with amplitude only.- Specified by:
playSamplein interfacePASamplerPlayable- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplier- Returns:
- actual event duration in samples
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, float pitch) Play subrange with amplitude and pitch (no envelope).- Specified by:
playSamplein interfacePASamplerPlayable- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierpitch- pitch or playback-rate multiplier- Returns:
- actual event duration in samples
-
playSample
-
playSample
Plays a sample range with envelope and pitch.- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplier- Returns:
- actual event duration in samples
-
playSample
public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Plays a range from a temporary buffer.- Parameters:
buffer- source buffer for playbacksamplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplierpan- stereo pan- Returns:
- actual event duration in samples
-
playSample
public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) Plays a range from a temporary buffer.- Parameters:
buffer- source buffer for playbacksamplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplier- Returns:
- actual event duration in samples
-
setBuffer
public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer) Swap the pool's shared buffer; keeps existing bufferSampleRate.- Parameters:
newBuffer- a new MultiChannelBuffer as audio source for this instrument
-
setBuffer
public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newBufferSampleRate) Swap the pool's shared buffer and update its buffer sample rate.- Parameters:
newBuffer- a new MultiChannelBuffer as audio source for this instrumentnewBufferSampleRate- sample rate of the replacement buffer in Hz
-
setBuffer
public void setBuffer(float[] newBuffer, float newBufferSampleRate) Propagate a new float[] buffer to all instruments in this pool.- Parameters:
newBuffer- replacement mono source buffernewBufferSampleRate- sample rate of the replacement buffer in Hz
-
updateRateFromOutput
public void updateRateFromOutput()Re-sync instruments to current AudioOutput sample rate (if output device changes). -
setGain
public void setGain(float linear) Sets pool output gain.- Parameters:
linear- linear gain value
-
getGain
public float getGain()- Returns:
- pool output gain as a linear value
-
setGainDb
public void setGainDb(float db) Sets pool output gain in decibels.- Parameters:
db- gain in decibels
-
getGainDb
public float getGainDb()- Returns:
- pool output gain in decibels
-
propagateParentGain
private void propagateParentGain() -
getMixProfile
- Returns:
- the pool-wide sampler mix profile
-
cycleMixProfile
Convenience: advance to the next mix profile and apply it pool-wide.- Returns:
- the newly selected profile
-
setGlobalPitch
public void setGlobalPitch(float pitch) Sets global pitch multiplier applied to subsequent playback.- Parameters:
pitch- pitch multiplier
-
getGlobalPitch
public float getGlobalPitch()- Returns:
- global pitch multiplier
-
setGlobalPan
public void setGlobalPan(float pan) Sets global pan used by overloads without explicit pan.- Parameters:
pan- stereo pan position
-
getGlobalPan
public float getGlobalPan()- Returns:
- global pan position
-
setDefaultEnv
Sets the default ADSR envelope for pooled instruments.- Parameters:
env- default ADSR envelope
-
getDefaultEnv
- Returns:
- default ADSR envelope
-
getMaxVoices
public int getMaxVoices()- Returns:
- maximum voices per instrument
-
setMaxVoices
public void setMaxVoices(int maxVoices) Sets maximum voices per instrument and reapplies pool configuration.- Parameters:
maxVoices- maximum voices per instrument
-
getPoolSize
public int getPoolSize()- Returns:
- number of instruments in the pool
-
setPoolSize
public void setPoolSize(int newSize) Sets the number of instruments in the pool.- Parameters:
newSize- desired pool size
-
getOutputSampleRate
public float getOutputSampleRate()- Returns:
- output sample rate in Hz
-
getOutputBufferSize
public int getOutputBufferSize()- Returns:
- output buffer size in samples
-
getBufferSampleRate
public float getBufferSampleRate()- Returns:
- source buffer sample rate in Hz
-
reinitInstruments
private void reinitInstruments()Resize the pool gracefully without disrupting existing instruments unnecessarily. Active instruments are preserved whenever possible. -
getInstruments
- Returns:
- mutable list of pooled instruments
-
schedulePlayAtMillis
public void schedulePlayAtMillis(long triggerTimeMillis, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Hook: schedule by future wallclock milliseconds; will be wired to TimedLocation later.- Parameters:
triggerTimeMillis- future trigger time in millisecondssamplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplierpan- stereo pan
-
schedulePlayAtFrame
public void schedulePlayAtFrame(long frameIndex, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Hook: schedule by audio-frame index (frame = one AudioOutput callback).- Parameters:
frameIndex- future audio callback frame indexsamplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelopepitch- pitch or playback-rate multiplierpan- stereo pan
-
clampPan
private static float clampPan(float pan) -
close
public void close()Close all instruments and free audio resources. -
isClosed
public boolean isClosed()Check whether the pool has been closed.- Returns:
- true after the pool has been closed
-