-
-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated aplitude.disconnect() method documentation #686
Conversation
Hi, sorry for the delay! I was testing this out some more because, ideally, we want examples to show a clear cause and effect of the method being applied, and I was having trouble getting let sound, amplitude, fft, button;
function preload(){
sound = loadSound('assets/beat.mp3');
}
function setup() {
let cnv = createCanvas(100,100);
cnv.mouseClicked(togglePlay);
amplitude = new p5.Amplitude();
button = createButton("tap to disconnect amp");
button.mouseClicked(amp_disconnect);
button.position(0,110);
fft = new p5.FFT();
// By default the gain value is 0, so without this, anything
// connecting it to anything doesn't do anything. Unfortunately
// this also causes us to hear the source sound twice.
amplitude.output.gain.value = 1;
// It doesn't seem to work if I use `amplitude.connect(fft) here,
// I suspect because the fft is already connected to p5.sound's
// master output
fft.setInput(amplitude);
sound.loop();
}
function draw() {
background(220);
// Drawing the FFT spectrum, copy-and-pasted from the FFT
// example, to make it clear that it's actually getting connected
// and disconnected
let spectrum = fft.analyze();
push();
noStroke();
fill(255, 0, 255);
for (let i = 0; i< spectrum.length; i++){
let x = map(i, 0, spectrum.length, 0, width);
let h = -height + map(spectrum[i], 0, 255, height, 0);
rect(x, height, width / spectrum.length, h )
}
pop();
text('tap to play', 20, 20);
let level = amplitude.getLevel();
let size = map(level, 0, 1, 0, 200);
ellipse(width/2, height/2, size, size);
}
function togglePlay() {
if (sound.isPlaying()){
sound.pause();
} else {
sound.play();
}
}
function amp_disconnect(){
if (button.html()=="tap to disconnect amp"){
amplitude.disconnect();
button.html("tap to connect fft");
} else{
amplitude.connect(fft);
button.html("tap to disconnect amp");
}
} Now the FFT connects and disconnects, but the doubled sound output isn't great. I wonder if this means |
@davepagurek @therewasaguy, I think connect, disconnect and dispose method is available almost all the classes of p5 sound. But it's not explained anywhere properly. |
Yes @davepagurek, you are right, that's the default setting, but once you disconnect manually, then you are unable to connect it. For this, we need to use amplitude.connect(fft) method. |
Updated documentation of amplitude.disconnect() method with an example.