Microphone input in Unity 5.x

This question is about getting the real-time microphone input and using it with an FFT in unity 5.x.

The method outlined in http://www.kaappine.fi/tutorials/using-microphone-input-in-unity3d/ (among other places) used to work fine, but this method no longer works in Unity 5.x. In summary, that method uses the following code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class MicrophoneInput : MonoBehaviour {
    public float sensitivity = 100;
    public float loudness = 0;

    void Start() {
        audio.clip = Microphone.Start(null, true, 10, 44100);
        audio.loop = true; // Set the AudioClip to loop
        audio.mute = true; // Mute the sound, we don't want the player to hear it
        while (!(Microphone.GetPosition(AudioInputDevice) > 0)){} // Wait until the recording has started
        audio.Play(); // Play the audio source!
    }

    void Update(){
        loudness = GetAveragedVolume() * sensitivity;
    }

float GetAveragedVolume()
    { 
        float[] data = new float[256];
        float a = 0;
        audio.GetOutputData(data,0);
        foreach(float s in data)
        {
            a += Mathf.Abs(s);
        }
        return a/256;
    }
}

Does anyone know a work-around or alternative method for getting the real-time mic input available for doing FFT analysis? I appreciate your help.

I have finally found the solution to having audio from a mic put into Unity so you can do FFT or whatever with it, without the sound actually coming out the speakers, that works perfectly. You can copy the code below and follow the instructions in the comments in the code. Add this code to an object that has an audiosource attached.

Unity deals with sound really nicely with the new Audio Mixer feature. I recommend watching this tutorial which will also help you:

using UnityEngine;
using System.Collections;
using UnityEngine.Audio; // required for dealing with audiomixers

[RequireComponent(typeof(AudioSource))]
public class MicrophoneListener : MonoBehaviour {

	//Written in part by Benjamin Outram

	//option to toggle the microphone listenter on startup or not
	public bool startMicOnStartup = true;

	//allows start and stop of listener at run time within the unity editor
	public bool stopMicrophoneListener = false;
	public bool startMicrophoneListener = false;

	private bool microphoneListenerOn = false;

	//public to allow temporary listening over the speakers if you want of the mic output
	//but internally it toggles the output sound to the speakers of the audiosource depending
	//on if the microphone listener is on or off
	public bool disableOutputSound = false; /

	//an audio source also attached to the same object as this script is
	AudioSource src;

	//make an audio mixer from the "create" menu, then drag it into the public field on this script.
	//double click the audio mixer and next to the "groups" section, click the "+" icon to add a 
	//child to the master group, rename it to "microphone".  Then in the audio source, in the "output" option, 
	//select this child of the master you have just created.
	//go back to the audiomixer inspector window, and click the "microphone" you just created, then in the 
	//inspector window, right click "Volume" and select "Expose Volume (of Microphone)" to script,
	//then back in the audiomixer window, in the corner click "Exposed Parameters", click on the "MyExposedParameter"
	//and rename it to "Volume"
	public AudioMixer masterMixer;


	float timeSinceRestart = 0;






	void Start() {        
		//start the microphone listener
		if (startMicOnStartup) {
			RestartMicrophoneListener ();
			StartMicrophoneListener ();
		}
	}

	void Update(){    

		//can use these variables that appear in the inspector, or can call the public functions directly from other scripts
		if (stopMicrophoneListener) {
			StopMicrophoneListener ();
		}
		if (startMicrophoneListener) {
			StartMicrophoneListener ();
		}
		//reset paramters to false because only want to execute once
		stopMicrophoneListener = false;
		startMicrophoneListener = false;

		//must run in update otherwise it doesnt seem to work
		MicrophoneIntoAudioSource (microphoneListenerOn);

		//can choose to unmute sound from inspector if desired
		DisableSound (!disableOutputSound);


	}


	//stops everything and returns audioclip to null
	public void StopMicrophoneListener(){
		//stop the microphone listener
		microphoneListenerOn = false;
		//reenable the master sound in mixer
		disableOutputSound = false;
		//remove mic from audiosource clip
		src.Stop ();
		src.clip = null;

		Microphone.End (null);
	}


