Microphone input threshold p5.js

Hi! How can I put a threshold on microphone input signal and modify the oscillator’s amplitude (with this new range)? I use “p5.Oscillator” to make sound and “p5.AudioIn()” to catch the microphone signal.
This example doesn’t solve my question: https://p5js.org/examples/sound-mic-threshold.html

Kind regards

1 Like

Hello and welcome @rose1,

Audio is not my expertise, but maybe I can help anyway if you share your code and tell me where you got stuck.

1 Like

Hello @Sven ! Thanks for the help.
I need to put a threshold on microphone input signal (like a noise gate) and modify the oscillator’s amplitude (with this new range).

Kind regards!

1 Like

Thanks! It’s looks like you’re pretty close to me.

var volume = mic.getLevel(0.005) * 0.5; // tiempo que tarda en 
// [...]
let amp1 = map(volume, 0, 1, 0, 0.8);
// [...]
sonido1.amp(amp1);

The code sets the variable volume by calling mic.getLevel() using smoothing value of 0.005 and multiplying the result with 0.5. That will return a value in the range 00.5. Is that what you want? If so, you should update the map call to reflect that: map(volume, 0, 0.5, 0, 0.8).

If you want to apply a noise gate/threshold on the signal, the logic should be like if volume is less than YOUR_THRESHOLD_VALUE set volume to 0. Now, how to implement that in code? The example you’re linking to shows that, in the first few lines of the draw function.

1 Like

Hi! Thanks!
The code is designed to run on a mobile device. Therefore, I need to apply the noise gate/threshold to get the signal only above a threshold, because mobile devices microphones are too sensitive. The problem of the example in the first post is that I don’t know how to put the amplitude of the oscillator inside of the conditional:

 if (volume > threshold) {
    stroke(0);
    fill(0, 100);
    rect(random(40, width), random(height), volume * 50, volume * 50);
  }
1 Like

Okay, if we leave that example for a while. How would you implement the logic below in JavaScript?

1 Like

Sorry, I have no idea. I’m a beginner :hugs: Thanks!

We were all beginners once. Here’s one implementation:

const THRESHOLD_VALUE = 0.1;

let volume = mic.getLevel();

if (volume < THRESHOLD_VALUE) {
  volume = 0;
}
1 Like

Great! Thank you so much! :wink: