[英]Web audio API oscillator onstarted event
The Web Audio API oscillator allows a script to be alerted when the oscillator stops with onended
. Web Audio API振荡器允许在振荡器以
onended
停止时向脚本发出警报。
How can the script be alerted when it starts?脚本启动时如何发出警报?
const ac = new AudioContext()
const osc = ac.createOscillator()
osc.type = 'sawtooth'
osc.connect(ac.destination)
osc.onstarted = () => console.log('ended!') // This doesn't work :'(
osc.onended = () => console.log('ended!') // :)
osc.start(5)
osc.stop(7)
The ended
event is the only event that an OscillatorNode
emits. ended
事件是OscillatorNode
发出的唯一事件。 I guess this is because this event is inherited from the AudioScheduledSourceNode
interface.我猜这是因为这个事件是继承自
AudioScheduledSourceNode
接口的。
Another node which inherits that interface is the AudioBufferSourceNode
.继承该接口的另一个节点是
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.对于
AudioBufferSourceNode
来说,通常很难判断它何时真正结束,这就是我认为该事件存在的原因。 This could also be the reason why there is no started
event.这也可能是没有
started
事件的原因。
Anyway you can re-build that functionality by using another AudioScheduledSourceNode
.无论如何,您可以使用另一个
AudioScheduledSourceNode
重新构建该功能。 I would recommend to use a ConstantSourceNode
since it is the one which is the most lightweight.我建议使用
ConstantSourceNode
,因为它是最轻量级的。
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.请注意,
ended
的事件不是很准确。 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.它们在音频线程上被触发,需要返回主线程,当有时间时它们最终会被触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.