#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 2 channels of PCM data",
channel_count, sample_count, bits_per_sample, encoding_parameters;
OUTPUT "uncompressed ALAC frame, frame's size in bits";
VAR encoding_parameters "encoding parameters";
VAR channel "channel";
VAR channel_count "channel count";
VAR sample_count "sample count";
VAR block_size "block size";
VAR bits_per_sample "bits-per-sample";
VAR uncompressed_frame "uncompressed frame";
VAR uncompressed_frame_size "uncompressed frame size";
block_size <- encoding_parameters;
0 -> write 16 unsigned /*unused*/;
if sample_count != block_size  {
    1 -> write 1 unsigned /*has sample count*/;
    0 -> write 2 unsigned /*uncompressed LSBs*/;
    1 -> write 1 unsigned /*not compressed*/;
    sample_count -> write 32 unsigned;
} else {
    0 -> write 1 unsigned /*has sample count*/;
    0 -> write 2 unsigned /*uncompressed LSBs*/;
    1 -> write 1 unsigned /*not compressed*/;
}
for i <- 0 to sample_count {
    for c <- 1 to channel_count {
        channel[c][i] -> write bits_per_sample signed;
    }
}
return uncompressed_frame,uncompressed_frame_size;
