setVolume: rampTime and timeFromNow Mystery?

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() {

function mousePressed() {

        case 0:
            //Nothing happens at all... this should fade in?
            sound.setVolume(1, 0.3);

        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(1, 0, 0.3);

        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(0, 0, 0.3);

        case 3:
            //Sound fades out over .3 sec, vol stays at 0 and cant be re-triggered?
            sound.setVolume(0, .3);


1 Like

If i‘m not mistaken, then (at least in the second case(case 1)) it seems like setting the TimeFromNow variable and afterwards only calling the setVolume method with Volume, the TimeFromNow from the last method call is still saved for some reason…

So after setVolume(1,0,0.3) is called, then calling setVolume(0.5) becomes the same as setVolume(0.5,0,0.3)… maybe try only using all 3 variables, to avoid this.

Also, i don‘t know why you always call .setVolume() with a Single variable first, but maybe that also cause some of the weird behaviour… although i‘m not that good with p5.js, so i might be wrong here.

Thanks for the input! The reason I only had the one variable is because the other 2 are optional. I tried as you suggested adding all 3 arguments to every call on setVolume but unfortunately I get the exact same results as before…

i just play on SONG and OSCILLATOR

function keyPressed() {
   if ( key == '+' )  song.setVolume(0.9, 5, 3); // after 3 sec ramp for 5 sec
   if ( key == '-' )  song.setVolume(0.1, 5, 3);
//   if ( key == '+' )  osc.amp(0.9, 5, 3); // after 3 sec ramp for 5 sec
//   if ( key == '-' )  osc.amp(0.1, 5, 3);

play online editor ( see version )

   <script src=""></script>

and both seem to work

i do this while i was in .loop() play the sound,

set ?start? fade 0.3 sec
and then start to play
sound like a bad attempt to me,
you know if the fade (timer) starts

  • after OS start to play the song or
  • directly after the fade command??
1 Like

I see what you mean, it seems like that maybe the whole intention is for this to be used to smooth changes in volume while a source is playing and not for fade in/ out the beginning and end of a file… I suppose that’s what the envelope ADSR is for!

1 Like