loop.c (952B)
1 #include <u.h> 2 #include <libc.h> 3 4 #include "util.h" 5 6 enum { 7 MSample, 8 MFrame, 9 }; 10 11 void 12 usage(void) 13 { 14 fprint(2, "usage: %s [-f] count < file\n", argv0); 15 exits("usage"); 16 } 17 18 void 19 main(int argc, char **argv) 20 { 21 int mode; 22 s16int in[MaxLength * 2], *out; 23 long i, inlen, outlen, arg; 24 char *rptr; 25 26 mode = MSample; 27 outlen = 0; 28 29 ARGBEGIN { 30 case 'f': 31 mode = MFrame; 32 break; 33 default: 34 usage(); 35 } ARGEND 36 37 if (argc != 1) usage(); 38 arg = strtoul(argv[0], &rptr, 0); 39 if (rptr == argv[0]) usage(); 40 41 inlen = read(0, in, sizeof(s16int) * MaxLength * 2); 42 if (inlen < 0) sysfatal("%r"); 43 inlen /= sizeof(s16int); 44 45 switch (mode) { 46 case MSample: 47 outlen = inlen * arg; 48 break; 49 case MFrame: 50 outlen = arg * 2; 51 break; 52 } 53 54 out = malloc(outlen * sizeof(s16int)); 55 56 for (i = 0; i < outlen; i += 2) { 57 out[i] = in[i % inlen]; 58 out[i + 1] = in[(i + 1) % inlen]; 59 } 60 61 outlen = write(1, out, outlen * sizeof(s16int)); 62 if (outlen < 0) sysfatal("%r"); 63 }