Mic Input

From Unify Community Wiki
Revision as of 19:54, 23 October 2013 by MarkD (Talk | contribs)

Jump to: navigation, search

Hello my name is Mark Duisters and I came across allot of people having problems with getting audio from the machines microphone.

I have made a ready to use script, just place it on an empty GameObject and you are good to go. Call MicInput.loudness to get the audio data in a float format.

To use this script across all languages place it in the standard assets folder (not your own asset folder).

You are free to use this script under the creative commons restriction policy handled by the unify community.

Example: var Myfloat:float=MicInput.loudness; This could be used to detect when a player is speaking, breathing or to implement interactive environments. Good luck and have fun.

#pragma strict
@script RequireComponent (AudioSource);
var audioSource:AudioSource;
//The maximum amount of spectrum data that gets loaded in, best is to leave it on 256, unless you know what you are doing.
var spectrumMax:float=256;
static var loudness:float;
var sensitivity:float=1;
var Mute:boolean=false;
var debug:boolean=false;
function Start () {
  audioSource = GetComponent(AudioSource);
//detect the default microphone
audio.clip = Microphone.Start(null, true, 10, 44100);
//loop the playing of the recording so it will be realtime
audio.loop = true;
//if you only need the data stream values  check Mute, if you want to hear yourself ingame don't check Mute. 
audio.mute = Mute;
//don't do anything until the microphone started up
while (!(Microphone.GetPosition("") > 0)){
//Put the clip on play so the data stream gets ingame on realtime
//apply the mic input data stream to a float;
function Update () {
  loudness = GetDataStream()*sensitivity;
function GetDataStream(){
   var dataStream: float[]  = new float[spectrumMax];
       var audioValue: float = 0;
        for(var i in dataStream){
            audioValue += Mathf.Abs(i);
        return audioValue/spectrumMax;
Personal tools