⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tconversiontool.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *	TConversionTool.java *//* *  Copyright (c) 1999,2000 by Florian Bomers <florian@bome.com> *  Copyright (c) 2000 by Matthias Pfisterer <matthias.pfisterer@gmx.de> * * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU Library General Public License as published *   by the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   This program is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU Library General Public License for more details. * *   You should have received a copy of the GNU Library General Public *   License along with this program; if not, write to the Free Software *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */package	org.tritonus.share.sampled;/** * Useful methods for converting audio data. * * @author Florian Bomers * @author Matthias Pfisterer *//*For convenience, a list of available methods is maintained here.Some hints:- buffers: always byte arrays- offsets: always in bytes- sampleCount: number of SAMPLES to read/write, as opposed to FRAMES or BYTES!- when in buffer and out buffer are given, the data is copied,  otherwise it is replaced in the same buffer (buffer size is not checked!)- a number (except "2") gives the number of bits in which format  the samples have to be.- >8 bits per sample is always treated signed.- all functions are tried to be optimized - hints welcome !  ** "high level" methods **changeOrderOrSign(buffer, nOffset, nByteLength, nBytesPerSample)changeOrderOrSign(inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength, nBytesPerSample)  ** PCM byte order and sign conversion **void 	convertSign8(buffer, byteOffset, sampleCount)void 	swapOrder16(buffer, byteOffset, sampleCount)void 	swapOrder24(buffer, byteOffset, sampleCount)void 	swapOrder32(buffer, byteOffset, sampleCount)void 	convertSign8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)void 	swapOrder16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)void 	swapOrder24(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)void 	swapOrder32(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)  ** conversion functions for byte arrays **** these are for reference to see how to implement these conversions **short 	bytesToShort16(highByte, lowByte)short 	bytesToShort16(buffer, byteOffset, bigEndian)short 	bytesToInt16(highByte, lowByte)short 	bytesToInt16(buffer, byteOffset, bigEndian)short 	bytesToInt24(buffer, byteOffset, bigEndian)short 	bytesToInt32(buffer, byteOffset, bigEndian)void 	shortToBytes16(sample, buffer, byteOffset, bigEndian)void 	intToBytes24(sample, buffer, byteOffset, bigEndian)void 	intToBytes32(sample, buffer, byteOffset, bigEndian)  ** ULAW <-> PCM **byte 	linear2ulaw(int sample)short 	ulaw2linear(int ulawbyte)void 	pcm162ulaw(buffer, byteOffset, sampleCount, bigEndian)void 	pcm162ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)void 	pcm82ulaw(buffer, byteOffset, sampleCount, signed)void 	pcm82ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)void 	ulaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)void 	ulaw2pcm8(buffer, byteOffset, sampleCount, signed)void 	ulaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)  ** ALAW <-> PCM **byte linear2alaw(short pcm_val)short alaw2linear(byte ulawbyte)void pcm162alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)void pcm162alaw(buffer, byteOffset, sampleCount, bigEndian)void pcm82alaw(buffer, byteOffset, sampleCount, signed)void pcm82alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)void alaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)void alaw2pcm8(buffer, byteOffset, sampleCount, signed)void alaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)  ** ULAW <-> ALAW **byte 	ulaw2alaw(byte sample)void 	ulaw2alaw(buffer, byteOffset, sampleCount)void 	ulaw2alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)byte 	alaw2ulaw(byte sample)void 	alaw2ulaw(buffer, byteOffset, sampleCount)void 	alaw2ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount) */public class TConversionTool {	///////////////// sign/byte-order conversion ///////////////////////////////////	public static void convertSign8(byte[] buffer, int byteOffset, int sampleCount) {		sampleCount+=byteOffset;		for (int i=byteOffset; i<sampleCount; i++) {			buffer[i]+=128;		}	}	public static void swapOrder16(byte[] buffer, int byteOffset, int sampleCount) {		int byteMax=sampleCount*2+byteOffset-1;		int i=byteOffset;		while (i<byteMax) {			byte h=buffer[i];			buffer[i]=buffer[++i];			buffer[i++]=h;		}	}	public static void swapOrder24(byte[] buffer, int byteOffset, int sampleCount) {		int byteMax=sampleCount*3+byteOffset-2;		int i=byteOffset;		while (i<byteMax) {			byte h=buffer[i];			buffer[i]=buffer[++i+1];			buffer[++i]=h;			i++;		}	}	public static void swapOrder32(byte[] buffer, int byteOffset, int sampleCount) {		int byteMax=sampleCount*4+byteOffset-3;		int i=byteOffset;		while (i<byteMax) {			byte h=buffer[i];			buffer[i]=buffer[i+3];			buffer[i+3]=h;			i++;			h=buffer[i];			buffer[i]=buffer[++i];			buffer[i++]=h;			i++;		}	}	public static void convertSign8(byte[] inBuffer, int inByteOffset,	                                byte[] outBuffer, int outByteOffset, int sampleCount) {		while (sampleCount>0) {			outBuffer[outByteOffset++]=(byte)(inBuffer[inByteOffset++]+128);			sampleCount--;		}	}	public static void swapOrder16(byte[] inBuffer, int inByteOffset,	                               byte[] outBuffer, int outByteOffset, int sampleCount) {		while (sampleCount>0) {			outBuffer[outByteOffset++]=inBuffer[inByteOffset+1];			outBuffer[outByteOffset++]=inBuffer[inByteOffset++];			inByteOffset++;			sampleCount--;		}	}	public static void swapOrder24(byte[] inBuffer, int inByteOffset,	                               byte[] outBuffer, int outByteOffset, int sampleCount) {		while (sampleCount>0) {			outBuffer[outByteOffset++]=inBuffer[inByteOffset+2];			outByteOffset++;			outBuffer[outByteOffset++]=inBuffer[inByteOffset++];			inByteOffset++;			inByteOffset++;			sampleCount--;		}	}	public static void swapOrder32(byte[] inBuffer, int inByteOffset,	                               byte[] outBuffer, int outByteOffset, int sampleCount) {		while (sampleCount>0) {			outBuffer[outByteOffset++]=inBuffer[inByteOffset+3];			outBuffer[outByteOffset++]=inBuffer[inByteOffset+2];			outBuffer[outByteOffset++]=inBuffer[inByteOffset+1];			outBuffer[outByteOffset++]=inBuffer[inByteOffset++];			inByteOffset++;			inByteOffset++;			inByteOffset++;			sampleCount--;		}	}	///////////////// conversion functions for byte arrays ////////////////////////////	/**	 * Converts 2 bytes to a signed sample of type <code>short</code>.	 * <p> This is a reference function.	 */	public static short bytesToShort16(byte highByte, byte lowByte) {		return (short) ((highByte<<8) | (lowByte & 0xFF));	}	/**	 * Converts 2 successive bytes starting at <code>byteOffset</code> in 	 * <code>buffer</code> to a signed sample of type <code>short</code>.	 * <p>	 * For little endian, buffer[byteOffset] is interpreted as low byte,	 * whereas it is interpreted as high byte in big endian.	 * <p> This is a reference function.	 */	public static short bytesToShort16(byte[] buffer, int byteOffset, boolean bigEndian) {		return bigEndian?		       ((short) ((buffer[byteOffset]<<8) | (buffer[byteOffset+1] & 0xFF))):		       ((short) ((buffer[byteOffset+1]<<8) | (buffer[byteOffset] & 0xFF)));	}	/**	 * Converts 2 bytes to a signed integer sample with 16bit range.	 * <p> This is a reference function.	 */	public static int bytesToInt16(byte highByte, byte lowByte) {		return (highByte<<8) | (lowByte & 0xFF);	}	/**	 * Converts 2 successive bytes starting at <code>byteOffset</code> in 	 * <code>buffer</code> to a signed integer sample with 16bit range.	 * <p>	 * For little endian, buffer[byteOffset] is interpreted as low byte,	 * whereas it is interpreted as high byte in big endian.	 * <p> This is a reference function.	 */	public static int bytesToInt16(byte[] buffer, int byteOffset, boolean bigEndian) {		return bigEndian?		       ((buffer[byteOffset]<<8) | (buffer[byteOffset+1] & 0xFF)):		       ((buffer[byteOffset+1]<<8) | (buffer[byteOffset] & 0xFF));	}	/**	 * Converts 3 successive bytes starting at <code>byteOffset</code> in 	 * <code>buffer</code> to a signed integer sample with 24bit range.	 * <p>	 * For little endian, buffer[byteOffset] is interpreted as lowest byte,	 * whereas it is interpreted as highest byte in big endian.	 * <p> This is a reference function.	 */	public static int bytesToInt24(byte[] buffer, int byteOffset, boolean bigEndian) {		return bigEndian?		       ((buffer[byteOffset]<<16)             // let Java handle sign-bit		        | ((buffer[byteOffset+1] & 0xFF)<<8) // inhibit sign-bit handling		        | (buffer[byteOffset+2] & 0xFF)):		       ((buffer[byteOffset+2]<<16)           // let Java handle sign-bit		        | ((buffer[byteOffset+1] & 0xFF)<<8) // inhibit sign-bit handling		        | (buffer[byteOffset] & 0xFF));	}	/**	 * Converts a 4 successive bytes starting at <code>byteOffset</code> in 	 * <code>buffer</code> to a signed 32bit integer sample.	 * <p>	 * For little endian, buffer[byteOffset] is interpreted as lowest byte,	 * whereas it is interpreted as highest byte in big endian.	 * <p> This is a reference function.	 */	public static int bytesToInt32(byte[] buffer, int byteOffset, boolean bigEndian) {		return bigEndian?		       ((buffer[byteOffset]<<24)              // let Java handle sign-bit		        | ((buffer[byteOffset+1] & 0xFF)<<16) // inhibit sign-bit handling		        | ((buffer[byteOffset+2] & 0xFF)<<8)  // inhibit sign-bit handling		        | (buffer[byteOffset+3] & 0xFF)):		       ((buffer[byteOffset+3]<<24)            // let Java handle sign-bit		        | ((buffer[byteOffset+2] & 0xFF)<<16) // inhibit sign-bit handling		        | ((buffer[byteOffset+1] & 0xFF)<<8)  // inhibit sign-bit handling		        | (buffer[byteOffset] & 0xFF));	}	/**	 * Converts a sample of type <code>short</code> to 2 bytes in an array.	 * <code>sample</code> is interpreted as signed (as Java does).	 * <p>	 * For little endian, buffer[byteOffset] is filled with low byte of sample, 	 * and buffer[byteOffset+1] is filled with high byte of sample.	 * <p> For big endian, this is reversed.	 * <p> This is a reference function.	 */	public static void shortToBytes16(short sample, byte[] buffer, int byteOffset, boolean bigEndian) {		intToBytes16(sample, buffer, byteOffset, bigEndian);	}	/**	 * Converts a 16 bit sample of type <code>int</code> to 2 bytes in an array.	 * <code>sample</code> is interpreted as signed (as Java does).	 * <p>	 * For little endian, buffer[byteOffset] is filled with low byte of sample, 	 * and buffer[byteOffset+1] is filled with high byte of sample + sign bit.	 * <p> For big endian, this is reversed.	 * <p> Before calling this function, it should be assured that <code>sample</code>	 * is in the 16bit range - it will not be clipped.	 * <p> This is a reference function.	 */	public static void intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {		if (bigEndian) {			buffer[byteOffset++]=(byte) (sample >> 8);			buffer[byteOffset]=(byte) (sample & 0xFF);		} else {			buffer[byteOffset++]=(byte) (sample & 0xFF);			buffer[byteOffset]=(byte) (sample >> 8);		}	}	/**	 * Converts a 24 bit sample of type <code>int</code> to 3 bytes in an array.	 * <code>sample</code> is interpreted as signed (as Java does).	 * <p>	 * For little endian, buffer[byteOffset] is filled with low byte of sample, 	 * and buffer[byteOffset+2] is filled with the high byte of sample + sign bit.	 * <p> For big endian, this is reversed.	 * <p> Before calling this function, it should be assured that <code>sample</code>	 * is in the 24bit range - it will not be clipped.	 * <p> This is a reference function.	 */	public static void intToBytes24(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {		if (bigEndian) {			buffer[byteOffset++]=(byte) (sample >> 16);			buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);			buffer[byteOffset]=(byte) (sample & 0xFF);		} else {			buffer[byteOffset++]=(byte) (sample & 0xFF);			buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);			buffer[byteOffset]=(byte) (sample >> 16);		}	}	/**	 * Converts a 32 bit sample of type <code>int</code> to 4 bytes in an array.	 * <code>sample</code> is interpreted as signed (as Java does).	 * <p>	 * For little endian, buffer[byteOffset] is filled with lowest byte of sample, 	 * and buffer[byteOffset+3] is filled with the high byte of sample + sign bit.	 * <p> For big endian, this is reversed.	 * <p> This is a reference function.	 */	public static void intToBytes32(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {		if (bigEndian) {			buffer[byteOffset++]=(byte) (sample >> 24);			buffer[byteOffset++]=(byte) ((sample >>> 16) & 0xFF);			buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);			buffer[byteOffset]=(byte) (sample & 0xFF);		} else {			buffer[byteOffset++]=(byte) (sample & 0xFF);			buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);			buffer[byteOffset++]=(byte) ((sample >>> 16) & 0xFF);			buffer[byteOffset]=(byte) (sample >> 24);		}	}	/////////////////////// ULAW ///////////////////////////////////////////	private static final boolean ZEROTRAP=true;	private static final short BIAS=0x84;	private static final int CLIP=32635;	private static final int exp_lut1[] ={	    0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,	    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,	    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,	    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,	    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,	    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,	    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,	    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,	    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7	};	/**	 * Converts a linear signed 16bit sample to a uLaw byte.	 * Ported to Java by fb.	 * <BR>Originally by:<BR>	 * Craig Reese: IDA/Supercomputing Research Center <BR>	 * Joe Campbell: Department of Defense <BR>	 * 29 September 1989 <BR>	 */	public static byte linear2ulaw(int sample) {		int sign, exponent, mantissa, ulawbyte;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -