rserota / wad

Web Audio DAW. Use the Web Audio API for dynamic sound synthesis. It's like jQuery for your ears.
MIT License
1.89k stars 160 forks source link

Any way to detect pitch from microphone input without calling play() ? #82

Closed marcaaron closed 6 years ago

marcaaron commented 6 years ago

Trying to implement a simple tuner, but can't seem to figure out how to simultaneously listen to the user mic for frequencies and disallow the audio from playing back via speakers (e.g. to prevent feedback when users are not using headphones). Is there are way to do this? I see the feedback issue is mentioned in the docs, but no solution. Thoughts?

Update:

setting the tuner.output.gain.value = 0; seems to have solved the feedback issue by muting the gain node, but still allows the audio to be analyzed. might be worth mentioning in the docs (or some better solution) for others looking to use WAD for audio analysis purposes.

rserota commented 6 years ago

Sorry I've been slow to respond.

You've got the right idea. You might prefer to use tuner.setVolume(0), which does exactly the same thing. I added a brief note to the docs that describes this solution.

dmmike commented 3 years ago

@rserota Sorry for the thread-necromancy here, but quick question:

I want to basically achieve this same thing where I don't want the microphone input to play back via speakers, but I would also like to be able to use the audiometer to determine what volume the input would be. I have found ways to do either, but not both.

Basically what I'm trying to make is a 'karaoke' engine where your input is used for the pitch detection, but you can configure what volume of input is minimally required to actually process the data (basically: ignore everything not loud enough to care about).

rserota commented 3 years ago

The example from the docs should mostly meet your needs, I would think.

var tuner = new Wad.Poly();
tuner.setVolume(0); // If you're not using headphones, you can eliminate microphone feedback by muting the output from the tuner.
tuner.add(voice);

voice.play(); // You must give your browser permission to access your microphone before calling play().

tuner.updatePitch() // The tuner is now calculating the pitch and note name of its input 60 times per second. These values are stored in <code>tuner.pitch</code> and <code>tuner.noteName</code>.

var logPitch = function(){
    console.log(tuner.pitch, tuner.noteName)
    requestAnimationFrame(logPitch)
};
logPitch();
// If you sing into your microphone, your pitch will be logged to the console in real time.

// tuner.stopUpdatingPitch(); // Stop calculating the pitch if you don't need to know it anymore.

Maybe try tweaking that a bit and see if you can do what you need?