#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 signed PCM sample data",
effective_channel_count,block_samples;
OUTPUT "the number of wasted bits-per-sample as an unsigned integer";
VAR block_samples "block samples";
VAR effective_channel_count "effective channel count";
VAR channel "channel";
VAR wasted_bps "wasted BPS";
VAR max_wasted_bps "maximum wasted BPS";
max_wasted_bps <- infinity /*maximum unsigned integer*/;
for c <- 0 to effective_channel_count {
    for i <- 0 to block_samples {
        wasted_bps[c][i] <- wasted(channel[c][i]);
        if wasted_bps[c][i] == 0 {
            return 0;
        } elif wasted_bps[c][i] < max_wasted_bps {
            max_wasted_bps <- wasted_bps[c][i];
        }
    }
}
if max_wasted_bps == infinity /*all samples are 0*/ {
    return 0;
} else {
    return max_wasted_bps;
}
