- Home /
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!
Answer by eguels · Apr 14, 2015 at 10:20 PM
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 () {
}
Answer by TheFloatingSheep · Apr 14, 2015 at 08:55 PM
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();
}
Answer by topher217 · Aug 03, 2017 at 06:56 AM
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 .
I'm glad it helped you! IIRC, I never knew what it did. Even now, I can't quite figure it out. In hindsight, it shouldn't do anything at all - even though there's a condition, the curly brackets are empty.
Ha, alright. I guess you found it in some other forum code?
Yes, I also did not understand why it affects anything, which is why I was so stumped after comparing with it in and then commenting it out...there was an OBVIOUS difference....BUT fiddling with this 5-$$anonymous$$ later, and commenting it out changes nothing now...looks like this wasn't actually what fixed my problem...I'm stumped again, but at least its working now... elusive bugs.
This means: when computer detect a sound(maybe all silence mark with null), it will jump out loop and play it?
The long answer is that you can't read directly from the analog to digital converter in a non-realtime OS, so there will always be some level of buffering before data becomes available in order for the audio data to be stable. Once the internal buffers fill to some threshold, data becomes available and this can be deter$$anonymous$$ed by checking that the microphone position has begun to advance. I would recommend a coroutine rather than a while {}. As written this will introduce a freeze or glitch during the initial buffering.
Answer by imranmouna · Sep 25, 2017 at 04:27 PM
@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
I'm glad you've found this helpful, but unfortunately, I did not find a way to reduce the delay, which was also quite horrible in my case too.
I haven't tested it extensively, and delays are not such a big issue with what I'm dealing with, but you might want to try the "Superpowered" library. It has an example Unity scene so you might be able to test it out fairly quickly with what you are trying to do.
http://superpowered.com/incredible-incredibly-fast-audio-library
Okay cool thanks, I'll check it out and let you know how it goes.
Your answer
Follow this Question
Related Questions
Microphone Position goes larger than predetermined number of samples 0 Answers
Multiple Microphone Inputs 0 Answers
Microphone capture different channels 0 Answers
How to record audio in chunks and send through websocket? 0 Answers
Is it possible to select channels for the microphone input? 0 Answers