Package net.paulhertz.pixelaudio.voices
Class PASamplerInstrumentPool
java.lang.Object
net.paulhertz.pixelaudio.voices.PASamplerInstrumentPool
- All Implemented Interfaces:
PAPlayable,PASamplerPlayable
PASamplerInstrumentPool
Manages a group of PASamplerInstruments sharing the same source buffer
and audio output. Provides polyphony management, buffer swapping, and
per-instrument voice recycling.
-- TODO Uses a Minim MultiChannelBuffer for its audio sample storage. This could change.
-- TODO Duplicates the MultiChannelBuffer. This may be overkill.
Features:
- 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.MultiChannelBufferprivate floatprivate ADSRParamsprivate floatprivate floatprivate booleanprivate intprivate final ddf.minim.AudioOutputprivate intprivate floatprivate final List<PASamplerInstrument>private int -
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.private PASamplerInstrumentFind a free instrument; otherwise least-busy; otherwise smooth-steal one.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) intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env) intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float 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) intplaySample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) private voidResize the pool gracefully without disrupting existing instruments unnecessarily.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 ms — 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) voidsetGlobalPan(float pan) voidsetGlobalPitch(float pitch) voidsetMaxVoices(int maxVoices) voidsetPoolSize(int newSize) voidstop()Stop playback immediately (implementation-defined).voidstopAll()voidRe-sync instruments to current AudioOutput sample rate (if 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.voices.PAPlayable
play, play, play, play, playMethods inherited from interface net.paulhertz.pixelaudio.voices.PASamplerPlayable
playSample, playSample, playSample, playSample, playSample
-
Field Details
-
out
private final ddf.minim.AudioOutput out -
buffer
private ddf.minim.MultiChannelBuffer buffer -
pool
-
poolSize
private int poolSize -
maxVoices
private int maxVoices -
defaultEnv
-
globalPitch
private float globalPitch -
globalPan
private float globalPan -
outputBufferSize
private int outputBufferSize -
bufferSampleRate
private float bufferSampleRate -
outputSampleRate
private float outputSampleRate -
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().
-
-
Method Details
-
initPool
private void initPool() -
getAvailableInstrument
Find a free instrument; otherwise 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
-
isLooping
public boolean isLooping() -
stopAll
public void stopAll() -
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Description copied from interface:PASamplerPlayablePlay a subrange of the buffer with full parameters.- Specified by:
playSamplein interfacePASamplerPlayable
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, float pitch, float pan) -
playSample
public int playSample(int samplePos, int sampleLen, float amplitude) Description copied from interface:PASamplerPlayablePlay subrange with amplitude only.- Specified by:
playSamplein interfacePASamplerPlayable
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, float pitch) Description copied from interface:PASamplerPlayablePlay subrange with amplitude and pitch (no envelope).- Specified by:
playSamplein interfacePASamplerPlayable
-
playSample
-
playSample
-
playSample
public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) -
playSample
public int playSample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) -
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-newBufferSampleRate-
-
setBuffer
public void setBuffer(float[] newBuffer, float newBufferSampleRate) Propagate a new float[] buffer to all instruments in this pool. -
updateRateFromOutput
public void updateRateFromOutput()Re-sync instruments to current AudioOutput sample rate (if device changes). -
setGlobalPitch
public void setGlobalPitch(float pitch) -
getGlobalPitch
public float getGlobalPitch() -
setGlobalPan
public void setGlobalPan(float pan) -
getGlobalPan
public float getGlobalPan() -
setDefaultEnv
-
getDefaultEnv
-
getMaxVoices
public int getMaxVoices() -
setMaxVoices
public void setMaxVoices(int maxVoices) -
getPoolSize
public int getPoolSize() -
setPoolSize
public void setPoolSize(int newSize) -
getOutputSampleRate
public float getOutputSampleRate() -
getOutputBufferSize
public int getOutputBufferSize() -
getBufferSampleRate
public float getBufferSampleRate() -
reinitInstruments
private void reinitInstruments()Resize the pool gracefully without disrupting existing instruments unnecessarily. Active instruments are preserved whenever possible. -
getInstruments
-
schedulePlayAtMillis
public void schedulePlayAtMillis(long triggerTimeMillis, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Hook: schedule by (future) wallclock ms — will be wired to TimedLocation later. -
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). -
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.
-