Example #1
0
 private static int[] RemoveLastItem(int[] array)
 {
     return(ArrayUtils.CutArray(array, 0, array.Length - 1));
 }
        private FlacMethod FindBestLpcMethod(int[] channelSamples, int bitsPerSample, FlacEncodingPolicy policy)
        {
            if (!policy.LpcOrder.HasValue)
            {
                return(null);
            }

            int minLpcOrder = policy.LpcOrder.Value.MinValue;
            int maxLpcOrder = Math.Min(policy.LpcOrder.Value.MaxValue,
                                       channelSamples.Length - 1);

            double[] r = new double[maxLpcOrder + 1];
            parallel.For(0, r.Length, i =>
            {
                double sum = 0;
                for (int j = 0, q = channelSamples.Length - i; j < i; j++, q++)
                {
                    sum += (double)channelSamples[j] * channelSamples[q];
                }
                for (int j = i, q = 0; j < channelSamples.Length; j++, q++)
                {
                    sum += (double)channelSamples[j] * channelSamples[q];
                }
                r[i] = sum;
            });

            FlacMethod[] methods = new FlacMethod[maxLpcOrder];
            parallel.For(minLpcOrder, maxLpcOrder + 1, order =>
            {
                double[] coef = SolveLpc(r, order);

                int[] integerCoefficients;
                int shift;
                int precision;
                ConvertLpcCoeficientsToIntegers(coef, out integerCoefficients, out precision, out shift);

                IPredictor predictor = PredictorFactory.CreateLpcPredictor(integerCoefficients, shift, ArrayUtils.CutArray(channelSamples, 0, order - 1));
                FlacResidualCoefficeints residual         = FindBestResidual(channelSamples, order, predictor, policy);
                FlacLpcMethodCoefficeints lpcCoefficients = new FlacLpcMethodCoefficeints(
                    precision, integerCoefficients, shift);

                FlacMethod method  = new FlacLpcMethod(bitsPerSample, lpcCoefficients, residual);
                methods[order - 1] = method;
            });

            return(FindBestMethod(methods));
        }