Hi there I am a veteran audio engineer and novice programmer, it is humbling to understand audio so well and yet be so mystified by coding it…
Anyway I was just playing around with the setVolume function and to my chagrin was getting very unexpected results when using the optional parameters.
My assumption: [rampTime] smooths between the previous volume and the new volume. This did not happen.
Assumption 2: [timeFromNow] delays said smoothing for a certain time ie begin fading out after 1 second. This did not happen either.
I created a little example showing some of the results I was getting and printed volume to the console to confirm my results. I would really like to better understand why this is happening, and how to use these parameters to get the results I was expecting,
let sound;
var fadeType = 1;
function preload() {
sound = loadSound("Sounds/sound1.wav");
}
function setup() {
createCanvas(200, 200);
}
function draw() {
clear();
print(sound.getVolume());
}
function mousePressed() {
switch(fadeType){
case 0:
//Nothing happens at all... this should fade in?
sound.setVolume(0);
sound.setVolume(1, 0.3);
sound.play();
break;
case 1:
//Sound fades in over 0.3 seconds, behaves as rampTime should?
//Also gets very wacky when sound triggered multiple times close together
sound.setVolume(0);
sound.setVolume(1, 0, 0.3);
sound.play();
break;
case 2:
//First time sound is played it gets cut off after 0.3 sec
//every time after that it fades nicely out over 0.3 sec?!
sound.setVolume(1);
sound.setVolume(0, 0, 0.3);
sound.play();
break;
case 3:
//Sound fades out over .3 sec, vol stays at 0 and cant be re-triggered?
sound.setVolume(1);
sound.setVolume(0, .3);
sound.play();
break;
}
}