Class PASamplerInstrument
- All Implemented Interfaces:
PAPlayable,PASamplerPlayable
TODO decide if MultiChannelBuffer or float[] is the appropriate structure for storing the buffer. float[] is preferred for the general style of PixelAudio, but MultiChannelBuffer support is a useful consideration for the future. DONE We are sticking with MultiChannelBuffer but may provide a future abstraction that works with float[] arrays and provide the desired sample format for each synth.
TODO sample accurate start for sampler events to unify timing API with PAGranularInstrument. Currently sampler play() is block-time accurate only. In the future, add sample-time scheduling (startAtSampleTime / startAfterDelaySamples) using AudioScheduler so sampler and granular share a transport model. This should go into a small demo sketch, SampleAccurateTriggers.
Supports:
- Global pitch scaling.
- Global stereo pan.
- Default ADSR envelope.
- Linear trim (gain) per instrument.
- Compensation for differing buffer vs. output sample rates.
- Cached buffer size for efficiency.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ddf.minim.MultiChannelBufferSource buffer for this instrument.private floatSample rate of the source from which the buffer was loaded.private intCached source buffer size in samples.private ADSRParamsDefault ADSR envelope for playback.private floatPer-instrument linear trim.private floatGlobal stereo pan offset, from -1.0 left to 1.0 right.private floatGlobal pitch multiplier.private booleanprivate intMaximum number of simultaneous voices for the sampler backend.private final ddf.minim.AudioOutputAudio output this instrument is patched to.private floatSample rate of the AudioOutput.private floatParent pool gain, set by PASamplerInstrumentPool.private final PASamplerSampler backend used to trigger playback.private floatBuffer-rate to output-rate ratio, used to correct playback speed. -
Constructor Summary
ConstructorsConstructorDescriptionPASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, float sampleRate, int maxVoices, ddf.minim.AudioOutput audioOut, ADSRParams env) Primary constructor for backward compatibility.PASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out) Convenience constructor assuming buffer and output share the same rate.PASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, PASampler sampler, ADSRParams defaultEnv, float bufferSampleRate) Full constructor with explicit buffer sample rate, custom sampler, and envelope. -
Method Summary
Modifier and TypeMethodDescriptionintCounts active or releasing voices.private static floatclampPan(float pan) voidclose()Stop all voices and disconnect UGens from the output.Convenience: cycle to the next available sampler mix profile.voidAlias for fadeToStop(), useful when calling from UI or pool code.voidSmoothly stop all active voices by triggering their envelope release.ddf.minim.AudioOutputddf.minim.MultiChannelBufferfloatintfloatgetGain()floatfloatfloat(package private) floatfloatfloatbooleanReports whether any voice is active or releasing.booleanReports whether this instrument has an available voice.intplay(float amplitude, float pitch, float pan) Generic play() from PAPlayable, will play the entire buffer from the beginning of the buffer with specified pitch and pan and default envelope.intplay(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) The primary play method, called by all playSample() methods, from PASamplerPlayable interface.intplaySample(int samplePos, int sampleLen, float amplitude) Convenience overload: uses default envelope, default pitch, and center pan.intplaySample(int samplePos, int sampleLen, float amplitude, float pitch) Convenience overload: uses default envelope, supplied pitch and center pan.intplaySample(int samplePos, int sampleLen, float amplitude, float pitch, float pan) All params except the envelope, so we use the default envelope.intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env) Plays a sample using a supplied envelope with default pitch and default pan.intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) Plays a sample using a supplied envelope, pitch, and current global pan.intplaySample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Trigger playback using all six standard per-voice parameters.intplaySample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch) Sets a new buffer for PASamplerInstrument and plays it with envelope, pitch, and default pan.intplaySample(ddf.minim.MultiChannelBuffer buffer, int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Sets a new buffer for PASamplerInstrument and plays it with envelope, pitch, and pan.voidSmoothly release all active voices (used only if we must recycle an instrument).voidsetBuffer(float[] newBuffer, float newSampleRate) Replace the instrument's active buffer contents and set a new sample rate.voidsetBuffer(ddf.minim.MultiChannelBuffer newBuffer) Copy the data and settings in the provided MultiChannelBuffer to this.buffer; bufferSampleRate is unchanged.voidsetBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newSampleRate) Copy the data and settings of the provided MultiChannelBuffer to this.buffer and set bufferSampleRate.voidsetBufferSampleRate(float newRate) Update the buffer's intrinsic sample rate.voidsetDefaultEnv(ADSRParams env) Sets the default ADSR envelope.voidsetGain(float linear) Sets per-instrument output gain.voidsetGainDb(float db) Sets per-instrument output gain in decibels.voidsetGlobalPan(float pan) Sets global pan.voidPass-through to the underlying PASharedBufferSampler mix behavior.voidsetOutputSampleRate(float newRate) Update the output sample rate, for example, if audio device changes.(package private) voidsetParentGain(float linear) voidsetPitchScale(float scale) Sets global pitch multiplier.voidstop()Stop playback (stop all active voices).private voidvoidSynchronize output sample rate from AudioOutput directly.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
-
sampler
Sampler backend used to trigger playback. -
buffer
private ddf.minim.MultiChannelBuffer bufferSource buffer for this instrument. -
out
private final ddf.minim.AudioOutput outAudio output this instrument is patched to. -
bufferSize
private int bufferSizeCached source buffer size in samples. TODO consider replacing with buffer.getBufferSize(). -
maxVoices
private int maxVoicesMaximum number of simultaneous voices for the sampler backend. -
bufferSampleRate
private float bufferSampleRateSample rate of the source from which the buffer was loaded. -
outputSampleRate
private float outputSampleRateSample rate of the AudioOutput. -
sampleRateRatio
private float sampleRateRatioBuffer-rate to output-rate ratio, used to correct playback speed. -
defaultEnv
Default ADSR envelope for playback. -
globalPitch
private volatile float globalPitchGlobal pitch multiplier. -
globalPan
private float globalPanGlobal stereo pan offset, from -1.0 left to 1.0 right. -
gain
private volatile float gainPer-instrument linear trim. -
parentGain
private volatile float parentGainParent pool gain, set by PASamplerInstrumentPool. -
isClosed
private boolean isClosed
-
-
Constructor Details
-
PASamplerInstrument
public PASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, float sampleRate, int maxVoices, ddf.minim.AudioOutput audioOut, ADSRParams env) Primary constructor for backward compatibility.- Parameters:
buffer- the shared MultiChannelBuffersampleRate- nominal sample rate of the buffer (Hz)maxVoices- number of simultaneous voices (polyphony)audioOut- target AudioOutputenv- default ADSR envelope parameters
-
PASamplerInstrument
public PASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out, PASampler sampler, ADSRParams defaultEnv, float bufferSampleRate) Full constructor with explicit buffer sample rate, custom sampler, and envelope.- Parameters:
buffer- shared MultiChannelBufferout- target AudioOutputsampler- sampler implementation to usedefaultEnv- default ADSR envelopebufferSampleRate- sample rate of the source buffer in Hz
-
PASamplerInstrument
public PASamplerInstrument(ddf.minim.MultiChannelBuffer buffer, ddf.minim.AudioOutput out) Convenience constructor assuming buffer and output share the same rate.- Parameters:
buffer- shared MultiChannelBufferout- target AudioOutput
-
-
Method Details
-
play
public int play(float amplitude, float pitch, float pan) Generic play() from PAPlayable, will play the entire buffer from the beginning of the buffer with specified pitch and pan and default envelope.- 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
-
play
public int play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) The primary play method, called by all playSample() methods, from PASamplerPlayable interface.- 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
-
stop
public void stop()Stop playback (stop all active voices).- Specified by:
stopin interfacePAPlayable
-
fadeToStop
public void fadeToStop()Smoothly stop all active voices by triggering their envelope release. This is the preferred performance stop. -
fadeOutAll
public void fadeOutAll()Alias for fadeToStop(), useful when calling from UI or pool code. -
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Trigger playback using all six standard per-voice parameters.- Specified by:
playSamplein interfacePASamplerPlayable- Parameters:
samplePos- start position (samples)sampleLen- playback length (samples)amplitude- per-voice amplitudeenv- ADSR envelope parameterspitch- playback rate (1.0 = normal)pan- stereo position (-1.0 = left, +1.0 = right)- Returns:
- the actual length of the audio event, in samples
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude, float pitch, float pan) All params except the envelope, so we use the default envelope.- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierpitch- pitch or playback-rate multiplierpan- stereo pan- Returns:
- the actual length of the audio event, in samples
-
playSample
public int playSample(int samplePos, int sampleLen, float amplitude) Convenience overload: uses default envelope, default pitch, and center pan.- 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) Convenience overload: uses default envelope, supplied pitch and center pan.- 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
Plays a sample using a supplied envelope with default pitch and default pan.- Parameters:
samplePos- buffer index to start playbacksampleLen- requested duration in samplesamplitude- gain multiplierenv- optional ADSR envelope- Returns:
- actual event duration in samples
-
playSample
Plays a sample using a supplied envelope, pitch, and current global pan.- 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) Sets a new buffer for PASamplerInstrument and plays it with envelope, pitch, and pan. For large buffers, there may be some latency.- 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) Sets a new buffer for PASamplerInstrument and plays it with envelope, pitch, and default pan.- 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
-
setGain
public void setGain(float linear) Sets per-instrument output gain.- Parameters:
linear- linear gain value
-
getGain
public float getGain()- Returns:
- per-instrument output gain as a linear value
-
setGainDb
public void setGainDb(float db) Sets per-instrument output gain in decibels.- Parameters:
db- gain in decibels
-
getGainDb
public float getGainDb()- Returns:
- per-instrument output gain in decibels
-
setParentGain
void setParentGain(float linear) -
getParentGain
float getParentGain() -
syncMasterGain
private void syncMasterGain() -
getMixProfile
- Returns:
- the current PASharedBufferSampler mix profile, or BALANCED if the sampler implementation is not PASharedBufferSampler.
-
cycleMixProfile
Convenience: cycle to the next available sampler mix profile.- Returns:
- the newly selected profile
-
hasAvailableVoice
public boolean hasAvailableVoice()Reports whether this instrument has an available voice.- Returns:
- true if at least one voice is available
-
hasActiveOrReleasingVoices
public boolean hasActiveOrReleasingVoices()Reports whether any voice is active or releasing.- Returns:
- true when one or more voices are active or releasing
-
activeOrReleasingVoiceCount
public int activeOrReleasingVoiceCount()Counts active or releasing voices.- Returns:
- active or releasing voice count
-
releaseAllVoices
public void releaseAllVoices()Smoothly release all active voices (used only if we must recycle an instrument). -
getSampler
- Returns:
- sampler implementation used by this instrument
-
getBuffer
public ddf.minim.MultiChannelBuffer getBuffer()- Returns:
- current source buffer
-
getBufferSize
public int getBufferSize()- Returns:
- current source buffer size in samples
-
setBuffer
public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer) Copy the data and settings in the provided MultiChannelBuffer to this.buffer; bufferSampleRate is unchanged. The sampler and its voices are also refreshed. Note that bufferSize will change if the buffers are not the same size.- Parameters:
newBuffer- buffer to replace the current buffer
-
setBuffer
public void setBuffer(ddf.minim.MultiChannelBuffer newBuffer, float newSampleRate) Copy the data and settings of the provided MultiChannelBuffer to this.buffer and set bufferSampleRate. The sampler and its voices are also refreshed. Note that bufferSize will change if the buffers are not the same size.- Parameters:
newBuffer- buffer to replace the current buffernewSampleRate- sample rate of the replacement buffer in Hz
-
setBuffer
public void setBuffer(float[] newBuffer, float newSampleRate) Replace the instrument's active buffer contents and set a new sample rate. Replace with a new signal array using MultiChannelBuffer setChannel(0, newBuffer), which creates a copy of the supplied array, and set a new bufferSamplerRate. The sampler and its voices are also refreshed. Note that bufferSize will change if the this.buffer and newBuffer are not the same size.The instrument retains its own MultiChannelBuffer but updates its internal copy. The sampler and its voices also receive a fresh copy.
- Parameters:
newBuffer- replacement mono source buffernewSampleRate- sample rate of the replacement buffer in Hz
-
getAudioOutput
public ddf.minim.AudioOutput getAudioOutput()- Returns:
- target AudioOutput
-
getBufferSampleRate
public float getBufferSampleRate()- Returns:
- source buffer sample rate in Hz
-
getOutputSampleRate
public float getOutputSampleRate()- Returns:
- output sample rate in Hz
-
getSampleRateRatio
public float getSampleRateRatio()- Returns:
- buffer-to-output sample-rate ratio
-
setBufferSampleRate
public void setBufferSampleRate(float newRate) Update the buffer's intrinsic sample rate.- Parameters:
newRate- new sample rate for the buffer, affects playback rate
-
setOutputSampleRate
public void setOutputSampleRate(float newRate) Update the output sample rate, for example, if audio device changes. This is unlikely to happen, but we'll be cautious.- Parameters:
newRate- new output sample rate in Hz
-
updateRateFromOutput
public void updateRateFromOutput()Synchronize output sample rate from AudioOutput directly. -
getDefaultEnv
- Returns:
- default ADSR envelope
-
setDefaultEnv
Sets the default ADSR envelope.- Parameters:
env- default ADSR envelope
-
setPitchScale
public void setPitchScale(float scale) Sets global pitch multiplier.- Parameters:
scale- pitch multiplier
-
getPitchScale
public float getPitchScale()- Returns:
- global pitch multiplier
-
setGlobalPan
public void setGlobalPan(float pan) Sets global pan.- Parameters:
pan- stereo pan position
-
getGlobalPan
public float getGlobalPan()- Returns:
- global pan position
-
clampPan
private static float clampPan(float pan) -
close
public void close()Stop all voices and disconnect UGens from the output.
-