PCM转G711的JAVA代码

  能跑,没测试。心情问题……

	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;
	}

猜你喜欢

转载自blog.csdn.net/quantum7/article/details/81005181