2 * Portions Copyright 2001 Sun Microsystems, Inc.
3 * Portions Copyright 1999-2001 Language Technologies Institute,
4 * Carnegie Mellon University.
5 * All Rights Reserved. Use is subject to license terms.
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
11 package com.sun.speech.freetts.util;
14 * Provides a set of utilities for prrocessing wave/audio data.
16 public class WaveUtils {
18 private static int[] exp_lut2 = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
19 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
20 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
21 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
22 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
23 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
24 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
25 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
26 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
27 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
28 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
29 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
30 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
31 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
32 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
33 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
35 private static final boolean ZEROTRAP = true;
36 private static final int CLIP = 32625;
37 private static final int BIAS = 0x84;
39 private final static int[] expLut = { 0, 132, 396, 924, 1980, 4092,
43 * Converts a raw short to ulaw.
45 * @param sampleData signed 16-bit linear sample
47 * @return 8-bit ulaw sample, normalized
49 public static final byte shortToUlaw(short sampleData) {
50 int sign, exponent, mantissa;
52 int sample = (int) sampleData;
54 sign = (sample >> 8) & 0x80; // set aside the sign
55 if (sign != 0) { // get the magnitude
58 if (sample > CLIP) { // clip the magnitude
61 sample = sample + BIAS;
62 exponent = exp_lut2[ (sample >> 7) & 0xFF ];
63 mantissa = (sample >> (exponent + 3)) & 0x0F;
64 ulawByte = ~(sign | (exponent << 4) | mantissa);
67 if (ulawByte == 0) { // optional CCITT trap
72 return (byte) ((ulawByte - 128) & 0xFF);
77 * Converts from ulaw to 16 bit linear.
79 * Craig Reese: IDA/Supercomputing Research Center
85 * 1) CCITT Recommendation G.711 (very difficult to follow)
87 * 2) MIL-STD-188-113,"Interoperability and Performance Standards
88 * for Analog-to_Digital Conversion Techniques," 17 February 1987
90 * @param uByte 8 bit ulaw sample
92 * @return signed 16 bit linear sample
94 public static final short ulawToShort(short uByte) {
95 int sign, exponent, mantissa;
99 ulawByte = ~(ulawByte);
100 sign = (ulawByte & 0x80);
101 exponent = (ulawByte >> 4) & 0x07;
102 mantissa = ulawByte & 0x0F;
103 sample = expLut[exponent] + ( mantissa << ( exponent + 3 ) );
108 return (short) sample;
112 * Reconstructs a short from its hi and low bytes.
114 * @param hiByte the high byte
115 * @param loByte the low byte
117 * @return the short value
119 public static final short bytesToShort(byte hiByte, byte loByte) {
120 int result = (0x000000FF & hiByte);
121 result = result << 8;
122 result |= (0x000000FF & loByte);
123 return (short) result;
127 * Provides test program for method ulawToShort().
129 * @param args not used
131 public static void main(String[] args) {
132 for (int i = 0; i < 256; i++) {
133 System.out.println("" + i + "=" + ulawToShort((short) i));