void Awake() { microphoneUI = transform.GetComponent <MicrophoneUI>(); if (microphoneUI != null) { prewarned = !microphoneUI.AskPermission(); } else { LogMT.Log("No UI detected, defulting to skip permission step"); prewarned = true; } /*if (FindObjectOfType<AudioListener>() == null) * { * LogMT.LogWarning("MicrophoneController: No AudioListener found, creating one"); * GameObject audioListener = new GameObject(); * audioListener.name = "AudioListener"; * audioListener.AddComponent<AudioListener>(); * } * * AudioMixer audioMixer = Resources.Load("MicrophoneToolsMixer") as AudioMixer; * if (audioMixer == null) * LogMT.LogError("MicrophoneController: Could not find Audio Mixer");*/ }
void OnSoundEvent(SoundEvent soundEvent) { LogMT.Log(soundEvent.ToString()); switch (soundEvent) { case SoundEvent.PermissionGranted: prewarned = true; break; } }
/// <summary> /// Watch for changes in microphone configuration status, possibly due to user interaction, and /// configure appropriately /// </summary> private void MicrophoneConfigurationUpdate() { if (microphoneAvailable) { if (!microphoneChoiceSent) { if (!prewarned) { if (!prewarningSent) { gameObject.SendMessage("OnSoundEvent", SoundEvent.PermissionRequired, SendMessageOptions.DontRequireReceiver); prewarningSent = true; } } else if (Application.HasUserAuthorization(UserAuthorization.Microphone)) { if (Microphone.devices.Length > 0) { if (microphoneUI != null) { if (microphoneUI.UseDefaultMic()) { UseDefaultDevice(); } else { microphoneUI.ChooseDevice(Microphone.devices); } } else { UseDefaultDevice(); } microphoneChoiceSent = true; } else { if (microphoneUI != null) { microphoneUI.NoMicrophonesFound(); } LogMT.LogWarning("MicrophoneController: No microphones found"); microphoneAvailable = false; } } else if (!authorizationRequestSent) { Application.RequestUserAuthorization(UserAuthorization.Microphone); LogMT.Log("MicrophoneController: User authorization requested for microphone"); authorizationRequestSent = true; } } } }
public void OnSoundEvent(SoundEvent e) { switch (e) { case SoundEvent.BufferReady: yin = new Yin(microphoneController.SampleRate, SyllableDetectionAlgorithm.windowSize); LogMT.Log("Window Size for Algorithm: " + SyllableDetectionAlgorithm.windowSize); syllableDetectionAlgorithm = new SyllableDetectionAlgorithm(microphoneController.SampleRate); break; } }
/// <summary> /// Will set the sampling rate to the default, or the highest available from the microphone if /// this is lower than the default /// </summary> private void SetSamplingRate() { int min, max; Microphone.GetDeviceCaps(microphoneDeviceName, out min, out max); if (max == 0) { sampleRate = defaultSampleRate; } else if (defaultSampleRate > max) { sampleRate = max; } LogMT.Log("MicrophoneController: Sampling rate: " + sampleRate); }
void initialize(float yinSampleRate, int yinBufferSize) { bufferSize = yinBufferSize; sampleRate = yinSampleRate; halfBufferSize = bufferSize / 2; threshold = 0.6; //was 0.15 probability = 0.0f; //initialize array and set it to zero yinBuffer = new float[halfBufferSize]; for (int i = 0; i < halfBufferSize; i++) { yinBuffer[i] = 0; } LogMT.Log("Yin sample rate: " + yinSampleRate); LogMT.Log("Yin buffer size: " + yinBufferSize); LogMT.Log("Yin threshold: " + threshold); }
/// <summary> /// Change the microphone to the default microphone. Will fail if user authorisation has not /// been obtained or there are no microphones. /// </summary> public void UseDefaultDevice() { if (Application.HasUserAuthorization(UserAuthorization.Microphone)) { if (Microphone.devices.Length > 0) { microphoneDeviceName = ""; microphoneDeviceSet = true; SetSamplingRate(); LogMT.Log("MicrophoneController: Using default microphone"); } else { LogMT.LogError("MicrophoneController: Cannot set device: No devices available", this); } } else { LogMT.LogError("MicrophoneController: Cannot set device: User Authorization required", this); } }
/// <summary> /// Change the microphone to the given ID. Will fail if user authorisation has not been obtained /// or device doesn't exist. /// </summary> public void SetDevice(int id) { if (Application.HasUserAuthorization(UserAuthorization.Microphone)) { if ((id >= 0) && (Microphone.devices.Length > 0)) { microphoneDeviceName = Microphone.devices[id]; LogMT.Log("MicrophoneController: Using microphone: " + microphoneDeviceName); microphoneDeviceSet = true; SetSamplingRate(); } else { LogMT.LogError("MicrophoneController: Cannot set device: Device not available", this); } } else { LogMT.LogError("MicrophoneController: Cannot set device: User Authorization required", this); } }
/// <summary> /// Start microphone access and collecting samples. Wait for a SoundEvent.BufferReady signal /// through OnSoundEvent() before attempting to access sample data. /// </summary> private void StartListening() { if (testClip != null) { audioClip = testClip; channels = audioClip.channels; sampleRate = audioClip.frequency; } else { audioClip = Microphone.Start(microphoneDeviceName, true, 1, sampleRate); channels = 1; // Fetching the number of channels from the audio clip gives incorrect // results, possibly due to a Unity bug } LogMT.Log("Audio Channels: " + channels); listening = true; waitingForAudio = true; gameObject.SendMessage("OnSoundEvent", SoundEvent.AudioStart, SendMessageOptions.DontRequireReceiver); }