Class PAGranularInstrumentDirector
PAGranularInstrumentDirector is the high-level entry point most callers use
after they have a mono source buffer, a GestureSchedule, runtime synthesis settings,
and per-event buffer indices. The director prepares the schedule, creates one
PABurstGranularSource for each scheduled event, and delegates playback to a
PAGranularInstrument, which further delegates audio generation.
Granular playback chain:
PAGranularInstrumentDirectorreceives a mono source buffer, gesture schedule, runtime synthesis parameters, and per-event parameters. It prepares event timing, resolves defaults, creates onePABurstGranularSourceper gesture event, and schedules each source at an absolute sample time.PAGranularInstrumentapplies instrument-level gain, pan, and envelope defaults, then forwards immediate or scheduled playback requests to the sampler.PAGranularSamplerowns the voice pool and sample-accurate scheduler. It starts voices immediately or when scheduled sample times arrive.PAGranularVoiceapplies the macro envelope, voice gain, pan, and lifecycle state while asking itsPASourceto render audio blocks.PABurstGranularSourcerenders the actual burst grains sample by sample, including source-buffer lookup, pitch-ratio playback, windowing, and overlap-add normalization.
This class owns the gesture-level decisions: when events occur, where each event reads
from the source buffer, which per-event pan/gain/pitch overrides apply, and which grain
window is used. PABurstGranularSource owns sample-level burst rendering for each
event.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate long[]private static final ddf.minim.analysis.WindowFunctionprivate final PAGranularInstrumentprivate GestureGranularParamsprivate GestureScheduleprivate GestureScheduleprivate floatprivate intprivate GestureScheduleprivate final float -
Constructor Summary
ConstructorsConstructorDescriptionPAGranularInstrumentDirector(PAGranularInstrument instrument) Creates a director for the supplied granular instrument. -
Method Summary
Modifier and TypeMethodDescriptionintCounts active or releasing granular voices in the underlying instrument.private static GestureScheduleapplyTimeTransform(GestureSchedule rawSchedule, GestureGranularParams ggParams) Applies the director's schedule-time transform pipeline.voidCancels pending events and releases all granular voices through their envelopes.voidCancels pending events and stops all granular voices immediately.private static floatclampPan(float p) voidClears pending scheduled granular events without stopping voices already playing.private static floateaseInOutPow(float u, float exp) private voidensureCache(GestureSchedule sched, GestureGranularParams params) Returns the underlying granular instrument.private static floatlerp(float a, float b, float u) private static longmsToSamples(float ms, float sr) voidplayGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, float[] panValues, long startSampleTime) Schedules a gesture at an absolute sample time using source-buffer indices and pan overrides.voidplayGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, long startSampleTime) Schedules a gesture at an absolute sample time using per-event source-buffer indices.voidplayGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, GestureEventParams evtParams, long startSampleTime) Schedules a gesture at an absolute sample time using complete per-event parameters.voidplayGestureAtSampleTimeTransformed(float[] monoBuf, GestureSchedule transformedSchedule, GestureGranularParams params, GestureEventParams evtParams, long startSampleTime) Schedules a gesture whose timing has already been transformed.voidplayGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices) Plays a gesture immediately using per-event source-buffer indices.voidplayGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, float[] panValues) Plays a gesture immediately using per-event source-buffer indices and pan overrides.voidplayGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, GestureEventParams evtParams) Plays a gesture immediately using a complete per-event parameter object.prepareSchedule(GestureSchedule rawSchedule, GestureGranularParams params) Returns a schedule prepared for playback by this director.voidReleases all currently playing granular voices through their envelopes.private static GestureScheduleresampleToCount(GestureSchedule in, int targetCount) private static ddf.minim.analysis.WindowFunctionprivate static GestureSchedulescaleToDuration(GestureSchedule in, float targetDurationMs) private voidscheduleEvents(float[] monoBuf, GestureSchedule sched, GestureGranularParams params, GestureEventParams evtParams, ddf.minim.analysis.WindowFunction wf, long startSampleTime) Creates and schedules one burst source for each gesture event.private static GestureSchedulewarpScheduleTimesMs(GestureSchedule in, GestureGranularParams.WarpShape shape, float exponent) private static floatwarpU(float u, GestureGranularParams.WarpShape shape, float exponent)
-
Field Details
-
instrument
-
sampleRate
private final float sampleRate -
lastScheduleSize
private int lastScheduleSize -
lastScheduleEndMs
private float lastScheduleEndMs -
cachedEventOffsetsSamples
private long[] cachedEventOffsetsSamples -
lastRawScheduleRef
-
lastOffsetsParamsRef
-
lastTransformedScheduleRef
-
lastOffsetsScheduleRef
-
DEFAULT_GRAIN_WINDOW
private static final ddf.minim.analysis.WindowFunction DEFAULT_GRAIN_WINDOW
-
-
Constructor Details
-
PAGranularInstrumentDirector
Creates a director for the supplied granular instrument.- Parameters:
instrument- instrument used to schedule and render granular voices- Throws:
NullPointerException- ifinstrumentis null
-
-
Method Details
-
resolveGrainWindow
-
playGestureNow
public void playGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices) Plays a gesture immediately using per-event source-buffer indices.The start time is the instrument's current sample cursor plus a short lead-in.
- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- gesture schedule whose size must matchstartIndices.lengthparams- runtime granular playback parametersstartIndices- per-event start indices intomonoBuf
-
playGestureNow
public void playGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, float[] panValues) Plays a gesture immediately using per-event source-buffer indices and pan overrides.- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- gesture schedule whose size must match the per-event arraysparams- runtime granular playback parametersstartIndices- per-event start indices intomonoBufpanValues- optional per-event pan values
-
playGestureNow
public void playGestureNow(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, GestureEventParams evtParams) Plays a gesture immediately using a complete per-event parameter object.- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- gesture schedule whose size must matchevtParams.nparams- runtime granular playback parametersevtParams- per-event source indices and optional pan, gain, and pitch overrides
-
playGestureAtSampleTime
public void playGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, long startSampleTime) Schedules a gesture at an absolute sample time using per-event source-buffer indices.- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- gesture schedule whose size must matchstartIndices.lengthparams- runtime granular playback parametersstartIndices- per-event start indices intomonoBufstartSampleTime- absolute sample time for the first gesture event
-
playGestureAtSampleTime
public void playGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, int[] startIndices, float[] panValues, long startSampleTime) Schedules a gesture at an absolute sample time using source-buffer indices and pan overrides.- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- gesture schedule whose size must match the per-event arraysparams- runtime granular playback parametersstartIndices- per-event start indices intomonoBufpanValues- optional per-event pan valuesstartSampleTime- absolute sample time for the first gesture event
-
playGestureAtSampleTime
public void playGestureAtSampleTime(float[] monoBuf, GestureSchedule schedule, GestureGranularParams params, GestureEventParams evtParams, long startSampleTime) Schedules a gesture at an absolute sample time using complete per-event parameters.This method prepares the schedule according to
params, validates the per-event parameter count, caches event offsets in samples, and schedules onePABurstGranularSourcefor each event.- Parameters:
monoBuf- mono source buffer containing audio samplesschedule- raw gesture schedule in millisecondsparams- runtime granular playback parametersevtParams- per-event source indices and optional pan, gain, and pitch overridesstartSampleTime- absolute sample time for the first gesture event
-
playGestureAtSampleTimeTransformed
public void playGestureAtSampleTimeTransformed(float[] monoBuf, GestureSchedule transformedSchedule, GestureGranularParams params, GestureEventParams evtParams, long startSampleTime) Schedules a gesture whose timing has already been transformed.Use this overload when schedule preparation has been performed elsewhere and
transformedSchedule.timesMsalready contains the desired event offsets.- Parameters:
monoBuf- mono source buffer containing audio samplestransformedSchedule- prepared schedule in millisecondsparams- runtime granular playback parametersevtParams- per-event source indices and optional pan, gain, and pitch overridesstartSampleTime- absolute sample time for the first gesture event
-
scheduleEvents
private void scheduleEvents(float[] monoBuf, GestureSchedule sched, GestureGranularParams params, GestureEventParams evtParams, ddf.minim.analysis.WindowFunction wf, long startSampleTime) Creates and schedules one burst source for each gesture event.- Parameters:
monoBuf- mono source buffer containing audio samplessched- prepared gesture schedule in millisecondsparams- runtime granular playback parametersevtParams- per-event source indices and optional pan, gain, and pitch overrideswf- grain window function resolved fromparams, or the default windowstartSampleTime- absolute sample time for the first gesture event
-
ensureCache
-
applyTimeTransform
private static GestureSchedule applyTimeTransform(GestureSchedule rawSchedule, GestureGranularParams ggParams) Applies the director's schedule-time transform pipeline.The pipeline applies, in order, optional resampling, optional duration scaling, and optional timing warp. Callers normally reach this method through
prepareSchedule(GestureSchedule, GestureGranularParams).- Parameters:
rawSchedule- original gesture scheduleggParams- runtime granular playback parameters- Returns:
- transformed schedule, or the input schedule when no transform applies
-
prepareSchedule
Returns a schedule prepared for playback by this director.If
params.timeTransformisGestureGranularParams.TimeTransform.RAW_GESTURE, the raw schedule is returned unchanged. Otherwise this method applies the director's transform pipeline and caches the result by raw schedule and parameter object identity.- Parameters:
rawSchedule- original gesture scheduleparams- runtime granular playback parameters- Returns:
- prepared schedule, or
rawSchedulewhen no transform is needed
-
msToSamples
private static long msToSamples(float ms, float sr) -
resampleToCount
-
scaleToDuration
-
warpScheduleTimesMs
private static GestureSchedule warpScheduleTimesMs(GestureSchedule in, GestureGranularParams.WarpShape shape, float exponent) -
warpU
-
easeInOutPow
private static float easeInOutPow(float u, float exp) -
lerp
private static float lerp(float a, float b, float u) -
clampPan
private static float clampPan(float p) -
getInstrument
Returns the underlying granular instrument.- Returns:
- granular instrument used by this director
-
clearScheduled
public void clearScheduled()Clears pending scheduled granular events without stopping voices already playing.For a musical stop, call this first and then call
releaseAll(). -
releaseAll
public void releaseAll()Releases all currently playing granular voices through their envelopes.A release is generally less abrupt than a stop, because each active voice is allowed to pass through its envelope release stage.
-
cancelAndStopAll
public void cancelAndStopAll()Cancels pending events and stops all granular voices immediately.A stop halts voice processing directly and is therefore more abrupt than a release.
-
cancelAndReleaseAll
public void cancelAndReleaseAll()Cancels pending events and releases all granular voices through their envelopes.This is the less abrupt counterpart to
cancelAndStopAll()and is generally better suited to musical fade-outs. -
activeOrReleasingVoiceCount
public int activeOrReleasingVoiceCount()Counts active or releasing granular voices in the underlying instrument.- Returns:
- active or releasing voice count
-