能跑,沒測試。心情問題……
private final static short SHORT_MAX = 0x7FFF;
public static byte[] convertPcmToG711(byte[] pcmBuffer, int length, byte[] g711Buffer)
{
length = length/2;
if (pcmBuffer == null)
{
pcmBuffer = new byte[length];
}
for (int i=0; i<length; i++)
{
short pcm = ToolKit.getShort(pcmBuffer, i*2);
int sign = (pcm & 0x8000) >> 8;
if (sign != 0)
{
pcm = (short)-pcm;
}
if (pcm > SHORT_MAX)
{
pcm = SHORT_MAX;
}
int exponent = 7;
int expMask;
//有的使用數組來代替這個步驟。
for (expMask = 0x4000; (pcm & expMask) == 0 && exponent>0; exponent--, expMask >>= 1)
{
//
}
int mantissa = (pcm >> ((exponent == 0) ? 4 : (exponent + 3))) & 0x0F;
byte alaw = (byte)(sign | exponent << 4 | mantissa);
g711Buffer[i] = (byte)(alaw^0xD5);
}
return g711Buffer;
}