Code: Select all
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
static const uint8_t qtab[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
};
static const int16_t rtab[8] = { -32768, -18596, -13872, -3135, 3135, 13872, 18596, 32767 };
int main(int argc, char *argv[])
{
int dithered = 1, shaped = 1;
if (argc == 2)
{
shaped = atoi(argv[1]) & 2;
dithered = atoi(argv[1]) & 1;
}
FILE *f = fopen("input.raw", "rb");
fseek(f, 0, SEEK_END);
int s = ftell(f);
rewind(f);
unsigned char *inbytes = (unsigned char *)malloc(s);
fread(inbytes, 1, s, f);
fclose(f);
int outs = ((s * 3) + 7) / 8;
unsigned char *outbytes = (unsigned char *)malloc(outs);
for (unsigned int b = 0; b < outs; b++)
{
outbytes[b] = 0;
}
float e1 = 0.0, e2 = 0.0;
for (unsigned int b = 0; b < s; b++)
{
unsigned char inbyte = inbytes[b];
float sample = (inbyte - 128) / 128.0;
if (shaped)
sample -= e1 * 0.61 + e2 * 0.21;
int inshort = rint(sample * 32768.0) + 32768;
if (dithered)
inshort += (rand() & 0x1fff) - (rand() & 0x1fff);
inshort = (inshort + 128) >> 8;
if (inshort < 0) inshort = 0;
if (inshort > 255) inshort = 255;
unsigned char result = qtab[inshort];
e2 = e1;
e1 = sample - (rtab[result] - 32768) / 32768.0;
unsigned char targeted_trip = b % 8;
// printf("Converting input byte %02X into output triplet %d.\n", inbyte, result);
switch (targeted_trip)
{
case (0):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 0);
} break;
case (1):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 3);
} break;
case (2):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 6);
outbytes[((b * 3 / 8) + 1)] |= (result >> 2);
} break;
case (3):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 1);
} break;
case (4):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 4);
} break;
case (5):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 7);
outbytes[((b * 3 / 8) + 1)] |= (result >> 1);
} break;
case (6):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 2);
} break;
case (7):
{
outbytes[((b * 3 / 8) + 0)] |= (result << 5);
} break;
}
}
FILE *fout = fopen("output.bin", "wb");
fwrite(outbytes, 1, outs, fout);
fclose(fout);
for (unsigned int b = 0; b < s; b++)
{
unsigned int targeted_trip = (b % 8);
unsigned char value;
switch (targeted_trip)
{
case (0):
{
value = (outbytes[((b * 3) / 8) + 0] >> 0);
} break;
case (1):
{
value = (outbytes[((b * 3) / 8) + 0] >> 3);
} break;
case (2):
{
value = (outbytes[((b * 3) / 8) + 0] >> 6)
| (outbytes[((b * 3) / 8) + 1] << 2);
} break;
case (3):
{
value = (outbytes[((b * 3) / 8) + 0] >> 1);
} break;
case (4):
{
value = (outbytes[((b * 3) / 8) + 0] >> 4);
} break;
case (5):
{
value = (outbytes[((b * 3) / 8) + 0] >> 7)
| (outbytes[((b * 3) / 8) + 1] << 1);
} break;
case (6):
{
value = (outbytes[((b * 3) / 8) + 0] >> 2);
} break;
case (7):
{
value = (outbytes[((b * 3) / 8) + 0] >> 5);
} break;
}
value &= 7;
// printf("Retrieved %d value from stream.\n", value);
long int valuelong = rtab[value];
valuelong >>= 8;
valuelong += 128;
inbytes[b] = (unsigned char) valuelong;
}
char filename[32];
sprintf(filename, "outputtestd%ds%d.raw", dithered ? 1 : 0, shaped ? 1 : 0);
FILE *out2 = fopen(filename, "wb"); // Open this as 8bit pcm to see what the system is trying to play.
fwrite(inbytes, 1, s, out2);
fclose(out2);
free(inbytes);
free(outbytes);
return 0;
}