Mic Input

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Hello my name is Mark Duisters and I came across allot of people having problems with getting audio from the machines microphone.
+
'''ATTENTION! THIS PROJECT IS NO LONGER OPEN SOURCE I WILL NO LONGER BE UPDATING THE JAVASCRIPT VERSION ON THIS WIKI! However don't worry the version you find on this wiki is still free to use for every one, even in commercial projects or to use as a base for your own scripts, I will just no longer be working on this project as an open source project. you are still free to use the scripts on this wiki as a base for your own commercial projects. The latest (close source) version will be updated frequently can be bought here''[https://www.assetstore.unity3d.com/en/#!/content/48547]'''''
 +
 
 +
Note that neither of these scripts (the original javascript and the translated C#) are up to date and contain several errors.
 +
 
 +
Hello my name is Mark Duisters and I came across allot of people having problems with getting audio from the machine's microphone.
  
 
I have made a ready to use script, just place it on an empty GameObject and you are good to go.
 
I have made a ready to use script, just place it on an empty GameObject and you are good to go.
Line 21: Line 25:
 
Note that some functions are script depended, for instance the java version does not have a push to talk option.
 
Note that some functions are script depended, for instance the java version does not have a push to talk option.
  
Update Log:
+
'''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.
+
  
-C# version of the script by djfunkey
+
'''1.2'''
  
-Changed naming of several inputs to be more clear.
+
''C#''
  
-Added Source volume selection.(converted from djfunkey).
+
    - Added 3 Talking methods:
 +
        ~ Hold To Talk: Hold down a button to activate the mic.
 +
        ~ Push To Talk: Press a button once to activate the mic, and press it again to stop it.  
 +
        ~ Constant Talk: The microphone will constantly be activated.
 +
    - Improved the GUI functionality built into the script.
 +
    - Editor GUI now shows which mic you are using.
 +
    - General Optimization and bug fixes
  
-Added ingame menu (selected by user whether he wants to use it or not).(converted from djfunkey).
+
'''1.1'''
  
 +
    - 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.
 +
    - C# version of the script by djfunkey
 +
    - Changed naming of several inputs to be more clear.
 +
    - Added Source volume selection.(converted from djfunkey).
 +
    - Added ingame menu (selected by user whether he wants to use it or not).(converted from djfunkey).
 +
    - Added memory cleaning to prevent audio lag, both in java and C#(djfunkey).
  
 
UnityScript (java) features:
 
UnityScript (java) features:
Line 65: Line 77:
 
//if false the below will override and set the mic selected in the editor
 
//if false the below will override and set the mic selected in the editor
 
  //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.
 
  //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}
+
enum Devices2 {DefaultDevice, Second, Third, Fourth, Fifth, Sixth}
 
+
var InputDevice : Devices;
+
var InputDevice : Devices2;
 
private var selectedDevice:String;
 
private var selectedDevice:String;
 
   
 
   
 
+
 
+
 
+
 
var audioSource:AudioSource;
 
var audioSource:AudioSource;
 
//The maximum amount of sample data that gets loaded in, best is to leave it on 256, unless you know what you are doing. A higher number gives more accuracy but  
 
//The maximum amount of sample data that gets loaded in, best is to leave it on 256, unless you know what you are doing. A higher number gives more accuracy but  
Line 82: Line 94:
 
private var minFreq: int;
 
private var minFreq: int;
 
private var maxFreq: int;
 
private var maxFreq: int;
 
+
var Mute:boolean=false;
+
var Mute:boolean=true;
 
var debug:boolean=false;
 
var debug:boolean=false;
 
var ShowDeviceName:boolean=false;
 
var ShowDeviceName:boolean=false;
 
private var micSelected:boolean=false;  
 
private var micSelected:boolean=false;  
  
 
+
private var mTimer:float=10;
 +
private var mRefTime:float=10;
 +
 +
 
function Start () {
 
function Start () {
 
+
 
if(!audioSource){
 
if(!audioSource){
 
   audioSource = GetComponent(AudioSource);
 
   audioSource = GetComponent(AudioSource);
 
}  
 
}  
 
+
 
var i=0;
 
var i=0;
 
//count amount of devices connected
 
//count amount of devices connected
Line 101: Line 116:
 
if(ShowDeviceName){
 
if(ShowDeviceName){
 
Debug.Log ("Devices number "+i+" Name"+"="+device);
 
Debug.Log ("Devices number "+i+" Name"+"="+device);
 
+
 
}
 
}
 
}
 
}
 
if(SelectIngame==false){
 
if(SelectIngame==false){
 
//select the device if possible else give error
 
//select the device if possible else give error
if(InputDevice==Devices.DefaultDevice){
+
if(InputDevice==Devices2.DefaultDevice){
 
if(i>=1){
 
if(i>=1){
 
selectedDevice= Microphone.devices[0];
 
selectedDevice= Microphone.devices[0];
Line 113: Line 128:
 
Debug.LogError ("No device detected on this slot");
 
Debug.LogError ("No device detected on this slot");
 
}
 
}
 
+
 
}
 
}
 
+
 
+
if(InputDevice==Devices.Second){
+
if(InputDevice==Devices2.Second){
 
if(i>=2){
 
if(i>=2){
 
selectedDevice= Microphone.devices[1];
 
selectedDevice= Microphone.devices[1];
Line 124: Line 139:
 
Debug.LogError ("No device detected on this slot");
 
Debug.LogError ("No device detected on this slot");
 
}
 
}
 
+
 
}
 
}
 
+
 
+
 
+
if(InputDevice==Devices.Third){
+
if(InputDevice==Devices2.Third){
 
if(i>=3){
 
if(i>=3){
 
selectedDevice= Microphone.devices[2];
 
selectedDevice= Microphone.devices[2];
Line 138: Line 153:
 
}
 
}
 
}
 
}
 
+
 
+
if(InputDevice==Devices.Fourth){
+
if(InputDevice==Devices2.Fourth){
 
if(i>=4){
 
if(i>=4){
 
selectedDevice= Microphone.devices[2];
 
selectedDevice= Microphone.devices[2];
Line 149: Line 164:
 
}
 
}
 
}
 
}
if(InputDevice==Devices.Fifth){
+
if(InputDevice==Devices2.Fifth){
 
if(i>=5){
 
if(i>=5){
 
selectedDevice= Microphone.devices[2];
 
selectedDevice= Microphone.devices[2];
Line 158: Line 173:
 
}
 
}
 
}
 
}
 
+
if(InputDevice==Devices.Sixth){
+
if(InputDevice==Devices2.Sixth){
 
if(i>=6){
 
if(i>=6){
 
selectedDevice= Microphone.devices[2];
 
selectedDevice= Microphone.devices[2];
Line 168: Line 183:
 
}
 
}
 
}
 
}
 
+
 
}
 
}
 
//detect the default microphone
 
//detect the default microphone
Line 178: Line 193:
 
//don't do anything until the microphone started up
 
//don't do anything until the microphone started up
 
while (!(Microphone.GetPosition(selectedDevice) > 0)){
 
while (!(Microphone.GetPosition(selectedDevice) > 0)){
 
+
 
}
 
}
 
//Put the clip on play so the data stream gets ingame on realtime
 
//Put the clip on play so the data stream gets ingame on realtime
 
audio.Play();
 
audio.Play();
 
+
 
}
 
}
 
+
 
+
 
+
 
//apply the mic input data stream to a float;
 
//apply the mic input data stream to a float;
 
function Update () {
 
function Update () {
 
+
//set timer for refreshing memory.
 +
mTimer += Time.deltaTime;
 +
//refresh the memory
 +
if (micSelected == true){
 +
if (mTimer >= mRefTime) {
 +
StopMicrophone();
 +
StartMicrophone();
 +
mTimer = 0;
 +
}
 +
  }
 +
 +
 
if(Microphone.IsRecording(selectedDevice)){
 
if(Microphone.IsRecording(selectedDevice)){
 
   loudness = GetDataStream()*sensitivity*(sourceVolume/10);
 
   loudness = GetDataStream()*sensitivity*(sourceVolume/10);
Line 196: Line 222:
 
   }
 
   }
 
   }
 
   }
 
+
 
   //the source volume
 
   //the source volume
 
   if (sourceVolume > 100){
 
   if (sourceVolume > 100){
Line 206: Line 232:
 
   }
 
   }
 
   audio.volume = (sourceVolume/100);
 
   audio.volume = (sourceVolume/100);
 
+
 
+
 
}
 
}
 
+
 
+
 
function GetDataStream(){
 
function GetDataStream(){
 
if(Microphone.IsRecording(selectedDevice)){
 
if(Microphone.IsRecording(selectedDevice)){
 
+
 
   var dataStream: float[]  = new float[amountSamples];
 
   var dataStream: float[]  = new float[amountSamples];
 
       var audioValue: float = 0;
 
       var audioValue: float = 0;
 
         audio.GetOutputData(dataStream,0);
 
         audio.GetOutputData(dataStream,0);
       
+
 
         for(var i in dataStream){
 
         for(var i in dataStream){
 
             audioValue += Mathf.Abs(i);
 
             audioValue += Mathf.Abs(i);
Line 223: Line 249:
 
         return audioValue/amountSamples;
 
         return audioValue/amountSamples;
 
         }
 
         }
 
+
 +
 +
 +
 +
 +
 
}
 
}
 
+
 
+
 
+
 
+
 
+
 
//select device ingame
 
//select device ingame
 
+
 
     function OnGUI () {
 
     function OnGUI () {
 
  if(SelectIngame==true){
 
  if(SelectIngame==true){
Line 253: Line 284:
 
  }
 
  }
 
     }
 
     }
   
+
   
+
   
+
 
     //for the above control the mic start or stop
 
     //for the above control the mic start or stop
   
+
   
+
    function StartMicrophone () {
+
public function StartMicrophone () {
 
         audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
 
         audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
 
         while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
 
         while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
Line 268: Line 299:
 
   
 
   
 
   
 
   
    function StopMicrophone () {
+
public function StopMicrophone () {
 
         audio.Stop();//Stops the audio
 
         audio.Stop();//Stops the audio
 
         Microphone.End(selectedDevice);//Stops the recording of the device   
 
         Microphone.End(selectedDevice);//Stops the recording of the device   
 
   
 
   
 
     }
 
     }
   
+
   
+
 
       function GetMicCaps () {
 
       function GetMicCaps () {
 
         Microphone.GetDeviceCaps(selectedDevice,  minFreq,  maxFreq);//Gets the frequency of the device
 
         Microphone.GetDeviceCaps(selectedDevice,  minFreq,  maxFreq);//Gets the frequency of the device
Line 281: Line 312:
 
   
 
   
 
     }
 
     }
 +
   
 +
   
 +
   
 +
   
 +
   
 +
   
 +
    //Create a gui button in another script that calls to this script
 +
        public function MicDeviceGUI (left:float , top:float, width:float, height:float, buttonSpaceTop:float, buttonSpaceLeft:float) {
 +
if (Microphone.devices.Length > 1 && micSelected == false)//If there is more than one device, choose one.
 +
for (var i:int=0; i < Microphone.devices.Length; ++i)
 +
if (GUI.Button(new Rect(left + (buttonSpaceLeft * i), top + (buttonSpaceTop * i), width, height), Microphone.devices[i].ToString())) {
 +
StopMicrophone();
 +
selectedDevice = Microphone.devices[i].ToString();
 +
GetMicCaps();
 +
StartMicrophone();
 +
micSelected = true;
 +
}
 +
if (Microphone.devices.Length < 2 && micSelected == false) {//If there is only 1 decive make it default
 +
selectedDevice = Microphone.devices[0].ToString();
 +
GetMicCaps();
 +
micSelected = true;
 +
}
 +
    }
 +
 +
   
 
    
 
    
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
djfunkey did an excelent job in converting the script to C#. Thanks to him even more people can take advantage of the microphone ingame.
+
djfunkey did an excelent job in converting the script to C#. Thanks to him even more people can take advantage of the microphone in game.
 
http://forum.unity3d.com/members/118660-djfunkey
 
http://forum.unity3d.com/members/118660-djfunkey
 +
 +
The Editor Script for the C# version is below.
 +
 +
*To talk in game using either of the methods press "'''T'''"*
  
 
C# features:
 
C# features:
Line 294: Line 354:
 
2: The user can now adjust the volume of their microphone, it will also affect the loudness variable proportionally.
 
2: The user can now adjust the volume of their microphone, it will also affect the loudness variable proportionally.
  
3: The user can choose for the microphone to be "Push to talk", or to just be constently on.
+
3: The user can choose from 3 settings: PushtToTalk, HoldToTalk, ConstantTalk. These determine what the user has to do for the microphone to be activated.  
  
 
4: If there is only 1 device detected it will make that device default.
 
4: If there is only 1 device detected it will make that device default.
Line 300: Line 360:
 
5: Call microphone data from any script with MicrophoneInput.loudness.
 
5: Call microphone data from any script with MicrophoneInput.loudness.
  
 +
6: The microphone RAM will still be flushed even if Time.timeScale = 0;
  
<syntaxhighlight lang="javascript">
+
7: You can set how often the michrophone RAM is flushed.
  
 +
 +
<syntaxhighlight lang="javascript">
 
using UnityEngine;
 
using UnityEngine;
 
using System.Collections;
 
using System.Collections;
 
+
 
[RequireComponent(typeof(AudioSource))]
 
[RequireComponent(typeof(AudioSource))]
public class MicrophoneInput : MonoBehaviour {
+
public class MicControlC : MonoBehaviour {
+
 
public string selectedDevice = "";
+
public enum micActivation {
    public float sensitivity = 100;
+
HoldToSpeak,
    public float loudness = 0;//dont touch
+
PushToSpeak,
public float sourceVolume = 80;//Between 0 and 100
+
ConstantSpeak
public bool buttonToSpeak = false;
+
}
 +
 
 +
public float sensitivity = 100;
 +
public float ramFlushSpeed = 5;//The smaller the number the faster it flush's the ram, but there might be performance issues...
 +
[Range(0,100)]
 +
public float sourceVolume = 100;//Between 0 and 100
 +
public bool GuiSelectDevice = true;
 +
public micActivation micControl;
 +
//
 +
public string selectedDevice { get; private set; }
 +
public float loudness { get; private set; } //dont touch
 
//
 
//
private int minFreq, maxFreq;
 
 
private bool micSelected = false;
 
private bool micSelected = false;
private float mTimer, mRefTime = 10;
+
private float ramFlushTimer;
private int amountSamples = 256;//increase to get better average, but will decrease performance. Best to leave it
+
private int amountSamples = 256; //increase to get better average, but will decrease performance. Best to leave it
+
private int minFreq, maxFreq;
 +
 
 
     void Start() {
 
     void Start() {
 
audio.loop = true; // Set the AudioClip to loop
 
audio.loop = true; // Set the AudioClip to loop
        audio.mute = false; // Mute the sound, we don't want the player to hear it
+
audio.mute = false; // Mute the sound, we don't want the player to hear it
 
selectedDevice = Microphone.devices[0].ToString();
 
selectedDevice = Microphone.devices[0].ToString();
 +
micSelected = true;
 
GetMicCaps();
 
GetMicCaps();
 
     }
 
     }
 
+
 
void OnGUI() {
 
void OnGUI() {
MicDeviceGUI(10,10, 300, 150, 160, 0);
+
MicDeviceGUI((Screen.width/2)-150, (Screen.height/2)-75, 300, 100, 10, -300);
 +
if (Microphone.IsRecording(selectedDevice)) {
 +
ramFlushTimer += Time.fixedDeltaTime;
 +
RamFlush();
 +
}
 
}
 
}
+
 
public void MicDeviceGUI (float left, float top, float width, float height, float buttonSpaceTop, float buttonSpaceLeft) {
 
public void MicDeviceGUI (float left, float top, float width, float height, float buttonSpaceTop, float buttonSpaceLeft) {
if (Microphone.devices.Length > 1 && micSelected == false)//If there is more than one device, choose one.
+
if (Microphone.devices.Length > 1 && GuiSelectDevice == true || micSelected == false)//If there is more than one device, choose one.
 
for (int i = 0; i < Microphone.devices.Length; ++i)
 
for (int i = 0; i < Microphone.devices.Length; ++i)
if (GUI.Button(new Rect(left + (buttonSpaceLeft * i), top + (buttonSpaceTop * i), width, height), Microphone.devices[i].ToString())) {
+
if (GUI.Button(new Rect(left + ((width + buttonSpaceLeft) * i), top + ((height + buttonSpaceTop) * i), width, height), Microphone.devices[i].ToString())) {
 
StopMicrophone();
 
StopMicrophone();
 
selectedDevice = Microphone.devices[i].ToString();
 
selectedDevice = Microphone.devices[i].ToString();
Line 347: Line 425:
 
}
 
}
 
}
 
}
+
void GetMicCaps () {
+
public void GetMicCaps () {
 
Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq);//Gets the frequency of the device
 
Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq);//Gets the frequency of the device
 
if ((minFreq + maxFreq) == 0)//These 2 lines of code are mainly for windows computers
 
if ((minFreq + maxFreq) == 0)//These 2 lines of code are mainly for windows computers
 
maxFreq = 44100;
 
maxFreq = 44100;
 
}
 
}
+
 
public void StartMicrophone () {
 
public void StartMicrophone () {
 
audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
 
audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
        while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
+
while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
        audio.Play(); // Play the audio source!
+
audio.Play(); // Play the audio source!
 
}
 
}
+
 
public void StopMicrophone () {
 
public void StopMicrophone () {
 
audio.Stop();//Stops the audio
 
audio.Stop();//Stops the audio
 
Microphone.End(selectedDevice);//Stops the recording of the device
 
Microphone.End(selectedDevice);//Stops the recording of the device
}
+
}
+
void AudioVolume () {
+
    void Update() {
if (sourceVolume > 100)
+
sourceVolume = 100;
+
if (sourceVolume < 0)
+
sourceVolume = 0;
+
 
audio.volume = (sourceVolume/100);
 
audio.volume = (sourceVolume/100);
}
+
loudness = GetAveragedVolume() * sensitivity * (sourceVolume/10);
 
+
//Hold To Speak!!
    void Update() {
+
if (micControl == micActivation.HoldToSpeak) {
mTimer += Time.deltaTime;
+
if (Microphone.IsRecording(selectedDevice) && Input.GetKey(KeyCode.T) == false)
AudioVolume();
+
StopMicrophone();
        loudness = GetAveragedVolume() * sensitivity * (sourceVolume/10);
+
//
if (buttonToSpeak == true) {
+
 
if (Input.GetKeyDown(KeyCode.T)) //Push to talk
 
if (Input.GetKeyDown(KeyCode.T)) //Push to talk
 
StartMicrophone();
 
StartMicrophone();
if (Input.GetKey(KeyCode.T)) { //Cleans ram while being able to talk
+
//
if (mTimer >= mRefTime) {
+
if (Input.GetKeyUp(KeyCode.T))
 +
StopMicrophone();
 +
//
 +
}
 +
//Push To Talk!!
 +
if (micControl == micActivation.PushToSpeak) {
 +
if (Input.GetKeyDown(KeyCode.T)) {
 +
if (Microphone.IsRecording(selectedDevice))
 
StopMicrophone();
 
StopMicrophone();
 +
 +
else if (!Microphone.IsRecording(selectedDevice))
 
StartMicrophone();
 
StartMicrophone();
mTimer = 0;
 
}
 
 
}
 
}
if (Input.GetKeyUp(KeyCode.T))
+
//
StopMicrophone();
+
 
}
 
}
if (buttonToSpeak == false && micSelected == true && !Microphone.IsRecording(selectedDevice))
+
//Constant Speak!!
StartMicrophone();
+
if (micControl == micActivation.ConstantSpeak)
if (buttonToSpeak == false && micSelected == true) {//Cleans the ram
+
if (!Microphone.IsRecording(selectedDevice))  
if (mTimer >= mRefTime) {
+
StopMicrophone();
+
 
StartMicrophone();
 
StartMicrophone();
mTimer = 0;
+
//
}
+
}
+
 
if (Input.GetKeyDown(KeyCode.G))
 
if (Input.GetKeyDown(KeyCode.G))
micSelected = false;
+
micSelected = false;  
 
+
 
     }
 
     }
  
 +
private void RamFlush () {
 +
if (ramFlushTimer >= ramFlushSpeed && Microphone.IsRecording(selectedDevice)) {
 +
StopMicrophone();
 +
StartMicrophone();
 +
ramFlushTimer = 0;
 +
}
 +
}
 +
 
float GetAveragedVolume() {
 
float GetAveragedVolume() {
 
         float[] data = new float[amountSamples];
 
         float[] data = new float[amountSamples];

Latest revision as of 23:04, 8 January 2016

ATTENTION! THIS PROJECT IS NO LONGER OPEN SOURCE I WILL NO LONGER BE UPDATING THE JAVASCRIPT VERSION ON THIS WIKI! However don't worry the version you find on this wiki is still free to use for every one, even in commercial projects or to use as a base for your own scripts, I will just no longer be working on this project as an open source project. you are still free to use the scripts on this wiki as a base for your own commercial projects. The latest (close source) version will be updated frequently can be bought here[1]

Note that neither of these scripts (the original javascript and the translated C#) are up to date and contain several errors.

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

I have made a ready to use script, just place it on an empty GameObject and you are good to go.

from external scripts:

Call MicInput.loudness to get the audio data in a float format. (this can be used for scaling objects, chaning color, etc...).

Call gameObject.Find("MicInput").SendMessage("StopMicrophone"); This will terminate any data coming trough the mic in-game.

Call gameObject.Find("MicInput").SendMessage("StartMicrophone"); to start the mic from an external script (you don't need to call this as it is already running by default).

Note! StartMicrophone can only be called in the start function of a script (as that is the point where the mic is selected).

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 under the name Mic Control (still pending for clearance).

Note that some functions are script depended, for instance the java version does not have a push to talk option.

Update Log:

1.2

C#

   - Added 3 Talking methods:
       ~ Hold To Talk: Hold down a button to activate the mic.
       ~ Push To Talk: Press a button once to activate the mic, and press it again to stop it. 
       ~ Constant Talk: The microphone will constantly be activated. 
   - Improved the GUI functionality built into the script.
   - Editor GUI now shows which mic you are using.
   - General Optimization and bug fixes

1.1

   - 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.
   - C# version of the script by djfunkey
   - Changed naming of several inputs to be more clear.
   - Added Source volume selection.(converted from djfunkey).
   - Added ingame menu (selected by user whether he wants to use it or not).(converted from djfunkey).
   - Added memory cleaning to prevent audio lag, both in java and C#(djfunkey).

UnityScript (java) features:

1: An Editor menu and an ingame menu so the user/player can pick the microphone they wish to use (handy if hey have a web-cam microphone, and a headset microphone).

2: The user can now adjust the volume of their microphone, it will also affect the loudness variable proportionally.

3: If there is only 1 device detected it will make that device default.

4: Call input data from any script trough MicInput.loudness.

5: Option for showing all device names in the console + debugging option for visually reading the input data.

6: Option to listen to your audio input by toggling the mute button.

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);
//if true a menu will apear ingame with all the microphones
var SelectIngame:boolean=false;
//if false the below will override and set the mic selected in the editor
 //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 Devices2 {DefaultDevice, Second, Third, Fourth, Fifth, Sixth}
 
var InputDevice : Devices2;
private var selectedDevice:String;
 
 
 
 
var audioSource:AudioSource;
//The maximum amount of sample data that gets loaded in, best is to leave it on 256, unless you know what you are doing. A higher number gives more accuracy but 
//lowers performance allot, it is best to leave it at 256.
var amountSamples:float=256;
static var loudness:float;
var sensitivity:float=1;
var sourceVolume:float=100;
private var minFreq: int;
private var maxFreq: int;
 
var Mute:boolean=true;
var debug:boolean=false;
var ShowDeviceName:boolean=false;
private var micSelected:boolean=false; 
 
private var mTimer:float=10;
private var mRefTime:float=10; 
 
 
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);
 
}
}
if(SelectIngame==false){
//select the device if possible else give error
if(InputDevice==Devices2.DefaultDevice){
if(i>=1){
selectedDevice= Microphone.devices[0];
}
else{
Debug.LogError ("No device detected on this slot");
}
 
}
 
 
if(InputDevice==Devices2.Second){
if(i>=2){
selectedDevice= Microphone.devices[1];
}
else{
Debug.LogError ("No device detected on this slot");
}
 
}
 
 
 
if(InputDevice==Devices2.Third){
if(i>=3){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
 
 
if(InputDevice==Devices2.Fourth){
if(i>=4){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
if(InputDevice==Devices2.Fifth){
if(i>=5){
selectedDevice= Microphone.devices[2];
}
else{
Debug.LogError ("No device detected on this slot");
return;
}
}
 
if(InputDevice==Devices2.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 () {
//set timer for refreshing memory.
 mTimer += Time.deltaTime;
 //refresh the memory
if (micSelected == true){
 if (mTimer >= mRefTime) {
				StopMicrophone();
				StartMicrophone();
				mTimer = 0;
			}
		   }	
 
 
if(Microphone.IsRecording(selectedDevice)){
  loudness = GetDataStream()*sensitivity*(sourceVolume/10);
  if(debug){
  Debug.Log(loudness);
  }
  }
 
  //the source volume
  if (sourceVolume > 100){
       sourceVolume = 100;
 }
 
  if (sourceVolume < 0){
   sourceVolume = 0;
   }
  audio.volume = (sourceVolume/100);
 
 
}
 
 
function GetDataStream(){
if(Microphone.IsRecording(selectedDevice)){
 
   var dataStream: float[]  = new float[amountSamples];
       var audioValue: float = 0;
        audio.GetOutputData(dataStream,0);
 
        for(var i in dataStream){
            audioValue += Mathf.Abs(i);
        }
        return audioValue/amountSamples;
        }
 
 
 
 
 
 
}
 
 
 
 
 
//select device ingame
 
    function OnGUI () {
 if(SelectIngame==true){
        if (Microphone.devices.Length > 1 && micSelected == false)//If there is more than one device, choose one.
             for (var i:int= 0; i < Microphone.devices.Length; ++i)
                 if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), Microphone.devices[i].ToString())) {
                     StopMicrophone();
                     selectedDevice = Microphone.devices[i].ToString();
                     GetMicCaps();
                    StartMicrophone();
                     micSelected = true;
 
                }
 
        if (Microphone.devices.Length < 2 && micSelected == false) {//If there is only 1 decive make it default
             selectedDevice = Microphone.devices[0].ToString();
            GetMicCaps();
             micSelected = true;
 
        }
 }
    }
 
 
 
    //for the above control the mic start or stop
 
 
 public function StartMicrophone () {
         audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
         while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
         audio.Play(); // Play the audio source!
 
    }
 
 
 
 public function StopMicrophone () {
         audio.Stop();//Stops the audio
         Microphone.End(selectedDevice);//Stops the recording of the device  
 
    }
 
 
      function GetMicCaps () {
         Microphone.GetDeviceCaps(selectedDevice,  minFreq,  maxFreq);//Gets the frequency of the device
         if ((minFreq + maxFreq) == 0)//These 2 lines of code are mainly for windows computers
             maxFreq = 44100;
 
    }
 
 
 
 
 
 
    //Create a gui button in another script that calls to this script
        public function MicDeviceGUI (left:float , top:float, width:float, height:float, buttonSpaceTop:float, buttonSpaceLeft:float) {
	if (Microphone.devices.Length > 1 && micSelected == false)//If there is more than one device, choose one.
		for (var i:int=0; i < Microphone.devices.Length; ++i)
			if (GUI.Button(new Rect(left + (buttonSpaceLeft * i), top + (buttonSpaceTop * i), width, height), Microphone.devices[i].ToString())) {
				StopMicrophone();
				selectedDevice = Microphone.devices[i].ToString();
				GetMicCaps();
				StartMicrophone();
				micSelected = true;
			}
	if (Microphone.devices.Length < 2 && micSelected == false) {//If there is only 1 decive make it default
		selectedDevice = Microphone.devices[0].ToString();
		GetMicCaps();
		micSelected = true;
	}
    }


djfunkey did an excelent job in converting the script to C#. Thanks to him even more people can take advantage of the microphone in game. http://forum.unity3d.com/members/118660-djfunkey

The Editor Script for the C# version is below.

  • To talk in game using either of the methods press "T"*

C# features:

1: A menu so that the user can pick the microphone they wish to use (handy if hey have a web-cam microphone, and a headset microphone)

2: The user can now adjust the volume of their microphone, it will also affect the loudness variable proportionally.

3: The user can choose from 3 settings: PushtToTalk, HoldToTalk, ConstantTalk. These determine what the user has to do for the microphone to be activated.

4: If there is only 1 device detected it will make that device default.

5: Call microphone data from any script with MicrophoneInput.loudness.

6: The microphone RAM will still be flushed even if Time.timeScale = 0;

7: You can set how often the michrophone RAM is flushed.


using UnityEngine;
using System.Collections;
 
[RequireComponent(typeof(AudioSource))]
public class MicControlC : MonoBehaviour {
 
	public enum micActivation {
		HoldToSpeak,
		PushToSpeak,
		ConstantSpeak
	}
 
	public float sensitivity = 100;
	public float ramFlushSpeed = 5;//The smaller the number the faster it flush's the ram, but there might be performance issues...
	[Range(0,100)]
	public float sourceVolume = 100;//Between 0 and 100
	public bool GuiSelectDevice = true;
	public micActivation micControl;
	//
	public string selectedDevice { get; private set; }	
	public float loudness { get; private set; } //dont touch
	//
	private bool micSelected = false;
	private float ramFlushTimer;
	private int amountSamples = 256; //increase to get better average, but will decrease performance. Best to leave it
	private int minFreq, maxFreq; 
 
    void Start() {
		audio.loop = true; // Set the AudioClip to loop
		audio.mute = false; // Mute the sound, we don't want the player to hear it
		selectedDevice = Microphone.devices[0].ToString();
		micSelected = true;
		GetMicCaps();
    }
 
	void OnGUI() {
		MicDeviceGUI((Screen.width/2)-150, (Screen.height/2)-75, 300, 100, 10, -300);
		if (Microphone.IsRecording(selectedDevice)) {
			ramFlushTimer += Time.fixedDeltaTime;
			RamFlush();
		}
	}
 
	public void MicDeviceGUI (float left, float top, float width, float height, float buttonSpaceTop, float buttonSpaceLeft) {
		if (Microphone.devices.Length > 1 && GuiSelectDevice == true || micSelected == false)//If there is more than one device, choose one.
			for (int i = 0; i < Microphone.devices.Length; ++i)
				if (GUI.Button(new Rect(left + ((width + buttonSpaceLeft) * i), top + ((height + buttonSpaceTop) * i), width, height), Microphone.devices[i].ToString())) {
					StopMicrophone();
					selectedDevice = Microphone.devices[i].ToString();
					GetMicCaps();
					StartMicrophone();
					micSelected = true;
				}
		if (Microphone.devices.Length < 2 && micSelected == false) {//If there is only 1 decive make it default
			selectedDevice = Microphone.devices[0].ToString();
			GetMicCaps();
			micSelected = true;
		}
	}
 
	public void GetMicCaps () {
		Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq);//Gets the frequency of the device
		if ((minFreq + maxFreq) == 0)//These 2 lines of code are mainly for windows computers
			maxFreq = 44100;
	}
 
	public void StartMicrophone () {
		audio.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
		while (!(Microphone.GetPosition(selectedDevice) > 0)){} // Wait until the recording has started
		audio.Play(); // Play the audio source!
	}
 
	public void StopMicrophone () {
		audio.Stop();//Stops the audio
		Microphone.End(selectedDevice);//Stops the recording of the device	
	}		
 
    void Update() {
		audio.volume = (sourceVolume/100);
		loudness = GetAveragedVolume() * sensitivity * (sourceVolume/10);
		//Hold To Speak!!
		if (micControl == micActivation.HoldToSpeak) {
			if (Microphone.IsRecording(selectedDevice) && Input.GetKey(KeyCode.T) == false)
				StopMicrophone();
			//
			if (Input.GetKeyDown(KeyCode.T)) //Push to talk
				StartMicrophone();
			//
			if (Input.GetKeyUp(KeyCode.T))
				StopMicrophone();
			//
		}
		//Push To Talk!!
		if (micControl == micActivation.PushToSpeak) {
			if (Input.GetKeyDown(KeyCode.T)) {
				if (Microphone.IsRecording(selectedDevice)) 
					StopMicrophone();
 
				else if (!Microphone.IsRecording(selectedDevice)) 
					StartMicrophone();
			}
			//
		}
		//Constant Speak!!
		if (micControl == micActivation.ConstantSpeak)
			if (!Microphone.IsRecording(selectedDevice)) 
				StartMicrophone();
		//
		if (Input.GetKeyDown(KeyCode.G))
			micSelected = false; 
    }
 
	private void RamFlush () {
		if (ramFlushTimer >= ramFlushSpeed && Microphone.IsRecording(selectedDevice)) {
			StopMicrophone();
			StartMicrophone();
			ramFlushTimer = 0;
		}
	}
 
	float GetAveragedVolume() {
        float[] data = new float[amountSamples];
        float a = 0;
        audio.GetOutputData(data,0);
        foreach(float s in data) {
            a += Mathf.Abs(s);
        }
        return a/amountSamples;
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox