Mic Input

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 7: Line 7:
  
 
You are free to use this script under the creative commons restriction policy handled by the unify community.
 
You are free to use this script under the creative commons restriction policy handled by the unify community.
 +
 +
This script can also be downloaded from the asset store.
  
 
Update Log:
 
Update Log:

Revision as of 19:01, 26 October 2013

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.

This script can also be downloaded from the asset store.

Update Log:

-Fixed some bugs pointed out by djfunkey from the community: http://forum.unity3d.com/members/118660-djfunkey

-Expanded the editor functionality: now you can choose the audio device which you want to use. Up to 6 devices are supported. If no device is found at the selected slot then the script will spit out an error message.

-Added ShowDeviceName toggle. If true it wil print the device name and slot number to the console.


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);
@script ExecuteInEditMode;
 
 //Select the microphone you want to use (supported up to 6 to choose from). If the device has number 1 in the console, you should select default as it is the first defice to be found.
enum Devices {DefaultDevice, Second, Third, Fourth, Fifth, Sixth}
 
var InputDevice : Devices;
private var selectedDevice:String;
 
 
 
 
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;
var ShowDeviceName:boolean=false;
 
function Start () {
 
if(!audioSource){
  audioSource = GetComponent(AudioSource);
	} 
 
var i=0;
//count amount of devices connected
for(device in Microphone.devices){
i++;
if(ShowDeviceName){
Debug.Log ("Devices number "+i+" Name"+"="+device);
 
}
}
 
//select the device if possible else give error
if(InputDevice==Devices.DefaultDevice){
if(i>=1){
selectedDevice= Microphone.devices[0];
}
else{
Debug.LogError ("No device detected on this slot");
}
 
}
 
 
if(InputDevice==Devices.Second){
if(i>=2){
selectedDevice= Microphone.devices[1];
}
else{
Debug.LogError ("No device detected on this slot");
}
 
}
 
 
 
if(InputDevice==Devices.Third){
if(i>=3){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
 
 
if(InputDevice==Devices.Fourth){
if(i>=4){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
if(InputDevice==Devices.Fifth){
if(i>=5){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
 
if(InputDevice==Devices.Sixth){
if(i>=6){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
 
 
 
 
 
//detect the default microphone
audio.clip = Microphone.Start(selectedDevice, 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(selectedDevice) > 0)){
 
}
//Put the clip on play so the data stream gets ingame on realtime
audio.Play();
 
}
 
 
 
//apply the mic input data stream to a float;
function Update () {
if(Microphone.IsRecording(selectedDevice)){
  loudness = GetDataStream()*sensitivity;
  if(debug){
  Debug.Log(loudness);
  }
  }
}
 
 
function GetDataStream(){
if(Microphone.IsRecording(selectedDevice)){
 
   var dataStream: float[]  = new float[spectrumMax];
       var audioValue: float = 0;
        audio.GetOutputData(dataStream,0);
 
        for(var i in dataStream){
            audioValue += Mathf.Abs(i);
        }
        return audioValue/spectrumMax;
        }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox