pcmprint.c (2145B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <string.h> 5 #include <fcntl.h> 6 7 static size_t BLKSIZE = 1024; 8 static int32_t CHLEN = 30; 9 char *intens = "`@"; 10 11 #define NORM(x) (CHLEN + CHLEN * x / 0x7fff) / 2 12 13 static void 14 usage(char *cmd) 15 { 16 dprintf(2, "usage: %s [-w WIDTH] [-b BLOCKSIZE] [pcmfile]\n", cmd); 17 exit(-1); 18 } 19 20 static void 21 drawch(int32_t min, int32_t max) 22 { 23 size_t i; 24 int32_t Min, Max; 25 Min = NORM(min); 26 Max = NORM(max); 27 for (i = 0; i < CHLEN; i++) 28 if ((i>=Min)&&(i<=Max)) write(1, &intens[1], 1); 29 else write(1, &intens[0], 1); 30 } 31 32 static ulong 33 readblk(int f, int16_t *blk) 34 { 35 size_t n, m; 36 m = 0; 37 while(m!=BLKSIZE){ 38 n = read(f, &blk[m/2], BLKSIZE-m); 39 m += n; 40 if (n == 0) break; 41 } 42 return m; 43 } 44 45 int 46 main(int argc, char **argv) 47 { 48 char buf[11]; 49 int ch; 50 size_t i, n, l, s; 51 int16_t *blk; 52 int32_t f, lmin, lmax, rmin,rmax; 53 f = 0; 54 while ((ch = getopt(argc, argv, "b:w:")) != -1){ 55 switch (ch){ 56 case 'b': 57 BLKSIZE = atoi(optarg); 58 if (BLKSIZE <= 0){ 59 dprintf(2, "Error: -b too small: %ld\n", BLKSIZE); 60 exit(1); 61 } 62 break; 63 case 'w': 64 CHLEN = atoi(optarg); 65 if (CHLEN <= 0){ 66 dprintf(2, "Error: -w too small: %d\n", CHLEN); 67 exit(1); 68 } 69 break; 70 default: 71 usage(argv[0]); 72 } 73 } 74 if (argc-optind > 1) usage(argv[0]); 75 if (argv[optind] != 0){ 76 f = open(argv[optind], O_RDONLY); 77 if (f == -1) { 78 dprintf(2, "Error: can't open %s\n", argv[optind]); 79 exit(1); 80 } 81 } 82 BLKSIZE = BLKSIZE * 4; /*2 channels x 2 bytes*/ 83 blk = malloc(BLKSIZE); 84 l = 44100*4; 85 s = -1; 86 while ((n = readblk(f, blk)) != 0){ 87 lmin = rmin = 0x7fff; 88 lmax = rmax = -0x7fff; 89 for (i=0; i<BLKSIZE/2; i+=2){ 90 if (blk[i] > lmax) lmax = blk[i]; 91 if (blk[i] < lmin) lmin = blk[i]; 92 if (blk[i+1] > rmax) rmax = blk[i+1]; 93 if (blk[i+1] < rmin) rmin = blk[i+1]; 94 blk[i] = blk[i+1] = 0; 95 } 96 l += n; 97 drawch(lmin, lmax); 98 if (l > 44100*4){ 99 s++; 100 l -= 44100*4; 101 snprintf(buf, 11, " %02d:%02d:%02d ", 102 (int)(s/3600)%99, (int)(s/60)%60, (int)s%60); 103 write(1, buf, 10); 104 } else write(1, " ", 10); 105 drawch(rmin, rmax); 106 write(1, "\n", 1); 107 } 108 return 0; 109 }