p5.js-sound icon indicating copy to clipboard operation
p5.js-sound copied to clipboard

PolySynth noteADSR error

Open balandinodidonato opened this issue 3 years ago • 2 comments

I'm trying to apply an envelope to a note generate using p5.PolySynth, when clicking on the canvas. I'm using the play() to play the note and noteADSR() to set the envelope (see code below).

I get the following error when calling playSynth(): Uncaught TypeError: this.notes[note] is undefined

Am I missing something? The documentation says that the envelope should be reset before setting a new one, but I'm not sure how to do it. Is that the issue?

Code

let synth;

function setup(){
  let cnv = createCanvas(400, 400);
  cnv.mousePressed(playSynth);
  synth = new p5.PolySynth();
}

function draw(){
  background('black');
}

function playSynth() {
  userStartAudio();
  let note1 = random(0, 127); // generates a random note.
  let dur = 1.5; // note duration (in seconds)
  let time = 0; // time from now (in seconds)
  let vel = 0.1; // velocity (volume, from 0 to 1)

  synth.play(note1, vel);
  synth.noteADSR(note1, 0.02, 0.1, 0.5, 10);
}

balandinodidonato avatar Mar 01 '21 10:03 balandinodidonato

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

welcome[bot] avatar Mar 01 '21 10:03 welcome[bot]

@balandinodidonato noteADSR only works when the current note is playing. So you should code like this

synth.noteAttack()
synth.noteADSR()
synth.noteRelease()

and hn currently use the secondFromNow param to get desired output.

Ajaya1000 avatar Apr 19 '21 04:04 Ajaya1000