private void Update() { var samplePosition = Microphone.GetPosition(_currentDevice); if (samplePosition < 0 || _headPosition == samplePosition) { return; } if (!IsMute) { _audioClip.GetData(_microphoneBuffer, 0); while (GetDataSize(_microphoneBuffer.Length, _headPosition, samplePosition) > _frameBuffer.Length) { var remain = _microphoneBuffer.Length - _headPosition; if (remain < _frameBuffer.Length) { Array.Copy(_microphoneBuffer, _headPosition, _frameBuffer, 0, remain); Array.Copy(_microphoneBuffer, 0, _frameBuffer, remain, _frameBuffer.Length - remain); } else { Array.Copy(_microphoneBuffer, _headPosition, _frameBuffer, 0, _frameBuffer.Length); } _headPosition = (_headPosition + _frameBuffer.Length) % _microphoneBuffer.Length; OnProcessFrame?.Invoke(_frameBuffer); } } }
public void Update() { // Nothing to do when the stream is inactive. if (!IsValid) { return; } while (_ring.Count > _frameBuffer.Length) { lock (_ring) { _ring.Dequeue(new Span <float>(_frameBuffer)); } OnProcessFrame?.Invoke(_frameBuffer); } }