private void PrepareLpcSubframe(int order, int subframeBitsPerSample) { const int InvalidLpcPrecision = 15; int[] warmupSamples = new int[order]; for (int i = 0; i < order; i++) { warmupSamples[i] = bitReader.ReadSignedBits(subframeBitsPerSample - this.wastedBitsPerSample); } uint precisionCode = bitReader.ReadBits(4); if (precisionCode == InvalidLpcPrecision) { throw new FlacException("Invalid subframe coefficient precision"); } int precision = (int)(precisionCode + 1); int shift = bitReader.ReadSignedBits(5); int[] coefficients = new int[order]; for (int i = 0; i < order; i++) { coefficients[i] = bitReader.ReadSignedBits(precision); } IEnumerator <int> residual = ReadResidualData(bitReader, BlockSize, order); IPredictor predictor = PredictorFactory.CreateLpcPredictor(coefficients, shift, RemoveLastItem(warmupSamples)); dataSource = GetPredictorSamples(BlockSize, warmupSamples, predictor, residual); }
private void PrepareFixedSubframe(int order, int subframeBitsPerSample) { int[] warmupSamples = new int[order]; for (int i = 0; i < order; i++) { warmupSamples[i] = bitReader.ReadSignedBits(subframeBitsPerSample - this.wastedBitsPerSample); } IEnumerator <int> residual = ReadResidualData(bitReader, BlockSize, order); IPredictor predictor = PredictorFactory.CreateFixedPredictor(order, RemoveLastItem(warmupSamples)); dataSource = GetPredictorSamples(BlockSize, warmupSamples, predictor, residual); }
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)); }
private FlacMethod FindBestFixedMethod(int[] channelSamples, int bitsPerSample, FlacEncodingPolicy policy) { if (!policy.FixedOrder.HasValue) { return(null); } int minFixedOrder = policy.FixedOrder.Value.MinValue; int maxFixedOrder = Math.Min(channelSamples.Length - 1, policy.FixedOrder.Value.MaxValue); FlacMethod[] methods = new FlacMethod[maxFixedOrder + 1]; parallel.For(minFixedOrder, maxFixedOrder + 1, order => { IPredictor predictor = PredictorFactory.CreateFixedPredictor(order, ArrayUtils.CutArray(channelSamples, 0, order - 1)); FlacResidualCoefficeints residual = FindBestResidual(channelSamples, order, predictor, policy); FlacMethod method = new FlacFixedMethod(bitsPerSample, order, residual); methods[order] = method; }); return(FindBestMethod(methods)); }
internal IPredictor CreatePredictor(int[] samples) { return(PredictorFactory.CreateLpcPredictor(Coefficients, ResultShift, samples)); }
internal override IPredictor CreatePredictor(int[] samples) { return(PredictorFactory.CreateFixedPredictor(Order, samples)); }