Package net.paulhertz.pixelaudio.sampler
Class PASharedBufferSampler
java.lang.Object
ddf.minim.UGen
net.paulhertz.pixelaudio.sampler.PASharedBufferSampler
- All Implemented Interfaces:
PASampler
UGen-based sampler that plays multiple PASamplerVoice instances
from a single shared mono buffer (channel 0 of a MultiChannelBuffer)
which is an array of floating point samples over (-1.0..1.0).
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.Nested 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 booleanTrue to release stolen voices smoothly instead of stopping them immediately.private final List<PASamplerVoice>Voice pool used for polyphonic sample playback. -
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 TypeMethodDescriptionintCounts 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.intfloatfloatintfloatReturns 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.booleanintplay(int samplePos, int sampleLen, float amplitude, ADSRParams env, float pitch, float pan) Play command with all the useful arguments in standard order, overrides PASampler.play().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.(package private) static floatsoftClipSoftsign(float x, float drive) Applies a softsign limiter to reduce overload without hard clipping.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, unpatchMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.paulhertz.pixelaudio.sampler.PASampler
setSampleRate
-
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. -
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. -
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) Play command with all the useful arguments in standard order, overrides PASampler.play().- Specified by:
playin interfacePASampler- Parameters:
samplePos- index into the buffer to start samplingsampleLen- expected duration of the audio event, which is expected to return its actual durationamplitude- maximum amplitude of signal generated by the sampler, linear valueenv- ADSR style envelope for the entire eventpitch- pitch multiplier, where 1.0f implies pitch is unchanged and all samples are readpan- location of audio event in stereo field, -1.0f = left, 0.0f = center, 1.0f = right- Returns:
- actual duration of event in samples, typically sampleLen + envelope release time
-
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. Constant-power panning and soft limiter added to support polyphonic voices.- 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
-
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.- 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
-