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.
Hi. You should break your problems into the different questions you have no solution for:
Q: How to get microphone input in Unity.
Q: How to do fft of an audio clip.
Now we can deal with them:
A: http://docs.unity3d.com/$$anonymous$$anual/class-$$anonymous$$icrophone.html
this was just a matter of typing Unity microphone into google. From what I see, the syntax for getting the mic audio is exactly the same as in the kaappine.fi tutorial.A: http://stackoverflow.com/questions/3224823/how-exactly-do-you-compute-the-fast-fourier-transform
Also google. Typing how to do fft.
Google is not your enemy. ;) In most cases a quick search can lead to amazing results, some even giving better solutions than the original one.
Here some quick tips:
Don't ask new questions before researching. This is a matter of common sense. :P
Don't ask multiple questions in one question. This overcomplicates things and is also not appropriate on UA.
Don't ask "how to" questions on Unity Answers. That's what the Forums are for.
Please read up on the FAQ and the User Guide.
Thanks for the reply. I have been searching for a long time about this question, and the standard solution, which has worked for a long time, is for some reason no longer working in the new version of Unity, i.e. Unity 5.
I am aware of the $$anonymous$$icrophone class, and AudioSource.GetSpectrumData for doing the FFT. The problem is that the standard method as outlined in the code I put in the question no longer works in Unity 5. This is therefore not a "How to" question, but a question of why the code is no longer working in Unity5 and a call for a solution.
I have searched around but this method always comes up and no where to my knowledge has written about how to update it for Unity5. I am sorry for not being clear enough in the original post.
(Can't reply to your last comment because comment chain is too long)
//this also seems to be necessary
while (!($$anonymous$$icrophone.GetPosition ("Built-in $$anonymous$$icrophone") > 0))
{
}
This is definitely not neccessary because it is a condition with no code to execute in it. Once again, this condition only executes the code inside it (if something actually were inside) if the microphone recorded the first sample.
Getting data from a muted audio source won't work. The reason for this would be the overhaul of the audio engine in Unity 5, which is now using Audio $$anonymous$$ixers and Audio Groups to control the effect chains and outputs, it acts kind of like any generic audio mixing program.
Btw: It doesn't really make sense to get sound from a muted device in the first place. It's only logical that this isn't working.
You can read all about that and how to get your AudioSource to send but be muted to the output in the documentation and the Tutorials here.
If however you don't want to use this features, you can simply have the Audio source set to be 100% 3D (Spatial Blend) and the max range to something very small so the camera (or where ever the audiolistener happens to be) won't hear it, but the internal audio code still works with it. When you are ready to hear it, you could switch to another audiosource, or reset the Spatial Blend to 100% 2D.
When doing that you can do nice fft while hearing nothing like this:
I hope this works now. I recommend you consult the manuals and tutorials more frequently, especially when you are thinking in Unity 4 terms of code. ;)
There are changelogs, roadmaps, etc to let users know what exactly changed and what is to be expected in the future.
I have the same problem from Virtual Human Toolkit microphone image. I got the idea and code but the problem is it does not show or display on Android phone. But it shows in the Inspector of Unity 5.1.3f.
I don't know where to put the $$anonymous$$icrophone code on this.
Please help.
Thank you very much.
I've edited and converted your text to a comment. Don't post an "answer" unless you have an actual solution. "I have the same problem" is not a solution, it makes no sense to post it as an answer.
I deleted the thousands of lines of code from your text because it is not appropriate to post scripts and asking for fixing them on UA.
Also, dont ask new questions in other people's questions. Your problem is Android related. You have to consult the android sdk and check if the methods used are compatible with the version you are targeting.
Answer by benjaminoutram · Jan 19, 2016 at 03:08 PM
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: https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/audiomixer-and-audiomixer-groups
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
}
}
}
}
This works very well on PC, but on Android I get a terrible lag.
Thank you so much! This is awesome and works! Live audio reactive visuals are now possible!
@benja$$anonymous$$outram
so if i wanted to add
`GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force); `
so that an object moves up when there is input from the mic would i add it under the update function over here
if (start$$anonymous$$icrophoneListener) {
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * force);
Start$$anonymous$$icrophoneListener ();
}
Like that?
THAN$$anonymous$$ YOU SO $$anonymous$$USH , i am an student and it really help me ...thank you . thanks a lot.....
Thank you for this code! For anyone using it, the line: master$$anonymous$$ixer.SetFloat ("$$anonymous$$asterVolume", volume); should be master$$anonymous$$ixer.SetFloat ("Volume", volume);
Also, you should check if there are devices connected ($$anonymous$$icrophone.devices.Length != 0). If not, unity may freeze.
Answer by U_Ku_Shu · Jul 29, 2016 at 03:32 PM
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);
}
}
Answer by alexchandriyaa · May 24, 2017 at 07:43 AM
i need to move tha player by giving commands like jump,pick,moveright,moveleft help me out of this with sample codes @username
Answer by alexchandriyaa · May 22, 2017 at 12:40 PM
i need to move tha player by giving commands like jump,pick,moveright,moveleft help me out of this with sample codes @username
Answer by TSantosFigueira · Jul 27, 2017 at 06:49 PM
this'll help anyone that still might have any further doubts: http://crazyminnowstudio.com/posts/unity-5.2-bug-audiosource.getoutputdata-no-data-when-source-is-muted/
Your answer
Follow this Question
Related Questions
How do I create a Continuous Turn in XR for my VR Rig instead of Snap Turn Provider? 0 Answers
Using voice recognition in unity to "read" to the game 0 Answers
Call for Unity geniuses: Multiple Simultaneous Live Microphones Inputs 1 Answer
Input.gyro seems not working on android 8.1 0 Answers
Microphone Input not working 0 Answers