web-audio-scheduler
Event Scheduler for Web Audio API
This module is developed based on the idea of this article.
Installation
npm
npm install web-audio-scheduler
downloads
Examples
const audioContext = ;const sched = context: audioContext ;let masterGain = null; { const t0 = eplaybackTime; sched; sched; sched; sched; sched;} { const t0 = eplaybackTime; const t1 = t0 + eargsduration; const osc = audioContext; const amp = audioContext; oscfrequencyvalue = eargsfrequency; oscstartt0; osc; osc; ampgain; ampgain; amp; sched;} sched; sched; document; document; document;
API
WebAudioScheduler
WebAudioScheduler(opts = {})
context: AudioContext
interval: number
default: 0.025 (25ms)aheadTime: number
default: 0.1 (100ms)timerAPI: object
default:window || global
Instance properties
context: AudioContext
interval: number
aheadTime: number
timerAPI: object
playbackTime: number
currentTime: number
state: string
events: object[]
Instance methods
start([ callback: function, args: any ]): self
- Start the timeline.
- The
callback
is inserted in the head of the event list if given.
stop([ reset = true: boolean ]): self
- Stop the timeline.
- The event list is cleared if
reset
is truthy.
insert(time: number, callback: function, [ args: any ]): number
- Insert the
callback
into the event list. - The return value is
schedId
. It is used to.remove()
the callback.
- Insert the
nextTick([ time: number ], callback: function, [ args: any ]): number
- Same as
.insert()
, but this callback is called at next tick. - This method is used to disconnect an audio node at the proper timing.
- Same as
remove(schedId: number): number
- Remove a callback function from the event list.
removeAll(): void
- Remove all callback functions from the event list.
process(): void
- process events immediately (this is useful when transition to background tabs)
Events
"start"
- emitted when the scheduler started.
"stop"
- emitted when the scheduler stopped.
"process"
- emitted before each scheduler process.
"processed"
- emitted after each scheduler process.
Callback
A callback function receives a schedule event and given arguments at .insert()
.
A schedule event has two parameters.
playbackTime: number
args: any
sched; { ; ; ;}
Customize
timeline
time(ms) 0----25---50---75---100--125--150--175--200---->
=====|====|====|====| | | | |
| =====|====|====|====| | | |
| | =====|====|====|====| | |
| | | | ===|====|====|====|== |
| | | | | ===|====|====|====|==
: : : : : : : : :
|<-->| : : |<------------------>|
interval (25ms) aheadTime (100ms)
= range of execution to events
The below example is the same configuration as defaults.
const sched = interval: 0025 aheadTime: 01 ;
timerAPI
TimerAPI is used instead of the native timer API. TimerAPI should have two functions, setInterval
and clearInterval
.
- mohayonao/worker-timer
- A timer that is stable in any situation. e.g. tabs in background, the invisible state page.
- mohayonao/tickable-timer
- Manual ticking
setTimeout
/setInterval
(for test CI)
- Manual ticking
The below example uses stable-timer instead of the native timer API.
const WorkerTimer = ;const sched = timerAPI: WorkerTimer ;
License
MIT