Class AudioScheduler<H>
Two event shapes
- PointEvent: happens once at an absolute sample time.
- SpanEvent: is active over an interval [startSample, endSample) and can report start/end offsets plus per-block callbacks while active.
Threading model
schedulePoint(...)andscheduleSpan(...)may be called from any thread.processBlock(...)must be called exactly once per audio block on the audio thread.
Happenings
The generic type parameter H is a user-defined "Happening": a compact piece of information
that you want to schedule in time (e.g., x/y coordinates, pan/gain, an instrument id, a brush reference,
or a small command object). A Happening is carried by events and delivered to handlers at the correct time.
Sample accuracy
processBlock(...) reports an offsetInBlock for point events and span boundaries, so you can
align actions to an exact sample within the current audio block. If your instrument can only start on
block boundaries, you can still use this scheduler for deterministic block timing today, and later
upgrade your instrument to accept offsets without changing scheduling code.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumPolicy for point events whose sampleTime is already in the past when processed.static final classA one-shot event that occurs at an absolute sample index.static interfacestatic final classA duration event active on [startSample, endSample) where endSample is exclusive.static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentLinkedQueue<Object>private AudioScheduler.LatePolicyprivate final PriorityQueue<Object> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all pending events (both newly scheduled and already queued).private voidCalled only on the audio thread.voidprocessBlock(long blockStartSample, int blockSize, AudioScheduler.PointHandler<H> pointHandler, AudioScheduler.SpanHandler<H> spanHandler) Process one audio block.voidschedulePoint(long sampleTime, H happening) Schedule a point event at an absolute sample time.voidscheduleSpan(long startSample, long endSample, H happening) Schedule a span event active on [startSample, endSample).voidSet how late point events are handled.private static long
-
Field Details
-
inbox
-
queue
-
latePolicy
-
-
Constructor Details
-
AudioScheduler
public AudioScheduler()
-
-
Method Details
-
setLatePolicy
Set how late point events are handled. Default isAudioScheduler.LatePolicy.DROP. -
latePolicy
-
schedulePoint
Schedule a point event at an absolute sample time.- Parameters:
sampleTime- absolute sample index (0-based)happening- the Happening to deliver
-
scheduleSpan
Schedule a span event active on [startSample, endSample).- Parameters:
startSample- absolute start sample (inclusive)endSample- absolute end sample (exclusive)happening- the Happening to deliver
-
clear
public void clear()Clears all pending events (both newly scheduled and already queued). Safe to call from any thread. -
drainInbox
private void drainInbox()Called only on the audio thread. -
startSampleOf
-
processBlock
public void processBlock(long blockStartSample, int blockSize, AudioScheduler.PointHandler<H> pointHandler, AudioScheduler.SpanHandler<H> spanHandler) Process one audio block. Call exactly once per block on the audio thread.All point events that occur within this block (or earlier, if
AudioScheduler.LatePolicy.CLAMP_TO_BLOCK_START) will be delivered topointHandler. Span events will deliver start/end callbacks (when the boundary falls within this block) and anonBlockcallback for each block they overlap.- Parameters:
blockStartSample- absolute sample index of the first sample in this blockblockSize- number of samples in this blockpointHandler- handler for point events (may be null if you never schedule points)spanHandler- handler for span events (may be null if you never schedule spans)
-