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!