javascript – Web audio API oscillator onstarted event
The ended
event is the only event that an OscillatorNode
emits. I guess this is because this event is inherited from the AudioScheduledSourceNode
interface.
Another node which inherits that interface is the AudioBufferSourceNode
. And for an AudioBufferSourceNode
it’s often not so easy to tell when it actually ends which is why I think the event exists. This could also be the reason why there is no started
event.
Anyway you can re-build that functionality by using another AudioScheduledSourceNode
. I would recommend to use a ConstantSourceNode
since it is the one which is the most lightweight.
const constantSourceNode = new ConstantSourceNode(ac);
constantSourceNode.onended = () => {
console.log('osc started');
};
constantSourceNode.start();
constantSourceNode.stop(5);
Please note that the ended
events are not very accurate. They get triggered on the audio thread and need to travel back to the main thread where they finally get fired when there is some time to do so.
Read more here: Source link