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.util.Vector;
|
|
29 |
import java.io.File;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.io.OutputStream;
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.EOFException;
|
|
34 |
import java.net.URL;
|
|
35 |
import java.net.MalformedURLException;
|
|
36 |
|
|
37 |
import java.io.BufferedInputStream;
|
|
38 |
import java.io.BufferedOutputStream;
|
|
39 |
import java.io.DataInputStream;
|
|
40 |
import java.io.FileInputStream;
|
|
41 |
import java.io.DataOutputStream;
|
|
42 |
import java.io.FileOutputStream;
|
|
43 |
import java.io.ByteArrayInputStream;
|
|
44 |
import java.io.ByteArrayOutputStream;
|
|
45 |
import java.io.SequenceInputStream;
|
|
46 |
|
|
47 |
import javax.sound.sampled.AudioFileFormat;
|
|
48 |
import javax.sound.sampled.AudioInputStream;
|
|
49 |
import javax.sound.sampled.AudioFormat;
|
|
50 |
import javax.sound.sampled.AudioSystem;
|
|
51 |
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
52 |
|
|
53 |
|
|
54 |
/**
|
|
55 |
* AU file reader.
|
|
56 |
*
|
|
57 |
* @author Kara Kytle
|
|
58 |
* @author Jan Borgersen
|
|
59 |
* @author Florian Bomers
|
|
60 |
*/
|
|
61 |
public class AuFileReader extends SunFileReader {
|
|
62 |
|
|
63 |
/**
|
|
64 |
* AU reader type
|
|
65 |
*/
|
|
66 |
|
|
67 |
public static final AudioFileFormat.Type types[] = {
|
|
68 |
AudioFileFormat.Type.AU
|
|
69 |
};
|
|
70 |
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Constructs a new AuFileReader object.
|
|
74 |
*/
|
|
75 |
public AuFileReader() {
|
|
76 |
}
|
|
77 |
|
|
78 |
|
|
79 |
// METHODS TO IMPLEMENT AudioFileReader
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Obtains the audio file format of the input stream provided. The stream must
|
|
83 |
* point to valid audio file data. In general, audio file providers may
|
|
84 |
* need to read some data from the stream before determining whether they
|
|
85 |
* support it. These parsers must
|
|
86 |
* be able to mark the stream, read enough data to determine whether they
|
|
87 |
* support the stream, and, if not, reset the stream's read pointer to its original
|
|
88 |
* position. If the input stream does not support this, this method may fail
|
|
89 |
* with an IOException.
|
|
90 |
* @param stream the input stream from which file format information should be
|
|
91 |
* extracted
|
|
92 |
* @return an <code>AudioFileFormat</code> object describing the audio file format
|
|
93 |
* @throws UnsupportedAudioFileException if the stream does not point to valid audio
|
|
94 |
* file data recognized by the system
|
|
95 |
* @throws IOException if an I/O exception occurs
|
|
96 |
* @see InputStream#markSupported
|
|
97 |
* @see InputStream#mark
|
|
98 |
*/
|
|
99 |
public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
|
|
100 |
|
|
101 |
AudioFormat format = null;
|
|
102 |
AuFileFormat fileFormat = null;
|
|
103 |
int maxReadLength = 28;
|
|
104 |
boolean bigendian = false;
|
|
105 |
int magic = -1;
|
|
106 |
int headerSize = -1;
|
|
107 |
int dataSize = -1;
|
|
108 |
int encoding_local = -1;
|
|
109 |
int sampleRate = -1;
|
|
110 |
int frameRate = -1;
|
|
111 |
int frameSize = -1;
|
|
112 |
int channels = -1;
|
|
113 |
int sampleSizeInBits = 0;
|
|
114 |
int length = 0;
|
|
115 |
int nread = 0;
|
|
116 |
AudioFormat.Encoding encoding = null;
|
|
117 |
|
|
118 |
DataInputStream dis = new DataInputStream( stream );
|
|
119 |
|
|
120 |
dis.mark(maxReadLength);
|
|
121 |
|
|
122 |
magic = dis.readInt();
|
|
123 |
|
|
124 |
if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
|
|
125 |
(magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
|
|
126 |
|
|
127 |
// not AU, reset the stream, place into exception, throw exception
|
|
128 |
dis.reset();
|
|
129 |
throw new UnsupportedAudioFileException("not an AU file");
|
|
130 |
}
|
|
131 |
|
|
132 |
if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
|
|
133 |
bigendian = true; // otherwise little-endian
|
|
134 |
}
|
|
135 |
|
|
136 |
headerSize = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;
|
|
137 |
dataSize = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;
|
|
138 |
encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;
|
|
139 |
sampleRate = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;
|
|
140 |
channels = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;
|
|
141 |
|
|
142 |
frameRate = sampleRate;
|
|
143 |
|
|
144 |
switch (encoding_local) {
|
|
145 |
case AuFileFormat.AU_ULAW_8:
|
|
146 |
encoding = AudioFormat.Encoding.ULAW;
|
|
147 |
sampleSizeInBits = 8;
|
|
148 |
break;
|
|
149 |
case AuFileFormat.AU_ALAW_8:
|
|
150 |
encoding = AudioFormat.Encoding.ALAW;
|
|
151 |
sampleSizeInBits = 8;
|
|
152 |
break;
|
|
153 |
case AuFileFormat.AU_LINEAR_8:
|
|
154 |
// $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
|
|
155 |
encoding = AudioFormat.Encoding.PCM_SIGNED;
|
|
156 |
sampleSizeInBits = 8;
|
|
157 |
break;
|
|
158 |
case AuFileFormat.AU_LINEAR_16:
|
|
159 |
encoding = AudioFormat.Encoding.PCM_SIGNED;
|
|
160 |
sampleSizeInBits = 16;
|
|
161 |
break;
|
|
162 |
case AuFileFormat.AU_LINEAR_24:
|
|
163 |
encoding = AudioFormat.Encoding.PCM_SIGNED;
|
|
164 |
|
|
165 |
sampleSizeInBits = 24;
|
|
166 |
break;
|
|
167 |
case AuFileFormat.AU_LINEAR_32:
|
|
168 |
encoding = AudioFormat.Encoding.PCM_SIGNED;
|
|
169 |
|
|
170 |
sampleSizeInBits = 32;
|
|
171 |
break;
|
|
172 |
// $jb: 03.19.99: we don't support these ...
|
|
173 |
/* case AuFileFormat.AU_FLOAT:
|
|
174 |
encoding = new AudioFormat.FLOAT;
|
|
175 |
sampleSizeInBits = 32;
|
|
176 |
break;
|
|
177 |
case AuFileFormat.AU_DOUBLE:
|
|
178 |
encoding = new AudioFormat.DOUBLE;
|
|
179 |
sampleSizeInBits = 8;
|
|
180 |
break;
|
|
181 |
case AuFileFormat.AU_ADPCM_G721:
|
|
182 |
encoding = new AudioFormat.G721_ADPCM;
|
|
183 |
sampleSizeInBits = 16;
|
|
184 |
break;
|
|
185 |
case AuFileFormat.AU_ADPCM_G723_3:
|
|
186 |
encoding = new AudioFormat.G723_3;
|
|
187 |
sampleSize = 24;
|
|
188 |
SamplePerUnit = 8;
|
|
189 |
break;
|
|
190 |
case AuFileFormat.AU_ADPCM_G723_5:
|
|
191 |
encoding = new AudioFormat.G723_5;
|
|
192 |
sampleSize = 40;
|
|
193 |
SamplePerUnit = 8;
|
|
194 |
break;
|
|
195 |
*/
|
|
196 |
default:
|
|
197 |
// unsupported filetype, throw exception
|
|
198 |
dis.reset();
|
|
199 |
throw new UnsupportedAudioFileException("not a valid AU file");
|
|
200 |
}
|
|
201 |
|
|
202 |
frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
|
|
203 |
//$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
|
|
204 |
if( dataSize < 0 ) {
|
|
205 |
length = AudioSystem.NOT_SPECIFIED;
|
|
206 |
} else {
|
|
207 |
//$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
|
|
208 |
length = dataSize / frameSize;
|
|
209 |
}
|
|
210 |
|
|
211 |
format = new AudioFormat( encoding, (float)sampleRate, sampleSizeInBits,
|
|
212 |
channels, frameSize, (float)frameRate, bigendian);
|
|
213 |
|
|
214 |
fileFormat = new AuFileFormat( AudioFileFormat.Type.AU, dataSize+headerSize,
|
|
215 |
format, length);
|
|
216 |
|
|
217 |
dis.reset(); // Throws IOException
|
|
218 |
return fileFormat;
|
|
219 |
|
|
220 |
}
|
|
221 |
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Obtains the audio file format of the URL provided. The URL must
|
|
225 |
* point to valid audio file data.
|
|
226 |
* @param url the URL from which file format information should be
|
|
227 |
* extracted
|
|
228 |
* @return an <code>AudioFileFormat</code> object describing the audio file format
|
|
229 |
* @throws UnsupportedAudioFileException if the URL does not point to valid audio
|
|
230 |
* file data recognized by the system
|
|
231 |
* @throws IOException if an I/O exception occurs
|
|
232 |
*/
|
|
233 |
public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
|
|
234 |
|
|
235 |
InputStream urlStream = null;
|
|
236 |
BufferedInputStream bis = null;
|
|
237 |
AudioFileFormat fileFormat = null;
|
|
238 |
AudioFormat format = null;
|
|
239 |
|
|
240 |
urlStream = url.openStream(); // throws IOException
|
|
241 |
|
|
242 |
try {
|
|
243 |
bis = new BufferedInputStream( urlStream, bisBufferSize );
|
|
244 |
|
|
245 |
fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException
|
|
246 |
} finally {
|
|
247 |
urlStream.close();
|
|
248 |
}
|
|
249 |
|
|
250 |
return fileFormat;
|
|
251 |
}
|
|
252 |
|
|
253 |
|
|
254 |
/**
|
|
255 |
* Obtains the audio file format of the File provided. The File must
|
|
256 |
* point to valid audio file data.
|
|
257 |
* @param file the File from which file format information should be
|
|
258 |
* extracted
|
|
259 |
* @return an <code>AudioFileFormat</code> object describing the audio file format
|
|
260 |
* @throws UnsupportedAudioFileException if the File does not point to valid audio
|
|
261 |
* file data recognized by the system
|
|
262 |
* @throws IOException if an I/O exception occurs
|
|
263 |
*/
|
|
264 |
public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {
|
|
265 |
|
|
266 |
FileInputStream fis = null;
|
|
267 |
BufferedInputStream bis = null;
|
|
268 |
AudioFileFormat fileFormat = null;
|
|
269 |
AudioFormat format = null;
|
|
270 |
|
|
271 |
fis = new FileInputStream( file ); // throws IOException
|
|
272 |
// part of fix for 4325421
|
|
273 |
try {
|
|
274 |
bis = new BufferedInputStream( fis, bisBufferSize );
|
|
275 |
fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException
|
|
276 |
} finally {
|
|
277 |
fis.close();
|
|
278 |
}
|
|
279 |
|
|
280 |
return fileFormat;
|
|
281 |
}
|
|
282 |
|
|
283 |
|
|
284 |
/**
|
|
285 |
* Obtains an audio stream from the input stream provided. The stream must
|
|
286 |
* point to valid audio file data. In general, audio file providers may
|
|
287 |
* need to read some data from the stream before determining whether they
|
|
288 |
* support it. These parsers must
|
|
289 |
* be able to mark the stream, read enough data to determine whether they
|
|
290 |
* support the stream, and, if not, reset the stream's read pointer to its original
|
|
291 |
* position. If the input stream does not support this, this method may fail
|
|
292 |
* with an IOException.
|
|
293 |
* @param stream the input stream from which the <code>AudioInputStream</code> should be
|
|
294 |
* constructed
|
|
295 |
* @return an <code>AudioInputStream</code> object based on the audio file data contained
|
|
296 |
* in the input stream.
|
|
297 |
* @throws UnsupportedAudioFileException if the stream does not point to valid audio
|
|
298 |
* file data recognized by the system
|
|
299 |
* @throws IOException if an I/O exception occurs
|
|
300 |
* @see InputStream#markSupported
|
|
301 |
* @see InputStream#mark
|
|
302 |
*/
|
|
303 |
public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
|
|
304 |
|
|
305 |
DataInputStream dis = null;
|
|
306 |
int headerSize;
|
|
307 |
AudioFileFormat fileFormat = null;
|
|
308 |
AudioFormat format = null;
|
|
309 |
|
|
310 |
|
|
311 |
fileFormat = getAudioFileFormat( stream ); // throws UnsupportedAudioFileException, IOException
|
|
312 |
|
|
313 |
// if we passed this call, we have an AU file.
|
|
314 |
|
|
315 |
format = fileFormat.getFormat();
|
|
316 |
|
|
317 |
dis = new DataInputStream(stream);
|
|
318 |
|
|
319 |
// now seek past the header
|
|
320 |
|
|
321 |
dis.readInt(); // magic
|
|
322 |
headerSize = (format.isBigEndian()==true ? dis.readInt() : rllong(dis) );
|
|
323 |
dis.skipBytes( headerSize - 8 );
|
|
324 |
|
|
325 |
|
|
326 |
// we've got everything, and the stream should be at the
|
|
327 |
// beginning of the data chunk, so return an AudioInputStream.
|
|
328 |
|
|
329 |
return new AudioInputStream(dis, format, fileFormat.getFrameLength());
|
|
330 |
}
|
|
331 |
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Obtains an audio stream from the URL provided. The URL must
|
|
335 |
* point to valid audio file data.
|
|
336 |
* @param url the URL for which the <code>AudioInputStream</code> should be
|
|
337 |
* constructed
|
|
338 |
* @return an <code>AudioInputStream</code> object based on the audio file data pointed
|
|
339 |
* to by the URL
|
|
340 |
* @throws UnsupportedAudioFileException if the URL does not point to valid audio
|
|
341 |
* file data recognized by the system
|
|
342 |
* @throws IOException if an I/O exception occurs
|
|
343 |
*/
|
|
344 |
public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
|
|
345 |
|
|
346 |
InputStream urlStream = null;
|
|
347 |
BufferedInputStream bis = null;
|
|
348 |
AudioFileFormat fileFormat = null;
|
|
349 |
|
|
350 |
urlStream = url.openStream(); // throws IOException
|
|
351 |
AudioInputStream result = null;
|
|
352 |
try {
|
|
353 |
bis = new BufferedInputStream( urlStream, bisBufferSize );
|
|
354 |
result = getAudioInputStream( (InputStream)bis );
|
|
355 |
} finally {
|
|
356 |
if (result == null) {
|
|
357 |
urlStream.close();
|
|
358 |
}
|
|
359 |
}
|
|
360 |
return result;
|
|
361 |
}
|
|
362 |
|
|
363 |
|
|
364 |
/**
|
|
365 |
* Obtains an audio stream from the File provided. The File must
|
|
366 |
* point to valid audio file data.
|
|
367 |
* @param file the File for which the <code>AudioInputStream</code> should be
|
|
368 |
* constructed
|
|
369 |
* @return an <code>AudioInputStream</code> object based on the audio file data pointed
|
|
370 |
* to by the File
|
|
371 |
* @throws UnsupportedAudioFileException if the File does not point to valid audio
|
|
372 |
* file data recognized by the system
|
|
373 |
* @throws IOException if an I/O exception occurs
|
|
374 |
*/
|
|
375 |
public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
|
|
376 |
|
|
377 |
FileInputStream fis = null;
|
|
378 |
BufferedInputStream bis = null;
|
|
379 |
AudioFileFormat fileFormat = null;
|
|
380 |
|
|
381 |
fis = new FileInputStream( file ); // throws IOException
|
|
382 |
AudioInputStream result = null;
|
|
383 |
// part of fix for 4325421
|
|
384 |
try {
|
|
385 |
bis = new BufferedInputStream( fis, bisBufferSize );
|
|
386 |
result = getAudioInputStream( (InputStream)bis );
|
|
387 |
} finally {
|
|
388 |
if (result == null) {
|
|
389 |
fis.close();
|
|
390 |
}
|
|
391 |
}
|
|
392 |
|
|
393 |
return result;
|
|
394 |
}
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
}
|