	public void StartMicrophoneListener(){
		//start the microphone listener
		microphoneListenerOn = true;
		//disable sound output (dont want to hear mic input on the output!)
		disableOutputSound = true;
		//reset the audiosource
		RestartMicrophoneListener ();
	}
	
	
	//controls whether the volume is on or off, use "off" for mic input (dont want to hear your own voice input!) 
	//and "on" for music input
	public void DisableSound(bool SoundOn){
		
		float volume = 0;
		
		if (SoundOn) {
			volume = 0.0f;
		} else {
			volume = -80.0f;
		}
		
		masterMixer.SetFloat ("MasterVolume", volume);
	}



	// restart microphone removes the clip from the audiosource
	public void RestartMicrophoneListener(){

		src = GetComponent<AudioSource>();
		
		//remove any soundfile in the audiosource
		src.clip = null;

		timeSinceRestart = Time.time;

	}

	//puts the mic into the audiosource
	void MicrophoneIntoAudioSource (bool MicrophoneListenerOn){

		if(MicrophoneListenerOn){
			//pause a little before setting clip to avoid lag and bugginess
			if (Time.time - timeSinceRestart > 0.5f && !Microphone.IsRecording (null)) {
				src.clip = Microphone.Start (null, true, 10, 44100);
				
				//wait until microphone position is found (?)
				while (!(Microphone.GetPosition (null) > 0)) {
				}
				
				src.Play (); // Play the audio source
			}
		}
	}

}

My solution of direct audioOutput from the microphone.

It’s works without additional actions.

using UnityEngine;
 
 [RequireComponent(typeof(AudioSource))]
 public class MicrophoneListener : MonoBehaviour
 {
     public bool IsWorking = true;
     private bool _lastValueOfIsWorking;

     public bool RaltimeOutput = true;
     private bool _lastValueOfRaltimeOutput;

     AudioSource _audioSource;
     private float _lastVolume = 0;
     
     void Start()
     {
         _audioSource = GetComponent<AudioSource>();

         if (IsWorking)
         {
             WorkStart();
         }

     }


     void Update()
     {
         CheckIfIsWorkingChanged();
         CheckIfRealtimeOutputChanged();
     }

     public void CheckIfIsWorkingChanged()
     {
         if (_lastValueOfIsWorking != IsWorking)
         {
             if (IsWorking)
             {
                 WorkStart();
             }
             else
             {
                 WorkStop();
             }
         }

         _lastValueOfIsWorking = IsWorking;
     }

     public void CheckIfRealtimeOutputChanged()
     {
         if (_lastValueOfRaltimeOutput != RaltimeOutput)
         {
             DisableSound(RaltimeOutput);
         }

         _lastValueOfRaltimeOutput = RaltimeOutput;
     }

     public void DisableSound(bool SoundOn)
     {
         if (SoundOn)
         {
             if (_lastVolume > 0)
             {
                 _audioSource.volume = _lastVolume;
             }
             else
             {
                 _audioSource.volume = 1f;
             }
         }
         else
         {
             _lastVolume = _audioSource.volume;
             _audioSource.volume = 0f;
         }
     }

     private void WorkStart()
     {
         _audioSource.clip = Microphone.Start(null, true, 10, 44100);
         _audioSource.loop = true;
         while (!(Microphone.GetPosition(null) > 0))
         {
             _audioSource.Play();
         }
     }

     private void WorkStop()
     {
         Microphone.End(null);
     }
 }

i need to move tha player by giving commands like jump,pick,moveright,moveleft help me out of this with sample codes @username

i need to move tha player by giving commands like jump,pick,moveright,moveleft help me out of this with sample codes @username

this’ll help anyone that still might have any further doubts: Unity 5.2 - AudioSource.GetOutputData [no data when source is muted]

Simplified answer: audioSource.mute will stop scripts from getting audio information from the audio source, but you can use the new AudioMixer system to get the desired behavior (namely, getting audio information from the microphone but not hearing it echoed out of the speakers).

To do this, create an AudioMixer, then add a child group to the Master group. Select the child group, and in the inspector, right-click Volume and select “Expose volume (of ChildGroup) to script”. In the Audio Mixer window, in the upper-right select “Exposed Parameters”, right-click “MyExposedParameter” and select Rename. Rename it to Volume.

Add a public variable to your Microphone script for the AudioMixer and select the proper AudioMixer in the Inspector. You can now use the following line to mute the audio output via script, while still being able to access the audio information:

masterMixer.SetFloat (“Volume”, -80.0f);

Setting Volume to 0 would restore the audio output. You can use the rest of your code as-is, such as the original code in the question. This is all the same as benjaminoutram’s answer, but focusing on the key part of the solution.