0001: /*
0002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
0003: *
0004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
0005: * royalty free, license to use, modify and redistribute this
0006: * software in source and binary code form,
0007: * provided that i) this copyright notice and license appear on all copies of
0008: * the software; and ii) Licensee does not utilize the software in a manner
0009: * which is disparaging to Silvere Martin-Michiellot.
0010: *
0011: * This software is provided "AS IS," without a warranty of any kind. ALL
0012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
0013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
0014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
0015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
0016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
0017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
0018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
0019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
0020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
0021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
0022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
0023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
0024: *
0025: * This software is not designed or intended for use in on-line control of
0026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
0027: * the design, construction, operation or maintenance of any nuclear
0028: * facility. Licensee represents and warrants that it will not use or
0029: * redistribute the Software for such purposes.
0030: *
0031: */
0032:
0033: /*
0034: * Copyright (c) 1999,2000 by Florian Bomers <florian@bome.com>
0035: * Copyright (c) 2000 by Matthias Pfisterer <matthias.pfisterer@gmx.de>
0036: *
0037: *
0038: * This program is free software; you can redistribute it and/or modify
0039: * it under the terms of the GNU Library General Public License as published
0040: * by the Free Software Foundation; either version 2 of the License, or
0041: * (at your option) any later version.
0042: *
0043: * This program is distributed in the hope that it will be useful,
0044: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0045: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0046: * GNU Library General Public License for more details.
0047: *
0048: * You should have received a copy of the GNU Library General Public
0049: * License along with this program; if not, write to the Free Software
0050: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0051: *
0052: */
0053:
0054: package com.db.media.audio;
0055:
0056: // This code is repackaged after the code from Florian Bomers & Matthias Pfisterer from class org.tritonus.sampled.TConversionTool extracted after the tritonus-0.3.0.tar.gz package
0057: // Site http://www.tritonus.org/
0058: // Email tritonus-user@lists.sourceforge.net
0059:
0060: /**
0061: * Useful methods for converting audio data.
0062: *
0063: */
0064:
0065: /*
0066: For convenience, a list of available methods is maintained here.
0067: Some hints:
0068: - buffers: always byte arrays
0069: - offsets: always in bytes
0070: - sampleCount: number of SAMPLES to read/write, as opposed to FRAMES or BYTES!
0071: - when in buffer and out buffer are given, the data is copied,
0072: otherwise it is replaced in the same buffer (buffer size is not checked!)
0073: - a number (except "2") gives the number of bits in which format
0074: the samples have to be.
0075: - >8 bits per sample is always treated signed.
0076: - all functions are tried to be optimized - hints welcome !
0077:
0078:
0079: ** "high level" methods **
0080: changeOrderOrSign(buffer, nOffset, nByteLength, nBytesPerSample)
0081: changeOrderOrSign(inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength, nBytesPerSample)
0082:
0083:
0084: ** PCM byte order and sign conversion **
0085: void convertSign8(buffer, byteOffset, sampleCount)
0086: void swapOrder16(buffer, byteOffset, sampleCount)
0087: void swapOrder24(buffer, byteOffset, sampleCount)
0088: void swapOrder32(buffer, byteOffset, sampleCount)
0089: void convertSign8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0090: void swapOrder16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0091: void swapOrder24(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0092: void swapOrder32(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0093:
0094:
0095: ** conversion functions for byte arrays **
0096: ** these are for reference to see how to implement these conversions **
0097: short bytesToShort16(highByte, lowByte)
0098: short bytesToShort16(buffer, byteOffset, bigEndian)
0099: short bytesToInt16(highByte, lowByte)
0100: short bytesToInt16(buffer, byteOffset, bigEndian)
0101: short bytesToInt24(buffer, byteOffset, bigEndian)
0102: short bytesToInt32(buffer, byteOffset, bigEndian)
0103: void shortToBytes16(sample, buffer, byteOffset, bigEndian)
0104: void intToBytes24(sample, buffer, byteOffset, bigEndian)
0105: void intToBytes32(sample, buffer, byteOffset, bigEndian)
0106:
0107:
0108: ** ULAW <-> PCM **
0109: byte linear2ulaw(int sample)
0110: short ulaw2linear(int ulawbyte)
0111: void pcm162ulaw(buffer, byteOffset, sampleCount, bigEndian)
0112: void pcm162ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
0113: void pcm82ulaw(buffer, byteOffset, sampleCount, signed)
0114: void pcm82ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
0115: void ulaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
0116: void ulaw2pcm8(buffer, byteOffset, sampleCount, signed)
0117: void ulaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
0118:
0119:
0120: ** ALAW <-> PCM **
0121: byte linear2alaw(short pcm_val)
0122: short alaw2linear(byte ulawbyte)
0123: void pcm162alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
0124: void pcm162alaw(buffer, byteOffset, sampleCount, bigEndian)
0125: void pcm82alaw(buffer, byteOffset, sampleCount, signed)
0126: void pcm82alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
0127: void alaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
0128: void alaw2pcm8(buffer, byteOffset, sampleCount, signed)
0129: void alaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
0130:
0131:
0132: ** ULAW <-> ALAW **
0133: byte ulaw2alaw(byte sample)
0134: void ulaw2alaw(buffer, byteOffset, sampleCount)
0135: void ulaw2alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0136: byte alaw2ulaw(byte sample)
0137: void alaw2ulaw(buffer, byteOffset, sampleCount)
0138: void alaw2ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
0139:
0140: */
0141:
0142: public class AudioConversionTool {
0143:
0144: ///////////////// sign/byte-order conversion ///////////////////////////////////
0145:
0146: public static void convertSign8(byte[] buffer, int byteOffset,
0147: int sampleCount) {
0148:
0149: sampleCount += byteOffset;
0150: for (int i = byteOffset; i < sampleCount; i++) {
0151: buffer[i] += 128;
0152: }
0153:
0154: }
0155:
0156: public static void swapOrder16(byte[] buffer, int byteOffset,
0157: int sampleCount) {
0158:
0159: int byteMax = sampleCount * 2 + byteOffset - 1;
0160: int i = byteOffset;
0161: while (i < byteMax) {
0162: byte h = buffer[i];
0163: buffer[i] = buffer[++i];
0164: buffer[i++] = h;
0165: }
0166:
0167: }
0168:
0169: public static void swapOrder24(byte[] buffer, int byteOffset,
0170: int sampleCount) {
0171:
0172: int byteMax = sampleCount * 3 + byteOffset - 2;
0173: int i = byteOffset;
0174: while (i < byteMax) {
0175: byte h = buffer[i];
0176: buffer[i] = buffer[++i + 1];
0177: buffer[++i] = h;
0178: i++;
0179: }
0180:
0181: }
0182:
0183: public static void swapOrder32(byte[] buffer, int byteOffset,
0184: int sampleCount) {
0185:
0186: int byteMax = sampleCount * 4 + byteOffset - 3;
0187: int i = byteOffset;
0188: while (i < byteMax) {
0189: byte h = buffer[i];
0190: buffer[i] = buffer[i + 3];
0191: buffer[i + 3] = h;
0192: i++;
0193: h = buffer[i];
0194: buffer[i] = buffer[++i];
0195: buffer[i++] = h;
0196: i++;
0197: }
0198:
0199: }
0200:
0201: public static void convertSign8(byte[] inBuffer, int inByteOffset,
0202: byte[] outBuffer, int outByteOffset, int sampleCount) {
0203:
0204: while (sampleCount > 0) {
0205: outBuffer[outByteOffset++] = (byte) (inBuffer[inByteOffset++] + 128);
0206: sampleCount--;
0207: }
0208:
0209: }
0210:
0211: public static void swapOrder16(byte[] inBuffer, int inByteOffset,
0212: byte[] outBuffer, int outByteOffset, int sampleCount) {
0213:
0214: while (sampleCount > 0) {
0215: outBuffer[outByteOffset++] = inBuffer[inByteOffset + 1];
0216: outBuffer[outByteOffset++] = inBuffer[inByteOffset++];
0217: inByteOffset++;
0218: sampleCount--;
0219: }
0220:
0221: }
0222:
0223: public static void swapOrder24(byte[] inBuffer, int inByteOffset,
0224: byte[] outBuffer, int outByteOffset, int sampleCount) {
0225:
0226: while (sampleCount > 0) {
0227: outBuffer[outByteOffset++] = inBuffer[inByteOffset + 2];
0228: outByteOffset++;
0229: outBuffer[outByteOffset++] = inBuffer[inByteOffset++];
0230: inByteOffset++;
0231: inByteOffset++;
0232: sampleCount--;
0233: }
0234:
0235: }
0236:
0237: public static void swapOrder32(byte[] inBuffer, int inByteOffset,
0238: byte[] outBuffer, int outByteOffset, int sampleCount) {
0239:
0240: while (sampleCount > 0) {
0241: outBuffer[outByteOffset++] = inBuffer[inByteOffset + 3];
0242: outBuffer[outByteOffset++] = inBuffer[inByteOffset + 2];
0243: outBuffer[outByteOffset++] = inBuffer[inByteOffset + 1];
0244: outBuffer[outByteOffset++] = inBuffer[inByteOffset++];
0245: inByteOffset++;
0246: inByteOffset++;
0247: inByteOffset++;
0248: sampleCount--;
0249: }
0250:
0251: }
0252:
0253: ///////////////// conversion functions for byte arrays ////////////////////////////
0254:
0255: /**
0256: * Converts 2 bytes to a signed sample of type <code>short</code>.
0257: * <p> This is a reference function.
0258: */
0259: public static short bytesToShort16(byte highByte, byte lowByte) {
0260:
0261: return (short) ((highByte << 8) | (lowByte & 0xFF));
0262:
0263: }
0264:
0265: /**
0266: * Converts 2 successive bytes starting at <code>byteOffset</code> in
0267: * <code>buffer</code> to a signed sample of type <code>short</code>.
0268: * <p>
0269: * For little endian, buffer[byteOffset] is interpreted as low byte,
0270: * whereas it is interpreted as high byte in big endian.
0271: * <p> This is a reference function.
0272: */
0273: public static short bytesToShort16(byte[] buffer, int byteOffset,
0274: boolean bigEndian) {
0275:
0276: return bigEndian ? ((short) ((buffer[byteOffset] << 8) | (buffer[byteOffset + 1] & 0xFF)))
0277: : ((short) ((buffer[byteOffset + 1] << 8) | (buffer[byteOffset] & 0xFF)));
0278:
0279: }
0280:
0281: /**
0282: * Converts 2 bytes to a signed integer sample with 16bit range.
0283: * <p> This is a reference function.
0284: */
0285: public static int bytesToInt16(byte highByte, byte lowByte) {
0286:
0287: return (highByte << 8) | (lowByte & 0xFF);
0288:
0289: }
0290:
0291: /**
0292: * Converts 2 successive bytes starting at <code>byteOffset</code> in
0293: * <code>buffer</code> to a signed integer sample with 16bit range.
0294: * <p>
0295: * For little endian, buffer[byteOffset] is interpreted as low byte,
0296: * whereas it is interpreted as high byte in big endian.
0297: * <p> This is a reference function.
0298: */
0299: public static int bytesToInt16(byte[] buffer, int byteOffset,
0300: boolean bigEndian) {
0301:
0302: return bigEndian ? ((buffer[byteOffset] << 8) | (buffer[byteOffset + 1] & 0xFF))
0303: : ((buffer[byteOffset + 1] << 8) | (buffer[byteOffset] & 0xFF));
0304:
0305: }
0306:
0307: /**
0308: * Converts 3 successive bytes starting at <code>byteOffset</code> in
0309: * <code>buffer</code> to a signed integer sample with 24bit range.
0310: * <p>
0311: * For little endian, buffer[byteOffset] is interpreted as lowest byte,
0312: * whereas it is interpreted as highest byte in big endian.
0313: * <p> This is a reference function.
0314: */
0315: public static int bytesToInt24(byte[] buffer, int byteOffset,
0316: boolean bigEndian) {
0317:
0318: return bigEndian ? ((buffer[byteOffset] << 16) // let Java handle sign-bit
0319: | ((buffer[byteOffset + 1] & 0xFF) << 8) // inhibit sign-bit handling
0320: | (buffer[byteOffset + 2] & 0xFF))
0321: : ((buffer[byteOffset + 2] << 16) // let Java handle sign-bit
0322: | ((buffer[byteOffset + 1] & 0xFF) << 8) // inhibit sign-bit handling
0323: | (buffer[byteOffset] & 0xFF));
0324:
0325: }
0326:
0327: /**
0328: * Converts a 4 successive bytes starting at <code>byteOffset</code> in
0329: * <code>buffer</code> to a signed 32bit integer sample.
0330: * <p>
0331: * For little endian, buffer[byteOffset] is interpreted as lowest byte,
0332: * whereas it is interpreted as highest byte in big endian.
0333: * <p> This is a reference function.
0334: */
0335: public static int bytesToInt32(byte[] buffer, int byteOffset,
0336: boolean bigEndian) {
0337:
0338: return bigEndian ? ((buffer[byteOffset] << 24) // let Java handle sign-bit
0339: | ((buffer[byteOffset + 1] & 0xFF) << 16) // inhibit sign-bit handling
0340: | ((buffer[byteOffset + 2] & 0xFF) << 8) // inhibit sign-bit handling
0341: | (buffer[byteOffset + 3] & 0xFF))
0342: : ((buffer[byteOffset + 3] << 24) // let Java handle sign-bit
0343: | ((buffer[byteOffset + 2] & 0xFF) << 16) // inhibit sign-bit handling
0344: | ((buffer[byteOffset + 1] & 0xFF) << 8) // inhibit sign-bit handling
0345: | (buffer[byteOffset] & 0xFF));
0346:
0347: }
0348:
0349: /**
0350: * Converts a sample of type <code>short</code> to 2 bytes in an array.
0351: * <code>sample</code> is interpreted as signed (as Java does).
0352: * <p>
0353: * For little endian, buffer[byteOffset] is filled with low byte of sample,
0354: * and buffer[byteOffset+1] is filled with high byte of sample.
0355: * <p> For big endian, this is reversed.
0356: * <p> This is a reference function.
0357: */
0358: public static void shortToBytes16(short sample, byte[] buffer,
0359: int byteOffset, boolean bigEndian) {
0360:
0361: intToBytes16(sample, buffer, byteOffset, bigEndian);
0362:
0363: }
0364:
0365: /**
0366: * Converts a 16 bit sample of type <code>int</code> to 2 bytes in an array.
0367: * <code>sample</code> is interpreted as signed (as Java does).
0368: * <p>
0369: * For little endian, buffer[byteOffset] is filled with low byte of sample,
0370: * and buffer[byteOffset+1] is filled with high byte of sample + sign bit.
0371: * <p> For big endian, this is reversed.
0372: * <p> Before calling this function, it should be assured that <code>sample</code>
0373: * is in the 16bit range - it will not be clipped.
0374: * <p> This is a reference function.
0375: */
0376: public static void intToBytes16(int sample, byte[] buffer,
0377: int byteOffset, boolean bigEndian) {
0378:
0379: if (bigEndian) {
0380: buffer[byteOffset++] = (byte) (sample >> 8);
0381: buffer[byteOffset] = (byte) (sample & 0xFF);
0382: } else {
0383: buffer[byteOffset++] = (byte) (sample & 0xFF);
0384: buffer[byteOffset] = (byte) (sample >> 8);
0385: }
0386:
0387: }
0388:
0389: /**
0390: * Converts a 24 bit sample of type <code>int</code> to 3 bytes in an array.
0391: * <code>sample</code> is interpreted as signed (as Java does).
0392: * <p>
0393: * For little endian, buffer[byteOffset] is filled with low byte of sample,
0394: * and buffer[byteOffset+2] is filled with the high byte of sample + sign bit.
0395: * <p> For big endian, this is reversed.
0396: * <p> Before calling this function, it should be assured that <code>sample</code>
0397: * is in the 24bit range - it will not be clipped.
0398: * <p> This is a reference function.
0399: */
0400: public static void intToBytes24(int sample, byte[] buffer,
0401: int byteOffset, boolean bigEndian) {
0402:
0403: if (bigEndian) {
0404: buffer[byteOffset++] = (byte) (sample >> 16);
0405: buffer[byteOffset++] = (byte) ((sample >>> 8) & 0xFF);
0406: buffer[byteOffset] = (byte) (sample & 0xFF);
0407: } else {
0408: buffer[byteOffset++] = (byte) (sample & 0xFF);
0409: buffer[byteOffset++] = (byte) ((sample >>> 8) & 0xFF);
0410: buffer[byteOffset] = (byte) (sample >> 16);
0411: }
0412:
0413: }
0414:
0415: /**
0416: * Converts a 32 bit sample of type <code>int</code> to 4 bytes in an array.
0417: * <code>sample</code> is interpreted as signed (as Java does).
0418: * <p>
0419: * For little endian, buffer[byteOffset] is filled with lowest byte of sample,
0420: * and buffer[byteOffset+3] is filled with the high byte of sample + sign bit.
0421: * <p> For big endian, this is reversed.
0422: * <p> This is a reference function.
0423: */
0424: public static void intToBytes32(int sample, byte[] buffer,
0425: int byteOffset, boolean bigEndian) {
0426:
0427: if (bigEndian) {
0428: buffer[byteOffset++] = (byte) (sample >> 24);
0429: buffer[byteOffset++] = (byte) ((sample >>> 16) & 0xFF);
0430: buffer[byteOffset++] = (byte) ((sample >>> 8) & 0xFF);
0431: buffer[byteOffset] = (byte) (sample & 0xFF);
0432: } else {
0433: buffer[byteOffset++] = (byte) (sample & 0xFF);
0434: buffer[byteOffset++] = (byte) ((sample >>> 8) & 0xFF);
0435: buffer[byteOffset++] = (byte) ((sample >>> 16) & 0xFF);
0436: buffer[byteOffset] = (byte) (sample >> 24);
0437: }
0438:
0439: }
0440:
0441: /////////////////////// ULAW ///////////////////////////////////////////
0442:
0443: private static final boolean ZEROTRAP = true;
0444: private static final short BIAS = 0x84;
0445: private static final int CLIP = 32635;
0446: private static final int exp_lut1[] = { 0, 0, 1, 1, 2, 2, 2, 2, 3,
0447: 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
0448: 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
0449: 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
0450: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
0451: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
0452: 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
0453: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0454: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0455: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0456: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0457: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0458: 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0459: 7, 7, 7, 7, 7, 7, 7 };
0460:
0461: /**
0462: * Converts a linear signed 16bit sample to a uLaw byte.
0463: * Ported to Java by fb.
0464: * <BR>Originally by:<BR>
0465: * Craig Reese: IDA/Supercomputing Research Center <BR>
0466: * Joe Campbell: Department of Defense <BR>
0467: * 29 September 1989 <BR>
0468: */
0469: public static byte linear2ulaw(int sample) {
0470:
0471: int sign, exponent, mantissa, ulawbyte;
0472:
0473: if (sample > 32767)
0474: sample = 32767;
0475: else if (sample < -32768)
0476: sample = -32768;
0477: /* Get the sample into sign-magnitude. */
0478: sign = (sample >> 8) & 0x80; /* set aside the sign */
0479: if (sign != 0)
0480: sample = -sample; /* get magnitude */
0481: if (sample > CLIP)
0482: sample = CLIP; /* clip the magnitude */
0483:
0484: /* Convert from 16 bit linear to ulaw. */
0485: sample = sample + BIAS;
0486: exponent = exp_lut1[(sample >> 7) & 0xFF];
0487: mantissa = (sample >> (exponent + 3)) & 0x0F;
0488: ulawbyte = ~(sign | (exponent << 4) | mantissa);
0489: if (ZEROTRAP)
0490: if (ulawbyte == 0)
0491: ulawbyte = 0x02; /* optional CCITT trap */
0492:
0493: return ((byte) ulawbyte);
0494:
0495: }
0496:
0497: /* u-law to linear conversion table */
0498: private static short[] u2l = { -32124, -31100, -30076, -29052,
0499: -28028, -27004, -25980, -24956, -23932, -22908, -21884,
0500: -20860, -19836, -18812, -17788, -16764, -15996, -15484,
0501: -14972, -14460, -13948, -13436, -12924, -12412, -11900,
0502: -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932,
0503: -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884,
0504: -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900,
0505: -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876,
0506: -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884,
0507: -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372,
0508: -1308, -1244, -1180, -1116, -1052, -988, -924, -876, -844,
0509: -812, -780, -748, -716, -684, -652, -620, -588, -556, -524,
0510: -492, -460, -428, -396, -372, -356, -340, -324, -308, -292,
0511: -276, -260, -244, -228, -212, -196, -180, -164, -148, -132,
0512: -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40,
0513: -32, -24, -16, -8, 0, 32124, 31100, 30076, 29052, 28028,
0514: 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836,
0515: 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948,
0516: 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852,
0517: 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396,
0518: 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900,
0519: 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620,
0520: 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628,
0521: 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988,
0522: 924, 876, 844, 812, 780, 748, 716, 684, 652, 620, 588, 556,
0523: 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276,
0524: 260, 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104,
0525: 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0 };
0526:
0527: public static short ulaw2linear(byte ulawbyte) {
0528:
0529: return u2l[ulawbyte & 0xFF];
0530:
0531: }
0532:
0533: /**
0534: * Converts a buffer of signed 16bit big endian samples to uLaw.
0535: * The uLaw bytes overwrite the original 16 bit values.
0536: * The first byte-offset of the uLaw bytes is byteOffset.
0537: * It will be written sampleCount/2 bytes.
0538: */
0539: public static void pcm162ulaw(byte[] buffer, int byteOffset,
0540: int sampleCount, boolean bigEndian) {
0541:
0542: int shortIndex = byteOffset;
0543: int ulawIndex = shortIndex;
0544: if (bigEndian) {
0545: while (sampleCount > 0) {
0546: buffer[ulawIndex++] = linear2ulaw(bytesToInt16(
0547: buffer[shortIndex], buffer[shortIndex + 1]));
0548: shortIndex++;
0549: shortIndex++;
0550: sampleCount--;
0551: }
0552: } else {
0553: while (sampleCount > 0) {
0554: buffer[ulawIndex++] = linear2ulaw(bytesToInt16(
0555: buffer[shortIndex + 1], buffer[shortIndex]));
0556: shortIndex++;
0557: shortIndex++;
0558: sampleCount--;
0559: }
0560: }
0561:
0562: }
0563:
0564: /**
0565: * Fills outBuffer with ulaw samples.
0566: * reading starts from inBuffer[inByteOffset].
0567: * writing starts at outBuffer[outByteOffset].
0568: * There will be sampleCount*2 bytes read from inBuffer;
0569: * There will be sampleCount <B>bytes</B> written to outBuffer.
0570: */
0571: public static void pcm162ulaw(byte[] inBuffer, int inByteOffset,
0572: byte[] outBuffer, int outByteOffset, int sampleCount,
0573: boolean bigEndian) {
0574:
0575: int shortIndex = inByteOffset;
0576: int ulawIndex = outByteOffset;
0577: if (bigEndian) {
0578: while (sampleCount > 0) {
0579: outBuffer[ulawIndex++] = linear2ulaw(bytesToInt16(
0580: inBuffer[shortIndex], inBuffer[shortIndex + 1]));
0581: shortIndex++;
0582: shortIndex++;
0583: sampleCount--;
0584: }
0585: } else {
0586: while (sampleCount > 0) {
0587: outBuffer[ulawIndex++] = linear2ulaw(bytesToInt16(
0588: inBuffer[shortIndex + 1], inBuffer[shortIndex]));
0589: shortIndex++;
0590: shortIndex++;
0591: sampleCount--;
0592: }
0593: }
0594:
0595: }
0596:
0597: // TODO: either direct 8bit pcm to ulaw, or better conversion from 8bit to 16bit
0598: /**
0599: * Converts a buffer of 8bit samples to uLaw.
0600: * The uLaw bytes overwrite the original 8 bit values.
0601: * The first byte-offset of the uLaw bytes is byteOffset.
0602: * It will be written sampleCount bytes.
0603: */
0604: public static void pcm82ulaw(byte[] buffer, int byteOffset,
0605: int sampleCount, boolean signed) {
0606:
0607: sampleCount += byteOffset;
0608: if (signed) {
0609: for (int i = byteOffset; i < sampleCount; i++) {
0610: buffer[i] = linear2ulaw(buffer[i] << 8);
0611: }
0612: } else {
0613: for (int i = byteOffset; i < sampleCount; i++) {
0614: buffer[i] = linear2ulaw(((byte) (buffer[i] + 128)) << 8);
0615: }
0616: }
0617:
0618: }
0619:
0620: /**
0621: * Fills outBuffer with ulaw samples.
0622: * reading starts from inBuffer[inByteOffset].
0623: * writing starts at outBuffer[outByteOffset].
0624: * There will be sampleCount <B>bytes</B> written to outBuffer.
0625: */
0626: public static void pcm82ulaw(byte[] inBuffer, int inByteOffset,
0627: byte[] outBuffer, int outByteOffset, int sampleCount,
0628: boolean signed) {
0629:
0630: int ulawIndex = outByteOffset;
0631: int pcmIndex = inByteOffset;
0632: if (signed) {
0633: while (sampleCount > 0) {
0634: outBuffer[ulawIndex++] = linear2ulaw(inBuffer[pcmIndex++] << 8);
0635: sampleCount--;
0636: }
0637: } else {
0638: while (sampleCount > 0) {
0639: outBuffer[ulawIndex++] = linear2ulaw(((byte) (inBuffer[pcmIndex++] + 128)) << 8);
0640: sampleCount--;
0641: }
0642: }
0643:
0644: }
0645:
0646: /**
0647: * Fills outBuffer with pcm signed 16 bit samples.
0648: * reading starts from inBuffer[inByteOffset].
0649: * writing starts at outBuffer[outByteOffset].
0650: * There will be sampleCount bytes read from inBuffer;
0651: * There will be sampleCount*2 bytes written to outBuffer.
0652: */
0653: public static void ulaw2pcm16(byte[] inBuffer, int inByteOffset,
0654: byte[] outBuffer, int outByteOffset, int sampleCount,
0655: boolean bigEndian) {
0656:
0657: int shortIndex = outByteOffset;
0658: int ulawIndex = inByteOffset;
0659: while (sampleCount > 0) {
0660: intToBytes16(u2l[inBuffer[ulawIndex++] & 0xFF], outBuffer,
0661: shortIndex++, bigEndian);
0662: shortIndex++;
0663: sampleCount--;
0664: }
0665:
0666: }
0667:
0668: // TODO: either direct 8bit pcm to ulaw, or better conversion from 8bit to 16bit
0669: /**
0670: * Inplace-conversion of a ulaw buffer to 8bit samples.
0671: * The 8bit bytes overwrite the original ulaw values.
0672: * The first byte-offset of the uLaw bytes is byteOffset.
0673: * It will be written sampleCount bytes.
0674: */
0675: public static void ulaw2pcm8(byte[] buffer, int byteOffset,
0676: int sampleCount, boolean signed) {
0677:
0678: sampleCount += byteOffset;
0679: if (signed) {
0680: for (int i = byteOffset; i < sampleCount; i++) {
0681: buffer[i] = (byte) ((u2l[buffer[i] & 0xFF] >> 8) & 0xFF);
0682: }
0683: } else {
0684: for (int i = byteOffset; i < sampleCount; i++) {
0685: buffer[i] = (byte) ((u2l[buffer[i] & 0xFF] >> 8) + 128);
0686: }
0687: }
0688:
0689: }
0690:
0691: /**
0692: * Fills outBuffer with ulaw samples.
0693: * reading starts from inBuffer[inByteOffset].
0694: * writing starts at outBuffer[outByteOffset].
0695: * There will be sampleCount <B>bytes</B> written to outBuffer.
0696: */
0697: public static void ulaw2pcm8(byte[] inBuffer, int inByteOffset,
0698: byte[] outBuffer, int outByteOffset, int sampleCount,
0699: boolean signed) {
0700:
0701: int ulawIndex = inByteOffset;
0702: int pcmIndex = outByteOffset;
0703: if (signed) {
0704: while (sampleCount > 0) {
0705: outBuffer[pcmIndex++] = (byte) ((u2l[inBuffer[ulawIndex++] & 0xFF] >> 8) & 0xFF);
0706: sampleCount--;
0707: }
0708: } else {
0709: while (sampleCount > 0) {
0710: outBuffer[pcmIndex++] = (byte) ((u2l[inBuffer[ulawIndex++] & 0xFF] >> 8) + 128);
0711: sampleCount--;
0712: }
0713: }
0714:
0715: }
0716:
0717: //////////////////// ALAW ////////////////////////////
0718:
0719: /*
0720: * This source code is a product of Sun Microsystems, Inc. and is provided
0721: * for unrestricted use. Users may copy or modify this source code without
0722: * charge.
0723: *
0724: * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
0725: *
0726: * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
0727: *
0728: * Linear Input Code Compressed Code
0729: * ------------------------ ---------------
0730: * 0000000wxyza 000wxyz
0731: * 0000001wxyza 001wxyz
0732: * 000001wxyzab 010wxyz
0733: * 00001wxyzabc 011wxyz
0734: * 0001wxyzabcd 100wxyz
0735: * 001wxyzabcde 101wxyz
0736: * 01wxyzabcdef 110wxyz
0737: * 1wxyzabcdefg 111wxyz
0738: *
0739: * For further information see John C. Bellamy's Digital Telephony, 1982,
0740: * John Wiley & Sons, pps 98-111 and 472-476.
0741: */
0742: private static final byte QUANT_MASK = 0xf; /* Quantization field mask. */
0743: private static final byte SEG_SHIFT = 4; /* Left shift for segment number. */
0744: private static final short[] seg_end = { 0xFF, 0x1FF, 0x3FF, 0x7FF,
0745: 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF };
0746:
0747: public static byte linear2alaw(short pcm_val) /* 2's complement (16-bit range) */{
0748:
0749: byte mask;
0750: byte seg = 8;
0751: byte aval;
0752:
0753: if (pcm_val >= 0) {
0754: mask = (byte) 0xD5; /* sign (7th) bit = 1 */
0755: } else {
0756: mask = 0x55; /* sign bit = 0 */
0757: pcm_val = (short) (-pcm_val - 8);
0758: }
0759:
0760: /* Convert the scaled magnitude to segment number. */
0761: for (int i = 0; i < 8; i++) {
0762: if (pcm_val <= seg_end[i]) {
0763: seg = (byte) i;
0764: break;
0765: }
0766: }
0767:
0768: /* Combine the sign, segment, and quantization bits. */
0769: if (seg >= 8) /* out of range, return maximum value. */
0770: return (byte) ((0x7F ^ mask) & 0xFF);
0771: else {
0772: aval = (byte) (seg << SEG_SHIFT);
0773: if (seg < 2)
0774: aval |= (pcm_val >> 4) & QUANT_MASK;
0775: else
0776: aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
0777: return (byte) ((aval ^ mask) & 0xFF);
0778: }
0779:
0780: }
0781:
0782: private static short[] a2l = { -5504, -5248, -6016, -5760, -4480,
0783: -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528,
0784: -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240,
0785: -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264,
0786: -3136, -3520, -3392, -22016, -20992, -24064, -23040,
0787: -17920, -16896, -19968, -18944, -30208, -29184, -32256,
0788: -31232, -26112, -25088, -28160, -27136, -11008, -10496,
0789: -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592,
0790: -16128, -15616, -13056, -12544, -14080, -13568, -344, -328,
0791: -376, -360, -280, -264, -312, -296, -472, -456, -504, -488,
0792: -408, -392, -440, -424, -88, -72, -120, -104, -24, -8, -56,
0793: -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376,
0794: -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888,
0795: -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688,
0796: -656, -752, -720, -560, -528, -624, -592, -944, -912,
0797: -1008, -976, -816, -784, -880, -848, 5504, 5248, 6016,
0798: 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528,
0799: 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496,
0800: 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
0801: 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
0802: 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
0803: 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104,
0804: 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344, 328,
0805: 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392,
0806: 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248,
0807: 232, 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120,
0808: 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760,
0809: 1696, 688, 656, 752, 720, 560, 528, 624, 592, 944, 912,
0810: 1008, 976, 816, 784, 880, 848 };
0811:
0812: public static short alaw2linear(byte ulawbyte) {
0813:
0814: return a2l[ulawbyte & 0xFF];
0815:
0816: }
0817:
0818: /**
0819: * Converts a buffer of signed 16bit big endian samples to uLaw.
0820: * The uLaw bytes overwrite the original 16 bit values.
0821: * The first byte-offset of the uLaw bytes is byteOffset.
0822: * It will be written sampleCount/2 bytes.
0823: */
0824: public static void pcm162alaw(byte[] buffer, int byteOffset,
0825: int sampleCount, boolean bigEndian) {
0826:
0827: int shortIndex = byteOffset;
0828: int alawIndex = shortIndex;
0829: if (bigEndian) {
0830: while (sampleCount > 0) {
0831: buffer[alawIndex++] = linear2alaw(bytesToShort16(
0832: buffer[shortIndex], buffer[shortIndex + 1]));
0833: shortIndex++;
0834: shortIndex++;
0835: sampleCount--;
0836: }
0837: } else {
0838: while (sampleCount > 0) {
0839: buffer[alawIndex++] = linear2alaw(bytesToShort16(
0840: buffer[shortIndex + 1], buffer[shortIndex]));
0841: shortIndex++;
0842: shortIndex++;
0843: sampleCount--;
0844: }
0845: }
0846:
0847: }
0848:
0849: /**
0850: * Fills outBuffer with alaw samples.
0851: * reading starts from inBuffer[inByteOffset].
0852: * writing starts at outBuffer[outByteOffset].
0853: * There will be sampleCount*2 bytes read from inBuffer;
0854: * There will be sampleCount <B>bytes</B> written to outBuffer.
0855: */
0856: public static void pcm162alaw(byte[] inBuffer, int inByteOffset,
0857: byte[] outBuffer, int outByteOffset, int sampleCount,
0858: boolean bigEndian) {
0859:
0860: int shortIndex = inByteOffset;
0861: int alawIndex = outByteOffset;
0862: if (bigEndian) {
0863: while (sampleCount > 0) {
0864: outBuffer[alawIndex++] = linear2alaw(bytesToShort16(
0865: inBuffer[shortIndex], inBuffer[shortIndex + 1]));
0866: shortIndex++;
0867: shortIndex++;
0868: sampleCount--;
0869: }
0870: } else {
0871: while (sampleCount > 0) {
0872: outBuffer[alawIndex++] = linear2alaw(bytesToShort16(
0873: inBuffer[shortIndex + 1], inBuffer[shortIndex]));
0874: shortIndex++;
0875: shortIndex++;
0876: sampleCount--;
0877: }
0878: }
0879:
0880: }
0881:
0882: /**
0883: * Converts a buffer of 8bit samples to alaw.
0884: * The alaw bytes overwrite the original 8 bit values.
0885: * The first byte-offset of the aLaw bytes is byteOffset.
0886: * It will be written sampleCount bytes.
0887: */
0888: public static void pcm82alaw(byte[] buffer, int byteOffset,
0889: int sampleCount, boolean signed) {
0890:
0891: sampleCount += byteOffset;
0892: if (signed) {
0893: for (int i = byteOffset; i < sampleCount; i++) {
0894: buffer[i] = linear2alaw((short) (buffer[i] << 8));
0895: }
0896: } else {
0897: for (int i = byteOffset; i < sampleCount; i++) {
0898: buffer[i] = linear2alaw((short) (((byte) (buffer[i] + 128)) << 8));
0899: }
0900: }
0901:
0902: }
0903:
0904: /**
0905: * Fills outBuffer with alaw samples.
0906: * reading starts from inBuffer[inByteOffset].
0907: * writing starts at outBuffer[outByteOffset].
0908: * There will be sampleCount <B>bytes</B> written to outBuffer.
0909: */
0910: public static void pcm82alaw(byte[] inBuffer, int inByteOffset,
0911: byte[] outBuffer, int outByteOffset, int sampleCount,
0912: boolean signed) {
0913:
0914: int alawIndex = outByteOffset;
0915: int pcmIndex = inByteOffset;
0916: if (signed) {
0917: while (sampleCount > 0) {
0918: outBuffer[alawIndex++] = linear2alaw((short) (inBuffer[pcmIndex++] << 8));
0919: sampleCount--;
0920: }
0921: } else {
0922: while (sampleCount > 0) {
0923: outBuffer[alawIndex++] = linear2alaw((short) (((byte) (inBuffer[pcmIndex++] + 128)) << 8));
0924: sampleCount--;
0925: }
0926: }
0927:
0928: }
0929:
0930: /**
0931: * Converts an alaw buffer to 8bit pcm samples
0932: * The 8bit bytes overwrite the original alaw values.
0933: * The first byte-offset of the aLaw bytes is byteOffset.
0934: * It will be written sampleCount bytes.
0935: */
0936: public static void alaw2pcm8(byte[] buffer, int byteOffset,
0937: int sampleCount, boolean signed) {
0938:
0939: sampleCount += byteOffset;
0940: if (signed) {
0941: for (int i = byteOffset; i < sampleCount; i++) {
0942: buffer[i] = (byte) ((a2l[buffer[i] & 0xFF] >> 8) & 0xFF);
0943: }
0944: } else {
0945: for (int i = byteOffset; i < sampleCount; i++) {
0946: buffer[i] = (byte) ((a2l[buffer[i] & 0xFF] >> 8) + 128);
0947: }
0948: }
0949:
0950: }
0951:
0952: /**
0953: * Fills outBuffer with alaw samples.
0954: * reading starts from inBuffer[inByteOffset].
0955: * writing starts at outBuffer[outByteOffset].
0956: * There will be sampleCount <B>bytes</B> written to outBuffer.
0957: */
0958: public static void alaw2pcm8(byte[] inBuffer, int inByteOffset,
0959: byte[] outBuffer, int outByteOffset, int sampleCount,
0960: boolean signed) {
0961:
0962: int alawIndex = inByteOffset;
0963: int pcmIndex = outByteOffset;
0964: if (signed) {
0965: while (sampleCount > 0) {
0966: outBuffer[pcmIndex++] = (byte) ((a2l[inBuffer[alawIndex++] & 0xFF] >> 8) & 0xFF);
0967: sampleCount--;
0968: }
0969: } else {
0970: while (sampleCount > 0) {
0971: outBuffer[pcmIndex++] = (byte) ((a2l[inBuffer[alawIndex++] & 0xFF] >> 8) + 128);
0972: sampleCount--;
0973: }
0974: }
0975:
0976: }
0977:
0978: /**
0979: * Fills outBuffer with pcm signed 16 bit samples.
0980: * reading starts from inBuffer[inByteOffset].
0981: * writing starts at outBuffer[outByteOffset].
0982: * There will be sampleCount bytes read from inBuffer;
0983: * There will be sampleCount*2 bytes written to outBuffer.
0984: */
0985: public static void alaw2pcm16(byte[] inBuffer, int inByteOffset,
0986: byte[] outBuffer, int outByteOffset,
0987:
0988: int sampleCount, boolean bigEndian) {
0989: int shortIndex = outByteOffset;
0990: int alawIndex = inByteOffset;
0991: while (sampleCount > 0) {
0992: intToBytes16(a2l[inBuffer[alawIndex++] & 0xFF], outBuffer,
0993: shortIndex++, bigEndian);
0994: shortIndex++;
0995: sampleCount--;
0996: }
0997:
0998: }
0999:
1000: //////////////////////// cross conversion alaw <-> ulaw ////////////////////////////////////////
1001: private static byte[] u2a = { -86, -85, -88, -87, -82, -81, -84,
1002: -83, -94, -93, -96, -95, -90, -89, -92, -91, -70, -69, -72,
1003: -71, -66, -65, -68, -67, -78, -77, -80, -79, -74, -73, -76,
1004: -75, -118, -117, -120, -119, -114, -113, -116, -115, -126,
1005: -125, -128, -127, -122, -121, -124, -123, -101, -104, -103,
1006: -98, -97, -100, -99, -110, -109, -112, -111, -106, -105,
1007: -108, -107, -22, -24, -23, -18, -17, -20, -19, -30, -29,
1008: -32, -31, -26, -25, -28, -27, -6, -8, -2, -1, -4, -3, -14,
1009: -13, -16, -15, -10, -9, -12, -11, -53, -55, -49, -51, -62,
1010: -61, -64, -63, -58, -57, -60, -59, -38, -37, -40, -39, -34,
1011: -33, -36, -35, -46, -46, -45, -45, -48, -48, -47, -47, -42,
1012: -42, -41, -41, -44, -44, -43, -43, 42, 43, 40, 41, 46, 47,
1013: 44, 45, 34, 35, 32, 33, 38, 39, 36, 37, 58, 59, 56, 57, 62,
1014: 63, 60, 61, 50, 51, 48, 49, 54, 55, 52, 53, 10, 11, 8, 9,
1015: 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 24, 25, 30, 31,
1016: 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 106, 104, 105, 110,
1017: 111, 108, 109, 98, 99, 96, 97, 102, 103, 100, 101, 122,
1018: 120, 126, 127, 124, 125, 114, 115, 112, 113, 118, 119, 116,
1019: 117, 75, 73, 79, 77, 66, 67, 64, 65, 70, 71, 68, 69, 90,
1020: 91, 88, 89, 94, 95, 92, 93, 82, 82, 83, 83, 80, 80, 81, 81,
1021: 86, 86, 87, 87, 84, 84, 85, 85, };
1022:
1023: public static byte ulaw2alaw(byte sample) {
1024:
1025: return u2a[sample & 0xFF];
1026:
1027: }
1028:
1029: /**
1030: * Converts a buffer of uLaw samples to aLaw.
1031: */
1032: public static void ulaw2alaw(byte[] buffer, int byteOffset,
1033: int sampleCount) {
1034:
1035: sampleCount += byteOffset;
1036: for (int i = byteOffset; i < sampleCount; i++) {
1037: buffer[i] = u2a[buffer[i] & 0xFF];
1038: }
1039:
1040: }
1041:
1042: /**
1043: * Fills outBuffer with alaw samples.
1044: */
1045: public static void ulaw2alaw(byte[] inBuffer, int inByteOffset,
1046: byte[] outBuffer, int outByteOffset, int sampleCount) {
1047:
1048: int ulawIndex = outByteOffset;
1049: int alawIndex = inByteOffset;
1050: while (sampleCount > 0) {
1051: outBuffer[alawIndex++] = u2a[inBuffer[ulawIndex++] & 0xFF];
1052: sampleCount--;
1053: }
1054:
1055: }
1056:
1057: private static byte[] a2u = { -86, -85, -88, -87, -82, -81, -84,
1058: -83, -94, -93, -96, -95, -90, -89, -92, -91, -71, -70, -73,
1059: -72, -67, -66, -69, -68, -79, -78, -80, -80, -75, -74, -77,
1060: -76, -118, -117, -120, -119, -114, -113, -116, -115, -126,
1061: -125, -128, -127, -122, -121, -124, -123, -102, -101, -104,
1062: -103, -98, -97, -100, -99, -110, -109, -112, -111, -106,
1063: -105, -108, -107, -30, -29, -32, -31, -26, -25, -28, -27,
1064: -35, -35, -36, -36, -33, -33, -34, -34, -12, -10, -16, -14,
1065: -4, -2, -8, -6, -22, -21, -24, -23, -18, -17, -20, -19,
1066: -56, -55, -58, -57, -52, -51, -54, -53, -64, -63, -65, -65,
1067: -60, -59, -62, -61, -42, -41, -44, -43, -38, -37, -40, -39,
1068: -49, -49, -50, -50, -46, -45, -48, -47, 42, 43, 40, 41, 46,
1069: 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37, 57, 58, 55, 56,
1070: 61, 62, 59, 60, 49, 50, 48, 48, 53, 54, 51, 52, 10, 11, 8,
1071: 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 26, 27, 24, 25,
1072: 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 98, 99, 96,
1073: 97, 102, 103, 100, 101, 93, 93, 92, 92, 95, 95, 94, 94,
1074: 116, 118, 112, 114, 124, 126, 120, 122, 106, 107, 104, 105,
1075: 110, 111, 108, 109, 72, 73, 70, 71, 76, 77, 74, 75, 64, 65,
1076: 63, 63, 68, 69, 66, 67, 86, 87, 84, 85, 90, 91, 88, 89, 79,
1077: 79, 78, 78, 82, 83, 80, 81, };
1078:
1079: public static byte alaw2ulaw(byte sample) {
1080:
1081: return a2u[sample & 0xFF];
1082:
1083: }
1084:
1085: /**
1086: * Converts a buffer of aLaw samples to uLaw.
1087: * The uLaw bytes overwrite the original aLaw values.
1088: * The first byte-offset of the uLaw bytes is byteOffset.
1089: * It will be written sampleCount bytes.
1090: */
1091: public static void alaw2ulaw(byte[] buffer, int byteOffset,
1092: int sampleCount) {
1093:
1094: sampleCount += byteOffset;
1095: for (int i = byteOffset; i < sampleCount; i++) {
1096: buffer[i] = a2u[buffer[i] & 0xFF];
1097: }
1098:
1099: }
1100:
1101: /**
1102: * Fills outBuffer with ulaw samples.
1103: * reading starts from inBuffer[inByteOffset].
1104: * writing starts at outBuffer[outByteOffset].
1105: * There will be sampleCount <B>bytes</B> written to outBuffer.
1106: */
1107: public static void alaw2ulaw(byte[] inBuffer, int inByteOffset,
1108: byte[] outBuffer, int outByteOffset, int sampleCount) {
1109:
1110: int ulawIndex = outByteOffset;
1111: int alawIndex = inByteOffset;
1112: while (sampleCount > 0) {
1113: outBuffer[ulawIndex++] = a2u[inBuffer[alawIndex++] & 0xFF];
1114: sampleCount--;
1115: }
1116:
1117: }
1118:
1119: //////////////////////// high level methods /////////////////////////////////////////////////
1120:
1121: /*
1122: * !! Here, unlike other functions in this class, the length is
1123: * in bytes rather than samples !!
1124: */
1125: public static void changeOrderOrSign(byte[] buffer, int nOffset,
1126: int nByteLength, int nBytesPerSample) {
1127:
1128: switch (nBytesPerSample) {
1129: case 1:
1130: convertSign8(buffer, nOffset, nByteLength);
1131: break;
1132:
1133: case 2:
1134: swapOrder16(buffer, nOffset, nByteLength / 2);
1135: break;
1136:
1137: case 3:
1138: swapOrder24(buffer, nOffset, nByteLength / 3);
1139: break;
1140:
1141: case 4:
1142: swapOrder32(buffer, nOffset, nByteLength / 4);
1143: break;
1144: }
1145:
1146: }
1147:
1148: /*
1149: * !! Here, unlike other functions in this class, the length is
1150: * in bytes rather than samples !!
1151: */
1152: public static void changeOrderOrSign(byte[] inBuffer,
1153: int nInOffset, byte[] outBuffer, int nOutOffset,
1154: int nByteLength, int nBytesPerSample) {
1155:
1156: switch (nBytesPerSample) {
1157: case 1:
1158: convertSign8(inBuffer, nInOffset, outBuffer, nOutOffset,
1159: nByteLength);
1160: break;
1161:
1162: case 2:
1163: swapOrder16(inBuffer, nInOffset, outBuffer, nOutOffset,
1164: nByteLength / 2);
1165: break;
1166:
1167: case 3:
1168: swapOrder24(inBuffer, nInOffset, outBuffer, nOutOffset,
1169: nByteLength / 3);
1170: break;
1171:
1172: case 4:
1173: swapOrder32(inBuffer, nInOffset, outBuffer, nOutOffset,
1174: nByteLength / 4);
1175: break;
1176: }
1177:
1178: }
1179:
1180: ///////////////// Annexe: how the arrays were created. //////////////////////////////////
1181:
1182: /*
1183: * Converts a uLaw byte to a linear signed 16bit sample.
1184: * Ported to Java by fb.
1185: * <BR>Originally by:<BR>
1186: *
1187: * Craig Reese: IDA/Supercomputing Research Center <BR>
1188: * 29 September 1989 <BR>
1189: *
1190: * References: <BR>
1191: * <OL>
1192: * <LI>CCITT Recommendation G.711 (very difficult to follow)</LI>
1193: * <LI>MIL-STD-188-113,"Interoperability and Performance Standards
1194: * for Analog-to_Digital Conversion Techniques,"
1195: * 17 February 1987</LI>
1196: * </OL>
1197: */
1198: /*
1199: private static final int exp_lut2[] = {
1200: 0,132,396,924,1980,4092,8316,16764
1201: };
1202:
1203: public static short _ulaw2linear(int ulawbyte) {
1204:
1205: int sign, exponent, mantissa, sample;
1206:
1207: ulawbyte = ~ulawbyte;
1208: sign = (ulawbyte & 0x80);
1209: exponent = (ulawbyte >> 4) & 0x07;
1210: mantissa = ulawbyte & 0x0F;
1211: sample = exp_lut2[exponent] + (mantissa << (exponent + 3));
1212: if (sign != 0) sample = -sample;
1213:
1214: return((short) sample);
1215:
1216: }*/
1217:
1218: /* u- to A-law conversions: copied from CCITT G.711 specifications */
1219: /*
1220: private static byte[] _u2a = {
1221: 1, 1, 2, 2, 3, 3, 4, 4,
1222: 5, 5, 6, 6, 7, 7, 8, 8,
1223: 9, 10, 11, 12, 13, 14, 15, 16,
1224: 17, 18, 19, 20, 21, 22, 23, 24,
1225: 25, 27, 29, 31, 33, 34, 35, 36,
1226: 37, 38, 39, 40, 41, 42, 43, 44,
1227: 46, 48, 49, 50, 51, 52, 53, 54,
1228: 55, 56, 57, 58, 59, 60, 61, 62,
1229: 64, 65, 66, 67, 68, 69, 70, 71,
1230: 72, 73, 74, 75, 76, 77, 78, 79,
1231: 81, 82, 83, 84, 85, 86, 87, 88,
1232: 89, 90, 91, 92, 93, 94, 95, 96,
1233: 97, 98, 99, 100, 101, 102, 103, 104,
1234: 105, 106, 107, 108, 109, 110, 111, 112,
1235: 113, 114, 115, 116, 117, 118, 119, 120,
1236: 121, 122, 123, 124, 125, 126, 127, (byte) 128};
1237: */
1238:
1239: /* u-law to A-law conversion */
1240: /*
1241: * This source code is a product of Sun Microsystems, Inc. and is provided
1242: * for unrestricted use. Users may copy or modify this source code without
1243: * charge.
1244: */
1245: /*
1246: public static byte _ulaw2alaw(byte sample) {
1247:
1248: sample &= 0xff;
1249:
1250: return (byte) (((sample & 0x80)!=0) ? (0xD5 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)) :
1251: (0x55 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)));
1252:
1253: }*/
1254:
1255: /* A- to u-law conversions */
1256: /*
1257: private static byte[] _a2u = {
1258: 1, 3, 5, 7, 9, 11, 13, 15,
1259: 16, 17, 18, 19, 20, 21, 22, 23,
1260: 24, 25, 26, 27, 28, 29, 30, 31,
1261: 32, 32, 33, 33, 34, 34, 35, 35,
1262: 36, 37, 38, 39, 40, 41, 42, 43,
1263: 44, 45, 46, 47, 48, 48, 49, 49,
1264: 50, 51, 52, 53, 54, 55, 56, 57,
1265: 58, 59, 60, 61, 62, 63, 64, 64,
1266: 65, 66, 67, 68, 69, 70, 71, 72,
1267: 73, 74, 75, 76, 77, 78, 79, 79,
1268: 80, 81, 82, 83, 84, 85, 86, 87,
1269: 88, 89, 90, 91, 92, 93, 94, 95,
1270: 96, 97, 98, 99, 100, 101, 102, 103,
1271: 104, 105, 106, 107, 108, 109, 110, 111,
1272: 112, 113, 114, 115, 116, 117, 118, 119,
1273: 120, 121, 122, 123, 124, 125, 126, 127};
1274: */
1275:
1276: /*
1277: * This source code is a product of Sun Microsystems, Inc. and is provided
1278: * for unrestricted use. Users may copy or modify this source code without
1279: * charge.
1280: */
1281: /*
1282: public static byte _alaw2ulaw(byte sample) {
1283:
1284: sample &= 0xff;
1285: return (byte) (((sample & 0x80)!=0) ? (0xFF ^ _a2u[(sample ^ 0xD5) & 0x7F]) :
1286: (0x7F ^ _a2u[(sample ^ 0x55) & 0x7F]));
1287: }
1288:
1289: public static void print_a2u() {
1290:
1291: System.out.println("\tprivate static byte[] a2u = {");
1292: for (int i=-128; i<128; i++) {
1293: if (((i+128) % 16)==0) {
1294: System.out.print("\t\t");
1295: }
1296: byte b=(byte) i;
1297: System.out.print(_alaw2ulaw(b)+", ");
1298: if (((i+128) % 16)==15) {
1299: System.out.println("");
1300: }
1301: }
1302: System.out.println("\t};");
1303:
1304: }
1305:
1306: public static void print_u2a() {
1307:
1308: System.out.println("\tprivate static byte[] u2a = {");
1309: for (int i=-128; i<128; i++) {
1310: if (((i+128) % 16)==0) {
1311: System.out.print("\t\t");
1312: }
1313: byte b=(byte) i;
1314: System.out.print(_ulaw2alaw(b)+", ");
1315: if (((i+128) % 16)==15) {
1316: System.out.println("");
1317: }
1318: }
1319: System.out.println("\t};");
1320:
1321: }
1322: */
1323:
1324: }
|