#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 "PCM frames, stream parameters, header bytes and size, footer bytes and size, default block size";
OUTPUT "an encoded Shorten file";
VAR header_size "header size";
VAR header "header";
VAR footer_size "footer size";
VAR footer "footer";
VAR total_file_size "total file size";
VAR bits_per_sample "bits-per-sample";
VAR is_signed "is signed";
VAR is_big_endian "is big endian";
VAR channels "channels";
VAR block_size "block size";
FUNC write_unsigned "write unsigned" "shorten:write_unsigned";
FUNC write_shorten_header "write Shorten header"
"shorten:write_header";
FUNC encode_audio "encode audio commands" "shorten:encode_audio";
FUNC byte_align "byte align output";
write_shorten_header(bits_per_sample,
                     is_signed,
                     is_big_endian,
                     channels,
                     block_size);

write_unsigned(2, 9) /*VERBATIM command*/;
write_unsigned(5, header_size);
for i <- 0 to header_size {
    write_unsigned(8, header[i]);
}

encode_audio(block_size, channels);

write_unsigned(2, 9) /*VERBATIM command*/;
write_unsigned(5, footer_size);
for i <- 0 to footer_size {
    write_unsigned(8, footer[i]);
}

write_unsigned(2, 5) /*QUIT command*/;
byte_align();
/*Shorten files must be a multiple of 4 bytes long (not including the 5 bytes of magic + version) or the reference decoder's bitstream reader will fail*/;
while (total_file_size - 5) % 4 != 0 {
    0 -> write 1 bytes;
}
