# Loop bug in applying envelope function

**URL:** https://discourse.processing.org/t/loop-bug-in-applying-envelope-function/10077
**Category:** Libraries
**Created:** [April 8, 2019, 10:39am UTC](https://discourse.processing.org/t/loop-bug-in-applying-envelope-function/10077 "2019-04-08T10:39:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![l3ehfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l3ehfar/32/4074_2.png) [@l3ehfar](https://discourse.processing.org/u/l3ehfar)
#### Post date: [April 8, 2019, 10:39am UTC](https://discourse.processing.org/t/loop-bug-in-applying-envelope-function/10077/1 "2019-04-08T10:39:09Z")

</div>

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.

```auto
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](https://editor.p5js.org/l3ehfar/sketches/JFf1Dxf2K)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [April 8, 2019, 12:27pm UTC](https://discourse.processing.org/t/loop-bug-in-applying-envelope-function/10077/2 "2019-04-08T12:27:39Z")

</div>

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

-b- i try like

```auto
//...
      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 );

---

<div class="post-metadata">

### Author: ![l3ehfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l3ehfar/32/4074_2.png) [@l3ehfar](https://discourse.processing.org/u/l3ehfar)
#### Post date: [April 8, 2019, 1:28pm UTC](https://discourse.processing.org/t/loop-bug-in-applying-envelope-function/10077/3 "2019-04-08T13:28:23Z")

</div>

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