#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 "1 or more channels of signed PCM frames, the number of channels",
encoding_parameters;
OUTPUT "1 or more ALAC frames as a frameset";
VAR encoding_parameters "encoding parameters";
VAR channel_count "channel count";
VAR frameset "frameset";
VAR mono "mono";
VAR fL "front left";
VAR fR "front right";
VAR fC "front center";
VAR bC "surround center";
VAR cL "center left";
VAR cR "center right";
VAR bL "surround left";
VAR bR "surround right";
VAR LFE "LFE";
FUNC encode_1ch_frame "encode 1 channel frame" "alac:encode_frame";
FUNC encode_2ch_frame "encode 2 channel frame" "alac:encode_frame";
FUNC byte_align "byte align output stream";
switch channel_count {
    case 1 {
        encode_1ch_frame(mono);
    }
    case 2 {
        encode_2ch_frame(fL, fR);
    }
    case 3 {
        encode_1ch_frame(fC);
        encode_2ch_frame(fL, fR);
    }
    case 4 {
        encode_1ch_frame(fC);
        encode_2ch_frame(fL, fR);
        encode_1ch_frame(bC);
    }
    case 5 {
        encode_1ch_frame(fC);
        encode_2ch_frame(fL, fR);
        encode_2ch_frame(bL, bR);
    }
    case 6 {
        encode_1ch_frame(fC);
        encode_2ch_frame(fL, fR);
        encode_2ch_frame(bL, bR);
        encode_1ch_frame(LFE);
    }
    case 7 {
        encode_1ch_frame(fC);
        encode_2ch_frame(fL, fR);
        encode_2ch_frame(bL, bR);
        encode_1ch_frame(bC);
        encode_1ch_frame(LFE);
    }
    case 8 {
        encode_1ch_frame(fC);
        encode_2ch_frame(cL, cR);
        encode_2ch_frame(fL, fR);
        encode_2ch_frame(bL, bR);
        encode_1ch_frame(LFE);
    }
}
7 -> write 3 unsigned;
byte_align();
return frameset;
