# Microphone input threshold p5.js

**URL:** <https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096>\
**Category:** Libraries\
**Created:** [May 20, 2020, 2:14pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096 "2020-05-20T14:14:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rose1](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rose1](https://discourse.processing.org/u/rose1)\
**Post date:** [May 20, 2020, 2:14pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/1 "2020-05-20T14:14:55Z")

</div>

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](https://p5js.org/examples/sound-mic-threshold.html)

Kind regards

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [May 27, 2020, 5:48pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/2 "2020-05-27T17:48:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rose1](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rose1](https://discourse.processing.org/u/rose1)\
**Post date:** [May 27, 2020, 6:13pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/3 "2020-05-27T18:13:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [May 27, 2020, 8:17pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/4 "2020-05-27T20:17:42Z")

</div>

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

```javascript
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 `0`–`0.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.

---

<div class="post-metadata">

**Author:** ![rose1](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rose1](https://discourse.processing.org/u/rose1)\
**Post date:** [May 27, 2020, 9:34pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/5 "2020-05-27T21:34:16Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [May 27, 2020, 10:00pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/6 "2020-05-27T22:00:33Z")

</div>

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

> [@Sven](#):
>
> if `volume` is less than `YOUR_THRESHOLD_VALUE` set `volume` to `0`

---

<div class="post-metadata">

**Author:** ![rose1](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rose1](https://discourse.processing.org/u/rose1)\
**Post date:** [May 27, 2020, 10:30pm UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/7 "2020-05-27T22:30:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [May 28, 2020, 6:36am UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/8 "2020-05-28T06:36:16Z")

</div>

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

```javascript
const THRESHOLD_VALUE = 0.1;

let volume = mic.getLevel();

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

```

---

<div class="post-metadata">

**Author:** ![rose1](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rose1](https://discourse.processing.org/u/rose1)\
**Post date:** [May 28, 2020, 11:54am UTC](https://discourse.processing.org/t/microphone-input-threshold-p5-js/21096/9 "2020-05-28T11:54:51Z")

</div>

Great! Thank you so much! 😉
