/// <summary> /// Subscribes to automatic voice detection. /// </summary> /// <param name="listener"> /// The listener which is to receive notification when the player starts and stops speaking via /// automatic voice detection. /// </param> public void SubcribeToVoiceActivation(IVoiceActivationListener listener) { if (listener == null) { throw new ArgumentNullException("listener", "Cannot subscribe with a null listener"); } if (MicCapture == null) { _activationListeners.Add(listener); } else { MicCapture.Subscribe(listener); } }
private void Start() { //Ensure that all settings are loaded before we access them (potentially from other threads) DebugSettings.Preload(); VoiceSettings.Preload(); //Write multithreaded logs ASAP so the logging system knows which is the main thread Logs.WriteMultithreadedLogs(); var net = gameObject.GetComponent <ICommsNetwork>(); if (net == null) { throw new Exception("Cannot find a voice network component. Please attach a voice network component appropriate to your network system to the DissonanceVoiceComms' entity."); } if (PlaybackPrefab == null) { Log.Info("Loading default playback prefab"); PlaybackPrefab = Resources.Load <GameObject>("PlaybackPrefab").GetComponent <VoicePlayback>(); } net.PlayerJoined += Net_PlayerJoined; net.PlayerLeft += Net_PlayerLeft; net.VoicePacketReceived += Net_VoicePacketReceived; net.PlayerStartedSpeaking += Net_PlayerStartedSpeaking; net.PlayerStoppedSpeaking += Net_PlayerStoppedSpeaking; net.TextPacketReceived += _text.OnMessageReceived; if (string.IsNullOrEmpty(LocalPlayerName)) { var guid = Guid.NewGuid().ToString(); LocalPlayerName = guid; } //mark this component as started, locking the LocalPlayerName, PlaybackPrefab and Microphone properties from changing _started = true; MicCapture = MicrophoneCapture.Start(_micName); _localPlayerState = new LocalVoicePlayerState(LocalPlayerName, this); _players.Add(_localPlayerState); _playersLookup.Add(LocalPlayerName, _localPlayerState); Action <NetworkMode> networkModeChanged = mode => { if (mode.IsClientEnabled()) { var encoder = new OpusEncoder(VoiceSettings.Instance.Quality, VoiceSettings.Instance.FrameSize); _decoderFrameSize = (uint)encoder.FrameSize; _decoderSampleRate = encoder.SampleRate; _transmissionPipeline = new EncoderPipeline(MicCapture, encoder, _net, () => _playerChannels.Count + _roomChannels.Count); for (var i = 0; i < _activationListeners.Count; i++) { MicCapture.Subscribe(_activationListeners[i]); } } else { if (_transmissionPipeline != null) { _transmissionPipeline.Dispose(); _transmissionPipeline = null; } for (var i = 0; i < _activationListeners.Count; i++) { MicCapture.Unsubscribe(_activationListeners[i]); } } }; if (MicCapture != null) { net.ModeChanged += networkModeChanged; } else { Log.Warn("No microphone detected; local voice transmission will be disabled."); } net.Initialize(LocalPlayerName, Rooms, PlayerChannels, RoomChannels); _net = net; }