# How to loop sound generated within a p5 envelope

**URL:** <https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889>\
**Category:** Libraries\
**Created:** [November 27, 2019, 5:58pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889 "2019-11-27T17:58:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjs](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@gjs](https://discourse.processing.org/u/gjs)\
**Post date:** [November 27, 2019, 5:58pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/1 "2019-11-27T17:58:13Z")

</div>

In p5, I’m trying to loop sound generated within a p5 envelope in response to a mousePressed() function. Multiple attempts at inserting it within the following code, copied from [p5 envelope reference](https://p5js.org/reference/#/p5.Envelope) and pasted below, have not worked. I’ve also noticed that loop() does not appear in the [p5 sound library reference page](https://p5js.org/reference/#/libraries/p5.sound) under p5.Envelope. Thoughts and suggestions would be most appreciated. Thanks in advance.

```auto
et attackLevel = 1.0;
let releaseLevel = 0;
let attackTime = 0.001;
let decayTime = 0.2;
let susPercent = 0.2;
let releaseTime = 0.5;
let env, triOsc;
t = 0;

function setup() {
    let cnv = createCanvas(100, 100);
    textAlign(CENTER);
    text('click to play', width / 2, height / 2);
    env = new p5.Envelope();
    env.setADSR(attackTime, decayTime, susPercent, releaseTime);
    env.setRange(attackLevel, releaseLevel);
    triOsc = new p5.Oscillator('triangle');
    triOsc.amp(env);
    triOsc.start();
    triOsc.freq(220);
    cnv.mousePressed(playEnv);
}

function playEnv() {
    env.play();
}

```

---

<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:** [November 27, 2019, 10:52pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/2 "2019-11-27T22:52:28Z")

</div>

here: win 10 // firefox browser //  
in online editor  
the example is working ( on mouse click hear a sound )

regarding you code, repair:

```auto
line 1 "et" > "let"
line 8 "t=0; > "let t = 0;"

```

and it works also.

---

<div class="post-metadata">

**Author:** ![gjs](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@gjs](https://discourse.processing.org/u/gjs)\
**Post date:** [November 28, 2019, 4:58pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/3 "2019-11-28T16:58:55Z")

</div>

Thank you for your comments kll. Please allow me to better explain what I am trying to do, that is, to repeat the [oscillator-generated/envelope-packaged sound](https://p5js.org/reference/#/p5.Envelope) multiple times after a single click. I tried inserting loop() and that failed (see commented out section). I tried writing a short loop with a counter and all that did was cap the number of click-generated sounds. I believe that the [loop() function](https://p5js.org/reference/#/p5.SoundFile/loop) works with a loaded sound file but am hoping to get the same result with sound created by the oscillator. Once again, grateful for the suggestions from you and the community.

```auto
let attackLevel = 1.0;
let releaseLevel = 0;
let attackTime = 0.001;
let decayTime = 0.2;
let susPercent = 0.2;
let releaseTime = 0.5;

let env, triOsc;
let t = 0;

function setup() {
  let cnv = createCanvas(100, 100);

  textAlign(CENTER);
  text('click to play', width / 2, height / 2);

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

  triOsc = new p5.Oscillator('triangle');
  triOsc.amp(env);
  triOsc.start();
  triOsc.freq(220);

  cnv.mousePressed(playEnv);
}

/*
function playEnv() {
    env.play(triOsc);
    loop()
}
*/

function playEnv() {
  if (t < 3) {
    env.play(triOsc);
    t = t + 1;
    print(t);
  }
}

```

```auto

```

---

<div class="post-metadata">

**Author:** ![gjs](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@gjs](https://discourse.processing.org/u/gjs)\
**Post date:** [December 1, 2019, 10:21pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/4 "2019-12-01T22:21:06Z")

</div>

After searching for cases similar to mine, I can clarify that what I’m hoping to achieve is to loop a Mouse event. Look forward to receiving your suggestions. Thank you.

---

<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:** [December 1, 2019, 10:48pm UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/5 "2019-12-01T22:48:36Z")

</div>

i try your above code:  
i can click 3 times with mouse and get sound

> [@gjs](#):
>
> I can clarify that what I’m hoping to achieve is to loop a Mouse event

sorry, not understand

if you need something like to repeatedly play that envelope  
( but not via sound tools )  
just by draw / loop need some timer code?

> **[p5.js Web Editor](https://editor.p5js.org/kll/sketches/e06SRjrK2)**
>
> A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators, and beginners.

---

<div class="post-metadata">

**Author:** ![gjs](https://avatars.discourse-cdn.com/v4/letter/g/eada6e/32.png) [@gjs](https://discourse.processing.org/u/gjs)\
**Post date:** [December 3, 2019, 1:32am UTC](https://discourse.processing.org/t/how-to-loop-sound-generated-within-a-p5-envelope/15889/6 "2019-12-03T01:32:58Z")

</div>

Thanks kll!!! Your insertion of a timer function solved my issue. I placed it within the mouse event and it now plays multiple times following a single click. And I added a second loop within the timer function to cap the total number of plays. I’m just starting to learn OOP and am not yet up to speed on how it could have been accomplished using sound tools. At your convenience, I would appreciate your pointing me in that direction. Thanks again.
