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

FFT.setInput() doesn't disconnect old input

Open jbakse opened this issue 4 years ago • 1 comments

Hello All,

I was just confused a bit by the behavior of FFT setInput().

I expected that setInput() would CHANGE what the FFT listened to, removing the previous source and setting a new source.

Instead, it appears that setInput() ADDs a source, after which FFT listens to both of them.

If you send an argument, it will disconnect from the global output, but will still be connected to other sources.

I was surprised by this, as I'm used to set*() functions changing rather than adding. I think the code is working as intended though, it appears that other p5.sound objects work in similar ways.

So maybe a note in the documentation could be helpful?

existing

Set the input source for the FFT analysis. If no source is provided, FFT will analyze all sound in the sketch.

maybe add

When a source is provided it will be connected to the FFT for analysis and the global output will be disconnected. Other inputs will remain connected.

In my case, I was able to work around it by calling .disconnect() on the old input.

FFT.setInput source for reference.

  setInput(source) {
    if (!source) {
      p5sound.fftMeter.connect(this.analyser);
    } else {
      if (source.output) {
        source.output.connect(this.analyser);
      } else if (source.connect) {
        source.connect(this.analyser);
      }
      p5sound.fftMeter.disconnect();
    }

jbakse avatar Apr 23 '21 18:04 jbakse

@therewasaguy i think if we intent to listen to all the sources then we should rename setInput to addInput ? or if we intent to just listen to the currently set Input, we must disconnect other existing input too when setInput is called for a new input as suggested by @jbakse

endurance21 avatar May 04 '21 04:05 endurance21