hearing yourself while using microphone

Hi there,

I’m trying to create a game that uses the microphone as input.
I’ve found a piece of code that puts microphone input into a float called loudness:

public class microphoneInput : MonoBehaviour
{
    public float sensitivity = 100;
    public float loudness = 0;
    private float total;

    void Start()
    {
        total = 0f;
        GetComponent<AudioSource>().clip = Microphone.Start(null, true, 10, 44100);
        GetComponent<AudioSource>().loop = true;
        GetComponent<AudioSource>().mute = false;
        while (!(Microphone.GetPosition(null) > 0)) { }
        GetComponent<AudioSource>().Play();
    }

    void Update()
    {
        loudness = GetAveragedVolume() * sensitivity;
    }

    float GetAveragedVolume()
    {
        float[] data = new float[256];
        float a = 0;
        GetComponent<AudioSource>().GetOutputData(data, 0);
        foreach (float s in data)
        {
            a += Mathf.Abs(s);
        }
        return a / 256;
    }
}

Now the problem is that when you speak the input gets played by the speakers (you hear yourself)
It’s probably really easy but I find it. I tried putting mute on true but then it won’t get any input at all!

Open to all suggestions, thanks very much!

anyone?

I’ve got no idea here, but did you try setting the AudioSource volume to 0 ?

Same problem here! I’m using a pitchDetector from the asset store to convert user audio to a music note on a staff, but the only way I can get it to work is if I make the volume a low level (like 0.14). Even then, I get bad reverb sometimes–it’s inconsistent.
I tried larku’s suggestion a while back, thinking that would work, but nothing.

For those interested:
To get rid of hearing the recorded sound:

1 Like

You could try storing the microphone output into a separate audio clip and assign that clip to your AudioSource after recording is done (when Microphone.IsRecording() returns false). You also seem to be missing a call to Microphone.End().