void Update()
        {
            if (recording == null)
            {
                return;
            }

            var now = Microphone.GetPosition(null);

            var length = now - lastPos;

            if (now < lastPos)
            {
                lastPos = 0;
                length  = now;
            }

            while (length >= recordingBuffer.Length)
            {
                if (_isListening && recording.GetData(recordingBuffer, lastPos))
                {
                    //Send..
                    var amplitude = AudioUtils.GetMaxAmplitude(recordingBuffer);
                    if (amplitude >= minAmplitude)
                    {
                        index++;
                        if (OnAudioGenerated != null)
                        {
                            //Downsample if needed.
                            if (recordingBuffer != resampleBuffer)
                            {
                                AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode));
                            }

                            var data = encoder.Encode(resampleBuffer);

                            VoipFragment frag = new VoipFragment(0, index, data, encoder.mode);

                            OnAudioGenerated?.Invoke(frag);
                        }
                    }
                }
                length  -= recordingBuffer.Length;
                lastPos += recordingBuffer.Length;
            }
        }
        void Update()
        {
            if (recording == null)
            {
                return;
            }

            var now    = Microphone.GetPosition(null);
            var length = now - lastPos;

            if (now < lastPos)
            {
                lastPos = 0;
                length  = now;
            }

            while (length >= recordingBuffer.Length)
            {
                if (recording.GetData(recordingBuffer, lastPos))
                {
                    //Send..
                    var amplitude = AudioUtils.GetMaxAmplitude(recordingBuffer);
                    if (amplitude >= minAmplitude)
                    {
                        index++;
                        if (OnAudioGenerated != null)
                        {
                            chunkCount++;

                            //Downsample if needed.
                            if (recordingBuffer != resampleBuffer)
                            {
                                AudioUtils.Resample(recordingBuffer, resampleBuffer, inputFreq, AudioUtils.GetFrequency(encoder.mode));
                            }

                            var data = encoder.Encode(resampleBuffer);

                            bytes += data.Length + 11;

                            VoipFragment frag = new VoipFragment(0, index, data, encoder.mode);

                            foreach (var action in OnAudioGenerated.GetInvocationList())
                            {
                                try
                                {
                                    action.DynamicInvoke(frag);
                                }
                                catch (Exception e)
                                {
                                    Misc.Logger.Error($"Exception on OnAudioGenerated event in {action.Target.GetType().ToString()}.{action.Method.Name}: {e}");
                                }
                            }
                        }
                    }
                }
                length  -= recordingBuffer.Length;
                lastPos += recordingBuffer.Length;
            }

            UpdateStats();
        }