1

The p5.js sound library documentation says that removeCue() can be used to cancel cued events. It says it takes an ID input that is returned from addCue().

When I invoke addCue and store the result to a variable it does not return an ID. It returns NaN.

The image below is a code example I wrote using the p5.js code editor.

How do I get the id ?

enter image description here

William
  • 4,422
  • 17
  • 55
  • 108

1 Answers1

0

OK, i found the issue.

Its an issue with the library https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js

look at this link https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2178

its using var id = this._cueIDCounter++; but _cueIDCounter was never defined.

so i tried to define it like the following for your code:

Object.defineProperty(mySound,'_cueIDCounter',{value:1,writable:true});

now it returned the id.

so then i tried to remove the cue with removeCue but to my surprise there is also an issue which is getting error Uncaught TypeError: Cannot read property 'splice' of undefined

so then i looked again at the library code and i realised on the following line https://github.com/processing/p5.js/blob/master/lib/addons/p5.sound.js#L2198 within the removeCue function there is an error the current code is

p5.SoundFile.prototype.removeCue = function (id) {
    var cueLength = this._cues.length;
    for (var i = 0; i < cueLength; i++) {
      var cue = this._cues[i];
      if (cue.id === id) {
        this.cues.splice(i, 1);
      }
    }
    if (this._cues.length === 0) {
    }
};

but it should be using this._cues.splice(i, 1); instead of this.cues.splice(i, 1);

Inus Saha
  • 1,918
  • 11
  • 17