const char * UsageLines [] = { "swpeaks: Display positive and negative peaks of specified", "signed 16-bit PCM sound files.", "Usage: swpeaks (input files)", "Uses standard input if no input files specified.", "Reads each sample as two bytes, least significant byte first.", "For the purposes of this program:", " MS LS", "01111111 11111111 = +32767", " .", " .", "00000000 00000000 = +0", "11111111 11111111 = -0", " .", " .", "10000000 00000000 = -32767", "Only displays + peak if there are + values. Only displays - peak if", "there are - values. If only one peak is shown, or if the peaks", "aren't reasonably symmetrical, check if input really is", "PCM signed ls-first.", "If no peaks are displayed then the input contains no samples.", "May 16, 2011. Newest is at gopher -p users/julianbr sdf.org", }; const int NumUsageLines = sizeof (UsageLines)/sizeof (UsageLines [0] ); #include <stdio.h> void Peaks (void) { unsigned long int value, peak, PositivePeak, NegativePeak; int positive, negative; int ls, ms; positive = 0; negative = 0; PositivePeak = 0; NegativePeak = 0; ms = EOF; ls = getchar (); if (ls != EOF) ms = getchar (); while (ms != EOF) { value = 256*(ms ^ 128) + ls; if (value < 32768) { negative = 1; peak = 32767 - value; if (NegativePeak < peak) NegativePeak = peak; } else { positive = 1; peak = value - 32768; if (PositivePeak < peak) PositivePeak = peak; } ms = EOF; ls = getchar (); if (ls != EOF) ms = getchar (); } if (negative) printf ("-%lu", NegativePeak); if (positive) printf (",+%lu", PositivePeak); putchar ('\n'); } int main (int argc, char * argv [] ) { int i; if (argc > 1) { fprintf (stderr, "***swpeaks: Unrecognized"); fprintf (stderr, " \"%s\".\n", argv [1] ); for (i = 0; i < NumUsageLines; i++) printf ("%s\n", UsageLines [i] ); } else Peaks (); return 0; }