private static byte encode(int pcm)
{
//Get the sign bit. Shift it for later use without further modification
int sign = (pcm & 0x8000) >> 8;
//If the number is negative, make it positive (now it's a magnitude)
if (sign != 0)
pcm = -pcm;
//The magnitude must be less than 32635 to avoid overflow
if (pcm > MAX) pcm = MAX;
//Add 132 to guarantee a 1 in the eight bits after the sign bit
pcm += BIAS;
/* Finding the "exponent"
* Bits:
* 1 2 3 4 5 6 7 8 9 A B C D E F G
* S 7 6 5 4 3 2 1 0 . . . . . . .
* We want to find where the first 1 after the sign bit is.
* We take the corresponding value from the second row as the exponent value.
* (i.e. if first 1 at position 7 -> exponent = 2) */
int exponent = 7;
//Move to the right and decrement exponent until we hit the 1
for (int expMask = 0x4000; (pcm & expMask) == 0; exponent--, expMask >>= 1) { }
/* The last part - the "mantissa"
* We need to take the four bits after the 1 we just found.
* To get it, we shift 0x0f :
* 1 2 3 4 5 6 7 8 9 A B C D E F G
* S 0 0 0 0 0 1 . . . . . . . . . (meaning exponent is 2)
* . . . . . . . . . . . . 1 1 1 1
* We shift it 5 times for an exponent of two, meaning
* we will shift our four bits (exponent + 3) bits.
* For convenience, we will actually just shift the number, then and with 0x0f. */
int mantissa = (pcm >> (exponent + 3)) & 0x0f;
//The mu-law byte bit arrangement is SEEEMMMM (Sign, Exponent, and Mantissa.)
byte mulaw = (byte)(sign | exponent << 4 | mantissa);
//Last is to flip the bits
return (byte)~mulaw;
}
}