- Home /
How do I get audio data from microphone in realtime?
Unity 3.5.2 OS windows 7 64bit
I want to develop applications with my microphone. Is it possible to access real time audio data from microphone? Actually I can access audio data, but below my code occurs 1 sec latency. Are there any ways?
void Start ()
{
audio.clip = Microphone.Start(Microphone.devices[0], true, 1, 44100);
audio.Play();
}
void Update ()
{
AudioListener.GetOutputData(samples, 0);
float sum = 0.0f;
for(int i=0; i
// Bunny83: Indent your code by 4 spaces
// UA has stripped a part of your code because you started
// a HTML tag with the lower-than char. edit your question and add
// the rest of your code. You can remove this comments of course ;)
Answer by Bunny83 · Aug 09, 2012 at 12:44 PM
You can actually never get Mic-data in realtime. The hardware needs some time to sample the data. Also Unity reads in blocks of data. Unlike when playing an audio-file you can't read into the future. I'm not sure if you can reduce the buffer size somewhere, but the latency will never disappear completely.
I don't $$anonymous$$d a bit of latency, but is this a viable method? If I can get latency down to 50ms I'd be happy for the moment. Are there other solutions to this problem, such as using f$$anonymous$$od and OnAudioFilterRead ?
I need to do some live audio input, and while latency isn't ideal, it's obviously going to be a factor, so I'll deal with a little bit
Thanks
Answer by PicklesIIDX · Aug 27, 2014 at 06:40 AM
I've been playing with this and I noticed that if I called Microphone.Start() in Awake, I would get a ton of latency, but sometimes not. I put the call in Start() and now I have almost no latency.
Thanks for this. $$anonymous$$y iOS app would crash/hang all the time on
while (!($$anonymous$$icrophone.GetPosition(null) > 0)){}
until I moved initialisation to Start (from Awake).
Answer by mcpepa · Dec 02, 2015 at 09:50 AM
this could help. there is a little delay, its normal. be carefull with the reverb that is made between the audio intput and the audio output.
void Start()
{
AudioSource aud = GetComponent<AudioSource>();
aud.clip = Microphone.Start(null, true, 1, 4400);
aud.loop = true;
while (!(Microphone.GetPosition(null) > 0)){}
aud.Play();
}
Your answer
Follow this Question
Related Questions
Playing SuperCollider realtime through audio sources 0 Answers
Record Audio for triggering something 0 Answers
Deleting an AudioClip created by the microphone 0 Answers
Capture audio without playing it back 2 Answers
Voice Recording issue ?? 0 Answers