summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/sampled/TConversionTool.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/sampled/TConversionTool.java')
-rw-r--r--songdbj/org/tritonus/share/sampled/TConversionTool.java1224
1 files changed, 1224 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/sampled/TConversionTool.java b/songdbj/org/tritonus/share/sampled/TConversionTool.java
new file mode 100644
index 0000000000..18673edf31
--- /dev/null
+++ b/songdbj/org/tritonus/share/sampled/TConversionTool.java
@@ -0,0 +1,1224 @@
1/*
2 * TConversionTool.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 1999,2000 by Florian Bomers <http://www.bomers.de>
9 * Copyright (c) 2000 by Matthias Pfisterer
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as published
14 * by the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 */
27
28/*
29|<--- this code is formatted to fit into 80 columns --->|
30*/
31
32package org.tritonus.share.sampled;
33
34
35/**
36 * Useful methods for converting audio data.
37 *
38 * @author Florian Bomers
39 * @author Matthias Pfisterer
40 */
41
42/*
43For convenience, a list of available methods is maintained here.
44Some hints:
45- buffers: always byte arrays
46- offsets: always in bytes
47- sampleCount: number of SAMPLES to read/write, as opposed to FRAMES or BYTES!
48- when in buffer and out buffer are given, the data is copied,
49 otherwise it is replaced in the same buffer (buffer size is not checked!)
50- a number (except "2") gives the number of bits in which format
51 the samples have to be.
52- >8 bits per sample is always treated signed.
53- all functions are tried to be optimized - hints welcome !
54
55
56** "high level" methods **
57changeOrderOrSign(buffer, nOffset, nByteLength, nBytesPerSample)
58changeOrderOrSign(inBuffer, nInOffset, outBuffer, nOutOffset, nByteLength, nBytesPerSample)
59
60
61** PCM byte order and sign conversion **
62void convertSign8(buffer, byteOffset, sampleCount)
63void swapOrder16(buffer, byteOffset, sampleCount)
64void swapOrder24(buffer, byteOffset, sampleCount)
65void swapOrder32(buffer, byteOffset, sampleCount)
66void convertSign8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
67void swapOrder16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
68void swapOrder24(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
69void swapOrder32(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
70
71
72** conversion functions for byte arrays **
73** these are for reference to see how to implement these conversions **
74short bytesToShort16(highByte, lowByte)
75short bytesToShort16(buffer, byteOffset, bigEndian)
76short bytesToInt16(highByte, lowByte)
77short bytesToInt16(buffer, byteOffset, bigEndian)
78short bytesToInt24(buffer, byteOffset, bigEndian)
79short bytesToInt32(buffer, byteOffset, bigEndian)
80void shortToBytes16(sample, buffer, byteOffset, bigEndian)
81void intToBytes24(sample, buffer, byteOffset, bigEndian)
82void intToBytes32(sample, buffer, byteOffset, bigEndian)
83
84
85** ULAW <-> PCM **
86byte linear2ulaw(int sample)
87short ulaw2linear(int ulawbyte)
88void pcm162ulaw(buffer, byteOffset, sampleCount, bigEndian)
89void pcm162ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
90void pcm82ulaw(buffer, byteOffset, sampleCount, signed)
91void pcm82ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
92void ulaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
93void ulaw2pcm8(buffer, byteOffset, sampleCount, signed)
94void ulaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
95
96
97** ALAW <-> PCM **
98byte linear2alaw(short pcm_val)
99short alaw2linear(byte ulawbyte)
100void pcm162alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
101void pcm162alaw(buffer, byteOffset, sampleCount, bigEndian)
102void pcm82alaw(buffer, byteOffset, sampleCount, signed)
103void pcm82alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
104void alaw2pcm16(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, bigEndian)
105void alaw2pcm8(buffer, byteOffset, sampleCount, signed)
106void alaw2pcm8(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount, signed)
107
108
109** ULAW <-> ALAW **
110byte ulaw2alaw(byte sample)
111void ulaw2alaw(buffer, byteOffset, sampleCount)
112void ulaw2alaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
113byte alaw2ulaw(byte sample)
114void alaw2ulaw(buffer, byteOffset, sampleCount)
115void alaw2ulaw(inBuffer, inByteOffset, outBuffer, outByteOffset, sampleCount)
116
117*/
118
119public class TConversionTool {
120
121 ///////////////// sign/byte-order conversion ///////////////////////////////////
122
123 public static void convertSign8(byte[] buffer, int byteOffset, int sampleCount) {
124 sampleCount+=byteOffset;
125 for (int i=byteOffset; i<sampleCount; i++) {
126 buffer[i]+=128;
127 }
128 }
129
130 public static void swapOrder16(byte[] buffer, int byteOffset, int sampleCount) {
131 int byteMax=sampleCount*2+byteOffset-1;
132 int i=byteOffset;
133 while (i<byteMax) {
134 byte h=buffer[i];
135 buffer[i]=buffer[++i];
136 buffer[i++]=h;
137 }
138 }
139
140 public static void swapOrder24(byte[] buffer, int byteOffset, int sampleCount) {
141 int byteMax=sampleCount*3+byteOffset-2;
142 int i=byteOffset;
143 while (i<byteMax) {
144 byte h=buffer[i];
145 buffer[i]=buffer[++i+1];
146 buffer[++i]=h;
147 i++;
148 }
149 }
150
151 public static void swapOrder32(byte[] buffer, int byteOffset, int sampleCount) {
152 int byteMax=sampleCount*4+byteOffset-3;
153 int i=byteOffset;
154 while (i<byteMax) {
155 byte h=buffer[i];
156 buffer[i]=buffer[i+3];
157 buffer[i+3]=h;
158 i++;
159 h=buffer[i];
160 buffer[i]=buffer[++i];
161 buffer[i++]=h;
162 i++;
163 }
164 }
165
166 public static void convertSign8(byte[] inBuffer, int inByteOffset,
167 byte[] outBuffer, int outByteOffset, int sampleCount) {
168 while (sampleCount>0) {
169 outBuffer[outByteOffset++]=(byte)(inBuffer[inByteOffset++]+128);
170 sampleCount--;
171 }
172 }
173
174 public static void swapOrder16(byte[] inBuffer, int inByteOffset,
175 byte[] outBuffer, int outByteOffset, int sampleCount) {
176 while (sampleCount>0) {
177 outBuffer[outByteOffset++]=inBuffer[inByteOffset+1];
178 outBuffer[outByteOffset++]=inBuffer[inByteOffset++];
179 inByteOffset++;
180 sampleCount--;
181 }
182 }
183
184 public static void swapOrder24(byte[] inBuffer, int inByteOffset,
185 byte[] outBuffer, int outByteOffset, int sampleCount) {
186 while (sampleCount>0) {
187 outBuffer[outByteOffset++]=inBuffer[inByteOffset+2];
188 outByteOffset++;
189 outBuffer[outByteOffset++]=inBuffer[inByteOffset++];
190 inByteOffset++;
191 inByteOffset++;
192 sampleCount--;
193 }
194 }
195
196 public static void swapOrder32(byte[] inBuffer, int inByteOffset,
197 byte[] outBuffer, int outByteOffset, int sampleCount) {
198 while (sampleCount>0) {
199 outBuffer[outByteOffset++]=inBuffer[inByteOffset+3];
200 outBuffer[outByteOffset++]=inBuffer[inByteOffset+2];
201 outBuffer[outByteOffset++]=inBuffer[inByteOffset+1];
202 outBuffer[outByteOffset++]=inBuffer[inByteOffset++];
203 inByteOffset++;
204 inByteOffset++;
205 inByteOffset++;
206 sampleCount--;
207 }
208 }
209
210
211 ///////////////// conversion functions for byte arrays ////////////////////////////
212
213
214 /**
215 * Converts 2 bytes to a signed sample of type <code>short</code>.
216 * <p> This is a reference function.
217 */
218 public static short bytesToShort16(byte highByte, byte lowByte) {
219 return (short) ((highByte<<8) | (lowByte & 0xFF));
220 }
221
222 /**
223 * Converts 2 successive bytes starting at <code>byteOffset</code> in
224 * <code>buffer</code> to a signed sample of type <code>short</code>.
225 * <p>
226 * For little endian, buffer[byteOffset] is interpreted as low byte,
227 * whereas it is interpreted as high byte in big endian.
228 * <p> This is a reference function.
229 */
230 public static short bytesToShort16(byte[] buffer, int byteOffset, boolean bigEndian) {
231 return bigEndian?
232 ((short) ((buffer[byteOffset]<<8) | (buffer[byteOffset+1] & 0xFF))):
233 ((short) ((buffer[byteOffset+1]<<8) | (buffer[byteOffset] & 0xFF)));
234 }
235
236 /**
237 * Converts 2 bytes to a signed integer sample with 16bit range.
238 * <p> This is a reference function.
239 */
240 public static int bytesToInt16(byte highByte, byte lowByte) {
241 return (highByte<<8) | (lowByte & 0xFF);
242 }
243
244 /**
245 * Converts 2 successive bytes starting at <code>byteOffset</code> in
246 * <code>buffer</code> to a signed integer sample with 16bit range.
247 * <p>
248 * For little endian, buffer[byteOffset] is interpreted as low byte,
249 * whereas it is interpreted as high byte in big endian.
250 * <p> This is a reference function.
251 */
252 public static int bytesToInt16(byte[] buffer, int byteOffset, boolean bigEndian) {
253 return bigEndian?
254 ((buffer[byteOffset]<<8) | (buffer[byteOffset+1] & 0xFF)):
255 ((buffer[byteOffset+1]<<8) | (buffer[byteOffset] & 0xFF));
256 }
257
258 /**
259 * Converts 3 successive bytes starting at <code>byteOffset</code> in
260 * <code>buffer</code> to a signed integer sample with 24bit range.
261 * <p>
262 * For little endian, buffer[byteOffset] is interpreted as lowest byte,
263 * whereas it is interpreted as highest byte in big endian.
264 * <p> This is a reference function.
265 */
266 public static int bytesToInt24(byte[] buffer, int byteOffset, boolean bigEndian) {
267 return bigEndian?
268 ((buffer[byteOffset]<<16) // let Java handle sign-bit
269 | ((buffer[byteOffset+1] & 0xFF)<<8) // inhibit sign-bit handling
270 | (buffer[byteOffset+2] & 0xFF)):
271 ((buffer[byteOffset+2]<<16) // let Java handle sign-bit
272 | ((buffer[byteOffset+1] & 0xFF)<<8) // inhibit sign-bit handling
273 | (buffer[byteOffset] & 0xFF));
274 }
275
276 /**
277 * Converts a 4 successive bytes starting at <code>byteOffset</code> in
278 * <code>buffer</code> to a signed 32bit integer sample.
279 * <p>
280 * For little endian, buffer[byteOffset] is interpreted as lowest byte,
281 * whereas it is interpreted as highest byte in big endian.
282 * <p> This is a reference function.
283 */
284 public static int bytesToInt32(byte[] buffer, int byteOffset, boolean bigEndian) {
285 return bigEndian?
286 ((buffer[byteOffset]<<24) // let Java handle sign-bit
287 | ((buffer[byteOffset+1] & 0xFF)<<16) // inhibit sign-bit handling
288 | ((buffer[byteOffset+2] & 0xFF)<<8) // inhibit sign-bit handling
289 | (buffer[byteOffset+3] & 0xFF)):
290 ((buffer[byteOffset+3]<<24) // let Java handle sign-bit
291 | ((buffer[byteOffset+2] & 0xFF)<<16) // inhibit sign-bit handling
292 | ((buffer[byteOffset+1] & 0xFF)<<8) // inhibit sign-bit handling
293 | (buffer[byteOffset] & 0xFF));
294 }
295
296
297 /**
298 * Converts a sample of type <code>short</code> to 2 bytes in an array.
299 * <code>sample</code> is interpreted as signed (as Java does).
300 * <p>
301 * For little endian, buffer[byteOffset] is filled with low byte of sample,
302 * and buffer[byteOffset+1] is filled with high byte of sample.
303 * <p> For big endian, this is reversed.
304 * <p> This is a reference function.
305 */
306 public static void shortToBytes16(short sample, byte[] buffer, int byteOffset, boolean bigEndian) {
307 intToBytes16(sample, buffer, byteOffset, bigEndian);
308 }
309
310 /**
311 * Converts a 16 bit sample of type <code>int</code> to 2 bytes in an array.
312 * <code>sample</code> is interpreted as signed (as Java does).
313 * <p>
314 * For little endian, buffer[byteOffset] is filled with low byte of sample,
315 * and buffer[byteOffset+1] is filled with high byte of sample + sign bit.
316 * <p> For big endian, this is reversed.
317 * <p> Before calling this function, it should be assured that <code>sample</code>
318 * is in the 16bit range - it will not be clipped.
319 * <p> This is a reference function.
320 */
321 public static void intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {
322 if (bigEndian) {
323 buffer[byteOffset++]=(byte) (sample >> 8);
324 buffer[byteOffset]=(byte) (sample & 0xFF);
325 } else {
326 buffer[byteOffset++]=(byte) (sample & 0xFF);
327 buffer[byteOffset]=(byte) (sample >> 8);
328 }
329 }
330
331 /**
332 * Converts a 24 bit sample of type <code>int</code> to 3 bytes in an array.
333 * <code>sample</code> is interpreted as signed (as Java does).
334 * <p>
335 * For little endian, buffer[byteOffset] is filled with low byte of sample,
336 * and buffer[byteOffset+2] is filled with the high byte of sample + sign bit.
337 * <p> For big endian, this is reversed.
338 * <p> Before calling this function, it should be assured that <code>sample</code>
339 * is in the 24bit range - it will not be clipped.
340 * <p> This is a reference function.
341 */
342 public static void intToBytes24(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {
343 if (bigEndian) {
344 buffer[byteOffset++]=(byte) (sample >> 16);
345 buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);
346 buffer[byteOffset]=(byte) (sample & 0xFF);
347 } else {
348 buffer[byteOffset++]=(byte) (sample & 0xFF);
349 buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);
350 buffer[byteOffset]=(byte) (sample >> 16);
351 }
352 }
353
354
355 /**
356 * Converts a 32 bit sample of type <code>int</code> to 4 bytes in an array.
357 * <code>sample</code> is interpreted as signed (as Java does).
358 * <p>
359 * For little endian, buffer[byteOffset] is filled with lowest byte of sample,
360 * and buffer[byteOffset+3] is filled with the high byte of sample + sign bit.
361 * <p> For big endian, this is reversed.
362 * <p> This is a reference function.
363 */
364 public static void intToBytes32(int sample, byte[] buffer, int byteOffset, boolean bigEndian) {
365 if (bigEndian) {
366 buffer[byteOffset++]=(byte) (sample >> 24);
367 buffer[byteOffset++]=(byte) ((sample >>> 16) & 0xFF);
368 buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);
369 buffer[byteOffset]=(byte) (sample & 0xFF);
370 } else {
371 buffer[byteOffset++]=(byte) (sample & 0xFF);
372 buffer[byteOffset++]=(byte) ((sample >>> 8) & 0xFF);
373 buffer[byteOffset++]=(byte) ((sample >>> 16) & 0xFF);
374 buffer[byteOffset]=(byte) (sample >> 24);
375 }
376 }
377
378
379 /////////////////////// ULAW ///////////////////////////////////////////
380
381 private static final boolean ZEROTRAP=true;
382 private static final short BIAS=0x84;
383 private static final int CLIP=32635;
384 private static final int exp_lut1[] ={
385 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
386 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
387 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
388 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
389 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
390 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
391 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
392 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
393 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
394 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
395 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
396 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
397 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
398 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
399 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
400 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
401 };
402
403
404 /**
405 * Converts a linear signed 16bit sample to a uLaw byte.
406 * Ported to Java by fb.
407 * <BR>Originally by:<BR>
408 * Craig Reese: IDA/Supercomputing Research Center <BR>
409 * Joe Campbell: Department of Defense <BR>
410 * 29 September 1989 <BR>
411 */
412 public static byte linear2ulaw(int sample) {
413 int sign, exponent, mantissa, ulawbyte;
414
415 if (sample>32767) sample=32767;
416 else if (sample<-32768) sample=-32768;
417 /* Get the sample into sign-magnitude. */
418 sign = (sample >> 8) & 0x80; /* set aside the sign */
419 if (sign != 0) sample = -sample; /* get magnitude */
420 if (sample > CLIP) sample = CLIP; /* clip the magnitude */
421
422 /* Convert from 16 bit linear to ulaw. */
423 sample = sample + BIAS;
424 exponent = exp_lut1[(sample >> 7) & 0xFF];
425 mantissa = (sample >> (exponent + 3)) & 0x0F;
426 ulawbyte = ~(sign | (exponent << 4) | mantissa);
427 if (ZEROTRAP)
428 if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */
429 return((byte) ulawbyte);
430 }
431
432 /* u-law to linear conversion table */
433 private static short[] u2l = {
434 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
435 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
436 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
437 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
438 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
439 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
440 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
441 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
442 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
443 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
444 -876, -844, -812, -780, -748, -716, -684, -652,
445 -620, -588, -556, -524, -492, -460, -428, -396,
446 -372, -356, -340, -324, -308, -292, -276, -260,
447 -244, -228, -212, -196, -180, -164, -148, -132,
448 -120, -112, -104, -96, -88, -80, -72, -64,
449 -56, -48, -40, -32, -24, -16, -8, 0,
450 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
451 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
452 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
453 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
454 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
455 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
456 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
457 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
458 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
459 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
460 876, 844, 812, 780, 748, 716, 684, 652,
461 620, 588, 556, 524, 492, 460, 428, 396,
462 372, 356, 340, 324, 308, 292, 276, 260,
463 244, 228, 212, 196, 180, 164, 148, 132,
464 120, 112, 104, 96, 88, 80, 72, 64,
465 56, 48, 40, 32, 24, 16, 8, 0
466 };
467 public static short ulaw2linear(byte ulawbyte) {
468 return u2l[ulawbyte & 0xFF];
469 }
470
471
472
473 /**
474 * Converts a buffer of signed 16bit big endian samples to uLaw.
475 * The uLaw bytes overwrite the original 16 bit values.
476 * The first byte-offset of the uLaw bytes is byteOffset.
477 * It will be written sampleCount/2 bytes.
478 */
479 public static void pcm162ulaw(byte[] buffer, int byteOffset, int sampleCount, boolean bigEndian) {
480 int shortIndex=byteOffset;
481 int ulawIndex=shortIndex;
482 if (bigEndian) {
483 while (sampleCount>0) {
484 buffer[ulawIndex++]=linear2ulaw
485 (bytesToInt16(buffer[shortIndex], buffer[shortIndex+1]));
486 shortIndex++;
487 shortIndex++;
488 sampleCount--;
489 }
490 } else {
491 while (sampleCount>0) {
492 buffer[ulawIndex++]=linear2ulaw
493 (bytesToInt16(buffer[shortIndex+1], buffer[shortIndex]));
494 shortIndex++;
495 shortIndex++;
496 sampleCount--;
497 }
498 }
499 }
500
501 /**
502 * Fills outBuffer with ulaw samples.
503 * reading starts from inBuffer[inByteOffset].
504 * writing starts at outBuffer[outByteOffset].
505 * There will be sampleCount*2 bytes read from inBuffer;
506 * There will be sampleCount <B>bytes</B> written to outBuffer.
507 */
508 public static void pcm162ulaw(byte[] inBuffer, int inByteOffset,
509 byte[] outBuffer, int outByteOffset,
510 int sampleCount, boolean bigEndian) {
511 int shortIndex=inByteOffset;
512 int ulawIndex=outByteOffset;
513 if (bigEndian) {
514 while (sampleCount>0) {
515 outBuffer[ulawIndex++]=linear2ulaw
516 (bytesToInt16(inBuffer[shortIndex], inBuffer[shortIndex+1]));
517 shortIndex++;
518 shortIndex++;
519 sampleCount--;
520 }
521 } else {
522 while (sampleCount>0) {
523 outBuffer[ulawIndex++]=linear2ulaw
524 (bytesToInt16(inBuffer[shortIndex+1], inBuffer[shortIndex]));
525 shortIndex++;
526 shortIndex++;
527 sampleCount--;
528 }
529 }
530 }
531
532 // TODO: either direct 8bit pcm to ulaw, or better conversion from 8bit to 16bit
533 /**
534 * Converts a buffer of 8bit samples to uLaw.
535 * The uLaw bytes overwrite the original 8 bit values.
536 * The first byte-offset of the uLaw bytes is byteOffset.
537 * It will be written sampleCount bytes.
538 */
539 public static void pcm82ulaw(byte[] buffer, int byteOffset, int sampleCount, boolean signed) {
540 sampleCount+=byteOffset;
541 if (signed) {
542 for (int i=byteOffset; i<sampleCount; i++) {
543 buffer[i]=linear2ulaw(buffer[i] << 8);
544 }
545 } else {
546 for (int i=byteOffset; i<sampleCount; i++) {
547 buffer[i]=linear2ulaw(((byte) (buffer[i]+128)) << 8);
548 }
549 }
550 }
551
552 /**
553 * Fills outBuffer with ulaw samples.
554 * reading starts from inBuffer[inByteOffset].
555 * writing starts at outBuffer[outByteOffset].
556 * There will be sampleCount <B>bytes</B> written to outBuffer.
557 */
558 public static void pcm82ulaw(byte[] inBuffer, int inByteOffset,
559 byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) {
560 int ulawIndex=outByteOffset;
561 int pcmIndex=inByteOffset;
562 if (signed) {
563 while (sampleCount>0) {
564 outBuffer[ulawIndex++]=linear2ulaw(inBuffer[pcmIndex++] << 8);
565 sampleCount--;
566 }
567 } else {
568 while (sampleCount>0) {
569 outBuffer[ulawIndex++]=linear2ulaw(((byte) (inBuffer[pcmIndex++]+128)) << 8);
570 sampleCount--;
571 }
572 }
573 }
574
575 /**
576 * Fills outBuffer with pcm signed 16 bit samples.
577 * reading starts from inBuffer[inByteOffset].
578 * writing starts at outBuffer[outByteOffset].
579 * There will be sampleCount bytes read from inBuffer;
580 * There will be sampleCount*2 bytes written to outBuffer.
581 */
582 public static void ulaw2pcm16(byte[] inBuffer, int inByteOffset,
583 byte[] outBuffer, int outByteOffset,
584 int sampleCount, boolean bigEndian) {
585 int shortIndex=outByteOffset;
586 int ulawIndex=inByteOffset;
587 while (sampleCount>0) {
588 intToBytes16
589 (u2l[inBuffer[ulawIndex++] & 0xFF], outBuffer, shortIndex++, bigEndian);
590 shortIndex++;
591 sampleCount--;
592 }
593 }
594
595
596 // TODO: either direct 8bit pcm to ulaw, or better conversion from 8bit to 16bit
597 /**
598 * Inplace-conversion of a ulaw buffer to 8bit samples.
599 * The 8bit bytes overwrite the original ulaw values.
600 * The first byte-offset of the uLaw bytes is byteOffset.
601 * It will be written sampleCount bytes.
602 */
603 public static void ulaw2pcm8(byte[] buffer, int byteOffset, int sampleCount, boolean signed) {
604 sampleCount+=byteOffset;
605 if (signed) {
606 for (int i=byteOffset; i<sampleCount; i++) {
607 buffer[i]=(byte) ((u2l[buffer[i] & 0xFF] >> 8) & 0xFF);
608 }
609 } else {
610 for (int i=byteOffset; i<sampleCount; i++) {
611 buffer[i]=(byte) ((u2l[buffer[i] & 0xFF]>>8)+128);
612 }
613 }
614 }
615
616 /**
617 * Fills outBuffer with ulaw samples.
618 * reading starts from inBuffer[inByteOffset].
619 * writing starts at outBuffer[outByteOffset].
620 * There will be sampleCount <B>bytes</B> written to outBuffer.
621 */
622 public static void ulaw2pcm8(byte[] inBuffer, int inByteOffset,
623 byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) {
624 int ulawIndex=inByteOffset;
625 int pcmIndex=outByteOffset;
626 if (signed) {
627 while (sampleCount>0) {
628 outBuffer[pcmIndex++]=
629 (byte) ((u2l[inBuffer[ulawIndex++] & 0xFF] >> 8) & 0xFF);
630 sampleCount--;
631 }
632 } else {
633 while (sampleCount>0) {
634 outBuffer[pcmIndex++]=
635 (byte) ((u2l[inBuffer[ulawIndex++] & 0xFF]>>8)+128);
636 sampleCount--;
637 }
638 }
639 }
640
641
642 //////////////////// ALAW ////////////////////////////
643
644
645 /*
646 * This source code is a product of Sun Microsystems, Inc. and is provided
647 * for unrestricted use. Users may copy or modify this source code without
648 * charge.
649 *
650 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
651 *
652 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
653 *
654 * Linear Input Code Compressed Code
655 * ------------------------ ---------------
656 * 0000000wxyza 000wxyz
657 * 0000001wxyza 001wxyz
658 * 000001wxyzab 010wxyz
659 * 00001wxyzabc 011wxyz
660 * 0001wxyzabcd 100wxyz
661 * 001wxyzabcde 101wxyz
662 * 01wxyzabcdef 110wxyz
663 * 1wxyzabcdefg 111wxyz
664 *
665 * For further information see John C. Bellamy's Digital Telephony, 1982,
666 * John Wiley & Sons, pps 98-111 and 472-476.
667 */
668 private static final byte QUANT_MASK = 0xf; /* Quantization field mask. */
669 private static final byte SEG_SHIFT = 4; /* Left shift for segment number. */
670 private static final short[] seg_end = {
671 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
672 };
673
674 public static byte linear2alaw(short pcm_val) /* 2's complement (16-bit range) */
675 {
676 byte mask;
677 byte seg=8;
678 byte aval;
679
680 if (pcm_val >= 0) {
681 mask = (byte) 0xD5; /* sign (7th) bit = 1 */
682 } else {
683 mask = 0x55; /* sign bit = 0 */
684 pcm_val = (short) (-pcm_val - 8);
685 }
686
687 /* Convert the scaled magnitude to segment number. */
688 for (int i = 0; i < 8; i++) {
689 if (pcm_val <= seg_end[i]) {
690 seg=(byte) i;
691 break;
692 }
693 }
694
695 /* Combine the sign, segment, and quantization bits. */
696 if (seg >= 8) /* out of range, return maximum value. */
697 return (byte) ((0x7F ^ mask) & 0xFF);
698 else {
699 aval = (byte) (seg << SEG_SHIFT);
700 if (seg < 2)
701 aval |= (pcm_val >> 4) & QUANT_MASK;
702 else
703 aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
704 return (byte) ((aval ^ mask) & 0xFF);
705 }
706 }
707
708 private static short[] a2l = {
709 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
710 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
711 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
712 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
713 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
714 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
715 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
716 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
717 -344, -328, -376, -360, -280, -264, -312, -296,
718 -472, -456, -504, -488, -408, -392, -440, -424,
719 -88, -72, -120, -104, -24, -8, -56, -40,
720 -216, -200, -248, -232, -152, -136, -184, -168,
721 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
722 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
723 -688, -656, -752, -720, -560, -528, -624, -592,
724 -944, -912, -1008, -976, -816, -784, -880, -848,
725 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
726 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
727 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
728 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
729 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
730 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
731 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
732 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
733 344, 328, 376, 360, 280, 264, 312, 296,
734 472, 456, 504, 488, 408, 392, 440, 424,
735 88, 72, 120, 104, 24, 8, 56, 40,
736 216, 200, 248, 232, 152, 136, 184, 168,
737 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
738 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
739 688, 656, 752, 720, 560, 528, 624, 592,
740 944, 912, 1008, 976, 816, 784, 880, 848
741 };
742
743 public static short alaw2linear(byte ulawbyte) {
744 return a2l[ulawbyte & 0xFF];
745 }
746
747 /**
748 * Converts a buffer of signed 16bit big endian samples to uLaw.
749 * The uLaw bytes overwrite the original 16 bit values.
750 * The first byte-offset of the uLaw bytes is byteOffset.
751 * It will be written sampleCount/2 bytes.
752 */
753 public static void pcm162alaw(byte[] buffer, int byteOffset, int sampleCount, boolean bigEndian) {
754 int shortIndex=byteOffset;
755 int alawIndex=shortIndex;
756 if (bigEndian) {
757 while (sampleCount>0) {
758 buffer[alawIndex++]=
759 linear2alaw(bytesToShort16
760 (buffer[shortIndex], buffer[shortIndex+1]));
761 shortIndex++;
762 shortIndex++;
763 sampleCount--;
764 }
765 } else {
766 while (sampleCount>0) {
767 buffer[alawIndex++]=
768 linear2alaw(bytesToShort16
769 (buffer[shortIndex+1], buffer[shortIndex]));
770 shortIndex++;
771 shortIndex++;
772 sampleCount--;
773 }
774 }
775 }
776
777 /**
778 * Fills outBuffer with alaw samples.
779 * reading starts from inBuffer[inByteOffset].
780 * writing starts at outBuffer[outByteOffset].
781 * There will be sampleCount*2 bytes read from inBuffer;
782 * There will be sampleCount <B>bytes</B> written to outBuffer.
783 */
784 public static void pcm162alaw(byte[] inBuffer, int inByteOffset,
785 byte[] outBuffer, int outByteOffset, int sampleCount, boolean bigEndian) {
786 int shortIndex=inByteOffset;
787 int alawIndex=outByteOffset;
788 if (bigEndian) {
789 while (sampleCount>0) {
790 outBuffer[alawIndex++]=linear2alaw
791 (bytesToShort16(inBuffer[shortIndex], inBuffer[shortIndex+1]));
792 shortIndex++;
793 shortIndex++;
794 sampleCount--;
795 }
796 } else {
797 while (sampleCount>0) {
798 outBuffer[alawIndex++]=linear2alaw
799 (bytesToShort16(inBuffer[shortIndex+1], inBuffer[shortIndex]));
800 shortIndex++;
801 shortIndex++;
802 sampleCount--;
803 }
804 }
805 }
806
807 /**
808 * Converts a buffer of 8bit samples to alaw.
809 * The alaw bytes overwrite the original 8 bit values.
810 * The first byte-offset of the aLaw bytes is byteOffset.
811 * It will be written sampleCount bytes.
812 */
813 public static void pcm82alaw(byte[] buffer, int byteOffset, int sampleCount, boolean signed) {
814 sampleCount+=byteOffset;
815 if (signed) {
816 for (int i=byteOffset; i<sampleCount; i++) {
817 buffer[i]=linear2alaw((short) (buffer[i] << 8));
818 }
819 } else {
820 for (int i=byteOffset; i<sampleCount; i++) {
821 buffer[i]=linear2alaw((short) (((byte) (buffer[i]+128)) << 8));
822 }
823 }
824 }
825
826 /**
827 * Fills outBuffer with alaw samples.
828 * reading starts from inBuffer[inByteOffset].
829 * writing starts at outBuffer[outByteOffset].
830 * There will be sampleCount <B>bytes</B> written to outBuffer.
831 */
832 public static void pcm82alaw(byte[] inBuffer, int inByteOffset,
833 byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) {
834 int alawIndex=outByteOffset;
835 int pcmIndex=inByteOffset;
836 if (signed) {
837 while (sampleCount>0) {
838 outBuffer[alawIndex++]=
839 linear2alaw((short) (inBuffer[pcmIndex++] << 8));
840 sampleCount--;
841 }
842 } else {
843 while (sampleCount>0) {
844 outBuffer[alawIndex++]=
845 linear2alaw((short) (((byte) (inBuffer[pcmIndex++]+128)) << 8));
846 sampleCount--;
847 }
848 }
849 }
850
851
852
853 /**
854 * Converts an alaw buffer to 8bit pcm samples
855 * The 8bit bytes overwrite the original alaw values.
856 * The first byte-offset of the aLaw bytes is byteOffset.
857 * It will be written sampleCount bytes.
858 */
859 public static void alaw2pcm8(byte[] buffer, int byteOffset, int sampleCount, boolean signed) {
860 sampleCount+=byteOffset;
861 if (signed) {
862 for (int i=byteOffset; i<sampleCount; i++) {
863 buffer[i]=(byte) ((a2l[buffer[i] & 0xFF] >> 8) & 0xFF);
864 }
865 } else {
866 for (int i=byteOffset; i<sampleCount; i++) {
867 buffer[i]=(byte) ((a2l[buffer[i] & 0xFF]>>8)+128);
868 }
869 }
870 }
871
872 /**
873 * Fills outBuffer with alaw samples.
874 * reading starts from inBuffer[inByteOffset].
875 * writing starts at outBuffer[outByteOffset].
876 * There will be sampleCount <B>bytes</B> written to outBuffer.
877 */
878 public static void alaw2pcm8(byte[] inBuffer, int inByteOffset,
879 byte[] outBuffer, int outByteOffset, int sampleCount, boolean signed) {
880 int alawIndex=inByteOffset;
881 int pcmIndex=outByteOffset;
882 if (signed) {
883 while (sampleCount>0) {
884 outBuffer[pcmIndex++]=
885 (byte) ((a2l[inBuffer[alawIndex++] & 0xFF] >> 8) & 0xFF);
886 sampleCount--;
887 }
888 } else {
889 while (sampleCount>0) {
890 outBuffer[pcmIndex++]=
891 (byte) ((a2l[inBuffer[alawIndex++] & 0xFF]>>8)+128);
892 sampleCount--;
893 }
894 }
895 }
896
897 /**
898 * Fills outBuffer with pcm signed 16 bit samples.
899 * reading starts from inBuffer[inByteOffset].
900 * writing starts at outBuffer[outByteOffset].
901 * There will be sampleCount bytes read from inBuffer;
902 * There will be sampleCount*2 bytes written to outBuffer.
903 */
904 public static void alaw2pcm16(byte[] inBuffer, int inByteOffset,
905 byte[] outBuffer, int outByteOffset,
906 int sampleCount, boolean bigEndian) {
907 int shortIndex=outByteOffset;
908 int alawIndex=inByteOffset;
909 while (sampleCount>0) {
910 intToBytes16
911 (a2l[inBuffer[alawIndex++] & 0xFF], outBuffer, shortIndex++, bigEndian);
912 shortIndex++;
913 sampleCount--;
914 }
915 }
916
917 //////////////////////// cross conversion alaw <-> ulaw ////////////////////////////////////////
918
919 private static byte[] u2a = {
920 -86, -85, -88, -87, -82, -81, -84, -83, -94, -93, -96, -95, -90, -89, -92, -91,
921 -70, -69, -72, -71, -66, -65, -68, -67, -78, -77, -80, -79, -74, -73, -76, -75,
922 -118, -117, -120, -119, -114, -113, -116, -115, -126, -125, -128, -127, -122, -121, -124, -123,
923 -101, -104, -103, -98, -97, -100, -99, -110, -109, -112, -111, -106, -105, -108, -107, -22,
924 -24, -23, -18, -17, -20, -19, -30, -29, -32, -31, -26, -25, -28, -27, -6, -8,
925 -2, -1, -4, -3, -14, -13, -16, -15, -10, -9, -12, -11, -53, -55, -49, -51,
926 -62, -61, -64, -63, -58, -57, -60, -59, -38, -37, -40, -39, -34, -33, -36, -35,
927 -46, -46, -45, -45, -48, -48, -47, -47, -42, -42, -41, -41, -44, -44, -43, -43,
928 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37,
929 58, 59, 56, 57, 62, 63, 60, 61, 50, 51, 48, 49, 54, 55, 52, 53,
930 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5,
931 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21, 106,
932 104, 105, 110, 111, 108, 109, 98, 99, 96, 97, 102, 103, 100, 101, 122, 120,
933 126, 127, 124, 125, 114, 115, 112, 113, 118, 119, 116, 117, 75, 73, 79, 77,
934 66, 67, 64, 65, 70, 71, 68, 69, 90, 91, 88, 89, 94, 95, 92, 93,
935 82, 82, 83, 83, 80, 80, 81, 81, 86, 86, 87, 87, 84, 84, 85, 85,
936 };
937
938 public static byte ulaw2alaw(byte sample) {
939 return u2a[sample & 0xFF];
940 }
941
942 /**
943 * Converts a buffer of uLaw samples to aLaw.
944 */
945 public static void ulaw2alaw(byte[] buffer, int byteOffset, int sampleCount) {
946 sampleCount+=byteOffset;
947 for (int i=byteOffset; i<sampleCount; i++) {
948 buffer[i]=u2a[buffer[i] & 0xFF];
949 }
950 }
951
952 /**
953 * Fills outBuffer with alaw samples.
954 */
955 public static void ulaw2alaw(byte[] inBuffer, int inByteOffset,
956 byte[] outBuffer, int outByteOffset, int sampleCount) {
957 int ulawIndex=outByteOffset;
958 int alawIndex=inByteOffset;
959 while (sampleCount>0) {
960 outBuffer[alawIndex++]=u2a[inBuffer[ulawIndex++] & 0xFF];
961 sampleCount--;
962 }
963 }
964
965 private static byte[] a2u = {
966 -86, -85, -88, -87, -82, -81, -84, -83, -94, -93, -96, -95, -90, -89, -92, -91,
967 -71, -70, -73, -72, -67, -66, -69, -68, -79, -78, -80, -80, -75, -74, -77, -76,
968 -118, -117, -120, -119, -114, -113, -116, -115, -126, -125, -128, -127, -122, -121, -124, -123,
969 -102, -101, -104, -103, -98, -97, -100, -99, -110, -109, -112, -111, -106, -105, -108, -107,
970 -30, -29, -32, -31, -26, -25, -28, -27, -35, -35, -36, -36, -33, -33, -34, -34,
971 -12, -10, -16, -14, -4, -2, -8, -6, -22, -21, -24, -23, -18, -17, -20, -19,
972 -56, -55, -58, -57, -52, -51, -54, -53, -64, -63, -65, -65, -60, -59, -62, -61,
973 -42, -41, -44, -43, -38, -37, -40, -39, -49, -49, -50, -50, -46, -45, -48, -47,
974 42, 43, 40, 41, 46, 47, 44, 45, 34, 35, 32, 33, 38, 39, 36, 37,
975 57, 58, 55, 56, 61, 62, 59, 60, 49, 50, 48, 48, 53, 54, 51, 52,
976 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5,
977 26, 27, 24, 25, 30, 31, 28, 29, 18, 19, 16, 17, 22, 23, 20, 21,
978 98, 99, 96, 97, 102, 103, 100, 101, 93, 93, 92, 92, 95, 95, 94, 94,
979 116, 118, 112, 114, 124, 126, 120, 122, 106, 107, 104, 105, 110, 111, 108, 109,
980 72, 73, 70, 71, 76, 77, 74, 75, 64, 65, 63, 63, 68, 69, 66, 67,
981 86, 87, 84, 85, 90, 91, 88, 89, 79, 79, 78, 78, 82, 83, 80, 81,
982 };
983
984 public static byte alaw2ulaw(byte sample) {
985 return a2u[sample & 0xFF];
986 }
987
988 /**
989 * Converts a buffer of aLaw samples to uLaw.
990 * The uLaw bytes overwrite the original aLaw values.
991 * The first byte-offset of the uLaw bytes is byteOffset.
992 * It will be written sampleCount bytes.
993 */
994 public static void alaw2ulaw(byte[] buffer, int byteOffset, int sampleCount) {
995 sampleCount+=byteOffset;
996 for (int i=byteOffset; i<sampleCount; i++) {
997 buffer[i]=a2u[buffer[i] & 0xFF];
998 }
999 }
1000
1001 /**
1002 * Fills outBuffer with ulaw samples.
1003 * reading starts from inBuffer[inByteOffset].
1004 * writing starts at outBuffer[outByteOffset].
1005 * There will be sampleCount <B>bytes</B> written to outBuffer.
1006 */
1007 public static void alaw2ulaw(byte[] inBuffer, int inByteOffset,
1008 byte[] outBuffer, int outByteOffset, int sampleCount) {
1009 int ulawIndex=outByteOffset;
1010 int alawIndex=inByteOffset;
1011 while (sampleCount>0) {
1012 outBuffer[ulawIndex++]=a2u[inBuffer[alawIndex++] & 0xFF];
1013 sampleCount--;
1014 }
1015 }
1016
1017
1018 //////////////////////// high level methods /////////////////////////////////////////////////
1019
1020 /*
1021 * !! Here, unlike other functions in this class, the length is
1022 * in bytes rather than samples !!
1023 */
1024 public static void changeOrderOrSign(byte[] buffer, int nOffset,
1025 int nByteLength, int nBytesPerSample) {
1026 switch (nBytesPerSample) {
1027 case 1:
1028 convertSign8(buffer, nOffset, nByteLength);
1029 break;
1030
1031 case 2:
1032 swapOrder16(buffer, nOffset, nByteLength / 2);
1033 break;
1034
1035 case 3:
1036 swapOrder24(buffer, nOffset, nByteLength / 3);
1037 break;
1038
1039 case 4:
1040 swapOrder32(buffer, nOffset, nByteLength / 4);
1041 break;
1042 }
1043 }
1044
1045
1046
1047 /*
1048 * !! Here, unlike other functions in this class, the length is
1049 * in bytes rather than samples !!
1050 */
1051 public static void changeOrderOrSign(
1052 byte[] inBuffer, int nInOffset,
1053 byte[] outBuffer, int nOutOffset,
1054 int nByteLength, int nBytesPerSample) {
1055 switch (nBytesPerSample) {
1056 case 1:
1057 convertSign8(
1058 inBuffer, nInOffset,
1059 outBuffer, nOutOffset,
1060 nByteLength);
1061 break;
1062
1063 case 2:
1064 swapOrder16(
1065 inBuffer, nInOffset,
1066 outBuffer, nOutOffset,
1067 nByteLength / 2);
1068 break;
1069
1070 case 3:
1071 swapOrder24(
1072 inBuffer, nInOffset,
1073 outBuffer, nOutOffset,
1074 nByteLength / 3);
1075 break;
1076
1077 case 4:
1078 swapOrder32(
1079 inBuffer, nInOffset,
1080 outBuffer, nOutOffset,
1081 nByteLength / 4);
1082 break;
1083 }
1084 }
1085
1086
1087 ///////////////// Annexe: how the arrays were created. //////////////////////////////////
1088
1089 /*
1090 * Converts a uLaw byte to a linear signed 16bit sample.
1091 * Ported to Java by fb.
1092 * <BR>Originally by:<BR>
1093 *
1094 * Craig Reese: IDA/Supercomputing Research Center <BR>
1095 * 29 September 1989 <BR>
1096 *
1097 * References: <BR>
1098 * <OL>
1099 * <LI>CCITT Recommendation G.711 (very difficult to follow)</LI>
1100 * <LI>MIL-STD-188-113,"Interoperability and Performance Standards
1101 * for Analog-to_Digital Conversion Techniques,"
1102 * 17 February 1987</LI>
1103 * </OL>
1104 */
1105 /*
1106 private static final int exp_lut2[] = {
1107 0,132,396,924,1980,4092,8316,16764
1108};
1109
1110 public static short _ulaw2linear(int ulawbyte) {
1111 int sign, exponent, mantissa, sample;
1112
1113 ulawbyte = ~ulawbyte;
1114 sign = (ulawbyte & 0x80);
1115 exponent = (ulawbyte >> 4) & 0x07;
1116 mantissa = ulawbyte & 0x0F;
1117 sample = exp_lut2[exponent] + (mantissa << (exponent + 3));
1118 if (sign != 0) sample = -sample;
1119 return((short) sample);
1120}*/
1121
1122
1123 /* u- to A-law conversions: copied from CCITT G.711 specifications */
1124 /*
1125 private static byte[] _u2a = {
1126 1, 1, 2, 2, 3, 3, 4, 4,
1127 5, 5, 6, 6, 7, 7, 8, 8,
1128 9, 10, 11, 12, 13, 14, 15, 16,
1129 17, 18, 19, 20, 21, 22, 23, 24,
1130 25, 27, 29, 31, 33, 34, 35, 36,
1131 37, 38, 39, 40, 41, 42, 43, 44,
1132 46, 48, 49, 50, 51, 52, 53, 54,
1133 55, 56, 57, 58, 59, 60, 61, 62,
1134 64, 65, 66, 67, 68, 69, 70, 71,
1135 72, 73, 74, 75, 76, 77, 78, 79,
1136 81, 82, 83, 84, 85, 86, 87, 88,
1137 89, 90, 91, 92, 93, 94, 95, 96,
1138 97, 98, 99, 100, 101, 102, 103, 104,
1139 105, 106, 107, 108, 109, 110, 111, 112,
1140 113, 114, 115, 116, 117, 118, 119, 120,
1141 121, 122, 123, 124, 125, 126, 127, (byte) 128};
1142 */
1143
1144 /* u-law to A-law conversion */
1145 /*
1146 * This source code is a product of Sun Microsystems, Inc. and is provided
1147 * for unrestricted use. Users may copy or modify this source code without
1148 * charge.
1149 */
1150 /*
1151 public static byte _ulaw2alaw(byte sample) {
1152 sample &= 0xff;
1153 return (byte) (((sample & 0x80)!=0) ? (0xD5 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)) :
1154 (0x55 ^ (_u2a[(0x7F ^ sample) & 0x7F] - 1)));
1155}*/
1156
1157 /* A- to u-law conversions */
1158 /*
1159 private static byte[] _a2u = {
1160 1, 3, 5, 7, 9, 11, 13, 15,
1161 16, 17, 18, 19, 20, 21, 22, 23,
1162 24, 25, 26, 27, 28, 29, 30, 31,
1163 32, 32, 33, 33, 34, 34, 35, 35,
1164 36, 37, 38, 39, 40, 41, 42, 43,
1165 44, 45, 46, 47, 48, 48, 49, 49,
1166 50, 51, 52, 53, 54, 55, 56, 57,
1167 58, 59, 60, 61, 62, 63, 64, 64,
1168 65, 66, 67, 68, 69, 70, 71, 72,
1169 73, 74, 75, 76, 77, 78, 79, 79,
1170 80, 81, 82, 83, 84, 85, 86, 87,
1171 88, 89, 90, 91, 92, 93, 94, 95,
1172 96, 97, 98, 99, 100, 101, 102, 103,
1173 104, 105, 106, 107, 108, 109, 110, 111,
1174 112, 113, 114, 115, 116, 117, 118, 119,
1175 120, 121, 122, 123, 124, 125, 126, 127};
1176 */
1177
1178 /*
1179 * This source code is a product of Sun Microsystems, Inc. and is provided
1180 * for unrestricted use. Users may copy or modify this source code without
1181 * charge.
1182 */
1183 /*
1184 public static byte _alaw2ulaw(byte sample) {
1185 sample &= 0xff;
1186 return (byte) (((sample & 0x80)!=0) ? (0xFF ^ _a2u[(sample ^ 0xD5) & 0x7F]) :
1187 (0x7F ^ _a2u[(sample ^ 0x55) & 0x7F]));
1188}
1189
1190 public static void print_a2u() {
1191 System.out.println("\tprivate static byte[] a2u = {");
1192 for (int i=-128; i<128; i++) {
1193 if (((i+128) % 16)==0) {
1194 System.out.print("\t\t");
1195 }
1196 byte b=(byte) i;
1197 System.out.print(_alaw2ulaw(b)+", ");
1198 if (((i+128) % 16)==15) {
1199 System.out.println("");
1200 }
1201}
1202 System.out.println("\t};");
1203}
1204
1205 public static void print_u2a() {
1206 System.out.println("\tprivate static byte[] u2a = {");
1207 for (int i=-128; i<128; i++) {
1208 if (((i+128) % 16)==0) {
1209 System.out.print("\t\t");
1210 }
1211 byte b=(byte) i;
1212 System.out.print(_ulaw2alaw(b)+", ");
1213 if (((i+128) % 16)==15) {
1214 System.out.println("");
1215 }
1216}
1217 System.out.println("\t};");
1218}
1219 */
1220
1221}
1222
1223
1224/*** TConversionTool.java ***/