Java tutorial
//package com.java2s; /* ** AlacDecodeUtils.java ** ** Copyright (c) 2011 Peter McQuillan ** ** All Rights Reserved. ** ** Distributed under the BSD Software License (see license.txt) ** */ public class Main { static int[] predictor_decompress_fir_adapt(int[] error_buffer, int output_size, int readsamplesize, int[] predictor_coef_table, int predictor_coef_num, int predictor_quantitization) { int buffer_out_idx = 0; int[] buffer_out; int bitsmove = 0; /* first sample always copies */ buffer_out = error_buffer; if (predictor_coef_num == 0) { if (output_size <= 1) return (buffer_out); int sizeToCopy = 0; sizeToCopy = (output_size - 1) * 4; System.arraycopy(error_buffer, 1, buffer_out, 1, sizeToCopy); return (buffer_out); } if (predictor_coef_num == 0x1f) // 11111 - max value of predictor_coef_num { /* second-best case scenario for fir decompression, * error describes a small difference from the previous sample only */ if (output_size <= 1) return (buffer_out); for (int i = 0; i < (output_size - 1); i++) { int prev_value = 0; int error_value = 0; prev_value = buffer_out[i]; error_value = error_buffer[i + 1]; bitsmove = 32 - readsamplesize; buffer_out[i + 1] = (((prev_value + error_value) << bitsmove) >> bitsmove); } return (buffer_out); } /* read warm-up samples */ if (predictor_coef_num > 0) { for (int i = 0; i < predictor_coef_num; i++) { int val = 0; val = buffer_out[i] + error_buffer[i + 1]; bitsmove = 32 - readsamplesize; val = ((val << bitsmove) >> bitsmove); buffer_out[i + 1] = val; } } /* general case */ if (predictor_coef_num > 0) { buffer_out_idx = 0; for (int i = predictor_coef_num + 1; i < output_size; i++) { int j; int sum = 0; int outval; int error_val = error_buffer[i]; for (j = 0; j < predictor_coef_num; j++) { sum += (buffer_out[buffer_out_idx + predictor_coef_num - j] - buffer_out[buffer_out_idx]) * predictor_coef_table[j]; } outval = (1 << (predictor_quantitization - 1)) + sum; outval = outval >> predictor_quantitization; outval = outval + buffer_out[buffer_out_idx] + error_val; bitsmove = 32 - readsamplesize; outval = ((outval << bitsmove) >> bitsmove); buffer_out[buffer_out_idx + predictor_coef_num + 1] = outval; if (error_val > 0) { int predictor_num = predictor_coef_num - 1; while (predictor_num >= 0 && error_val > 0) { int val = buffer_out[buffer_out_idx] - buffer_out[buffer_out_idx + predictor_coef_num - predictor_num]; int sign = ((val < 0) ? (-1) : ((val > 0) ? (1) : (0))); predictor_coef_table[predictor_num] -= sign; val *= sign; // absolute value error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num)); predictor_num--; } } else if (error_val < 0) { int predictor_num = predictor_coef_num - 1; while (predictor_num >= 0 && error_val < 0) { int val = buffer_out[buffer_out_idx] - buffer_out[buffer_out_idx + predictor_coef_num - predictor_num]; int sign = -((val < 0) ? (-1) : ((val > 0) ? (1) : (0))); predictor_coef_table[predictor_num] -= sign; val *= sign; // neg value error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num)); predictor_num--; } } buffer_out_idx++; } } return (buffer_out); } }