【问题标题】:Web Audio Oscillator unable to create new oscillatorWeb Audio Oscillator 无法创建新的振荡器
【发布时间】:2014-06-21 19:01:58
【问题描述】:

在下面的代码中,我希望在每次按下按钮元素时创建一个分配给 o 的新振荡器节点,允许多次按下,从而在 440 处产生多个 1 秒的音调。但是我发现按钮只能按下一次以产生音调,之后它就死了,好像我没有创建新的振荡器节点一样。我正在引导使用 JavaScript,我怀疑它一定是微不足道的。有人有想法吗?

<html>
<body>
<button id='but' label='Button'/>
<script>
   function secondContext(){
     play([440],0,1);
   }

    function play(freqs, delay, duration) {
        freqs.forEach(function(freq){
                o = audio_context.createOscillator();
                o.frequency.value = freq;
                o.connect(audio_context.destination);
                o.start(0);
                o.stop(1);
        });
    }
    var audio_context = new (AudioContext || webkitAudioContext);
    button_ele = document.getElementById('but')
    button_ele.addEventListener('click',function(){secondContext()});
</script>
</body>
</html>

编辑:

刚刚找到解决问题的this question。解决方案是将 audio_context.currentTime() 添加到偏移量,如下所示:

o.start(audio_context.currentTime + 0);
o.stop(audio_context.currentTime + 1);

【问题讨论】:

    标签: javascript web-audio-api


    【解决方案1】:

    正如您所提到的,start()stop() 方法的第一个参数是基于audioContext.currentTime 维护的时钟的时间戳。

    如果时间戳小于当前时间,则立即执行 start/stop 方法。否则,当时钟与时间戳相同时,将执行方法(分别启动或停止振荡器)。

    when 参数描述声音应该在什么时间(以秒为单位)开始播放。它与 AudioContext 的 currentTime 属性在同一时间坐标系中。如果为该值传入 0 或该值小于 currentTime,则声音将立即开始播放。 start 只能被调用一次,并且必须在 stop 之前调用......

    http://webaudio.github.io/web-audio-api/#widl-AudioBufferSourceNode-start-void-double-when-double-offset-double-duration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-29
      • 2020-07-12
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多