📄 tconversiontool.java
字号:
buffer[i]=linear2alaw((short) (((byte) (buffer[i]+128)) << 8)); } } } /** * Fills outBuffer with alaw samples. * reading starts from inBuffer[inByteOffset]. * writing starts at outBuffer[outByteOffset]. * There will be sampleCount <B>bytes</B> written to outBuffer. */ public static void pcm82alaw(byte[] inBuffer, int inByteOffset, byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) { int alawIndex=outByteOffset; int pcmIndex=inByteOffset; if (signed) { while (sampleCount>0) { outBuffer[alawIndex++]= linear2alaw((short) (inBuffer[pcmIndex++] << 8)); sampleCount--; } } else { while (sampleCount>0) { outBuffer[alawIndex++]= linear2alaw((short) (((byte) (inBuffer[pcmIndex++]+128)) << 8)); sampleCount--; } } } /** * Converts an alaw buffer to 8bit pcm samples * The 8bit bytes overwrite the original alaw values. * The first byte-offset of the aLaw bytes is byteOffset. * It will be written sampleCount bytes. */ public static void alaw2pcm8(byte[] buffer, int byteOffset, int sampleCount, boolean signed) { sampleCount+=byteOffset; if (signed) { for (int i=byteOffset; i<sampleCount; i++) { buffer[i]=(byte) ((a2l[buffer[i] & 0xFF] >> 8) & 0xFF); } } else { for (int i=byteOffset; i<sampleCount; i++) { buffer[i]=(byte) ((a2l[buffer[i] & 0xFF]>>8)+128); } } } /** * Fills outBuffer with alaw samples. * reading starts from inBuffer[inByteOffset]. * writing starts at outBuffer[outByteOffset]. * There will be sampleCount <B>bytes</B> written to outBuffer. */ public static void alaw2pcm8(byte[] inBuffer, int inByteOffset, byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) { int alawIndex=inByteOffset; int pcmIndex=outByteOffset; if (signed) { while (sampleCount>0) { outBuffer[pcmIndex++]= (byte) ((a2l[inBuffer[alawIndex++] & 0xFF] >> 8) & 0xFF); sampleCount--; } } else { while (sampleCount>0) { outBuffer[pcmIndex++]= (byte) ((a2l[inBuffer[alawIndex++] & 0xFF]>>8)+128); sampleCount--; } } } /** * Fills outBuffer with pcm signed 16 bit samples. * reading starts from inBuffer[inByteOffset]. * writing starts at outBuffer[outByteOffset]. * There will be sampleCount bytes read from inBuffer; * There will be sampleCount*2 bytes written to outBuffer. */ public static void alaw2pcm16(byte[] inBuffer, int inByteOffset, byte[] outBuffer, int outByteOffset, int sampleCount, boolean bigEndian) { int shortIndex=outByteOffset; int alawIndex=inByteOffset; while (sampleCount>0) { intToBytes16 (a2l[inBuffer[alawIndex++] & 0xFF], outBuffer, shortIndex++, bigEndian); shortIndex++; sampleCount--; } } //////////////////////// cross conversion alaw <-> ulaw //////////////////////////////////////// private static byte[] u2a = { -86, -85, -88, -87, -82, -81, -84, -83, -94, -93, -96, -95, -90, -89, -92, -91, -70, -69, -72, -71, -66, -65, -68, -67, -78, -77, -80, -79, -74, -73, -76, -75, -118, -117, -120, -119, -114, -113, -116, -115, -126, -125, -128, -127, -122, -121, -124, -123, -101, -104, -103, -98, -97, -100, -99, -110, -109, -112, -111, -106, -105, -108, -107, -22, -24, -23, -18, -17, -20, -19, -30, -29, -32, -31, -26, -25, -28, -27, -6, -8, -2, -1, -4, -3, -14, -13, -16, -15, -10, -9, -12, -11, -53, -55, -49, -51, -62, -61, -64, -63, -58, -57, -60, -59, -38, -37, -40, -39, -34, -33, -36, -35, -46, -46, -45, -45, -48, -48, -47, -47, -42, -42, -41, -41, -44, -44, -43, -43, 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37, 58, 59, 56, 57, 62, 63, 60, 61, 50, 51, 48, 49, 54, 55, 52, 53, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 106, 104, 105, 110, 111, 108, 109, 98, 99, 96, 97, 102, 103, 100, 101, 122, 120, 126, 127, 124, 125, 114, 115, 112, 113, 118, 119, 116, 117, 75, 73, 79, 77, 66, 67, 64, 65, 70, 71, 68, 69, 90, 91, 88, 89, 94, 95, 92, 93, 82, 82, 83, 83, 80, 80, 81, 81, 86, 86, 87, 87, 84, 84, 85, 85, }; public static byte ulaw2alaw(byte sample) { return u2a[sample & 0xFF]; } /** * Converts a buffer of uLaw samples to aLaw. */ public static void ulaw2alaw(byte[] buffer, int byteOffset, int sampleCount) { sampleCount+=byteOffset; for (int i=byteOffset; i<sampleCount; i++) { buffer[i]=u2a[buffer[i] & 0xFF]; } } /** * Fills outBuffer with alaw samples. */ public static void ulaw2alaw(byte[] inBuffer, int inByteOffset, byte[] outBuffer, int outByteOffset, int sampleCount) { int ulawIndex=outByteOffset; int alawIndex=inByteOffset; while (sampleCount>0) { outBuffer[alawIndex++]=u2a[inBuffer[ulawIndex++] & 0xFF]; sampleCount--; } } private static byte[] a2u = { -86, -85, -88, -87, -82, -81, -84, -83, -94, -93, -96, -95, -90, -89, -92, -91, -71, -70, -73, -72, -67, -66, -69, -68, -79, -78, -80, -80, -75, -74, -77, -76, -118, -117, -120, -119, -114, -113, -116, -115, -126, -125, -128, -127, -122, -121, -124, -123, -102, -101, -104, -103, -98, -97, -100, -99, -110, -109, -112, -111, -106, -105, -108, -107, -30, -29, -32, -31, -26, -25, -28, -27, -35, -35, -36, -36, -33, -33, -34, -34, -12, -10, -16, -14, -4, -2, -8, -6, -22, -21, -24, -23, -18, -17, -20, -19, -56, -55, -58, -57, -52, -51, -54, -53, -64, -63, -65, -65, -60, -59, -62, -61, -42, -41, -44, -43, -38, -37, -40, -39, -49, -49, -50, -50, -46, -45, -48, -47, 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37, 57, 58, 55, 56, 61, 62, 59, 60, 49, 50, 48, 48, 53, 54, 51, 52, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 26, 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 98, 99, 96, 97, 102, 103, 100, 101, 93, 93, 92, 92, 95, 95, 94, 94, 116, 118, 112, 114, 124, 126, 120, 122, 106, 107, 104, 105, 110, 111, 108, 109, 72, 73, 70, 71, 76, 77, 74, 75, 64, 65, 63, 63, 68, 69, 66, 67, 86, 87, 84, 85, 90, 91, 88, 89, 79, 79, 78, 78, 82, 83, 80, 81, }; public static byte alaw2ulaw(byte sample) { return a2u[sample & 0xFF]; } /** * Converts a buffer of aLaw samples to uLaw. * The uLaw bytes overwrite the original aLaw values. * The first byte-offset of the uLaw bytes is byteOffset. * It will be written sampleCount bytes. */ public static void alaw2ulaw(byte[] buffer, int byteOffset, int sampleCount) { sampleCount+=byteOffset; for (int i=byteOffset; i<sampleCount; i++) { buffer[i]=a2u[buffer[i] & 0xFF]; } } /** * Fills outBuffer with ulaw samples. * reading starts from inBuffer[inByteOffset]. * writing starts at outBuffer[outByteOffset]. * There will be sampleCount <B>bytes</B> written to outBuffer. */ public static void alaw2ulaw(byte[] inBuffer, int inByteOffset, byte[] outBuffer, int outByteOffset, int sampleCount) { int ulawIndex=outByteOffset; int alawIndex=inByteOffset; while (sampleCount>0) { outBuffer[ulawIndex++]=a2u[inBuffer[alawIndex++] & 0xFF]; sampleCount--; } } //////////////////////// high level methods ///////////////////////////////////////////////// /* * !! Here, unlike other functions in this class, the length is * in bytes rather than samples !! */ public static void changeOrderOrSign(byte[] buffer, int nOffset, int nByteLength, int nBytesPerSample) { switch (nBytesPerSample) { case 1: convertSign8(buffer, nOffset, nByteLength); break; case 2: swapOrder16(buffer, nOffset, nByteLength / 2); break; case 3: swapOrder24(buffer, nOffset, nByteLength / 3); break; case 4: swapOrder32(buffer, nOffset, nByteLength / 4); break; } } /* * !! Here, unlike other functions in this class, the length is * in bytes rather than samples !! */ public static void changeOrderOrSign( byte[] inBuffer, int nInOffset, byte[] outBuffer, int nOutOffset, int nByteLength, int nBytesPerSample) { switch (nBytesPerSample) { case 1: convertSign8( inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength); break; case 2: swapOrder16( inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength / 2); break; case 3: swapOrder24( inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength / 3); break; case 4: swapOrder32( inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength / 4); break; } } ///////////////// Annexe: how the arrays were created. ////////////////////////////////// /* * Converts a uLaw byte to a linear signed 16bit sample. * Ported to Java by fb. * <BR>Originally by:<BR> * * Craig Reese: IDA/Supercomputing Research Center <BR> * 29 September 1989 <BR> * * References: <BR> * <OL> * <LI>CCITT Recommendation G.711 (very difficult to follow)</LI> * <LI>MIL-STD-188-113,"Interoperability and Performance Standards * for Analog-to_Digital Conversion Techniques," * 17 February 1987</LI> * </OL> */ /* private static final int exp_lut2[] = { 0,132,396,924,1980,4092,8316,16764}; public static short _ulaw2linear(int ulawbyte) { int sign, exponent, mantissa, sample; ulawbyte = ~ulawbyte; sign = (ulawbyte & 0x80); exponent = (ulawbyte >> 4) & 0x07; mantissa = ulawbyte & 0x0F; sample = exp_lut2[exponent] + (mantissa << (exponent + 3)); if (sign != 0) sample = -sample; return((short) sample);}*/ /* u- to A-law conversions: copied from CCITT G.711 specifications */ /* private static byte[] _u2a = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, (byte) 128}; */ /* u-law to A-law conversion */ /* * This source code is a product of Sun Microsystems, Inc. and is provided * for unrestricted use. Users may copy or modify this source code without * charge. */ /* public static byte _ulaw2alaw(byte sample) { sample &= 0xff; return (byte) (((sample & 0x80)!=0) ? (0xD5 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)) : (0x55 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)));}*/ /* A- to u-law conversions */ /* private static byte[] _a2u = { 1, 3, 5, 7, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}; */ /* * This source code is a product of Sun Microsystems, Inc. and is provided * for unrestricted use. Users may copy or modify this source code without * charge. */ /* public static byte _alaw2ulaw(byte sample) { sample &= 0xff; return (byte) (((sample & 0x80)!=0) ? (0xFF ^ _a2u[(sample ^ 0xD5) & 0x7F]) : (0x7F ^ _a2u[(sample ^ 0x55) & 0x7F]));} public static void print_a2u() { System.out.println("\tprivate static byte[] a2u = {"); for (int i=-128; i<128; i++) { if (((i+128) % 16)==0) { System.out.print("\t\t"); } byte b=(byte) i; System.out.print(_alaw2ulaw(b)+", "); if (((i+128) % 16)==15) { System.out.println(""); }} System.out.println("\t};");} public static void print_u2a() { System.out.println("\tprivate static byte[] u2a = {"); for (int i=-128; i<128; i++) { if (((i+128) % 16)==0) { System.out.print("\t\t"); } byte b=(byte) i; System.out.print(_ulaw2alaw(b)+", "); if (((i+128) % 16)==15) { System.out.println(""); }} System.out.println("\t};");} */}/*** TConversionTool.java ***/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -