pcmprint

CLI PCM visualiser
git clone git://nsmpr.xyz/pcmprint.git
Log | Files | Refs | README

commit e0212198dd7ab453a0616bdeda6a395a1811f204
parent 39493ed6c4967b95c18a4c62c47bd12c8e07a3ce
Author: zavok <an2qzavok@gmail.com>
Date:   Tue, 19 Mar 2019 15:25:35 +0300

added -w -b parameters, reading from file

Diffstat:
Mpcmprint.c | 72++++++++++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 48 insertions(+), 24 deletions(-)

diff --git a/pcmprint.c b/pcmprint.c @@ -1,18 +1,17 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> -#include <assert.h> #include <string.h> +#include <fcntl.h> +static int BLKSIZE = 1024; +static int CHLEN = 32; -#define BLKSIZE 1024 //256 -#define CHLEN 32 - -char *intens[]={".", "0"}; +char intens[] = {'.', '0'}; static void usage(char *cmd) { - dprintf(2, "usage: %s [pcmfile]", cmd); + dprintf(2, "usage: %s [-w WIDTH] [-b BLOCKSIZE] [pcmfile]\n", cmd); exit(-1); } @@ -24,32 +23,49 @@ norm(int v){ static void drawch(int min, int max) { - int cc, i, Min, Max; - char *cv; + int i, Min, Max; Min = norm(min); Max = norm(max); - cv = intens[0]; - cc = strlen(cv); - for (i = 0; i < Min; i++) write(1, cv, cc); - cv = intens[1]; - cc = strlen(cv); - for (i = Min; i < Max; i++) write(1, cv, cc); - cv = intens[0]; - cc = strlen(cv); - for (i = Max; i < CHLEN; i++) write(1, cv, cc);; + for (i = 0; i < CHLEN; i++) + if ((i>=Min)&&(i<=Max)) write(1, &intens[1], 1); + else write(1, &intens[0], 1); } int main(int argc, char **argv) { - ulong i, n; + char buf[11]; + ulong i, n, l, s; int16_t *blk; - int lmin, lmax, rmin,rmax; - blk = malloc(BLKSIZE); - if (argc > 1){ - usage(*argv); + int ch, f, lmin, lmax, rmin,rmax; + f = 0; + while ((ch = getopt(argc, argv, "b:w:")) != -1){ + switch (ch){ + case 'b': + BLKSIZE = atoi(optarg); + if (BLKSIZE <= 0) BLKSIZE = 1; + break; + case 'w': + CHLEN = atoi(optarg); + if (CHLEN <= 0) CHLEN = 1; + break; + default: + usage(argv[0]); + } } - while ((n = read(0, blk, BLKSIZE)) > 0){ + if (argc-optind > 1) usage(argv[0]); + if (argv[optind] != 0){ + f = open(argv[optind], O_RDONLY); + if (f == -1) { + dprintf(2, "Error: can't open %s\n", argv[optind]); + exit(-1); + } + } + BLKSIZE = BLKSIZE * 4; /*2 channels x 2 bytes*/ + blk = malloc(BLKSIZE); + l = 44100*4; + s = -1; + while ((n = read(f, blk, BLKSIZE)) > 0){ lmin = rmin = 0x7fff; lmax = rmax = -0x7fff; for (i=0; i<BLKSIZE/2; i+=2){ @@ -58,9 +74,17 @@ main(int argc, char **argv) if (blk[i+1] > rmax) rmax = blk[i+1]; if (blk[i+1] < rmin) rmin = blk[i+1]; } + l += n; drawch(lmin, lmax); - write(1, " ", 1); + if (l > 44100*4){ + s++; + l -= 44100*4; + snprintf(buf, 11, " %02d:%02d:%02d ", + (s/3600)%99, (s/60)%60, s%60); + write(1, buf, 10); + } else write(1, " ", 10); drawch(rmin, rmax); write(1, "\n", 1); } + return 0; }