简体   繁体   English

使用WebAudio API,如何调制信号?

[英]Using the WebAudio API, how to modulate a signal?

Using the Web Audio API, I want to use one signal to modulate another. 使用Web Audio API,我想使用一个信号来调制另一个。 I find the modulation does not behave as expected. 我发现调制的行为并不像预期的那样。 The example code below creates two oscillators, one at 440Hz and one at 1Hz, and uses the 1Hz one to modulate the gain of the 440Hz, via the gain AudioParam of a gain node. 下面的示例代码创建了两个振荡器,一个在440Hz,一个在1Hz,并使用1Hz的振荡器通过增益节点的增益AudioParam调制440Hz的增益。

Expected behaviour: The oscillators output values from -1 to +1. 预期行为:振荡器输出值从-1到+1。 Therefore, the gain should oscillate between -1 and +1, once per second, ie the gain should be zero (no sound) twice per second. 因此,增益应在-1和+1之间振荡,每秒一次,即增益应为每秒两次(无声)。

Actual behaviour: Instead, it is only silent once per second. 实际行为:相反,它每秒只沉默一次。 It seems that the gain values are being remapped, so that the range (-1, +1) is mapped to (0, +1). 似乎重新映射了增益值,因此范围(-1,+ 1)被映射到(0,+ 1)。

Questions: Why is this remapping happening? 问题:为什么要重新映射? It doesn't happen if I set the gain to single floating point value -- there, +1 means +1 and 0 means 0. Can I turn this remapping off, so that a zero in the modulating signal results in a zero amplitude sound? 如果我将增益设置为单个浮点值,则不会发生 - 其中,+ 1表示+1,0表示0.我可以关闭此重映射,以便调制信号中的零点会产生零幅度声音?

Edit: To clarify, my aim is to use an arbitrary envelope, E, whose value varies between 0 and 1, to modulate the amplitude of a carrier, C, so that the output is C*E, such that C*E has amplitude=0 when E=0, and C*E has amplitude=1 when E=1. 编辑:为了澄清,我的目标是使用任意包络E,其值在0和1之间变化,以调制载波的幅度C,使得输出为C * E,使得C * E具有幅度当E = 0时= 0,并且当E = 1时C * E具有幅度= 1。

window.AudioContext = ( window.AudioContext || window.webkitAudioContext );
var context = new AudioContext();

var gain = context.createGain();
gain.connect(context.destination);

var osc = context.createOscillator();
osc.connect(gain);
osc.start(0);

var mod = context.createOscillator();
mod.frequency.value = 1;
mod.connect(gain.gain);
mod.start(0);

It's not a remapping at all. 它根本不是重新映射。 What is happening is that the gain.value defaults to 1, and the audio-rate signal connected to the gain is being SUMMED with that default value - so the signal is oscillating between (1 - 1) and (1 + 1): that is, between 0 and 2. 发生的事情是gain.value默认为1,连接到增益的音频信号正在使用该默认值进行SUMMED - 因此信号在(1 - 1)和(1 + 1)之间振荡:是,介于0和2之间。

Just set gain.gain.value=0; 只需设置gain.gain.value = 0; in your example above, and it should work as you expect. 在上面的示例中,它应该按预期工作。

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

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