Really a number of questions about the Web Audio API here, like: Is there a limit to how many nodes you can have in a graph? How do I know if I am approaching the limit? Why do non-started nodes still consume resources?
Here is a JSFiddle for reference: https://jsfiddle.net/Lv84woph/
When you hit "play" it plays a sequence of 12 chords. The function creates 36 OscillatorNode
(3 notes per chord). If you increase repetitions to 2, it will play the sequence twice - scheduling 72 notes total, doubling the play time.
If you increase repetitions to 200, it will schedule 7200 and might stutter or play and then cut off - signs of resource starvation. If you increase it to 1000 then it'll probably crash the page.
Note that at no point is polyphony ever greater than 3 notes at once: adding more repetitions just causes more oscillators started at the end of the sequence (not overlapping). These should be inactive, not playing, contributing nothing to resource usage. Why, then, do these new nodes cause problems for the entire graph?
One way to work around this would be to run a companion scheduler with setTimeout()
and have that put new notes in (e.g. have it schedule the next pattern loop), to avoid creating too many at once. An approach like this for creating a metronome is described here: https://web.dev/audio-scheduling/ However, how do I know how many notes is "safe"? It varies by device, so, will the browser tell me?
And why should I have to care about this at all? If the WebAudio API's entire thing is "create as many nodes as you want, one for every note in fact, they're basically free" then why does scheduling e.g. a MIDI file's worth of nodes flatly not work?