const char * UsageLines [] = {
       "Usage: swsample (upsample factor) (downsample factor)",
       "Reads two-byte samples from standard input.",
       "Writes resampled version to standard output.",
       "The two factors must be integers > 0.",
       "The samples are not changed, simply repeated 0 or more times per",
       "the specified ratio.  Filtering may be needed after upsampling.",
       "May 16, 2011.  Newest is at gopher -p users/julianbr sdf.org",
       };
const int NumUsageLines = sizeof (UsageLines)/sizeof (UsageLines [0] );

#include <stdio.h>
#define SampleSize 2


void Sample (unsigned long int up, unsigned long int down)
       {
       unsigned long int remainder;
       unsigned char sample [SampleSize];

       remainder = down/2;
       while (fread (sample, sizeof (sample), 1, stdin) == 1) {
               remainder += up;
               while (remainder >= down) {
                       fwrite (sample, sizeof (sample), 1, stdout);
                       remainder -= down;
                       }
               }
       }


int main (int argc, char * argv [] )
       {
       unsigned long int up, down;
       int i, ok;
       char c;

       if (argc < 2) {
               for (i = 0; i < NumUsageLines; i++)
                       printf ("%s\n", UsageLines [i] );
               }
       else if (argc == 3) {
               ok = 1;
               if (sscanf (argv [1], "%lu%c", & up, & c) != 1
                                       || up < 1) {
                       fprintf (stderr, "***swsample: Expecting number > 0");
                       fprintf (stderr, " for upsample factor, found");
                       fprintf (stderr, " \"%s\".\n", argv [1] );
                       ok = 0;
                       }
               if (sscanf (argv [2], "%lu%c", & down, & c) != 1
                                       || down < 1) {
                       fprintf (stderr, "***swsample: Expecting number > 0");
                       fprintf (stderr, " for upsample factor, found");
                       fprintf (stderr, " \"%s\".\n", argv [2] );
                       ok = 0;
                       }
               if (ok)
                       Sample (up, down);
               }
       else {
               fprintf (stderr, "Usage: swsample");
               fprintf (stderr, " (upsample factor) (downsample factor)\n");
               }
       return 0;
       }