Web Audio启动和停止振荡器然后再次启动它 [英] Web Audio start and stop oscillator then start it again

查看:70
本文介绍了Web Audio启动和停止振荡器然后再次启动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始和停止声音。这很有效。但是我无法重新开始播放声音。

I am trying to start and stop a sound. And that works. But I can't start the sound up again.

我是否真的需要重新制作另一个振荡器?这看起来非常不直观。必须有一个更好的方法。

Do i really have to make another oscillator again? This just seems extremely un-intuitive. There must be a better way.

这就是我的全部工作:

oscillator1.noteOn(0);
oscillator1.noteOff(0);

再次拨打noteOn并没有做任何事情。为什么?超出我的范围。

Calling noteOn again doesnt do anything. Why? Is beyond me.

我也尝试设置音量,或者在Web音频人物的上下文中,增益,等于零。但由于某种原因,增益为零会产生声音。什么样的增益值不会发出任何声音?

I also tried setting the volume, or in the context of the Web Audio people, "gain", equal to zero. But for some reason, a gain of zero makes sound. What value of gain would not make any sound?

男人,我无法相信这有多难:/

man, i can't believe how difficult this is :/

推荐答案

实际上,是的,你必须创建一个新的振荡器节点。 API经过精心设计和优化,可以使用该模式。

Actually, yes, you have to create a new oscillator node. The API is designed and optimised to work with that pattern.

道教代码中的断开模式基本上是一种创建新振荡器的复杂方式(每次运行oscOn时都会这样做)。它永远不会在断开连接的旧振荡器上明确调用noteOff,因此它可能仍然在后台运行(不确定Web音频如何处理这个),尽管由于它与音频链断开连接而无法听到。所以它可能会在运行和耗尽CPU的后台堆叠振荡器。

The disconnect pattern in Taoist's code is basically a convoluted way of creating a new oscillator (it does every time oscOn is run). It never explicitly calls noteOff on the old oscillator that is disconnected, so it might still be running in the background (not sure how web audio handles this) though it's not audible since it's disconnected from the audio chain. So it could potentially stack oscillators in the background that are running and draining CPU.

这是相同的代码,尽管正确使用了noteOff()。 http://codepen.io/Theodeus/pen/afgqk

这是相同的代码,虽然使用增益节点进行调整以控制振荡器,因此始终只使用相同的振荡器(尽管不建议这样做,最好创建一个我认为每个音符的新振荡器) http://codepen.io/Theodeus/pen/aKFje

编辑2015年4月

由于代码示例似乎在网络空间丢失,这里是一个我在振荡器上写了一篇教程,其中包含代码示例,展示了振荡器的一次性特性。它与上面引用的代码不完全相同,但它显示了相同的概念。 http://codepen.io/Theodeus/blog/web -audio-synth-part-1-generating-sound - 它的要点如下:

Edit April 2015
Since the code samples seems to be lost in cyberspace, here's a tutorial I wrote on oscillators that contains code examples that shows the one-shot nature of oscillators. It's not exactly the same code as the one referenced above, but it shows the same concept. http://codepen.io/Theodeus/blog/web-audio-synth-part-1-generating-sound - the gist of it is this:

//This won't work. Can't call play twice.
var context = new AudioContext(),
    oscillator = context.createOscillator();

oscillator.connect(context.destination);
oscillator.start(context.currentTime);
oscillator.stop(context.currentTime + 0.5);
oscillator.start(context.currentTime + 1);
oscillator.stop(context.currentTime + 1.5);


//this will work!
var context = new AudioContext(),
    oscillator;

function playOscillator(startTime, endTime) {
    oscillator = context.createOscillator();
    oscillator.connect(context.destination);
    oscillator.start(startTime);
    oscillator.stop(endTime);
}

playOscillator(context.currentTime, context.currentTime + 0.5);
playOscillator(context.currentTime + 1, context.currentTime + 1.5);

这篇关于Web Audio启动和停止振荡器然后再次启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