1 /*
2 * Copyright (c) 1999, 2014, 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
23 * questions.
24 */
25
26 package com.sun.media.sound;
27
28 import java.io.IOException;
29 import java.util.Vector;
30
31 import javax.sound.sampled.AudioFormat;
32 import javax.sound.sampled.AudioInputStream;
33 import javax.sound.sampled.AudioSystem;
34
35
36 /**
37 * A-law encodes linear data, and decodes a-law data to linear data.
38 *
39 * @author Kara Kytle
40 */
41 public final class AlawCodec extends SunCodec {
42
43 /* Tables used for A-law decoding */
44
45 private static final byte[] ALAW_TABH = new byte[256];
46 private static final byte[] ALAW_TABL = new byte[256];
47
48 private static final AudioFormat.Encoding[] alawEncodings = { AudioFormat.Encoding.ALAW, AudioFormat.Encoding.PCM_SIGNED };
102 } else if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW ) ) {
103
104 if( sourceFormat.getSampleSizeInBits() == 8 ) {
105
106 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
107 enc[0] = AudioFormat.Encoding.PCM_SIGNED;
108 return enc;
109
110 } else {
111 return new AudioFormat.Encoding[0];
112 }
113
114 } else {
115 return new AudioFormat.Encoding[0];
116 }
117 }
118
119 /**
120 */
121 public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
122 if( (targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW)) ||
123 (targetEncoding.equals( AudioFormat.Encoding.ALAW) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED)) ) {
124 return getOutputFormats( sourceFormat );
125 } else {
126 return new AudioFormat[0];
127 }
128 }
129
130 /**
131 */
132 public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
133 AudioFormat sourceFormat = sourceStream.getFormat();
134 AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
135
136 if( sourceEncoding.equals( targetEncoding ) ) {
137 return sourceStream;
138 } else {
139 AudioFormat targetFormat = null;
140 if( !isConversionSupported(targetEncoding,sourceStream.getFormat()) ) {
141 throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
|
1 /*
2 * Copyright (c) 1999, 2015, 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
23 * questions.
24 */
25
26 package com.sun.media.sound;
27
28 import java.io.IOException;
29 import java.util.Objects;
30 import java.util.Vector;
31
32 import javax.sound.sampled.AudioFormat;
33 import javax.sound.sampled.AudioInputStream;
34 import javax.sound.sampled.AudioSystem;
35
36
37 /**
38 * A-law encodes linear data, and decodes a-law data to linear data.
39 *
40 * @author Kara Kytle
41 */
42 public final class AlawCodec extends SunCodec {
43
44 /* Tables used for A-law decoding */
45
46 private static final byte[] ALAW_TABH = new byte[256];
47 private static final byte[] ALAW_TABL = new byte[256];
48
49 private static final AudioFormat.Encoding[] alawEncodings = { AudioFormat.Encoding.ALAW, AudioFormat.Encoding.PCM_SIGNED };
103 } else if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW ) ) {
104
105 if( sourceFormat.getSampleSizeInBits() == 8 ) {
106
107 AudioFormat.Encoding enc[] = new AudioFormat.Encoding[1];
108 enc[0] = AudioFormat.Encoding.PCM_SIGNED;
109 return enc;
110
111 } else {
112 return new AudioFormat.Encoding[0];
113 }
114
115 } else {
116 return new AudioFormat.Encoding[0];
117 }
118 }
119
120 /**
121 */
122 public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
123 Objects.requireNonNull(sourceFormat);
124 if( (targetEncoding.equals( AudioFormat.Encoding.PCM_SIGNED ) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.ALAW)) ||
125 (targetEncoding.equals( AudioFormat.Encoding.ALAW) && sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED)) ) {
126 return getOutputFormats( sourceFormat );
127 } else {
128 return new AudioFormat[0];
129 }
130 }
131
132 /**
133 */
134 public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream){
135 AudioFormat sourceFormat = sourceStream.getFormat();
136 AudioFormat.Encoding sourceEncoding = sourceFormat.getEncoding();
137
138 if( sourceEncoding.equals( targetEncoding ) ) {
139 return sourceStream;
140 } else {
141 AudioFormat targetFormat = null;
142 if( !isConversionSupported(targetEncoding,sourceStream.getFormat()) ) {
143 throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString());
|