public static FixResult FindLengthOfClick(
            AudioData audioData,
            int initPosition,
            int maxLength,
            int positionOfLastProcessedSample)
        {
            var bestResult = new FixResult
            {
                Success = false
            };

            var position    = initPosition;
            var minPosition = GetMinPosition(
                position,
                positionOfLastProcessedSample,
                10);

            while (position > minPosition)
            {
                var backup = BackupPredictionErrAverage(
                    audioData,
                    position,
                    audioData.AudioProcessingSettings.HistoryLengthSamples + maxLength);

                var result = TryToFix(audioData,
                                      position,
                                      maxLength,
                                      initPosition - position);

                if (result.BetterThan(bestResult))
                {
                    bestResult = result;
                }

                audioData.CurrentChannelRestoreInitState(position, maxLength + 16);
                RestorePredictionErrAverage(
                    audioData,
                    position,
                    audioData.AudioProcessingSettings.HistoryLengthSamples + maxLength,
                    backup);

                position--;
            }

            return(bestResult);
        }
        private static FixResult TryToFix(
            AudioData audioData,
            int index,
            int maxLength,
            int minLength)
        {
            var result = new FixResult
            {
                Success  = false,
                Position = index,
                Length   = minLength,
                ErrSum   = float.MaxValue
            };

            ClickRepairer.Repair(audioData, index, minLength);

            while (result.Length < maxLength)
            {
                ClickRepairer.Repair(audioData, index + result.Length, 1);
                result.Length++;

                if (!SeveralSamplesInARowAreSuspicious(
                        audioData, index + result.Length, 3))
                {
                    result.ErrSum = CalcErrSum(
                        audioData, index + result.Length, 4);

                    // if click fixed
                    if (result.ErrSum < 0.01F) //0.005F //0.03F
                    {
                        result.Success = true;
                        break;
                    }
                }
            }

            return(result);
        }