Class PASharedBufferSampler
- All Implemented Interfaces:
PASampler
Features:
- Shared buffer (no duplication)
- Polyphony with voice pooling
- Looping (global default + per-voice)
- ADSR per voice via ADSRParams
- Oldest-first voice recycling, optional smooth stealing
- Thread-safe triggering
- Presets for noise reduction with MixProfile and
PASamplerInstrumentPoolcycleMixProfile().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumMix-density normalization profiles for polyphonic sampler output, used to control noise and distortion.private static final classNested classes/interfaces inherited from class ddf.minim.UGen
ddf.minim.UGen.InputType, ddf.minim.UGen.UGenInput -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate float[]Mono source buffer, typically channel 0 from a MultiChannelBuffer.private intCached source buffer length in samples.protected booleanEnables diagnostic voice-trigger logging when true.private booleanDefault looping state for newly triggered voices.private floatMaster linear gain applied to mixed output.private intMaximum number of simultaneous voices.private floatSmoothed mix-normalization gain.private PASharedBufferSampler.MixProfileprivate final ddf.minim.AudioOutputAudio output this sampler is patched to.private floatSample rate of the source buffer in Hz.private longAbsolute sample counter advanced by the audio callback.private final AudioScheduler<PASharedBufferSampler.ScheduledPlay>Sample-accurate scheduler for launching sampler voices.private booleanTrue to release stolen voices smoothly instead of stopping them immediately.private final List<PASamplerVoice>Voice pool used for polyphonic sample playback.private booleanTrue to wrap source-buffer reads for newly triggered voices. -
Constructor Summary
ConstructorsConstructorDescriptionPASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float sampleRate, ddf.minim.AudioOutput out) Construct a sampler over a shared MultiChannelBuffer.PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float playbackSampleRate, ddf.minim.AudioOutput out, int maxVoices) Constructs a sampler over a shared buffer with explicit polyphony.PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, ddf.minim.AudioOutput out) Constructs a sampler and infers playback sample rate from the output. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears pending scheduled starts without stopping active voices.private intcomputeEventSamples(int samplePos, int sampleLen, ADSRParams env, float pitch) Computes the expected rendered duration of a voice in samples.intCounts voices that are neither active nor releasing.voidAdvances to the next mix normalization profile.ddf.minim.AudioOutputprivate PASamplerVoiceGet a free voice, or recycle the oldest active one if at the polyphony limit.intlongReturns the sampler-local audio-thread sample clock.floatfloatintfloatReturns the current sample rate of this sampler.Read-only list of voices for GUI or debugging.booleanbooleanReturns true if any currently active voice is looping.booleanbooleanReports whether new sampler voices wrap source-buffer reads at the buffer end.private int[]normalizeRange(int samplePos, int sampleLen) Validates and clamps a source-buffer playback range.intplay(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Plays a buffer region immediately.voidReleases all voices through their envelopes.voidsetBuffer(float[] buffer) Replaces the sampler source buffer.voidsetBuffer(float[] buffer, float playbackSampleRate) Replaces the sampler source buffer and playback sample rate.voidsetGlobalLooping(boolean looping) Sets default looping for newly triggered voices.voidsetMasterGain(float linear) Sets master output gain.voidsetMasterGainDb(float db) Sets master output gain in decibels.voidsetMaxVoices(int maxVoices) Change maximum polyphony at runtime.voidSets the mix normalization profile.voidsetPlaybackSampleRate(float newRate) Updates the playback sample rate used for reading from the buffer.voidsetSmoothSteal(boolean smoothSteal) Enable/disable smooth stealing (release envelope) on voice recycle.voidsetWrapAround(boolean wrapAround) Sets whether newly triggered voices wrap source-buffer reads.(package private) static floatsoftClipSoftsign(float x, float drive) Applies a softsign limiter to reduce overload without hard clipping.voidstartAfterDelaySamples(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long delaySamples) Schedules a sampler voice to start after a sample delay from the current sampler clock.voidstartAtSampleTime(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long startSample) Schedules a sampler voice to start at an absolute sample time.voidstopAll()Stops all voices immediately.protected voiduGenerate(float[] channels) Required by Minim.UGen, core method for audio synthesis called by Minim.voidConvenience: synchronize playback rate with AudioOutput's sample rate.Methods inherited from class ddf.minim.UGen
addAudio, addControl, addControl, addInput, channelCount, channelCountChanged, getLastValues, patch, patch, patch, printInputs, removeInput, sampleRate, sampleRateChanged, setChannelCount, setSampleRate, tick, unpatch, unpatch
-
Field Details
-
buffer
private float[] bufferMono source buffer, typically channel 0 from a MultiChannelBuffer. -
bufferLen
private int bufferLenCached source buffer length in samples. -
playbackSampleRate
private float playbackSampleRateSample rate of the source buffer in Hz. -
out
private final ddf.minim.AudioOutput outAudio output this sampler is patched to. -
voices
Voice pool used for polyphonic sample playback. -
maxVoices
private int maxVoicesMaximum number of simultaneous voices. -
globalLooping
private boolean globalLoopingDefault looping state for newly triggered voices. -
wrapAround
private boolean wrapAroundTrue to wrap source-buffer reads for newly triggered voices. -
smoothSteal
private boolean smoothStealTrue to release stolen voices smoothly instead of stopping them immediately. -
mixNorm
private float mixNormSmoothed mix-normalization gain. -
masterGain
private volatile float masterGainMaster linear gain applied to mixed output. -
DEBUG
protected boolean DEBUGEnables diagnostic voice-trigger logging when true. -
scheduler
Sample-accurate scheduler for launching sampler voices. -
sampleCursor
private long sampleCursorAbsolute sample counter advanced by the audio callback. -
mixProfile
-
-
Constructor Details
-
PASharedBufferSampler
public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float sampleRate, ddf.minim.AudioOutput out) Construct a sampler over a shared MultiChannelBuffer. Automatically patches to the provided AudioOutput.- Parameters:
multiBuffer- shared source buffer (mono or stereo)sampleRate- sample rate of the bufferout- target AudioOutput for playback
-
PASharedBufferSampler
public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, float playbackSampleRate, ddf.minim.AudioOutput out, int maxVoices) Constructs a sampler over a shared buffer with explicit polyphony.- Parameters:
multiBuffer- shared source bufferplaybackSampleRate- sample rate of the source buffer in Hzout- target AudioOutput for playbackmaxVoices- maximum simultaneous voices
-
PASharedBufferSampler
public PASharedBufferSampler(ddf.minim.MultiChannelBuffer multiBuffer, ddf.minim.AudioOutput out) Constructs a sampler and infers playback sample rate from the output.- Parameters:
multiBuffer- shared source bufferout- target AudioOutput for playback
-
-
Method Details
-
play
public int play(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Plays a buffer region immediately.The requested
samplePosandsampleLenare normalized against the current source buffer before a voice is activated. This method starts playback from the calling thread's request as soon as the audio callback next renders this sampler; usestartAtSampleTime(int, int, float, ADSRParams, float, float, long)when the launch must align to a specific sampler-clock sample.- Specified by:
playin interfacePASampler- Parameters:
samplePos- source-buffer index to start playbacksampleLen- requested source-buffer duration in samplesamplitude- per-voice gain multiplierenv- ADSR envelope parameters, or null for no voice envelopepitch- playback-rate multiplierpan- stereo pan position- Returns:
- computed voice duration in output samples, or 0 if playback could not start
-
startAtSampleTime
public void startAtSampleTime(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long startSample) Schedules a sampler voice to start at an absolute sample time.startSampleis measured on this sampler's local audio-thread clock, returned bygetCurrentSampleTime(). The event is enqueued throughAudioSchedulerand activated insideuGenerate(float[])when the clock reaches that sample.- Specified by:
startAtSampleTimein interfacePASampler- Parameters:
samplePos- source-buffer index to start playbacksampleLen- requested source-buffer duration in samplesamplitude- per-voice gain multiplierenv- ADSR envelope parameters, or null for no voice envelopepitch- playback-rate multiplierpan- stereo pan positionstartSample- absolute sample time on this sampler's local clock
-
startAfterDelaySamples
public void startAfterDelaySamples(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan, long delaySamples) Schedules a sampler voice to start after a sample delay from the current sampler clock.- Specified by:
startAfterDelaySamplesin interfacePASampler- Parameters:
samplePos- source-buffer index to start playbacksampleLen- requested source-buffer duration in samplesamplitude- per-voice gain multiplierenv- ADSR envelope parameters, or null for no voice envelopepitch- playback-rate multiplierpan- stereo pan positiondelaySamples- delay from the current sampler clock, clamped to 0 or greater
-
getCurrentSampleTime
public long getCurrentSampleTime()Returns the sampler-local audio-thread sample clock.The clock advances once for each
uGenerate(float[])call. It is suitable for scheduling future events on this sampler; applications that need multiple engines to align should choose one shared transport clock and convert all event times to that same absolute sample domain.- Specified by:
getCurrentSampleTimein interfacePASampler- Returns:
- current sampler-local sample cursor
-
clearScheduled
public void clearScheduled()Clears pending scheduled starts without stopping active voices.- Specified by:
clearScheduledin interfacePASampler
-
normalizeRange
private int[] normalizeRange(int samplePos, int sampleLen) Validates and clamps a source-buffer playback range.- Parameters:
samplePos- requested start position in the source buffersampleLen- requested playback length in source-buffer samples- Returns:
- a two-element array where index 0 is the clamped start position and index 1 is the clamped length, or null when the request cannot produce playback
-
computeEventSamples
Computes the expected rendered duration of a voice in samples.- Parameters:
samplePos- normalized source-buffer start positionsampleLen- normalized source-buffer playback lengthenv- ADSR envelope parameterspitch- playback-rate multiplier- Returns:
- expected voice duration in samples, including release where applicable
-
getAvailableVoice
Get a free voice, or recycle the oldest active one if at the polyphony limit. -
uGenerate
protected void uGenerate(float[] channels) Required by Minim.UGen, core method for audio synthesis called by Minim.Scheduled point events are processed first so voices that start at the current
sampleCursorcontribute to this output sample. The method then mixes active voices, applies constant-power panning, density normalization, and soft limiting, and finally advances the sampler-local clock by one sample.- Specified by:
uGeneratein classddf.minim.UGen
-
softClipSoftsign
static float softClipSoftsign(float x, float drive) Applies a softsign limiter to reduce overload without hard clipping.The input is first scaled by
drive, then mapped throughy / (1 + abs(y)). Small values stay close to linear, while large positive or negative values asymptotically approach -1 or 1.A drive value of 1.0 is basically neutral. Typical active values are in the range 1.2 to 1.6. 2.0 to 3.3 provides obvious compress, above 4.0 probably distorts. MixProfile provides :TRANSPARENT: 1.35, BALANCED: 1.60, PROTECTIVE: 1.25.
- Parameters:
x- input sampledrive- pre-limiter gain controlling how quickly the curve saturates- Returns:
- softly limited sample
-
stopAll
public void stopAll()Description copied from interface:PASamplerStops all voices immediately. -
releaseAll
public void releaseAll()Description copied from interface:PASamplerReleases all voices through their envelopes.- Specified by:
releaseAllin interfacePASampler
-
isLooping
public boolean isLooping()Description copied from interface:PASamplerReturns true if any currently active voice is looping. -
setGlobalLooping
public void setGlobalLooping(boolean looping) Sets default looping for newly triggered voices.- Parameters:
looping- true to loop newly triggered voices
-
isGlobalLooping
public boolean isGlobalLooping()- Returns:
- true when newly triggered voices loop by default
-
setWrapAround
public void setWrapAround(boolean wrapAround) Sets whether newly triggered voices wrap source-buffer reads.- Specified by:
setWrapAroundin interfacePASampler- Parameters:
wrapAround- true to wrap reads that pass the end of the source buffer
-
isWrapAround
public boolean isWrapAround()Description copied from interface:PASamplerReports whether new sampler voices wrap source-buffer reads at the buffer end.- Specified by:
isWrapAroundin interfacePASampler- Returns:
- true when newly triggered voices wrap source-buffer reads
-
setSmoothSteal
public void setSmoothSteal(boolean smoothSteal) Enable/disable smooth stealing (release envelope) on voice recycle.- Parameters:
smoothSteal- true to release stolen voices smoothly
-
isSmoothSteal
public boolean isSmoothSteal()- Returns:
- true when stolen voices release smoothly
-
setMaxVoices
public void setMaxVoices(int maxVoices) Change maximum polyphony at runtime.- Parameters:
maxVoices- maximum simultaneous voices
-
getMaxVoices
public int getMaxVoices()- Returns:
- maximum simultaneous voices
-
getVoices
Read-only list of voices for GUI or debugging.- Returns:
- unmodifiable voice list
-
setMasterGain
public void setMasterGain(float linear) Sets master output gain.- Parameters:
linear- linear gain value
-
getMasterGain
public float getMasterGain()- Returns:
- master output gain as a linear value
-
setMasterGainDb
public void setMasterGainDb(float db) Sets master output gain in decibels.- Parameters:
db- gain in decibels
-
getMasterGainDb
public float getMasterGainDb()- Returns:
- master output gain in decibels
-
setBuffer
public void setBuffer(float[] buffer) Replaces the sampler source buffer. -
setBuffer
public void setBuffer(float[] buffer, float playbackSampleRate) Replaces the sampler source buffer and playback sample rate. -
countAvailableVoices
public int countAvailableVoices()Counts voices that are neither active nor releasing.- Returns:
- available voice count
-
getMixProfile
- Returns:
- active mix normalization profile
-
getMixProfileName
- Returns:
- active mix profile name
-
cycleMixProfile
public void cycleMixProfile()Advances to the next mix normalization profile. -
getPlaybackSampleRate
public float getPlaybackSampleRate()Returns the current sample rate of this sampler.- Returns:
- playback sample rate in Hz
-
setPlaybackSampleRate
public void setPlaybackSampleRate(float newRate) Updates the playback sample rate used for reading from the buffer. Does not affect Minim's UGen sample rate.- Specified by:
setPlaybackSampleRatein interfacePASampler- Parameters:
newRate- playback sample rate in Hz
-
updatePlaybackRateFromOutput
public void updatePlaybackRateFromOutput()Convenience: synchronize playback rate with AudioOutput's sample rate. Useful if you want playback speed tied to system rate. -
getBufferLength
public int getBufferLength()- Returns:
- source buffer length in samples
-
getAudioOutput
public ddf.minim.AudioOutput getAudioOutput()- Returns:
- target audio output
-