upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / util / WaveUtils.java
1 /**
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.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.util;
12
13 /**
14  * Provides a set of utilities for prrocessing wave/audio data.
15  */
16 public class WaveUtils {
17
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};
34
35     private static final boolean ZEROTRAP = true;
36     private static final int CLIP = 32625;
37     private static final int BIAS = 0x84;
38         
39     private final static int[] expLut = { 0, 132, 396, 924, 1980, 4092, 
40                                           8316, 16764 };
41     
42     /**
43      * Converts a raw short to ulaw.
44      *
45      * @param sampleData signed 16-bit linear sample
46      *
47      * @return 8-bit ulaw sample, normalized
48      */
49     public static final byte shortToUlaw(short sampleData) {
50         int sign, exponent, mantissa;
51         int ulawByte;
52         int sample = (int) sampleData;
53                 
54         sign = (sample >> 8) & 0x80; // set aside the sign
55         if (sign != 0) {             // get the magnitude
56             sample = -sample;
57         }
58         if (sample > CLIP) {         // clip the magnitude
59             sample = CLIP;
60         }
61         sample = sample + BIAS;
62         exponent = exp_lut2[ (sample >> 7) & 0xFF ];
63         mantissa = (sample >> (exponent + 3)) & 0x0F;
64         ulawByte = ~(sign | (exponent << 4) | mantissa);
65         
66         if (ZEROTRAP) {
67             if (ulawByte == 0) { // optional CCITT trap
68                 ulawByte = 0x02;
69             }
70         }
71         
72         return (byte) ((ulawByte - 128) & 0xFF);
73     }
74
75
76     /**
77      * Converts from ulaw to 16 bit linear.
78      * <p>
79      * Craig Reese: IDA/Supercomputing Research Center
80      * <br>
81      * 29 September 1989
82      * <p>
83      * References:
84      * <br>
85      * 1) CCITT Recommendation G.711  (very difficult to follow)
86      * <br>
87      * 2) MIL-STD-188-113,"Interoperability and Performance Standards
88      *    for Analog-to_Digital Conversion Techniques," 17 February 1987
89      *
90      * @param uByte 8 bit ulaw sample
91      *
92      * @return signed 16 bit linear sample
93      */
94     public static final short ulawToShort(short uByte) {
95         int sign, exponent, mantissa;
96         int sample;
97         int ulawByte = uByte;
98                 
99         ulawByte = ~(ulawByte);
100         sign = (ulawByte & 0x80);
101         exponent = (ulawByte >> 4) & 0x07;
102         mantissa = ulawByte & 0x0F;
103         sample = expLut[exponent] + ( mantissa << ( exponent + 3 ) );
104         if ( sign != 0 ) {
105             sample = -sample;
106         }
107         
108         return (short) sample;
109     }
110
111     /**
112      * Reconstructs a short from its hi and low bytes.
113      *
114      * @param hiByte the high byte
115      * @param loByte the low byte
116      * 
117      * @return the short value
118      */
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;
124     }
125
126     /**
127      * Provides test program for method ulawToShort().
128      *
129      * @param args not used
130      */
131     public static void main(String[] args) {
132         for (int i = 0; i < 256; i++) {
133             System.out.println("" + i + "=" + ulawToShort((short) i));
134         }
135     }
136
137 }