| 1 | /************************************************************************************ |
| 2 | Filename : OVRMicInput.cs |
| 3 | Content : Interface to microphone input |
| 4 | Created : May 12, 2015 |
| 5 | Copyright : Copyright 2015 Oculus VR, Inc. All Rights reserved. |
| 6 | |
| 7 | Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License"); |
| 8 | you may not use the Oculus VR Rift SDK except in compliance with the License, |
| 9 | which is provided at the time of installation or download, or which |
| 10 | otherwise accompanies this software in either electronic or hard copy form. |
| 11 | |
| 12 | You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.oculusvr.com/licenses/LICENSE-3.1 |
| 15 | |
| 16 | Unless required by applicable law or agreed to in writing, the Oculus VR SDK |
| 17 | distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | See the License for the specific language governing permissions and |
| 20 | limitations under the License. |
| 21 | ************************************************************************************/ |
| 22 | using UnityEngine; |
| 23 | using System.Collections; |
| 24 | |
| 25 | [RequireComponent(typeof(AudioSource))] |
| 26 | |
| 27 | public class OVRMicInput : MonoBehaviour |
| 28 | { |
| 29 | public enum micActivation |
| 30 | { |
| 31 | HoldToSpeak, |
| 32 | PushToSpeak, |
| 33 | ConstantSpeak |
| 34 | } |
| 35 | |
| 36 | // PUBLIC MEMBERS |
| 37 | public AudioSource audioSource = null; |
| 38 | public bool GuiSelectDevice = true; |
| 39 | |
| 40 | [SerializeField] |
| 41 | private float sensitivity = 100; |
| 42 | public float Sensitivity |
| 43 | { |
| 44 | get{return sensitivity;} |
| 45 | set{sensitivity = Mathf.Clamp (value, 0, 100);} |
| 46 | } |
| 47 | |
| 48 | [SerializeField] |
| 49 | private float sourceVolume = 100; |
| 50 | public float SourceVolume |
| 51 | { |
| 52 | get{return sourceVolume;} |
| 53 | set{sourceVolume = Mathf.Clamp (value, 0, 100);} |
| 54 | } |
| 55 | |
| 56 | [SerializeField] |
| 57 | private int micFrequency = 16000; |
| 58 | public float MicFrequency |
| 59 | { |
| 60 | get{return micFrequency;} |
| 61 | set{micFrequency = (int)Mathf.Clamp ((float)value, 0, 96000);} |
| 62 | } |
| 63 | |
| 64 | |
| 65 | public micActivation micControl; |
| 66 | |
| 67 | public string selectedDevice; |
| 68 | |
| 69 | public float loudness; // Use this to chenge visual values. Range is 0 - 100 |
| 70 | |
| 71 | // PRIVATE MEMBERS |
| 72 | private bool micSelected = false; |
| 73 | private int minFreq, maxFreq; |
| 74 | private bool focused = true; |
| 75 | |
| 76 | //---------------------------------------------------- |
| 77 | // MONOBEHAVIOUR OVERRIDE FUNCTIONS |
| 78 | //---------------------------------------------------- |
| 79 | |
| 80 | /// <summary> |
| 81 | /// Awake this instance. |
| 82 | /// </summary> |
| 83 | void Awake() |
| 84 | { |
| 85 | // First thing to do, cache the unity audio source (can be managed by the |
| 86 | // user if audio source can change) |
| 87 | if (!audioSource) audioSource = GetComponent<AudioSource>(); |
| 88 | if (!audioSource) return; // this should never happen |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Start this instance. |
| 93 | /// </summary> |
| 94 | void Start() |
| 95 | { |
| 96 | audioSource.loop = true; // Set the AudioClip to loop |
| 97 | audioSource.mute = false; |
| 98 | |
| 99 | if(Microphone.devices.Length!= 0) |
| 100 | { |
| 101 | selectedDevice = Microphone.devices[0].ToString(); |
| 102 | micSelected = true; |
| 103 | GetMicCaps(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Update this instance. |
| 109 | /// </summary> |
| 110 | void Update() |
| 111 | { |
| 112 | if (!focused) |
| 113 | StopMicrophone(); |
| 114 | |
| 115 | if (!Application.isPlaying) |
| 116 | StopMicrophone(); |
| 117 | |
| 118 | audioSource.volume = (sourceVolume / 100); |
| 119 | loudness = Mathf.Clamp(GetAveragedVolume() * sensitivity * (sourceVolume / 10), 0, 100); |
| 120 | |
| 121 | //Hold To Speak |
| 122 | if (micControl == micActivation.HoldToSpeak) |
| 123 | { |
| 124 | if (Microphone.IsRecording(selectedDevice) && Input.GetKey(KeyCode.Space) == false) |
| 125 | StopMicrophone(); |
| 126 | |
| 127 | if (Input.GetKeyDown(KeyCode.Space)) //Push to talk |
| 128 | StartMicrophone(); |
| 129 | |
| 130 | if (Input.GetKeyUp(KeyCode.Space)) |
| 131 | StopMicrophone(); |
| 132 | } |
| 133 | |
| 134 | //Push To Talk |
| 135 | if (micControl == micActivation.PushToSpeak) |
| 136 | { |
| 137 | if (Input.GetKeyDown(KeyCode.Space)) |
| 138 | { |
| 139 | if (Microphone.IsRecording(selectedDevice)) |
| 140 | StopMicrophone(); |
| 141 | else if (!Microphone.IsRecording(selectedDevice)) |
| 142 | StartMicrophone(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | //Constant Speak |
| 147 | if (micControl == micActivation.ConstantSpeak) |
| 148 | if (!Microphone.IsRecording(selectedDevice)) |
| 149 | StartMicrophone(); |
| 150 | |
| 151 | |
| 152 | //Mic Slected = False |
| 153 | if (Input.GetKeyDown(KeyCode.M)) |
| 154 | micSelected = false; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /// <summary> |
| 159 | /// Raises the application focus event. |
| 160 | /// </summary> |
| 161 | /// <param name="focus">If set to <c>true</c> focus.</param> |
| 162 | void OnApplicationFocus(bool focus) |
| 163 | { |
| 164 | focused = focus; |
| 165 | |
| 166 | // fixes app with a delayed buffer if going out of focus |
| 167 | if (!focused) |
| 168 | StopMicrophone(); |
| 169 | } |
| 170 | |
| 171 | /// <summary> |
| 172 | /// Raises the application pause event. |
| 173 | /// </summary> |
| 174 | /// <param name="focus">If set to <c>true</c> focus.</param> |
| 175 | void OnApplicationPause(bool focus) |
| 176 | { |
| 177 | focused = focus; |
| 178 | |
| 179 | // fixes app with a delayed buffer if going out of focus |
| 180 | if (!focused) |
| 181 | StopMicrophone(); |
| 182 | } |
| 183 | |
| 184 | void OnDisable() |
| 185 | { |
| 186 | StopMicrophone(); |
| 187 | } |
| 188 | |
| 189 | /// <summary> |
| 190 | /// Raises the GU event. |
| 191 | /// </summary> |
| 192 | void OnGUI() |
| 193 | { |
| 194 | MicDeviceGUI((Screen.width/2)-150, (Screen.height/2)-75, 300, 50, 10, -300); |
| 195 | } |
| 196 | |
| 197 | //---------------------------------------------------- |
| 198 | // PUBLIC FUNCTIONS |
| 199 | //---------------------------------------------------- |
| 200 | |
| 201 | /// <summary> |
| 202 | /// Mics the device GU. |
| 203 | /// </summary> |
| 204 | /// <param name="left">Left.</param> |
| 205 | /// <param name="top">Top.</param> |
| 206 | /// <param name="width">Width.</param> |
| 207 | /// <param name="height">Height.</param> |
| 208 | /// <param name="buttonSpaceTop">Button space top.</param> |
| 209 | /// <param name="buttonSpaceLeft">Button space left.</param> |
| 210 | public void MicDeviceGUI (float left, float top, float width, float height, float buttonSpaceTop, float buttonSpaceLeft) |
| 211 | { |
| 212 | //If there is more than one device, choose one. |
| 213 | if (Microphone.devices.Length >= 1 && GuiSelectDevice == true && micSelected == false) |
| 214 | { |
| 215 | for (int i = 0; i < Microphone.devices.Length; ++i) |
| 216 | { |
| 217 | if (GUI.Button(new Rect(left + ((width + buttonSpaceLeft) * i), top + ((height + buttonSpaceTop) * i), width, height), |
| 218 | Microphone.devices[i].ToString())) |
| 219 | { |
| 220 | StopMicrophone(); |
| 221 | selectedDevice = Microphone.devices[i].ToString(); |
| 222 | micSelected = true; |
| 223 | GetMicCaps(); |
| 224 | StartMicrophone(); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// <summary> |
| 231 | /// Gets the mic caps. |
| 232 | /// </summary> |
| 233 | public void GetMicCaps () |
| 234 | { |
| 235 | if(micSelected == false) return; |
| 236 | |
| 237 | //Gets the frequency of the device |
| 238 | Microphone.GetDeviceCaps(selectedDevice, out minFreq, out maxFreq); |
| 239 | |
| 240 | if ( minFreq == 0 && maxFreq == 0 ) |
| 241 | { |
| 242 | Debug.LogWarning ("GetMicCaps warning:: min and max frequencies are 0"); |
| 243 | minFreq = 44100; |
| 244 | maxFreq = 44100; |
| 245 | } |
| 246 | |
| 247 | if (micFrequency > maxFreq) |
| 248 | micFrequency = maxFreq; |
| 249 | } |
| 250 | |
| 251 | /// <summary> |
| 252 | /// Starts the microphone. |
| 253 | /// </summary> |
| 254 | public void StartMicrophone () |
| 255 | { |
| 256 | if(micSelected == false) return; |
| 257 | |
| 258 | //Starts recording |
| 259 | audioSource.clip = Microphone.Start(selectedDevice, true, 1, micFrequency); |
| 260 | |
| 261 | // Wait until the recording has started |
| 262 | while (!(Microphone.GetPosition(selectedDevice) > 0)){} |
| 263 | |
| 264 | // Play the audio source |
| 265 | audioSource.Play(); |
| 266 | } |
| 267 | |
| 268 | /// <summary> |
| 269 | /// Stops the microphone. |
| 270 | /// </summary> |
| 271 | public void StopMicrophone () |
| 272 | { |
| 273 | if(micSelected == false) return; |
| 274 | |
| 275 | // Overriden with a clip to play? Don't stop the audio source |
| 276 | if((audioSource != null) && (audioSource.clip != null) &&(audioSource.clip.name == "Microphone")) |
| 277 | audioSource.Stop(); |
| 278 | |
| 279 | Microphone.End(selectedDevice); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | //---------------------------------------------------- |
| 284 | // PRIVATE FUNCTIONS |
| 285 | //---------------------------------------------------- |
| 286 | |
| 287 | /// <summary> |
| 288 | /// Gets the averaged volume. |
| 289 | /// </summary> |
| 290 | /// <returns>The averaged volume.</returns> |
| 291 | float GetAveragedVolume() |
| 292 | { |
| 293 | // We will use the SR to get average volume |
| 294 | // return OVRSpeechRec.GetAverageVolume(); |
| 295 | return 0.0f; |
| 296 | } |
| 297 | } |