Class PAGranularSampler
PASource playback.
PAGranularSampler is the voice-pool and sample-accurate scheduling layer in
PixelAudio's granular synthesis chain. PAGranularInstrument passes immediate and
scheduled playback requests to this class; the sampler allocates or reuses
PAGranularVoice instances, starts scheduled voices at the requested sample time,
and mixes active voices into Minim's audio callback.
Core responsibilities:
- manage a bounded pool of
PAGranularVoiceinstances; - start sources immediately or through
AudioScheduler; - carry per-voice envelope, gain, pan, looping, and grain-window settings;
- mix active voices sample by sample in
uGenerate(float[]); - apply light mix normalization and soft clipping to reduce overload.
When the voice pool is full, the sampler may steal the oldest active voice. With smooth stealing enabled, the old voice is released before reuse; otherwise it is stopped immediately.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class ddf.minim.UGen
ddf.minim.UGen.InputType, ddf.minim.UGen.UGenInput -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate floatprivate intprivate floatprivate final ddf.minim.AudioOutputprivate longprivate final AudioScheduler<PAGranularSampler.ScheduledPlay>private booleanprivate final float[]private final List<PAGranularVoice> -
Constructor Summary
ConstructorsConstructorDescriptionPAGranularSampler(ddf.minim.AudioOutput out) Creates a granular sampler with a default maximum of 32 voices.PAGranularSampler(ddf.minim.AudioOutput out, int maxVoices) Initializes this PAGranularSampler, which extends UGen and gets patched to an AudioOutput, with the result that its uGenerate method is called on each audio block. -
Method Summary
Modifier and TypeMethodDescriptionintCounts voices that are active or releasing.voidClear pending scheduled starts and release currently sounding voices.voidClear pending scheduled starts and immediately stop all active voices.voidRemove all pending scheduled starts that have not yet been launched.ddf.minim.AudioOutputprivate PAGranularVoicegetAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping) private PAGranularVoicegetAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples) Allocates or reuses a voice for a source.longReturns the current absolute sample cursor for higher-level scheduling.intbooleanReports whether smooth voice stealing is enabled.longplay(PASource src, float gain, float pan, ADSRParams defaultEnv, boolean looping) Convenience overload for callers that supply an already-resolved default envelope.longplay(PASource src, ADSRParams env, float gain, float pan) Convenience overload that disables looping.longplay(PASource src, ADSRParams env, float gain, float pan, boolean looping) Plays a granular source immediately as a voice.voidRelease all currently active or releasing voices.voidsetMaxVoices(int maxVoices) Sets the maximum number of voices in the pool.voidsetSmoothSteal(boolean smoothSteal) Enables or disables smooth voice stealing.private static floatsoftClipSoftsign(float x, float drive) voidstartAfterDelaySamples(PASource src, ADSRParams env, float gain, float pan, boolean looping, long delaySamples) Schedules a new voice to start after a delay in samples.voidstartAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample) Schedules a new voice to start at an absolute sample time.voidstartAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples) Schedules a new voice to start at an absolute sample time with grain-window settings.voidstopAll()Immediately stops all voices without clearing pending scheduled starts.protected voiduGenerate(float[] channels) Generates one audio sample frame for Minim.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
-
out
private final ddf.minim.AudioOutput out -
voices
-
maxVoices
private int maxVoices -
blockSize
private int blockSize -
smoothSteal
private boolean smoothSteal -
scheduler
-
sampleCursor
private long sampleCursor -
tmpStereo
private final float[] tmpStereo -
mixNorm
private float mixNorm -
globalMakeUpGain
private float globalMakeUpGain
-
-
Constructor Details
-
PAGranularSampler
public PAGranularSampler(ddf.minim.AudioOutput out, int maxVoices) Initializes this PAGranularSampler, which extends UGen and gets patched to an AudioOutput, with the result that its uGenerate method is called on each audio block.- Parameters:
out- a Minim AudioOutput that this PAGranularSampler will patch tomaxVoices- maximum number of voices to allocate
-
PAGranularSampler
public PAGranularSampler(ddf.minim.AudioOutput out) Creates a granular sampler with a default maximum of 32 voices.- Parameters:
out- Minim audio output this sampler patches to
-
-
Method Details
-
getAvailableVoice
private PAGranularVoice getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples) Allocates or reuses a voice for a source.If no inactive voice is available and the pool is below
maxVoices, a new voice is created. If the pool is full, the oldest active voice is stolen. Smooth stealing releases the old voice first; hard stealing stops it immediately.- Parameters:
src- source to renderenv- ADSRParams envelope, or null for the voice defaultgain- linear voice gainpan- stereo pan in the range [-1, 1]looping- true to loop the source path where supportedgrainWindow- window function for shaping grain amplitude, or null for source defaultgrainLenSamples- number of samples in one grain- Returns:
- allocated voice, or null if no voice could be allocated
-
getAvailableVoice
private PAGranularVoice getAvailableVoice(PASource src, ADSRParams env, float gain, float pan, boolean looping) -
play
Plays a granular source immediately as a voice.- Parameters:
src- source to renderenv- ADSR for the macro envelopegain- linear voice gainpan- stereo pan in the range [-1, 1]looping- true to loop the source path where supported- Returns:
- voice id, or -1 if playback could not start
-
play
Convenience overload that disables looping. -
play
Convenience overload for callers that supply an already-resolved default envelope. -
startAtSampleTime
public void startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample) Schedules a new voice to start at an absolute sample time.- Parameters:
src- source to renderenv- ADSR (already resolved: either custom or default)gain- final linear voice gainpan- final stereo panlooping- true to loop the source path where supportedstartSample- absolute sample index at which to start the voice
-
startAtSampleTime
public void startAtSampleTime(PASource src, ADSRParams env, float gain, float pan, boolean looping, long startSample, ddf.minim.analysis.WindowFunction grainWindow, int grainLenSamples) Schedules a new voice to start at an absolute sample time with grain-window settings.Called by
PAGranularInstrument.startAtSampleTime(PASource, float, float, ADSRParams, boolean, long, WindowFunction, int). This method packages the source and rendering settings in a scheduled payload that is launched from Minim'suGenerate(float[])callback.- Parameters:
src- source to renderenv- ADSR (already resolved: either custom or default)gain- final linear voice gainpan- final stereo panlooping- true to loop the source path where supportedstartSample- absolute sample index at which to start the voicegrainWindow- a window function for shaping grain amplitudegrainLenSamples- number of samples in one grain
-
startAfterDelaySamples
public void startAfterDelaySamples(PASource src, ADSRParams env, float gain, float pan, boolean looping, long delaySamples) Schedules a new voice to start after a delay in samples.- Parameters:
src- source to renderenv- ADSRgain- final linear voice gainpan- final stereo panlooping- true to loop the source path where supporteddelaySamples- how many samples from "now" to start
-
getCurrentSampleTime
public long getCurrentSampleTime()Returns the current absolute sample cursor for higher-level scheduling.- Returns:
- current sample time maintained by this sampler
-
uGenerate
protected void uGenerate(float[] channels) Generates one audio sample frame for Minim.This method advances the scheduler, launches voices whose start time has arrived, renders active voices, applies mix normalization and soft clipping, and increments the sample cursor.
- Specified by:
uGeneratein classddf.minim.UGen
-
softClipSoftsign
private static float softClipSoftsign(float x, float drive) -
stopAll
public void stopAll()Immediately stops all voices without clearing pending scheduled starts.A stop halts voice processing directly and is more abrupt than a release.
-
clearScheduled
public void clearScheduled()Remove all pending scheduled starts that have not yet been launched. Does not affect currently active voices. -
releaseAll
public void releaseAll()Release all currently active or releasing voices.A release is generally less abrupt than a stop, because each active voice is allowed to pass through its envelope release stage. This is useful for a musical stop after clearing pending scheduled starts.
-
cancelAndStopAll
public void cancelAndStopAll()Clear pending scheduled starts and immediately stop all active voices.A stop halts voice processing directly and is therefore more abrupt than a release.
-
cancelAndReleaseAll
public void cancelAndReleaseAll()Clear pending scheduled starts and release currently sounding voices.This is the less abrupt counterpart to
cancelAndStopAll(). -
activeOrReleasingVoiceCount
public int activeOrReleasingVoiceCount()Counts voices that are active or releasing.- Returns:
- active or releasing voice count
-
setMaxVoices
public void setMaxVoices(int maxVoices) Sets the maximum number of voices in the pool.- Parameters:
maxVoices- maximum voices; values below 1 are clamped to 1
-
getMaxVoices
public int getMaxVoices() -
setSmoothSteal
public void setSmoothSteal(boolean smoothSteal) Enables or disables smooth voice stealing.Voice stealing occurs when all voices are busy and a new source must start. With smooth stealing enabled, the oldest active voice is released before reuse; with it disabled, the oldest active voice is stopped immediately.
- Parameters:
smoothSteal- true to release stolen voices, false to stop them immediately
-
isSmoothSteal
public boolean isSmoothSteal()Reports whether smooth voice stealing is enabled.- Returns:
- true when stolen voices are released instead of stopped immediately
-
getVoices
-
getAudioOutput
public ddf.minim.AudioOutput getAudioOutput()
-