Real time Microphone (line input) FFT analysis

Hi all,
so I’ve implemented a decent FFT analysis in code, but I don’t know how to get realtime mic /line audio into unity.

I see that there is a Microphone class that has some basic functionality, but in playing with it so far there doesn’t seem to be any way to pipe it into the scene to be picked up by the AudioListener (which is where my FFT analysis happens with AudioListener.GetSpectrumData() ).

I’ve looked around on the forums and here, and all I see are questions circa 2010- mid 2012 with no answers. Is there more recent info on this? Has anyone gotten this to work? Are there other solutions I should look at, such as fMod?
I need to get this working, and short of having a 2nd program do audio capture and fill an audio buffer that then gets read into OnAudioFilterRead (which is beyond the scope of my time and knowledge at the moment), I’m at a loss of what to do.

Help?

This works fine. What I do is set the Microphone to loop and set it as the AudioClip on an AudioSource, then use the spectrum data off the AudioSource, rather than listener.

this is the answer I’ve currently got going. would have posted as a comment, but couldn’t post this much code.

ah, hmm. i’m trying to get down as close to real time as possible for the purposes of music visualization. i realize unity isn’t really built for that, so i’m kinda trying to just work with what I can.

using this code, I’ve got down to about 50 milliseconds, but there’s a bit of a stutter every time the buffer switches. but in this case the audio only needs to be analyzed, not actually heard, but it does seem to choke the game in that moment.

This is the entire script I created to make this happen. I haven’t revisited it to make it cleaner, just set it up to work.

Also, if you’re looking for non-realtime audio analysis (e.g. running audio analysis on saved audio files that are playing back), this script works well with very little latency

using UnityEngine;
using System.Collections;


public class FFT : MonoBehaviour
{
	
	#region vars
	public string CurrentAudioInput;
	
	private AudioObj[] audioObj = new AudioObj[2];
	
	private const int BANDS = 4;
	
	//public float[] curve = new float[BANDS]; //scale output of band analysis
	public float[] output = new float[BANDS];
	
	public string[] inputDevices;
	private int[] crossovers = new int[BANDS];
	private float[] freqData = new float[8192];
	private float[] band;
	
	public GameObject playerPrefab;
	private int index = 0;
	
	public static FFT Instance;
	private bool doSound = true;
	private int deviceNum;
	
	
	private struct AudioObj
	{
		public GameObject player;
		public AudioClip clip;
		public void SetClip(AudioClip c)
		{
			clip = c;
			player.audio.clip = c;
			/*
    slowing the playback down a small amount allows enough space between
    recording and output so that analysis does not overtake the recording.
    this helps with stutter and distortion, but doesn't solve it completely
    */
			player.audio.pitch = .95f;
		}
	}
	#endregion
	
	#region Unity Methods
	void Start()
	{
		Instance = this;
		
		crossovers[0] = 30; //guesstimating sample lengths for frequency bands
		crossovers[1] = 50;
		crossovers[2] = 600;
		crossovers[3] = freqData.Length;
		
		band = new float[BANDS];
		output = new float[BANDS];
		
		for (int i = 0; i < audioObj.Length; i++)
		{
			audioObj*.player = (GameObject)Instantiate(playerPrefab);*

_ audioObj*.player.transform.parent = transform;_
_ audioObj.player.transform.position = Vector3.zero;
audioObj.clip = new AudioClip();
}*_

* inputDevices = new string[Microphone.devices.Length];*
* deviceNum = Microphone.devices.Length - 1;*

* for (int i = 0; i < Microphone.devices.Length; i++)*
inputDevices = Microphone.devices*.ToString();*

* CurrentAudioInput = Microphone.devices[deviceNum].ToString();*

* InvokeRepeating(“Check”, 0, 1.0f / 15.0f);*
* StartCoroutine(StartRecord());*

* }*

* void Update()*
* {*
* KeyInput();*
* }*
* #endregion*

* #region Actions*

* private void Check()*
* {*
* if (!doSound)*
* return;*

* audioObj[index].player.audio.GetSpectrumData(freqData, 0, FFTWindow.Hamming);*

* bool cutoff = false;*
* int k = 0;*
* float[] lengths = new float[BANDS];*
* for(int i = 0; i < BANDS; i++)*
* {*
* float min = (i > 0 ? crossovers[i-1] : 0);*
lengths = crossovers - min;
_ band = 0f;
* }*_

* for (int i = 0; i < freqData.Length; i++)*
* {*

* if (k > BANDS - 1)*
* break;*

_ band[k] += freqData*;
if(i > crossovers[k])
{
output[k] = Mathf.Abs(band[k] / lengths[k]);
k++;
}
if (i > crossovers[BANDS - 1] - 10)
cutoff = true;
}
}*_

* private IEnumerator StartRecord()*
* {*

* audioObj[index].clip = Microphone.Start(Microphone.devices[deviceNum], true, 5, 24000);*
_ /
the longer the mic recording time, the less often there are “hiccups” in game performance
but also due to being pitched down, the playback gradually falls farther behind the recording
/_

* print("recording to audioObj " + index);*
* StartCoroutine(StartPlay(audioObj[index].clip));*
* yield return new WaitForSeconds(5);*
* StartCoroutine(StartRecord()); //swaps audio buffers, begins recording and playback of new buffer*
_ /* it is necessary to swap buffers, otherwise the audioclip quickly becomes too large and begins to slow down the system */_

* }*

* private IEnumerator StartPlay(AudioClip buffer)*
* {*
* audioObj[index].SetClip(buffer);*
* yield return new WaitForSeconds(.01f);*
* audioObj[index].player.SetActive(true);*
* audioObj[index].player.audio.Play();*

* audioObj[Mathf.Abs((index % 2) - 1)].player.audio.Stop();*

* index++;*
* if (index > 1)*
* index = 0;*

* }*

* private void KeyInput()*
* {*
* if (Input.GetKeyDown(KeyCode.A))*
* {*
* doSound = !doSound;*
* }*
* if (Input.GetKeyDown(KeyCode.Equals))*
* {*
* deviceNum++;*
* if (deviceNum > Microphone.devices.Length - 1)*
* deviceNum = 0;*
* CurrentAudioInput = Microphone.devices[deviceNum].ToString();*
* }*
* }*
* #endregion*

}

I know this one has been answered but have you seen this one on the asset store, its a free script Unity Asset Store - The Best Assets for Game Making

I’ve posted the code from the original answer (with some tidying) up at GitHub - robksawyer/Unity-Live-Input-FFT-Analysis: The goal of this script is to handle live audio input analysis as most efficiently as possible within Unity..