- Home /
How to use AudioSource.GetData() while source is muted?
I'm working on a game which will use the player voice via microphone as input to do some actions. This should happen in real time. My code is working very well; I'm used this article as base to create my voice detection system. But I'm having a minor (but very problematic) issue: I can't mute the audio source.
The microphone records the player voice in an audio clip which is continuously played by an audio source.
audio = GetComponent<AudioSource>();
audio.clip = Microphone.Start(null, true, 2, 44100);
audio.loop = true;
while (!(Microphone.GetPosition(null) > 0)) { }
audio.Play();
Then I read what's been played in the source and analyze if the action should happen or not.
float[] samples = new float[64];
audio.GetOutputData(samples, 0);
float averageNoise = 0;
foreach (float sample in samples) {
averageNoise += Mathf.Abs(sample);
}
averageNoise = averageNoise / samples.Length;
But when the audio source is muted, I can't read anything. I tried to set volume to 0 and the same thing happened. I can't understand how the guy in the article make it work. I need a way to mute without screwing up with the GetData() or make the audio listener ignore what this audio source is playing, otherwise the player will hear himself all the time. Do you know a workaround to achieve this?
Thanks in advance!
Your answer
Follow this Question
Related Questions
Audio Record Limit? 1 Answer
Trying to monitor the incoming audio from external usb interface 0 Answers
Voice Chat Prototype 0 Answers
Microphone on iOS check if permission given 2 Answers
does anyone know how to set load type to streaming? 0 Answers