Loop bug in applying envelope function

I’m trying to add an envelope to my samples but it seems according to loop function(I guess) for playing any of sample the previous samples play too.

var song=[];
var doorbell=[];
var amp;
var playBackRate;
var time=0;
EXT='.mp3';
// ADSR params
var attackLevel = 1.0;
var releaseLevel = 0;

var attackTime = 0.001;
var decayTime = 0.2;
var susPercent = 0.2;
var releaseTime = 0.5;

var env;

function preload(){
    for (let i = 1; i < 10; ++i){
    song[i] = loadSound(i+EXT);
    }
}
function setup() {
  var cnv=createCanvas(600, 600);
  amp=new p5.Amplitude();
  
  
  env = new p5.Envelope();
  env.setADSR(attackTime, decayTime, susPercent, releaseTime);
  env.setRange(attackLevel, releaseLevel);


  playBackRate = 1.0;
  doorbell[1] = new Doorbell(width / 8, height / 8);
  doorbell[2] = new Doorbell(width / 8, 500);
  doorbell[3] = new Doorbell(500, height / 8);
  doorbell[4] = new Doorbell(500, 500);
  doorbell[5] = new Doorbell(width / 2, height / 8);
  doorbell[6] = new Doorbell(width / 2, 500);
  doorbell[7] = new Doorbell(width / 2, height / 2);
  doorbell[8] = new Doorbell(width / 8, height / 2);
  doorbell[9] = new Doorbell(500, height / 2);

}

function draw() {
  background(0);
   micLevel = amp.getLevel();
  for(let i=1;i<10;i++){
    doorbell[i].display(mouseX,mouseY);
  }
  granularPlay();
}

class Doorbell {
  constructor(x_, y_) {
    // Location and size
    this.x = x_;
    this.y = y_;
  }

  contains(mx, my) {
    return dist(mx, my, this.x, this.y) < 50;
  }

  display(mx, my) {
    stroke(255);
    strokeWeight(4);
    noFill();
    ellipse(this.x, this.y, 50, 50);
  }
}

function granularPlay(){
  for(let i=1;i<10;i++){
    if (doorbell[i].contains(mouseX, mouseY)) {

      randomStartPos = float(random(0.01, (song[i].duration())-0.001));
  		
  		//play sound

	
    song[i].loop(0.01,playBackRate,0.25,randomStartPos,0.100);
   env.setInput(song[i]);
      env.play();
      
}
	
}
}

https://editor.p5js.org/l3ehfar/sketches/JFf1Dxf2K

-a- from the reference it is clear that they all add up.
https://p5js.org/reference/#/p5.Envelope/setInput

-b- i try like

//...
      reset();
      env.setInput(song[i]);
      env.play();
    }
  }
}

function reset() {
  env = new p5.Envelope();
  env.setADSR(attackTime, decayTime, susPercent, releaseTime);
  env.setRange(attackLevel, releaseLevel);
}

first it looks like it is working,
but then my browser still hangs ( check on memory );

1 Like

It works fine for me, I don’t get what was the problem you’ve mentioned then

1 Like