public void SendVoicePacket(PcmArray floatData) { // Don't send anything out if we're muted if (OurUserState == null || OurUserState.Mute) { floatData.UnRef(); return; } if (_manageSendBuffer != null) { _manageSendBuffer.SendVoice(floatData, SpeechTarget.Normal, 0); } }
public CompressedBuffer Encode(OpusEncoder encoder, out bool isStop, out bool isEmpty) { isStop = false; isEmpty = false; PcmArray nextPcmToSend = null; ArraySegment <byte> encoder_buffer; lock (_bufferLock) { // Make sure we have data, or an end event if (_unencodedBuffer.Count == 0) { Monitor.Wait(_bufferLock); } // If there are still no unencoded buffers, then we return an empty packet if (_unencodedBuffer.Count == 0) { isEmpty = true; } else { if (_unencodedBuffer.Count == 1 && _isWaitingToSendLastPacket) { isStop = true; } TargettedSpeech speech = _unencodedBuffer.Dequeue(); isStop = isStop || speech.IsStop; nextPcmToSend = speech.PcmData; if (isStop) { _isWaitingToSendLastPacket = false; } } } if (nextPcmToSend == null || nextPcmToSend.Pcm.Length == 0) { isEmpty = true; } encoder_buffer = isEmpty ? EmptyByteSegment : encoder.Encode(nextPcmToSend.Pcm); byte[] pos = nextPcmToSend == null ? null : nextPcmToSend.PositionalData; int posLen = nextPcmToSend == null ? 0 : nextPcmToSend.PositionalDataLength; if (nextPcmToSend != null) { nextPcmToSend.UnRef(); } if (isStop) { Debug.Log("Resetting encoder state"); encoder.ResetState(); } CompressedBuffer compressedBuffer = new CompressedBuffer { EncodedData = encoder_buffer, PositionalData = pos, PositionalDataLength = posLen }; return(compressedBuffer); }
void SendVoiceIfReady() { int currentPosition = Microphone.GetPosition(_currentMic); //Debug.Log(currentPosition + " " + Microphone.IsRecording(_currentMic)); //Debug.Log(currentPosition); if (currentPosition < _previousPosition) { _numTimesLooped++; } //Debug.Log("mic position: " + currentPosition + " was: " + _previousPosition + " looped: " + _numTimesLooped); int totalSamples = currentPosition + _numTimesLooped * NumSamplesInMicBuffer; bool isEmpty = currentPosition == 0 && _previousPosition == 0; bool isFirstSample = _numTimesLooped == 0 && _previousPosition == 0; _previousPosition = currentPosition; if (isEmpty) { _secondsWithoutMicSamples += Time.deltaTime; } else { _secondsWithoutMicSamples = 0; } if (_secondsWithoutMicSamples > MaxSecondsWithoutMicData) { // For 5 times in a row, we received no usable data // this normally means that the mic we were using disconnected Debug.Log("Mic has disconnected!"); StopSendingAudio(); if (OnMicDisconnect != null) { OnMicDisconnect(); } return; } // We drop the first sample, because it generally starts with // a lot of pre-existing, stale, audio data which we couldn't // use b/c it's too old if (isFirstSample) { _totalNumSamplesSent = totalSamples; return; } while (totalSamples - _totalNumSamplesSent >= NumSamplesPerOutgoingPacket) { PcmArray newData = _mumbleClient.GetAvailablePcmArray(); if (!_mumbleClient.UseSyntheticSource) { SendAudioClip.GetData(newData.Pcm, _totalNumSamplesSent % NumSamplesInMicBuffer); } else { TestingClipToUse.GetData(newData.Pcm, _totalNumSamplesSent % TestingClipToUse.samples); } //Debug.Log(Time.frameCount + " " + currentPosition); _totalNumSamplesSent += NumSamplesPerOutgoingPacket; if (VoiceSendingType == MicType.Amplitude) { if (AmplitudeHigherThan(MinAmplitude, newData.Pcm)) { _sampleNumberOfLastMinAmplitudeVoice = _totalNumSamplesSent; if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); } else { if (_totalNumSamplesSent > _sampleNumberOfLastMinAmplitudeVoice + _voiceHoldSamples) { newData.UnRef(); continue; } if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); // If this is the sample before the hold turns off, stop sending after it's sent if (_totalNumSamplesSent + NumSamplesPerOutgoingPacket > _sampleNumberOfLastMinAmplitudeVoice + _voiceHoldSamples) { _mumbleClient.StopSendingVoice(); } } } else { if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); } } }
void SendVoiceIfReady() { int currentPosition = Microphone.GetPosition(_currentMic); //Debug.Log(currentPosition + " " + Microphone.IsRecording(_currentMic)); if (currentPosition < _previousPosition) { _numTimesLooped++; } //Debug.Log("mic position: " + currentPosition + " was: " + _previousPosition + " looped: " + _numTimesLooped); int totalSamples = currentPosition + _numTimesLooped * NumSamplesInMicBuffer; _previousPosition = currentPosition; while (totalSamples - _totalNumSamplesSent >= NumSamplesPerOutgoingPacket) { PcmArray newData = _mumbleClient.GetAvailablePcmArray(); if (!_mumbleClient.UseSyntheticSource) { SendAudioClip.GetData(newData.Pcm, _totalNumSamplesSent % NumSamplesInMicBuffer); } else { TestingClipToUse.GetData(newData.Pcm, _totalNumSamplesSent % TestingClipToUse.samples); } //Debug.Log(Time.frameCount + " " + currentPosition); _totalNumSamplesSent += NumSamplesPerOutgoingPacket; if (VoiceSendingType == MicType.Amplitude) { if (AmplitudeHigherThan(MinAmplitude, newData.Pcm)) { _sampleNumberOfLastMinAmplitudeVoice = _totalNumSamplesSent; if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); } else { if (_totalNumSamplesSent > _sampleNumberOfLastMinAmplitudeVoice + _voiceHoldSamples) { newData.UnRef(); continue; } if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); // If this is the sample before the hold turns off, stop sending after it's sent if (_totalNumSamplesSent + NumSamplesPerOutgoingPacket > _sampleNumberOfLastMinAmplitudeVoice + _voiceHoldSamples) { _mumbleClient.StopSendingVoice(); } } } else { if (OnMicData != null) { OnMicData(newData); } if (_writePositionalDataFunc != null) { _writePositionalDataFunc(ref newData.PositionalData, ref newData.PositionalDataLength); } else { newData.PositionalDataLength = 0; } _mumbleClient.SendVoicePacket(newData); } } }