How to loop sound generated within a p5 envelope

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 and pasted below, have not worked. I’ve also noticed that loop() does not appear in the p5 sound library reference page under p5.Envelope. Thoughts and suggestions would be most appreciated. Thanks in advance.

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();
}
1 Like

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

regarding you code, repair:

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

and it works also.

1 Like

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 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 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.

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);
  }
}
1 Like

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.

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

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?
https://editor.p5js.org/kll/sketches/e06SRjrK2

1 Like

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.

2 Likes