commit 39493ed6c4967b95c18a4c62c47bd12c8e07a3ce
parent 777a39d75f570dbfbbb4bf2a05a6358b296329ad
Author: zavok <an2qzavok@gmail.com>
Date: Tue, 19 Mar 2019 00:15:10 +0300
simplified code somewhat
Diffstat:
M | pcmprint.c | | | 61 | +++++++++++++++++++++++++++++-------------------------------- |
1 file changed, 29 insertions(+), 32 deletions(-)
diff --git a/pcmprint.c b/pcmprint.c
@@ -4,11 +4,10 @@
#include <assert.h>
#include <string.h>
-#define BLKSIZE 256
+#define BLKSIZE 1024 //256
#define CHLEN 32
-char *intens[]={"░", "▒", "▓", "█"};
-//char *intens[]={" ", ".", "o", "8"};
+char *intens[]={".", "0"};
static void
usage(char *cmd)
@@ -17,29 +16,27 @@ usage(char *cmd)
exit(-1);
}
-static void
-chpress(unsigned int *ch, int16_t v)
-{
- int n;
- n = (CHLEN + CHLEN * v / 0x7fff) / 2;
- if (n > 31) n--;
- assert(n>=0);
- assert(n<32);
- ch[n]++;
+static int
+norm(int v){
+ return (CHLEN + CHLEN * v / 0x7fff) / 2;
}
static void
-writech(unsigned int *ch)
+drawch(int min, int max)
{
- int i;
- for (i=0; i<CHLEN; i++){
- unsigned int c;
- c = 0;
- if (ch[i] > 0) c = 1;
- if (ch[i] > 16) c = 2;
- if (ch[i] > 32) c = 3;
- write(1, intens[c], strlen(intens[c]));
- }
+ int cc, i, Min, Max;
+ char *cv;
+ 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);;
}
int
@@ -47,23 +44,23 @@ main(int argc, char **argv)
{
ulong i, n;
int16_t *blk;
+ int lmin, lmax, rmin,rmax;
blk = malloc(BLKSIZE);
if (argc > 1){
usage(*argv);
}
while ((n = read(0, blk, BLKSIZE)) > 0){
- unsigned int *lch = calloc(sizeof (int), CHLEN);
- unsigned int *rch = calloc(sizeof (int), CHLEN);
+ lmin = rmin = 0x7fff;
+ lmax = rmax = -0x7fff;
for (i=0; i<BLKSIZE/2; i+=2){
- chpress(lch, blk[i]);
- chpress(rch, blk[i+1]);
+ if (blk[i] > lmax) lmax = blk[i];
+ if (blk[i] < lmin) lmin = blk[i];
+ if (blk[i+1] > rmax) rmax = blk[i+1];
+ if (blk[i+1] < rmin) rmin = blk[i+1];
}
- writech(lch);
- write(1, " ", 2);
- write(1, " ", 2);
- writech(rch);
+ drawch(lmin, lmax);
+ write(1, " ", 1);
+ drawch(rmin, rmax);
write(1, "\n", 1);
- free(lch);
- free(rch);
}
}