Mic Input

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
Line 12: Line 12:
 
-Fixed some bugs pointed out by djfunkey from the community: http://forum.unity3d.com/members/118660-djfunkey
 
-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
+
-Expanded the editor functionality: now you can choose the audio device which you want to use. Up to 6 devices are supported.  
the selected slot then the script will spit out an error message.
+
If no device is found at the selected slot then the script will spit out an error message.
 +
 
  
  

Revision as of 21:01, 23 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.

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.


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 3 to choose from), if you only have one mic the other two will
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;
 
function Start () {
 
if(!audioSource){
  audioSource = GetComponent(AudioSource);
	} 
 
var i=0;
//count amount of devices connected
for(device in Microphone.devices){
i++;
Debug.Log(i);
}
 
//select the device if possible else give error
if(i>=1){
if(InputDevice==Devices.DefaultDevice){
selectedDevice= Microphone.devices[0];
}
}
else{
Debug.LogError ("No device detected on this slot");
}
 
 
 
if(i>=2){
if(InputDevice==Devices.Second){
selectedDevice= Microphone.devices[1];
}
}
else{
Debug.LogError ("No device detected on this slot");
}
 
 
if(i>=3){
if(InputDevice==Devices.Third){
selectedDevice= Microphone.devices[2];
}
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
 
 
if(i>=4){
if(InputDevice==Devices.Fourth){
selectedDevice= Microphone.devices[2];
}
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
if(i>=5){
if(InputDevice==Devices.Fifth){
selectedDevice= Microphone.devices[2];
}
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
 
if(i>=6){
if(InputDevice==Devices.Sixth){
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