Polyphonic Web Audio API - Using Two Different Amplification Nodes Doesn't Work?

I cannot create two oscillators with independent amplification factors.

In the code below, two buttons are created, each of which reproduces a sinus tone with a different step. When I press the first button, I hear how the tone grows in volume, as it should. But, when I press the second button, the tone reacts as if it is connected to the amplification of the first tone. For example, if I press the second button (turning on the second tone) while the first tone is at volume 1, the second tone will go into volume 1, although it should go around from 0 to 1 to 0 at the rate of 10 seconds.

Can I get only one node gain per audio context? Or is there some other reason that the benefits of these generators are related? Also, after I play the tones once, I cannot play them again, which makes me especially think that I am doing something wrong. :)

Thanks. Below is the link, and the code is below. This is my first post, so let me know if you need anything else. This code must be running on versions of Chrome or Safari that support web audio api.

http://whitechord.org/just_mod/poly_test.html

WAAPI Tests

<button onclick="play()">play one</button> <button onclick="play2()">play two</button> <script> var context; window.addEventListener('load', initAudio, false); function initAudio() { try { context = new webkitAudioContext(); } catch(e) { onError(e); } } function play() { var oscillator = context.createOscillator(); var gainNode = context.createGainNode(); gainNode.gain.value = 0.0; oscillator.connect(gainNode); gainNode.connect(context.destination); oscillator.frequency.value = 700; gainNode.gain.linearRampToValueAtTime(0.0, 0); // envelope gainNode.gain.linearRampToValueAtTime(0.1, 5); // envelope gainNode.gain.linearRampToValueAtTime(0.0, 10); // envelope oscillator.noteOn(0); } function play2() { var oscillator2 = context.createOscillator(); var gainNode2 = context.createGainNode(); gainNode2.gain.value = 0.0; oscillator2.connect(gainNode2); gainNode2.connect(context.destination); oscillator2.frequency.value = 400; gainNode2.gain.linearRampToValueAtTime(0.0, 0); // envelope gainNode2.gain.linearRampToValueAtTime(0.1, 5); // envelope gainNode2.gain.linearRampToValueAtTime(0.0, 10); // envelope oscillator2.noteOn(0); } /* error */ function onError(e) { alert(e); } </script> </body> </html> 
+4
source share
2 answers

Can I get only one node gain per audio context? Or is there some other reason that the benefits of these generators are related? Also, after I play the tones once, I cannot play them again, which makes me especially think that I am doing something wrong. :)

You can have as many gain nodes as you want (for example, you could achieve mixing bus settings) so that there is no problem. Your problem is this:

Remember that the second parameter linearRampToValueAtTime () is the time in the same time coordinate system as your context.currentTime.

And your context.currentTime always moves forward in real time, so all your ramps, curves, etc. must be calculated relative to it .

If you want something to happen after 4 seconds, you must pass context.currentTime + 4 to the web audio API function.

So, modify all your linearRampToValueAtTime () calls in your code so that they look like this:

 gainNode2.gain.linearRampToValueAtTime(0.0, context.currentTime); // envelope gainNode2.gain.linearRampToValueAtTime(0.1, context.currentTime + 5); // envelope gainNode2.gain.linearRampToValueAtTime(0.0, context.currentTime + 10); // envelope 

And that should take care of your problems.

By the way, you have a double quote in your BODY markup tag.

+4
source

Ask Conklin

 gainNode2.gain.linearRampToValueAtTime(0.1, context.currentTime + 5); // envelope 
0
source

Source: https://habr.com/ru/post/1432128/


All Articles