Skip to content
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

Merged
merged 5 commits into from
Dec 6, 2022
Merged

Updated aplitude.disconnect() method documentation #686

merged 5 commits into from
Dec 6, 2022

Conversation

Nitesh639
Copy link
Contributor

Updated documentation of amplitude.disconnect() method with an example.

@davepagurek
Copy link
Contributor

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 amplitude.disconnect to actually do anything. I ended up arriving at this modified version of your code (comments around the modified parts):

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 connect and disconnect on the Amplitude class are not meant to be publicly used? I noticed this line in the source code which might indicate that it's only there to fix a bug: https://github.com/processing/p5.js-sound/blob/main/src/amplitude.js#L97 Unfortunately I'm not familiar enough with the inner workings of this library to know for sure, @therewasaguy maybe you'll have some more insight?

@Nitesh639
Copy link
Contributor Author

@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.

@Nitesh639
Copy link
Contributor Author

  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();

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.

@davepagurek davepagurek merged commit 128fa86 into processing:main Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants