/Users/lyon/j4p/src/sound/Ulaw.java

1    package sound; 
2     
3    public abstract class Ulaw { 
4        private final static int uBIAS = 0x84; 
5        private final static int uCLIP = 32635; 
6        private final static int ACLIP = 31744; 
7        private final static int maxSample = 8031; 
8     
9        // 8 elements 
10       private final static int 
11               exp2[] = {0, 132, 396, 924, 1980, 
12                         4092, 8316, 16764}; 
13    
14    
15       // 256 elements 
16       private final static int 
17               exp_lut[] = {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 
18                            4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
19                            5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
20                            5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
21                            6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
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                            7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
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    
34    
35       // make an 8 bit byte u-law encoding of 
36       // x. X is restricted to 0<|x|<1 
37       public static byte toUlaw(double x) { 
38           // max allowed by 8-bit ulaw encoder is uCLIP 
39           x = x * uCLIP; 
40           // cast x as an int to change the signature of 
41           // the invoked method 
42           return ((byte) toUlaw((int) x)); 
43       } 
44    
45    
46       public static byte toUlaw(int sample) { 
47    
48           int sign; 
49           int exponent; 
50           int mantissa; 
51           byte ulawbyte; 
52    
53    
54           if (Math.abs(sample) >= 1) { 
55               System.out.println("X is not well conditioned in Ulaw.java"); 
56           } 
57    
58           /* Get the sample into sign-magnitude. */ 
59           sign = (sample >> 8) & 0x80;        /* set aside the sign */ 
60           if (sign != 0) sample = -sample;        /* get magnitude */ 
61           if (sample > uCLIP) sample = uCLIP;     /* clip the magnitude */ 
62    
63           /* Convert from 16 bit linear to ulaw. */ 
64           sample = sample + uBIAS; 
65           exponent = exp_lut[(sample >> 7) & 0xFF]; 
66           mantissa = (sample >> (exponent + 3)) & 0x0F; 
67           ulawbyte = (byte) ~(sign | (exponent << 4) | mantissa); 
68           return ulawbyte; 
69       } 
70    
71       public static double ulawToDouble(byte ulawbyte) { 
72           return (double) ulawToLinear(ulawbyte) / uCLIP; 
73       } 
74    
75       public static int ulawToLinear(byte ulawbyte) { 
76    
77           int sign, exponent, mantissa, sample; 
78           ulawbyte = (byte) ~ulawbyte; 
79           sign = (ulawbyte & 0x80); 
80           exponent = (ulawbyte >> 4) & 0x07; 
81           mantissa = ulawbyte & 0x0F; 
82           sample = exp2[exponent] + (mantissa << (exponent + 3)); 
83           if (sign != 0) sample = -sample; 
84    
85           return sample; 
86       } 
87    
88    
89   } 
90    
91