0

I am currently busy with an assignment (using P5.JS) that involves using the webcam as user input to trigger sounds. This works by creating a grid across the camera input and using color differencing to detect whether there is movement in a specific grid block. If there is movement, the grid block is "activated", which triggers something visual on screen. I have linked each grid block to a monosynth note, and when the grid block is activated, the corresponding monosynth note plays. However, when I run it, I get a scratchy and unpleasant sound, which I assume is because monosynth is trying to play many different notes simultaneously. Is there a way I can limit it only to playing one note at a time?

https://p5js.org/reference/#/p5.MonoSynth

In the above link, I see there is a "duration" argument for the .play method function. Is there a way that I can wait for that duration to finish before allowing it to play another note? Or perhaps a method function that checks whether the Monosynth is currently playing something or not, and then use that in an if statement before using the .play method.

Any advice would be appreciated.

PS: for plagiarism reasons, I can't share any of my code online.

  • Not _any_ of your code? Not even a minimal example that exhibits how to get a "scratchy and unpleasant sound" out of the monosynth? It's hard to help otherwise. – AKX Aug 30 '22 at 06:35

1 Answers1

0

The MonoSynth example on the documentation page does not exhibit a "scratchy and unpleasant sound" even if you hit the tap to play button as quickly as you possibly can.

Since the monosynth literally is a single oscillator and a single envelope, there is no way it could play multiple sounds at once (and start clipping).

Since we can't see your code, I have to rely on my crystal ball with this guess: Are you possibly instantiating a new monosynth whenever you start playing a note instead of telling a single monosynth to change up its pitch? That would explain it.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • You are 100% right. I do apologize for not being able to display my code, but I fear that I may be penalized for doing that. The layout of the code is as follows: a grid object, containing an array of "note objects". Each note has its over monosynth note, and activated flag, which gets decremented as time passes, so its activated for about 3 seconds total. The grid object iterates through the notes, and if the note is activated, it plays the synth. The problem was that while the note was activated, the same note would be triggered continuously until the note was inactive. – Keean Ferreira Aug 30 '22 at 07:34
  • Thus, as a solution, I created a flag variable in the Note class, so that once the sound was played, the flag would be turned to false, preventing the sound from being played more than once per activation. I hope all of that makes sense. But thank you for taking the time to read and respond. – Keean Ferreira Aug 30 '22 at 07:38
  • Right – if you don't want multiple notes to play at once, then use a single monosynth object. If you want polyphony and don't want them to clip, lower the amplitude of each monosynth object. – AKX Aug 30 '22 at 07:39
  • Optionally, you could use a single [`PolySynth`](https://p5js.org/reference/#/p5.PolySynth) (though you do still need to keep track of whether to "retrigger" a region). – AKX Aug 30 '22 at 07:42