1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Audio; // required for dealing with audiomixers
  4.  
  5. [RequireComponent(typeof(AudioSource))]
  6. public class MicrophoneListener : MonoBehaviour {
  7.  
  8. //Written in part by Benjamin Outram
  9.  
  10. //option to toggle the microphone listenter on startup or not
  11. public bool startMicOnStartup = true;
  12.  
  13. //allows start and stop of listener at run time within the unity editor
  14. public bool stopMicrophoneListener = false;
  15. public bool startMicrophoneListener = false;
  16.  
  17. private bool microphoneListenerOn = false;
  18.  
  19. //public to allow temporary listening over the speakers if you want of the mic output
  20. //but internally it toggles the output sound to the speakers of the audiosource depending
  21. //on if the microphone listener is on or off
  22. public bool disableOutputSound = false; /
  23.  
  24. //an audio source also attached to the same object as this script is
  25. AudioSource src;
  26.  
  27. //make an audio mixer from the "create" menu, then drag it into the public field on this script.
  28. //double click the audio mixer and next to the "groups" section, click the "+" icon to add a
  29. //child to the master group, rename it to "microphone". Then in the audio source, in the "output" option,
  30. //select this child of the master you have just created.
  31. //go back to the audiomixer inspector window, and click the "microphone" you just created, then in the
  32. //inspector window, right click "Volume" and select "Expose Volume (of Microphone)" to script,
  33. //then back in the audiomixer window, in the corner click "Exposed Parameters", click on the "MyExposedParameter"
  34. //and rename it to "Volume"
  35. public AudioMixer masterMixer;
  36.  
  37. float timeSinceRestart = 0;
  38.  
  39. void Start() {
  40. //start the microphone listener
  41. if (startMicOnStartup) {
  42. RestartMicrophoneListener ();
  43. StartMicrophoneListener ();
  44. }
  45. }
  46.  
  47. void Update(){
  48.  
  49. //can use these variables that appear in the inspector, or can call the public functions directly from other scripts
  50. if (stopMicrophoneListener) {
  51. StopMicrophoneListener ();
  52. }
  53. if (startMicrophoneListener) {
  54. StartMicrophoneListener ();
  55. }
  56. //reset paramters to false because only want to execute once
  57. stopMicrophoneListener = false;
  58. startMicrophoneListener = false;
  59.  
  60. //must run in update otherwise it doesnt seem to work
  61. MicrophoneIntoAudioSource (microphoneListenerOn);
  62.  
  63. //can choose to unmute sound from inspector if desired
  64. DisableSound (!disableOutputSound);
  65.  
  66. }
  67.  
  68. //stops everything and returns audioclip to null
  69. public void StopMicrophoneListener(){
  70. //stop the microphone listener
  71. microphoneListenerOn = false;
  72. //reenable the master sound in mixer
  73. disableOutputSound = false;
  74. //remove mic from audiosource clip
  75. src.Stop ();
  76. src.clip = null;
  77.  
  78. Microphone.End (null);
  79. }
  80.  
  81. public void StartMicrophoneListener(){
  82. //start the microphone listener
  83. microphoneListenerOn = true;
  84. //disable sound output (dont want to hear mic input on the output!)
  85. disableOutputSound = true;
  86. //reset the audiosource
  87. RestartMicrophoneListener ();
  88. }
  89.  
  90. //controls whether the volume is on or off, use "off" for mic input (dont want to hear your own voice input!)
  91. //and "on" for music input
  92. public void DisableSound(bool SoundOn){
  93.  
  94. float volume = 0;
  95.  
  96. if (SoundOn) {
  97. volume = 0.0f;
  98. } else {
  99. volume = -80.0f;
  100. }
  101.  
  102. masterMixer.SetFloat ("MasterVolume", volume);
  103. }
  104.  
  105. // restart microphone removes the clip from the audiosource
  106. public void RestartMicrophoneListener(){
  107.  
  108. src = GetComponent<AudioSource>();
  109.  
  110. //remove any soundfile in the audiosource
  111. src.clip = null;
  112.  
  113. timeSinceRestart = Time.time;
  114.  
  115. }
  116.  
  117. //puts the mic into the audiosource
  118. void MicrophoneIntoAudioSource (bool MicrophoneListenerOn){
  119.  
  120. if(MicrophoneListenerOn){
  121. //pause a little before setting clip to avoid lag and bugginess
  122. if (Time.time - timeSinceRestart > 0.5f && !Microphone.IsRecording (null)) {
  123. src.clip = Microphone.Start (null, true, 10, 44100);
  124.  
  125. //wait until microphone position is found (?)
  126. while (!(Microphone.GetPosition (null) > 0)) {
  127. }
  128.  
  129. src.Play (); // Play the audio source
  130. }
  131. }
  132. }
  133.  
  134. }