Search code examples
c#audiounity-game-enginemicrophone

Capture audio from Microphone without playing it back


I am trying to use a microphone and process the audio captured. I managed to do all that, but I have a problem... My audio captured is automatically played to the user, and I don't want that.

    audioSrc.clip = Microphone.Start(null, true, 1000, 44100);
    while (!(Microphone.GetPosition(null) > 0)) { }
    audioSrc.Play();

I tried to disable the audio listener and lower the volume of the audio source, but that didn't work.

Anyone knows how I can capture audio without playing it back?

EDIT 1:

void Start()
{
    GameObject a = new GameObject("AudioSource");
    audioSrc = a.AddComponent<AudioSource>();
    Instantiate(a);

    string deviceName = Microphone.devices[0];
    audioSrc.clip = Microphone.Start(deviceName, true, 1000, 44100);
    audioSrc.volume = 0;
    while (!(Microphone.GetPosition(null) > 0)) { }

    audioSrc.Play();
}

void Update()
{
    audioSrc.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
    frq = findPitch(spectrum);
    txtFreq.text = "Hz: " + frq;
    trigger.update(Time.deltaTime, frq);
}

Solution

  • Anyone knows how I can capture audio without playing it back?

    There are two methods to do this:

    1.Set the volume to a very low number. This cannot be 0. The value of 0.001f is totally fine.

    audioSrc.volume = 0.001f;
    

    Works in the Editor. Not tested on any other platform except on Windows. Use method 2 if there is a problem.


    2.Use AudioMixer to handle it

    If setting the volume to 0.001f did not completely mute the audio then use AudioMixer to fix it.

    A.Create AudioMixer. Assets ---> Create ---> Audio Mixer and name is "MicMixer".

    B.Create new Group under the Mixer and name it "MuteMic".

    C.Change the attenuation of that "MuteMic" group to -80.

    D.Drag that "MuteMic" group to your Mic's AudioSource's Output slot. That's it. The GetOutputData function properly without any sound coming out from the speaker.

    Animated gif for this: