/* Convert linear byte samples to 4-bit 3db/step log samples. */ /* Pack the output samples 2 to a byte. */ #include const int step[16] = { 0x01, 0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0F, 0x16, 0x1F, 0x2D, 0x3F, 0x5A, 0x7F, 0xB5, 0xFF }; int samp[65536]; main() { int c; int i, diff, min_diff, l; int b, j, e; int tot = 0, cnt = 0, x, max = 0, min = 0xFFFF, avg; j = b = 0; while ((c = getchar()) != EOF) { c &= 0xFF; samp[cnt++] = c; } /* Filter out 60Hz hum */ for (i = 0; i < cnt; i++) { c = samp[i] = samp[i] + samp[i + 150]; tot += c; if (c > max) max = c; if (c < min) min = c; } avg = tot / cnt; /* normalize */ for (i = 0; i < cnt; i++) { c = samp[i]; c -= avg; c *= 256 / (max - avg); if (c < 0) c = 0; samp[i] = c; } x = 0; e = 0; while (x < cnt) { c = samp[x++] + e; l = -1; min_diff = 0x1000; for (i = 0; i < 16; i++) { diff = abs(c - step[i]); if (diff < min_diff) { l = i; min_diff = diff; } } e = c - step[l]; b = b << 4; b |= (l & 15); if (j & 1) { // if ((j&0xE)==0) printf(" DCW "); // printf("$%.2X%c", b & 0xFF, (x==(cnt-1)) || (j&0xE)==0xE ? '\n' : ','); printf("00 %.2X%c",b & 0xFF,(x==(cnt-1)) || (j&0xE)==0xE ? '\n' : ' '); } j++; } printf("\n"); return 0; }