1 /*
2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 import java.util.Vector;
31
32 import javax.sound.sampled.AudioFormat;
33 import javax.sound.sampled.AudioFormat.Encoding;
34 import javax.sound.sampled.AudioInputStream;
35 import javax.sound.sampled.AudioSystem;
36 import javax.sound.sampled.spi.FormatConversionProvider;
37
38 /**
39 * U-law encodes linear data, and decodes u-law data to linear data.
40 *
41 * @author Kara Kytle
42 */
43 public final class UlawCodec extends FormatConversionProvider {
44
45 /* Tables used for U-law decoding */
46
47 private static final byte[] ULAW_TABH = new byte[256];
48 private static final byte[] ULAW_TABL = new byte[256];
49
50 private static final short seg_end[] = {
51 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
52 };
53
54 /**
55 * Initializes the decode tables.
56 */
57 static {
58 for (int i=0;i<256;i++) {
59 int ulaw = ~i;
60 int t;
61
62 ulaw &= 0xFF;
63 t = ((ulaw & 0xf)<<3) + 132;
64 t <<= ((ulaw & 0x70) >> 4);
65 t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132);
66
67 ULAW_TABL[i] = (byte) (t&0xff);
68 ULAW_TABH[i] = (byte) ((t>>8) & 0xff);
69 }
70 }
71
72 @Override
73 public AudioFormat.Encoding[] getSourceEncodings() {
74 return new Encoding[]{Encoding.ULAW, Encoding.PCM_SIGNED};
75 }
76
77 @Override
78 public AudioFormat.Encoding[] getTargetEncodings() {
79 return getSourceEncodings();
80 }
81
82 @Override
83 public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
84 if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) {
85 if( sourceFormat.getSampleSizeInBits() == 16 ) {
86 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
87 enc[0] = AudioFormat.Encoding.ULAW;
88 return enc;
89 } else {
90 return new AudioFormat.Encoding[0];
91 }
92 } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) {
93 if (sourceFormat.getSampleSizeInBits() == 8) {
94 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
95 enc[0] = AudioFormat.Encoding.PCM_SIGNED;
96 return enc;
97 } else {
98 return new AudioFormat.Encoding[0];
99 }
100 } else {
101 return new AudioFormat.Encoding[0];
102 }
103 }
104
105 @Override
106 public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
107 Objects.requireNonNull(targetEncoding);
108 Objects.requireNonNull(sourceFormat);
109 if( (AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)
110 && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding()))
111 ||
112 (AudioFormat.Encoding.ULAW.equals(targetEncoding)
113 && AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()))) {
114 return getOutputFormats(sourceFormat);
217 formats.addElement(format);
218
219 format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
220 inputFormat.getSampleRate(), 16,
221 inputFormat.getChannels(),
222 inputFormat.getChannels() * 2,
223 inputFormat.getSampleRate(), true);
224 formats.addElement(format);
225 }
226
227 AudioFormat[] formatArray = new AudioFormat[formats.size()];
228 for (int i = 0; i < formatArray.length; i++) {
229 formatArray[i] = formats.elementAt(i);
230 }
231 return formatArray;
232 }
233
234 private final class UlawCodecStream extends AudioInputStream {
235
236 private static final int tempBufferSize = 64;
237 private byte tempBuffer [] = null;
238
239 /**
240 * True to encode to u-law, false to decode to linear.
241 */
242 boolean encode = false;
243
244 AudioFormat encodeFormat;
245 AudioFormat decodeFormat;
246
247 byte tabByte1[] = null;
248 byte tabByte2[] = null;
249 int highByte = 0;
250 int lowByte = 1;
251
252 UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
253 super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);
254
255 AudioFormat inputFormat = stream.getFormat();
256
257 // throw an IllegalArgumentException if not ok
258 if (!(isConversionSupported(outputFormat, inputFormat))) {
259 throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
260 }
261
262 //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
263 boolean PCMIsBigEndian;
264
265 // determine whether we are encoding or decoding
266 if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
267 encode = false;
268 encodeFormat = inputFormat;
288 highByte = 1;
289 lowByte = 0;
290 }
291
292 // set the AudioInputStream length in frames if we know it
293 if (stream instanceof AudioInputStream) {
294 frameLength = stream.getFrameLength();
295 }
296 // set framePos to zero
297 framePos = 0;
298 frameSize = inputFormat.getFrameSize();
299 if (frameSize == AudioSystem.NOT_SPECIFIED) {
300 frameSize = 1;
301 }
302 }
303
304 /*
305 * $$jb 2/23/99
306 * Used to determine segment number in uLaw encoding
307 */
308 private short search(short val, short table[], short size) {
309 for(short i = 0; i < size; i++) {
310 if (val <= table[i]) { return i; }
311 }
312 return size;
313 }
314
315 /**
316 * Note that this won't actually read anything; must read in
317 * two-byte units.
318 */
319 @Override
320 public int read() throws IOException {
321 byte[] b = new byte[1];
322 if (read(b, 0, b.length) == 1) {
323 return b[1] & 0xFF;
324 }
325 return -1;
326 }
327
328 @Override
|
1 /*
2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
30 import java.util.Vector;
31
32 import javax.sound.sampled.AudioFormat;
33 import javax.sound.sampled.AudioFormat.Encoding;
34 import javax.sound.sampled.AudioInputStream;
35 import javax.sound.sampled.AudioSystem;
36 import javax.sound.sampled.spi.FormatConversionProvider;
37
38 /**
39 * U-law encodes linear data, and decodes u-law data to linear data.
40 *
41 * @author Kara Kytle
42 */
43 public final class UlawCodec extends FormatConversionProvider {
44
45 /* Tables used for U-law decoding */
46
47 private static final byte[] ULAW_TABH = new byte[256];
48 private static final byte[] ULAW_TABL = new byte[256];
49
50 private static final short[] seg_end = {
51 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
52 };
53
54 /**
55 * Initializes the decode tables.
56 */
57 static {
58 for (int i=0;i<256;i++) {
59 int ulaw = ~i;
60 int t;
61
62 ulaw &= 0xFF;
63 t = ((ulaw & 0xf)<<3) + 132;
64 t <<= ((ulaw & 0x70) >> 4);
65 t = ( (ulaw&0x80) != 0 ) ? (132-t) : (t-132);
66
67 ULAW_TABL[i] = (byte) (t&0xff);
68 ULAW_TABH[i] = (byte) ((t>>8) & 0xff);
69 }
70 }
71
72 @Override
73 public AudioFormat.Encoding[] getSourceEncodings() {
74 return new Encoding[]{Encoding.ULAW, Encoding.PCM_SIGNED};
75 }
76
77 @Override
78 public AudioFormat.Encoding[] getTargetEncodings() {
79 return getSourceEncodings();
80 }
81
82 @Override
83 public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
84 if( AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()) ) {
85 if( sourceFormat.getSampleSizeInBits() == 16 ) {
86 AudioFormat.Encoding[] enc = new AudioFormat.Encoding[1];
87 enc[0] = AudioFormat.Encoding.ULAW;
88 return enc;
89 } else {
90 return new AudioFormat.Encoding[0];
91 }
92 } else if (AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding())) {
93 if (sourceFormat.getSampleSizeInBits() == 8) {
94 AudioFormat.Encoding[] enc = new AudioFormat.Encoding[1];
95 enc[0] = AudioFormat.Encoding.PCM_SIGNED;
96 return enc;
97 } else {
98 return new AudioFormat.Encoding[0];
99 }
100 } else {
101 return new AudioFormat.Encoding[0];
102 }
103 }
104
105 @Override
106 public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
107 Objects.requireNonNull(targetEncoding);
108 Objects.requireNonNull(sourceFormat);
109 if( (AudioFormat.Encoding.PCM_SIGNED.equals(targetEncoding)
110 && AudioFormat.Encoding.ULAW.equals(sourceFormat.getEncoding()))
111 ||
112 (AudioFormat.Encoding.ULAW.equals(targetEncoding)
113 && AudioFormat.Encoding.PCM_SIGNED.equals(sourceFormat.getEncoding()))) {
114 return getOutputFormats(sourceFormat);
217 formats.addElement(format);
218
219 format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
220 inputFormat.getSampleRate(), 16,
221 inputFormat.getChannels(),
222 inputFormat.getChannels() * 2,
223 inputFormat.getSampleRate(), true);
224 formats.addElement(format);
225 }
226
227 AudioFormat[] formatArray = new AudioFormat[formats.size()];
228 for (int i = 0; i < formatArray.length; i++) {
229 formatArray[i] = formats.elementAt(i);
230 }
231 return formatArray;
232 }
233
234 private final class UlawCodecStream extends AudioInputStream {
235
236 private static final int tempBufferSize = 64;
237 private byte[] tempBuffer = null;
238
239 /**
240 * True to encode to u-law, false to decode to linear.
241 */
242 boolean encode = false;
243
244 AudioFormat encodeFormat;
245 AudioFormat decodeFormat;
246
247 byte[] tabByte1 = null;
248 byte[] tabByte2 = null;
249 int highByte = 0;
250 int lowByte = 1;
251
252 UlawCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
253 super(stream, outputFormat, AudioSystem.NOT_SPECIFIED);
254
255 AudioFormat inputFormat = stream.getFormat();
256
257 // throw an IllegalArgumentException if not ok
258 if (!(isConversionSupported(outputFormat, inputFormat))) {
259 throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
260 }
261
262 //$$fb 2002-07-18: fix for 4714846: JavaSound ULAW (8-bit) encoder erroneously depends on endian-ness
263 boolean PCMIsBigEndian;
264
265 // determine whether we are encoding or decoding
266 if (AudioFormat.Encoding.ULAW.equals(inputFormat.getEncoding())) {
267 encode = false;
268 encodeFormat = inputFormat;
288 highByte = 1;
289 lowByte = 0;
290 }
291
292 // set the AudioInputStream length in frames if we know it
293 if (stream instanceof AudioInputStream) {
294 frameLength = stream.getFrameLength();
295 }
296 // set framePos to zero
297 framePos = 0;
298 frameSize = inputFormat.getFrameSize();
299 if (frameSize == AudioSystem.NOT_SPECIFIED) {
300 frameSize = 1;
301 }
302 }
303
304 /*
305 * $$jb 2/23/99
306 * Used to determine segment number in uLaw encoding
307 */
308 private short search(short val, short[] table, short size) {
309 for(short i = 0; i < size; i++) {
310 if (val <= table[i]) { return i; }
311 }
312 return size;
313 }
314
315 /**
316 * Note that this won't actually read anything; must read in
317 * two-byte units.
318 */
319 @Override
320 public int read() throws IOException {
321 byte[] b = new byte[1];
322 if (read(b, 0, b.length) == 1) {
323 return b[1] & 0xFF;
324 }
325 return -1;
326 }
327
328 @Override
|