protected override void Awake() { base.Awake(); AudioListener audioListener = FindObjectOfType <AudioListener>(); if (audioListener != null) { this.ac = audioListener.GetComponent <AudioOutCapture>(); if (this.ac == null) { this.ac = audioListener.gameObject.AddComponent <AudioOutCapture>(); } } else if (this.Logger.IsErrorEnabled) { this.Logger.LogError("AudioListener component is required"); } }
private bool SetOrSwitchAudioListener(AudioListener audioListener, bool extraChecks) { if (audioListener == null) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("audioListener passed is null or is being destroyed"); } return(false); } if (extraChecks) { if (audioListener.gameObject.activeInHierarchy) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("The GameObject to which the audioListener is attached is not active in hierarchy"); } return(false); } if (!audioListener.enabled) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("audioListener passed is disabled"); } return(false); } } AudioOutCapture audioOutCapture = audioListener.GetComponent <AudioOutCapture>(); if (audioOutCapture == null) { audioOutCapture = audioListener.gameObject.AddComponent <AudioOutCapture>(); } return(this.SetOrSwitchAudioOutCapture(audioOutCapture, false)); }
/// <summary> /// Set the AudioOutCapture to be used with this WebRtcAudioDsp /// </summary> /// <param name="audioOutCapture">The audioOutCapture to be used</param> /// <returns>Success or failure</returns> public bool SetOrSwitchAudioOutCapture(AudioOutCapture audioOutCapture) { return(this.SetOrSwitchAudioOutCapture(audioOutCapture, true)); }
private bool SetOrSwitchAudioOutCapture(AudioOutCapture audioOutCapture, bool extraChecks) { if (audioOutCapture == null) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("audioOutCapture passed is null or is being destroyed"); } return(false); } if (!audioOutCapture.enabled) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("audioOutCapture passed is disabled"); } return(false); } if (extraChecks) { if (audioOutCapture.gameObject.activeInHierarchy) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("The GameObject to which the audioOutCapture is attached is not active in hierarchy"); } return(false); } AudioListener audioListener = audioOutCapture.GetComponent <AudioListener>(); if (audioListener == null) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("The AudioListener attached to the same GameObject as the audioOutCapture is null or being destroyed"); } return(false); } if (!audioListener.enabled) { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("The AudioListener attached to the same GameObject as the audioOutCapture is disabled"); } return(false); } } if (this.ac != null) { if (this.ac != audioOutCapture) { if (this.started) { this.ac.OnAudioFrame -= this.OnAudioOutFrameFloat; } } else { if (this.Logger.IsErrorEnabled) { this.Logger.LogError("The same audioOutCapture is being used already"); } return(false); } } this.ac = audioOutCapture; if (this.started) { this.ac.OnAudioFrame += this.OnAudioOutFrameFloat; } return(true); }
public MicWrapperPusher(string device, Transform parentTransform, int suggestedFrequency, ILogger lg, bool destroyOnStop = true) { try { this.logger = lg; this.device = device; this.destroyGameObjectOnStop = destroyOnStop; if (UnityMicrophone.devices.Length < 1) { this.Error = "No microphones found (Microphone.devices is empty)"; this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error); return; } if (!string.IsNullOrEmpty(device) && !UnityMicrophone.devices.Contains(device)) { this.logger.LogError("[PV] MicWrapperPusher: \"{0}\" is not a valid Unity microphone device, falling back to default one", device); device = UnityMicrophone.devices[0]; } this.sampleRate = AudioSettings.outputSampleRate; switch (AudioSettings.speakerMode) { case AudioSpeakerMode.Mono: this.channels = 1; break; case AudioSpeakerMode.Stereo: this.channels = 2; break; default: this.Error = string.Concat("Only Mono and Stereo project speaker mode supported. Current mode is ", AudioSettings.speakerMode); this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error); return; } int minFreq; int maxFreq; this.logger.LogInfo("[PV] MicWrapperPusher: initializing microphone '{0}', suggested frequency = {1}).", device, suggestedFrequency); UnityMicrophone.GetDeviceCaps(device, out minFreq, out maxFreq); int frequency = suggestedFrequency; // minFreq = maxFreq = 44100; // test like android client if (suggestedFrequency < minFreq || maxFreq != 0 && suggestedFrequency > maxFreq) { this.logger.LogWarning("[PV] MicWrapperPusher does not support suggested frequency {0} (min: {1}, max: {2}). Setting to {2}", suggestedFrequency, minFreq, maxFreq); frequency = maxFreq; } GameObject gO = new GameObject("[PV] MicWrapperPusher: AudioSource + AudioOutCapture"); if (!parentTransform || parentTransform == null) { this.logger.LogWarning("[PV] MicWrapperPusher: Parent transform passed is destroyed or null. Creating AudioSource GameObject at root."); } else { gO.transform.SetParent(parentTransform, false); if (!gO.activeSelf) { this.logger.LogWarning("[PV] MicWrapperPusher: AudioSource GameObject inactive, activating it."); this.audioSource.gameObject.SetActive(true); } if (!gO.activeInHierarchy) { this.Error = "AudioSource GameObject is not active in hierarchy, audio input can't work."; this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error); return; } } this.audioSource = gO.AddComponent <AudioSource>(); this.audioOutCapture = this.audioSource.gameObject.AddComponent <AudioOutCapture>(); this.mic = UnityMicrophone.Start(device, true, 1, frequency); this.audioSource.mute = true; this.audioSource.volume = 0f; this.audioSource.clip = this.mic; this.audioSource.loop = true; this.audioSource.Play(); this.logger.LogInfo("[PV] MicWrapperPusher: microphone '{0}' initialized, frequency = in:{1}|out:{2}, channels = in:{3}|out:{4}.", device, this.mic.frequency, this.SamplingRate, this.mic.channels, this.Channels); } catch (Exception e) { this.Error = e.ToString(); if (this.Error == null) // should never happen but since Error used as validity flag, make sure that it's not null { this.Error = "Exception in MicWrapperPusher constructor"; } this.logger.LogError("[PV] MicWrapperPusher: {0}", this.Error); } }