#This work is licensed under the
#Creative Commons Attribution-Share Alike 3.0 United States License.
#To view a copy of this license, visit
#http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
#Creative Commons,
#171 Second Street, Suite 300,
#San Francisco, California, 94105, USA.

INPUT block_samples,effective_channel_count,channels,
decorrelation_term_count,decorrelation_terms,
decorrelation_deltas,decorrelation_weights,decorrelation_samples;
OUTPUT correlated,decorrelation_weights,decorrelation_samples;
VAR channels "channels";
VAR effective_channel_count "effective channel count";
VAR block_samples "block samples";
VAR correlated "correlated";
VAR decorrelation_term_count "term count";
VAR decorrelation_terms "terms";
VAR decorrelation_deltas "deltas";
VAR decorrelation_weights "weights";
VAR decorrelation_samples "samples";
FUNC correlate_1ch "correlate 1 channel" "wavpack:correlate_1ch";
FUNC correlate_2ch "correlate 2 channels" "wavpack:correlate_2ch";
if effective_channel_count == 1 {
    correlated[0] <- channels[0];
    for p <- decorrelation_term_count downto 0 {
        correlated[0],
        decorrelation_weights[p][0],
        decorrelation_samples[p][0] <-
            correlate_1ch(block_samples,
                          correlated[0],
                          decorrelation_terms[p],
                          decorrelation_deltas[p],
                          decorrelation_weights[p][0],
                          decorrelation_samples[p][0]);
    }
} else {
    correlated <- channels;
    for p <- decorrelation_term_count downto 0 {
        correlated,
        decorrelation_weights[p],
        decorrelation_samples[p] <-
            correlate_2ch(block_samples,
                          correlated,
                          decorrelation_terms[p],
                          decorrelation_deltas[p],
                          decorrelation_weights[p],
                          decorrelation_samples[p]);
    }
}
return correlated,
decorrelation_weights,
decorrelation_samples;
