As mentioned by Philipp, The Microphone
class is indeed what you want to use.
The AudioClip
that it produces is your audio buffer.
When you call the Microphone.Start()
method, you are opening the connection to the physical microphone, and the AudioClip
is where the input is stored for you to access the sounds.
The Microphone.Start()
method has a parameter loop
which tells the Microphone
to continue recording over the beginning of the AudioClip
when the lengthSec
is reached.
So as a quick and dirty example:
using UnityEngine;
using System.Collections;
public class MicrophonePoll : MonoBehavior {
private AudioClip clip;
private float[] samples;
void Start()
{
//Start recording 1 second blocks of audio from the default microphone, and loop the recording
clip = Microphone.Start(null,true,1,44100);
}
void Update()
{
if(clip!=null)
{
//Sample from the AudioClip to determine how loud the room was over the last second of recording
if(samples == null)
{
samples=new float[clip.samples*clip.channels];
}
clip.GetData(samples,0);
foreach(float sample in samples)
{
//do something with the value (ranges from -1.0f to 1.0f)
}
}
}
}
Microphone
class? \$\endgroup\$