p5.js-sound
p5.js-sound copied to clipboard
FFT.setInput() doesn't disconnect old input
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();
}
@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