#include #include int UlawToLinear(int in) { int S, E, M, V; S = (in & 0x80) >> 7; E = (in & 0x70) >> 4; M = in & 0x0f; V = (1 - 2*S) * ( pow(2,E) * (2*M + 33) - 33 ); return(V); } int LinearToUlaw(int x) { int S, i, curr, last, V; if (x>=0) S = 0; /* Keep track of sign and make x positive. */ else { S = 0x80; x = -x; } last = 0; V = 127; for (i=1; i<128; i++) { curr = UlawToLinear(i); if (curr >= x) { if ( (curr-x) < (x-last) ) V = i; else V = i - 1; break; } last = curr; } return(V+S); } void main() { int i, j, position; int pcmIN, linearOUT, pcmOUT; double atten, attenDB, fpcmOUT; for (position=0; position<=31; position++) { /* Make 32 256-byte tables. */ printf("%i attenuation (dB): "); /* Get amount in dB from designer, */ scanf("%f\n", attenDB); /* negative for attenuation, positive for gain. */ atten = exp(log(10)*attenDB/10); /* Convert to fraction. */ for (i=0; i<=15; i++) { /* Construct output file in rows of 16. */ printf("%04x:", position*256 + i*16); for (j=0; j<=15; j++) { pcmIN = i*16 + j; fpcmOUT = atten * UlawToLinear(pcmIN); if (fpcmOUT >=0) linearOUT = floor(fpcmOUT + 0.5); /* Rounding */ else linearOUT = ceil(fpcmOUT - 0.5); pcmOUT = LinearToUlaw(linearOUT); printf(" %2x", pcmOUT); } printf("\n"); } } }