简体   繁体   English

网络音频计时性能

[英]Webaudio timing performance

The file below uses ToneJS to play a steam of steady 8th notes.下面的文件使用 ToneJS 播放稳定的 8 分音符。 According to the log of the timing, those 8th notes are precisely 0.25 seconds apart.根据计时日志,那些 8 分音符正好相隔 0.25 秒。

However, they don't sound even.然而,它们听起来并不均匀。 The time intervals between the notes are distinctly irregular.音符之间的时间间隔明显不规则。

Why is it so?为什么会这样? Is there anything that can be done about it?有什么可以做的吗? Or is this a performance limitation of Javascript/webaudio-api?或者这是 Javascript/webaudio-api 的性能限制? I have tested it in Chrome, Firefox, and Safari, all to the same result.我已经在 Chrome、Firefox 和 Safari 中对其进行了测试,结果都是一样的。

Thanks for any information or suggestions about this!感谢您提供有关此的任何信息或建议!

<!DOCTYPE html>
<html>
<head>
<title>Tone Timing Tester</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.32/Tone.min.js"></script>
</head>

<body>
<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>
<form>
    <input id="bpm" type="number" value="120">
    <button type="button" onclick="submitBPM()">Enter BPM</button>
</form>

<script type="text/javascript">
    
    var synth = new Tone.Synth().toDestination()

    Tone.Transport.scheduleRepeat(function(time){
        console.log('time', time);
        synth.triggerAttackRelease('C4', '8n')
    }, "8n");

    async function start() {
        await Tone.start()
        Tone.Transport.start();
    }

    function stop() {
        Tone.Transport.stop();
    }

    function submitBPM() {
        var bpm = document.getElementById('bpm').value;
        Tone.Transport.bpm.value = bpm;
    }

</script>
</body>
</html>

For a scheduled triggerAttackRelease , you should pass the time value as the third argument.对于预定的triggerAttackRelease ,您应该将time值作为第三个参数传递。

Tone.Transport.scheduleRepeat(function(time){
    console.log('time', time);
    synth.triggerAttackRelease('C4', '8n', time);
}, "8n");

Here's a codepen that contains the working code.这是一个包含工作代码的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM