2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.media.sound;
|
|
27 |
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
|
|
31 |
import java.util.Vector;
|
|
32 |
|
|
33 |
import javax.sound.sampled.AudioFormat;
|
|
34 |
import javax.sound.sampled.AudioSystem;
|
|
35 |
import javax.sound.sampled.AudioInputStream;
|
|
36 |
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Converts among signed/unsigned and little/big endianness of sampled.
|
|
40 |
*
|
|
41 |
* @author Jan Borgersen
|
|
42 |
*/
|
|
43 |
public class PCMtoPCMCodec extends SunCodec {
|
|
44 |
|
|
45 |
|
|
46 |
private static final AudioFormat.Encoding[] inputEncodings = {
|
|
47 |
AudioFormat.Encoding.PCM_SIGNED,
|
|
48 |
AudioFormat.Encoding.PCM_UNSIGNED,
|
|
49 |
};
|
|
50 |
|
|
51 |
private static final AudioFormat.Encoding[] outputEncodings = {
|
|
52 |
AudioFormat.Encoding.PCM_SIGNED,
|
|
53 |
AudioFormat.Encoding.PCM_UNSIGNED,
|
|
54 |
};
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
private static final int tempBufferSize = 64;
|
|
59 |
private byte tempBuffer [] = null;
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Constructs a new PCMtoPCM codec object.
|
|
63 |
*/
|
|
64 |
public PCMtoPCMCodec() {
|
|
65 |
|
|
66 |
super( inputEncodings, outputEncodings);
|
|
67 |
}
|
|
68 |
|
|
69 |
// NEW CODE
|
|
70 |
|
|
71 |
|
|
72 |
/**
|
|
73 |
*/
|
|
74 |
public AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat){
|
|
75 |
|
|
76 |
if( sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_SIGNED ) ||
|
|
77 |
sourceFormat.getEncoding().equals( AudioFormat.Encoding.PCM_UNSIGNED ) ) {
|
|
78 |
|
|
79 |
AudioFormat.Encoding encs[] = new AudioFormat.Encoding[2];
|
|
80 |
encs[0] = AudioFormat.Encoding.PCM_SIGNED;
|
|
81 |
encs[1] = AudioFormat.Encoding.PCM_UNSIGNED;
|
|
82 |
return encs;
|
|
83 |
} else {
|
|
84 |
return new AudioFormat.Encoding[0];
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
|
|
89 |
/**
|
|
90 |
*/
|
|
91 |
public AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat){
|
|
92 |
|
|
93 |
// filter out targetEncoding from the old getOutputFormats( sourceFormat ) method
|
|
94 |
|
|
95 |
AudioFormat[] formats = getOutputFormats( sourceFormat );
|
|
96 |
Vector newFormats = new Vector();
|
|
97 |
for(int i=0; i<formats.length; i++ ) {
|
|
98 |
if( formats[i].getEncoding().equals( targetEncoding ) ) {
|
|
99 |
newFormats.addElement( formats[i] );
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
AudioFormat[] formatArray = new AudioFormat[newFormats.size()];
|
|
104 |
|
|
105 |
for (int i = 0; i < formatArray.length; i++) {
|
|
106 |
formatArray[i] = (AudioFormat)(newFormats.elementAt(i));
|
|
107 |
}
|
|
108 |
|
|
109 |
return formatArray;
|
|
110 |
}
|
|
111 |
|
|
112 |
|
|
113 |
/**
|
|
114 |
*/
|
|
115 |
public AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding, AudioInputStream sourceStream) {
|
|
116 |
|
|
117 |
if( isConversionSupported(targetEncoding, sourceStream.getFormat()) ) {
|
|
118 |
|
|
119 |
AudioFormat sourceFormat = sourceStream.getFormat();
|
|
120 |
AudioFormat targetFormat = new AudioFormat( targetEncoding,
|
|
121 |
sourceFormat.getSampleRate(),
|
|
122 |
sourceFormat.getSampleSizeInBits(),
|
|
123 |
sourceFormat.getChannels(),
|
|
124 |
sourceFormat.getFrameSize(),
|
|
125 |
sourceFormat.getFrameRate(),
|
|
126 |
sourceFormat.isBigEndian() );
|
|
127 |
|
|
128 |
return getAudioInputStream( targetFormat, sourceStream );
|
|
129 |
|
|
130 |
} else {
|
|
131 |
throw new IllegalArgumentException("Unsupported conversion: " + sourceStream.getFormat().toString() + " to " + targetEncoding.toString() );
|
|
132 |
}
|
|
133 |
|
|
134 |
}
|
|
135 |
/**
|
|
136 |
* use old code
|
|
137 |
*/
|
|
138 |
public AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream){
|
|
139 |
|
|
140 |
return getConvertedStream( targetFormat, sourceStream );
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
// OLD CODE
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Opens the codec with the specified parameters.
|
|
150 |
* @param stream stream from which data to be processed should be read
|
|
151 |
* @param outputFormat desired data format of the stream after processing
|
|
152 |
* @return stream from which processed data may be read
|
|
153 |
* @throws IllegalArgumentException if the format combination supplied is
|
|
154 |
* not supported.
|
|
155 |
*/
|
|
156 |
/* public AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {*/
|
|
157 |
private AudioInputStream getConvertedStream(AudioFormat outputFormat, AudioInputStream stream) {
|
|
158 |
|
|
159 |
AudioInputStream cs = null;
|
|
160 |
|
|
161 |
AudioFormat inputFormat = stream.getFormat();
|
|
162 |
|
|
163 |
if( inputFormat.matches(outputFormat) ) {
|
|
164 |
|
|
165 |
cs = stream;
|
|
166 |
} else {
|
|
167 |
|
|
168 |
cs = (AudioInputStream) (new PCMtoPCMCodecStream(stream, outputFormat));
|
|
169 |
tempBuffer = new byte[tempBufferSize];
|
|
170 |
}
|
|
171 |
return cs;
|
|
172 |
}
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
/**
|
|
177 |
* Obtains the set of output formats supported by the codec
|
|
178 |
* given a particular input format.
|
|
179 |
* If no output formats are supported for this input format,
|
|
180 |
* returns an array of length 0.
|
|
181 |
* @return array of supported output formats.
|
|
182 |
*/
|
|
183 |
/* public AudioFormat[] getOutputFormats(AudioFormat inputFormat) { */
|
|
184 |
private AudioFormat[] getOutputFormats(AudioFormat inputFormat) {
|
|
185 |
|
|
186 |
Vector formats = new Vector();
|
|
187 |
AudioFormat format;
|
|
188 |
|
|
189 |
int sampleSize = inputFormat.getSampleSizeInBits();
|
|
190 |
boolean isBigEndian = inputFormat.isBigEndian();
|
|
191 |
|
|
192 |
|
|
193 |
if ( sampleSize==8 ) {
|
|
194 |
if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) ) {
|
|
195 |
|
|
196 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
197 |
inputFormat.getSampleRate(),
|
|
198 |
inputFormat.getSampleSizeInBits(),
|
|
199 |
inputFormat.getChannels(),
|
|
200 |
inputFormat.getFrameSize(),
|
|
201 |
inputFormat.getFrameRate(),
|
|
202 |
false );
|
|
203 |
formats.addElement(format);
|
|
204 |
}
|
|
205 |
|
|
206 |
if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) ) {
|
|
207 |
|
|
208 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
209 |
inputFormat.getSampleRate(),
|
|
210 |
inputFormat.getSampleSizeInBits(),
|
|
211 |
inputFormat.getChannels(),
|
|
212 |
inputFormat.getFrameSize(),
|
|
213 |
inputFormat.getFrameRate(),
|
|
214 |
false );
|
|
215 |
formats.addElement(format);
|
|
216 |
}
|
|
217 |
|
|
218 |
} else if ( sampleSize==16 ) {
|
|
219 |
|
|
220 |
if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) && isBigEndian ) {
|
|
221 |
|
|
222 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
223 |
inputFormat.getSampleRate(),
|
|
224 |
inputFormat.getSampleSizeInBits(),
|
|
225 |
inputFormat.getChannels(),
|
|
226 |
inputFormat.getFrameSize(),
|
|
227 |
inputFormat.getFrameRate(),
|
|
228 |
true );
|
|
229 |
formats.addElement(format);
|
|
230 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
231 |
inputFormat.getSampleRate(),
|
|
232 |
inputFormat.getSampleSizeInBits(),
|
|
233 |
inputFormat.getChannels(),
|
|
234 |
inputFormat.getFrameSize(),
|
|
235 |
inputFormat.getFrameRate(),
|
|
236 |
false );
|
|
237 |
formats.addElement(format);
|
|
238 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
239 |
inputFormat.getSampleRate(),
|
|
240 |
inputFormat.getSampleSizeInBits(),
|
|
241 |
inputFormat.getChannels(),
|
|
242 |
inputFormat.getFrameSize(),
|
|
243 |
inputFormat.getFrameRate(),
|
|
244 |
false );
|
|
245 |
formats.addElement(format);
|
|
246 |
}
|
|
247 |
|
|
248 |
if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) && isBigEndian ) {
|
|
249 |
|
|
250 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
251 |
inputFormat.getSampleRate(),
|
|
252 |
inputFormat.getSampleSizeInBits(),
|
|
253 |
inputFormat.getChannels(),
|
|
254 |
inputFormat.getFrameSize(),
|
|
255 |
inputFormat.getFrameRate(),
|
|
256 |
true );
|
|
257 |
formats.addElement(format);
|
|
258 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
259 |
inputFormat.getSampleRate(),
|
|
260 |
inputFormat.getSampleSizeInBits(),
|
|
261 |
inputFormat.getChannels(),
|
|
262 |
inputFormat.getFrameSize(),
|
|
263 |
inputFormat.getFrameRate(),
|
|
264 |
false );
|
|
265 |
formats.addElement(format);
|
|
266 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
267 |
inputFormat.getSampleRate(),
|
|
268 |
inputFormat.getSampleSizeInBits(),
|
|
269 |
inputFormat.getChannels(),
|
|
270 |
inputFormat.getFrameSize(),
|
|
271 |
inputFormat.getFrameRate(),
|
|
272 |
false );
|
|
273 |
formats.addElement(format);
|
|
274 |
}
|
|
275 |
|
|
276 |
if ( AudioFormat.Encoding.PCM_SIGNED.equals(inputFormat.getEncoding()) && !isBigEndian ) {
|
|
277 |
|
|
278 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
279 |
inputFormat.getSampleRate(),
|
|
280 |
inputFormat.getSampleSizeInBits(),
|
|
281 |
inputFormat.getChannels(),
|
|
282 |
inputFormat.getFrameSize(),
|
|
283 |
inputFormat.getFrameRate(),
|
|
284 |
false );
|
|
285 |
formats.addElement(format);
|
|
286 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
287 |
inputFormat.getSampleRate(),
|
|
288 |
inputFormat.getSampleSizeInBits(),
|
|
289 |
inputFormat.getChannels(),
|
|
290 |
inputFormat.getFrameSize(),
|
|
291 |
inputFormat.getFrameRate(),
|
|
292 |
true );
|
|
293 |
formats.addElement(format);
|
|
294 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
295 |
inputFormat.getSampleRate(),
|
|
296 |
inputFormat.getSampleSizeInBits(),
|
|
297 |
inputFormat.getChannels(),
|
|
298 |
inputFormat.getFrameSize(),
|
|
299 |
inputFormat.getFrameRate(),
|
|
300 |
true );
|
|
301 |
formats.addElement(format);
|
|
302 |
}
|
|
303 |
|
|
304 |
if ( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputFormat.getEncoding()) && !isBigEndian ) {
|
|
305 |
|
|
306 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
307 |
inputFormat.getSampleRate(),
|
|
308 |
inputFormat.getSampleSizeInBits(),
|
|
309 |
inputFormat.getChannels(),
|
|
310 |
inputFormat.getFrameSize(),
|
|
311 |
inputFormat.getFrameRate(),
|
|
312 |
false );
|
|
313 |
formats.addElement(format);
|
|
314 |
format = new AudioFormat(AudioFormat.Encoding.PCM_UNSIGNED,
|
|
315 |
inputFormat.getSampleRate(),
|
|
316 |
inputFormat.getSampleSizeInBits(),
|
|
317 |
inputFormat.getChannels(),
|
|
318 |
inputFormat.getFrameSize(),
|
|
319 |
inputFormat.getFrameRate(),
|
|
320 |
true );
|
|
321 |
formats.addElement(format);
|
|
322 |
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
|
|
323 |
inputFormat.getSampleRate(),
|
|
324 |
inputFormat.getSampleSizeInBits(),
|
|
325 |
inputFormat.getChannels(),
|
|
326 |
inputFormat.getFrameSize(),
|
|
327 |
inputFormat.getFrameRate(),
|
|
328 |
true );
|
|
329 |
formats.addElement(format);
|
|
330 |
}
|
|
331 |
}
|
|
332 |
AudioFormat[] formatArray;
|
|
333 |
|
|
334 |
synchronized(formats) {
|
|
335 |
|
|
336 |
formatArray = new AudioFormat[formats.size()];
|
|
337 |
|
|
338 |
for (int i = 0; i < formatArray.length; i++) {
|
|
339 |
|
|
340 |
formatArray[i] = (AudioFormat)(formats.elementAt(i));
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
|
344 |
return formatArray;
|
|
345 |
}
|
|
346 |
|
|
347 |
|
|
348 |
class PCMtoPCMCodecStream extends AudioInputStream {
|
|
349 |
|
|
350 |
private final int PCM_SWITCH_SIGNED_8BIT = 1;
|
|
351 |
private final int PCM_SWITCH_ENDIAN = 2;
|
|
352 |
private final int PCM_SWITCH_SIGNED_LE = 3;
|
|
353 |
private final int PCM_SWITCH_SIGNED_BE = 4;
|
|
354 |
private final int PCM_UNSIGNED_LE2SIGNED_BE = 5;
|
|
355 |
private final int PCM_SIGNED_LE2UNSIGNED_BE = 6;
|
|
356 |
private final int PCM_UNSIGNED_BE2SIGNED_LE = 7;
|
|
357 |
private final int PCM_SIGNED_BE2UNSIGNED_LE = 8;
|
|
358 |
|
|
359 |
private int sampleSizeInBytes = 0;
|
|
360 |
private int conversionType = 0;
|
|
361 |
|
|
362 |
|
|
363 |
PCMtoPCMCodecStream(AudioInputStream stream, AudioFormat outputFormat) {
|
|
364 |
|
|
365 |
super(stream, outputFormat, -1);
|
|
366 |
|
|
367 |
int sampleSizeInBits = 0;
|
|
368 |
AudioFormat.Encoding inputEncoding = null;
|
|
369 |
AudioFormat.Encoding outputEncoding = null;
|
|
370 |
boolean inputIsBigEndian;
|
|
371 |
boolean outputIsBigEndian;
|
|
372 |
|
|
373 |
AudioFormat inputFormat = stream.getFormat();
|
|
374 |
|
|
375 |
// throw an IllegalArgumentException if not ok
|
|
376 |
if ( ! (isConversionSupported(inputFormat, outputFormat)) ) {
|
|
377 |
|
|
378 |
throw new IllegalArgumentException("Unsupported conversion: " + inputFormat.toString() + " to " + outputFormat.toString());
|
|
379 |
}
|
|
380 |
|
|
381 |
inputEncoding = inputFormat.getEncoding();
|
|
382 |
outputEncoding = outputFormat.getEncoding();
|
|
383 |
inputIsBigEndian = inputFormat.isBigEndian();
|
|
384 |
outputIsBigEndian = outputFormat.isBigEndian();
|
|
385 |
sampleSizeInBits = inputFormat.getSampleSizeInBits();
|
|
386 |
sampleSizeInBytes = sampleSizeInBits/8;
|
|
387 |
|
|
388 |
// determine conversion to perform
|
|
389 |
|
|
390 |
if( sampleSizeInBits==8 ) {
|
|
391 |
if( AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) &&
|
|
392 |
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) ) {
|
|
393 |
conversionType = PCM_SWITCH_SIGNED_8BIT;
|
|
394 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
|
|
395 |
|
|
396 |
} else if( AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) &&
|
|
397 |
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) ) {
|
|
398 |
conversionType = PCM_SWITCH_SIGNED_8BIT;
|
|
399 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_SIGNED_8BIT");
|
|
400 |
}
|
|
401 |
} else {
|
|
402 |
|
|
403 |
if( inputEncoding.equals(outputEncoding) && (inputIsBigEndian != outputIsBigEndian) ) {
|
|
404 |
|
|
405 |
conversionType = PCM_SWITCH_ENDIAN;
|
|
406 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SWITCH_ENDIAN");
|
|
407 |
|
|
408 |
|
|
409 |
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && !inputIsBigEndian &&
|
|
410 |
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && outputIsBigEndian) {
|
|
411 |
|
|
412 |
conversionType = PCM_UNSIGNED_LE2SIGNED_BE;
|
|
413 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_LE2SIGNED_BE");
|
|
414 |
|
|
415 |
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && !inputIsBigEndian &&
|
|
416 |
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && outputIsBigEndian) {
|
|
417 |
|
|
418 |
conversionType = PCM_SIGNED_LE2UNSIGNED_BE;
|
|
419 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_LE2UNSIGNED_BE");
|
|
420 |
|
|
421 |
} else if (AudioFormat.Encoding.PCM_UNSIGNED.equals(inputEncoding) && inputIsBigEndian &&
|
|
422 |
AudioFormat.Encoding.PCM_SIGNED.equals(outputEncoding) && !outputIsBigEndian) {
|
|
423 |
|
|
424 |
conversionType = PCM_UNSIGNED_BE2SIGNED_LE;
|
|
425 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_UNSIGNED_BE2SIGNED_LE");
|
|
426 |
|
|
427 |
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(inputEncoding) && inputIsBigEndian &&
|
|
428 |
AudioFormat.Encoding.PCM_UNSIGNED.equals(outputEncoding) && !outputIsBigEndian) {
|
|
429 |
|
|
430 |
conversionType = PCM_SIGNED_BE2UNSIGNED_LE;
|
|
431 |
if(Printer.debug) Printer.debug("PCMtoPCMCodecStream: conversionType = PCM_SIGNED_BE2UNSIGNED_LE");
|
|
432 |
|
|
433 |
}
|
|
434 |
}
|
|
435 |
|
|
436 |
// set the audio stream length in frames if we know it
|
|
437 |
|
|
438 |
frameSize = inputFormat.getFrameSize();
|
|
439 |
if( frameSize == AudioSystem.NOT_SPECIFIED ) {
|
|
440 |
frameSize=1;
|
|
441 |
}
|
|
442 |
if( stream instanceof AudioInputStream ) {
|
|
443 |
frameLength = stream.getFrameLength();
|
|
444 |
} else {
|
|
445 |
frameLength = AudioSystem.NOT_SPECIFIED;
|
|
446 |
}
|
|
447 |
|
|
448 |
// set framePos to zero
|
|
449 |
framePos = 0;
|
|
450 |
|
|
451 |
}
|
|
452 |
|
|
453 |
/**
|
|
454 |
* Note that this only works for sign conversions.
|
|
455 |
* Other conversions require a read of at least 2 bytes.
|
|
456 |
*/
|
|
457 |
|
|
458 |
public int read() throws IOException {
|
|
459 |
|
|
460 |
// $$jb: do we want to implement this function?
|
|
461 |
|
|
462 |
int temp;
|
|
463 |
byte tempbyte;
|
|
464 |
|
|
465 |
if( frameSize==1 ) {
|
|
466 |
if( conversionType == PCM_SWITCH_SIGNED_8BIT ) {
|
|
467 |
temp = super.read();
|
|
468 |
|
|
469 |
if( temp < 0 ) return temp; // EOF or error
|
|
470 |
|
|
471 |
tempbyte = (byte) (temp & 0xf);
|
|
472 |
tempbyte = (tempbyte >= 0) ? (byte)(0x80 | tempbyte) : (byte)(0x7F & tempbyte);
|
|
473 |
temp = (int) tempbyte & 0xf;
|
|
474 |
|
|
475 |
return temp;
|
|
476 |
|
|
477 |
} else {
|
|
478 |
// $$jb: what to return here?
|
|
479 |
throw new IOException("cannot read a single byte if frame size > 1");
|
|
480 |
}
|
|
481 |
} else {
|
|
482 |
throw new IOException("cannot read a single byte if frame size > 1");
|
|
483 |
}
|
|
484 |
}
|
|
485 |
|
|
486 |
|
|
487 |
public int read(byte[] b) throws IOException {
|
|
488 |
|
|
489 |
return read(b, 0, b.length);
|
|
490 |
}
|
|
491 |
|
|
492 |
public int read(byte[] b, int off, int len) throws IOException {
|
|
493 |
|
|
494 |
|
|
495 |
int i;
|
|
496 |
|
|
497 |
// don't read fractional frames
|
|
498 |
if ( len%frameSize != 0 ) {
|
|
499 |
len -= (len%frameSize);
|
|
500 |
}
|
|
501 |
// don't read past our own set length
|
|
502 |
if( (frameLength!=AudioSystem.NOT_SPECIFIED) && ( (len/frameSize) >(frameLength-framePos)) ) {
|
|
503 |
len = (int)(frameLength-framePos) * frameSize;
|
|
504 |
}
|
|
505 |
|
|
506 |
int readCount = super.read(b, off, len);
|
|
507 |
byte tempByte;
|
|
508 |
|
|
509 |
if(readCount<0) { // EOF or error
|
|
510 |
return readCount;
|
|
511 |
}
|
|
512 |
|
|
513 |
// now do the conversions
|
|
514 |
|
|
515 |
switch( conversionType ) {
|
|
516 |
|
|
517 |
case PCM_SWITCH_SIGNED_8BIT:
|
|
518 |
switchSigned8bit(b,off,len,readCount);
|
|
519 |
break;
|
|
520 |
|
|
521 |
case PCM_SWITCH_ENDIAN:
|
|
522 |
switchEndian(b,off,len,readCount);
|
|
523 |
break;
|
|
524 |
|
|
525 |
case PCM_SWITCH_SIGNED_LE:
|
|
526 |
switchSignedLE(b,off,len,readCount);
|
|
527 |
break;
|
|
528 |
|
|
529 |
case PCM_SWITCH_SIGNED_BE:
|
|
530 |
switchSignedBE(b,off,len,readCount);
|
|
531 |
break;
|
|
532 |
|
|
533 |
case PCM_UNSIGNED_LE2SIGNED_BE:
|
|
534 |
case PCM_SIGNED_LE2UNSIGNED_BE:
|
|
535 |
switchSignedLE(b,off,len,readCount);
|
|
536 |
switchEndian(b,off,len,readCount);
|
|
537 |
break;
|
|
538 |
|
|
539 |
case PCM_UNSIGNED_BE2SIGNED_LE:
|
|
540 |
case PCM_SIGNED_BE2UNSIGNED_LE:
|
|
541 |
switchSignedBE(b,off,len,readCount);
|
|
542 |
switchEndian(b,off,len,readCount);
|
|
543 |
break;
|
|
544 |
|
|
545 |
default:
|
|
546 |
// do nothing
|
|
547 |
}
|
|
548 |
|
|
549 |
// we've done the conversion, just return the readCount
|
|
550 |
return readCount;
|
|
551 |
|
|
552 |
}
|
|
553 |
|
|
554 |
private void switchSigned8bit(byte[] b, int off, int len, int readCount) {
|
|
555 |
|
|
556 |
for(int i=off; i < (off+readCount); i++) {
|
|
557 |
b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
|
|
558 |
}
|
|
559 |
}
|
|
560 |
|
|
561 |
private void switchSignedBE(byte[] b, int off, int len, int readCount) {
|
|
562 |
|
|
563 |
for(int i=off; i < (off+readCount); i+= sampleSizeInBytes ) {
|
|
564 |
b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
|
|
565 |
}
|
|
566 |
}
|
|
567 |
|
|
568 |
private void switchSignedLE(byte[] b, int off, int len, int readCount) {
|
|
569 |
|
|
570 |
for(int i=(off+sampleSizeInBytes-1); i < (off+readCount); i+= sampleSizeInBytes ) {
|
|
571 |
b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
|
|
572 |
}
|
|
573 |
}
|
|
574 |
|
|
575 |
private void switchEndian(byte[] b, int off, int len, int readCount) {
|
|
576 |
|
|
577 |
if(sampleSizeInBytes == 2) {
|
|
578 |
for(int i=off; i < (off+readCount); i += sampleSizeInBytes ) {
|
|
579 |
byte temp;
|
|
580 |
temp = b[i];
|
|
581 |
b[i] = b[i+1];
|
|
582 |
b[i+1] = temp;
|
|
583 |
}
|
|
584 |
}
|
|
585 |
}
|
|
586 |
|
|
587 |
|
|
588 |
|
|
589 |
} // end class PCMtoPCMCodecStream
|
|
590 |
|
|
591 |
} // end class PCMtoPCMCodec
|