Microphone input to Audio Clip.

Hi,

Does anyone know how to get Unity to play back a microphone input in real time? I’ve managed to get it record an audio clip and then play it back on demand, but no methods I’ve used will make it play back real-time, it will either make an inaudible chopped noise, or an infinite feedback loop.

Please help, I’m getting desperate here!

Update:
I figured it out, it now works.
Here’s the code:

	function Start () {
	    var aud = GetComponent.<AudioSource>();
		aud.clip = Microphone.Start("Built-in Microphone", true, 10, 44100);
		aud.loop = true;
		while (!(Microphone.GetPosition(null) > 0)){}
		aud.Play();
	}
function Update () {

}

First thing… without headphones you will can’t do that.

Ok, so:
You probably tried this:

function Start() {
	var aud = GetComponent.<AudioSource>();
	aud.clip = Microphone.Start("Built-in Microphone", true, 10, 44100);
	aud.Play();
}

You need a delay there so try this instead:

function Start() {
	var aud = GetComponent.<AudioSource>();
	aud.clip = Microphone.Start("Built-in Microphone", true, 10, 44100);
    // \/
    yield WaitForSeconds(1);
	aud.Play();
}

@eguels

Can you explain what this bit is doing and why it is necessary?

while (!(Microphone.GetPosition(null) > 0)){}

I just added that and it solved a lot of my problems! Thank you very much. Just wanting to understand why it is necessary, and why the only reference I’ve found to such a fix is a 0-voted comment in the forums :S .

@eguels
Thanks for posting this, I have been struggling with this problem for a long time. Unfortunately when I tried your solution I still get a very noticeable delay (about 1 second) between when I say something and when I hear it through my headset. Is that to be expected? I’m not using the wait function shown in the other answer.

Thanks in advance for any help