rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.9k stars 160 forks source link

setVolume does not effect #38

Closed timotoots closed 9 years ago

timotoots commented 9 years ago

Hello @rserota! Thanks for the great work, I really like the script!

I do have one problem. I try to get setVolume working at later times after loading, but it seems not to have effect. I am using latest Chrome. Here is a demo, bell.setVolume(0.5) works, but the one inside of the setTimeout does not. Also it does not work from any other function. Or am I missing anything here?

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="wad/build/wad.js"></script>
</head>
<body>
  <script>

var bell = new Wad({source : 'test.wav', volume : 1})
bell.play()
bell.setVolume(0.5);
setTimeout('bell.setVolume(0.1);console.log("voldown")',1000);

</script>
</body>
</html>
rserota commented 9 years ago

Hey @timotoots, thanks for using wad.js.

According to the Web Audio spec, gain nodes will ignore changes to their value if there is currently automation scheduled. All basic wads have automation scheduled to implement their envelopes, so this would prevent setVolume from changing the volume while a note is playing. I don't think it has anything to do with setTimeout specifically. I'm not sure if the web audio API always worked this way, because I could swear this feature used to work.

You can work around this issue by wrapping the bell in a polywad, because polywads have their own gain nodes, but they have no automation.

var bell = new Wad({source : 'test.wav', volume : 1});
var wrapper = new Wad.Poly();
wrapper.add(bell)
bell.play()
setTimeout('wrapper.setVolume(0.1);console.log("voldown")',1000);
notthetup commented 9 years ago

As per the current implementation of WebAudio (the spec has some upcoming changes about this) if the AudioParam is in 'automation mode' then the setters/getters are disabled. The setter doesn't change anything, and the getter returns a 'last stable value'.

https://github.com/WebAudio/web-audio-api/issues/503

rserota commented 9 years ago

@notthetup Thanks for the link. That adds some clarity to this issue for me.

I don't think I'll be able to find a better solution to this issue than the workaround I posted above, due to how web audio handles automation. In that case, I will close this issue.

timotoots commented 9 years ago

I got it working that way! Thanks for the workaround @rserota and also the extra info @notthetup!