author | serb |
Wed, 04 May 2016 01:03:46 +0300 | |
changeset 38399 | bd91ce346b5b |
parent 36888 | 0905e6c2d1af |
child 41394 | da0318151636 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36888
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
2 |
* Copyright (c) 1999, 2016, 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 |
||
18215 | 28 |
import java.io.DataInputStream; |
29 |
import java.io.IOException; |
|
2 | 30 |
import java.io.InputStream; |
31 |
||
36888
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
32 |
import javax.sound.sampled.AudioFileFormat.Type; |
18215 | 33 |
import javax.sound.sampled.AudioFormat; |
2 | 34 |
import javax.sound.sampled.AudioSystem; |
35 |
import javax.sound.sampled.UnsupportedAudioFileException; |
|
36 |
||
37 |
/** |
|
38 |
* AU file reader. |
|
39 |
* |
|
40 |
* @author Kara Kytle |
|
41 |
* @author Jan Borgersen |
|
42 |
* @author Florian Bomers |
|
43 |
*/ |
|
18215 | 44 |
public final class AuFileReader extends SunFileReader { |
2 | 45 |
|
32121
93ad23865c9e
8013586: audioInputStream.close() does not release the resource
serb
parents:
25859
diff
changeset
|
46 |
@Override |
38399
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
47 |
public StandardFileFormat getAudioFileFormatImpl(final InputStream stream) |
32121
93ad23865c9e
8013586: audioInputStream.close() does not release the resource
serb
parents:
25859
diff
changeset
|
48 |
throws UnsupportedAudioFileException, IOException { |
35656 | 49 |
final DataInputStream dis = new DataInputStream(stream); |
50 |
final int magic = dis.readInt(); |
|
2 | 51 |
|
35656 | 52 |
if (magic != AuFileFormat.AU_SUN_MAGIC) { |
32121
93ad23865c9e
8013586: audioInputStream.close() does not release the resource
serb
parents:
25859
diff
changeset
|
53 |
// not AU, throw exception |
2 | 54 |
throw new UnsupportedAudioFileException("not an AU file"); |
55 |
} |
|
56 |
||
35656 | 57 |
final int headerSize = dis.readInt(); |
38399
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
58 |
final long /* unsigned int */ dataSize = dis.readInt() & 0xffffffffL; |
36888
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
59 |
final int auType = dis.readInt(); |
35656 | 60 |
final int sampleRate = dis.readInt(); |
61 |
final int channels = dis.readInt(); |
|
21245
786207514920
7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents:
18215
diff
changeset
|
62 |
if (channels <= 0) { |
786207514920
7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents:
18215
diff
changeset
|
63 |
throw new UnsupportedAudioFileException("Invalid number of channels"); |
786207514920
7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents:
18215
diff
changeset
|
64 |
} |
2 | 65 |
|
35656 | 66 |
final int sampleSizeInBits; |
67 |
final AudioFormat.Encoding encoding; |
|
36888
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
68 |
switch (auType) { |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
69 |
case AuFileFormat.AU_ULAW_8: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
70 |
encoding = AudioFormat.Encoding.ULAW; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
71 |
sampleSizeInBits = 8; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
72 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
73 |
case AuFileFormat.AU_ALAW_8: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
74 |
encoding = AudioFormat.Encoding.ALAW; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
75 |
sampleSizeInBits = 8; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
76 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
77 |
case AuFileFormat.AU_LINEAR_8: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
78 |
// $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned* |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
79 |
encoding = AudioFormat.Encoding.PCM_SIGNED; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
80 |
sampleSizeInBits = 8; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
81 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
82 |
case AuFileFormat.AU_LINEAR_16: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
83 |
encoding = AudioFormat.Encoding.PCM_SIGNED; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
84 |
sampleSizeInBits = 16; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
85 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
86 |
case AuFileFormat.AU_LINEAR_24: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
87 |
encoding = AudioFormat.Encoding.PCM_SIGNED; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
88 |
sampleSizeInBits = 24; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
89 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
90 |
case AuFileFormat.AU_LINEAR_32: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
91 |
encoding = AudioFormat.Encoding.PCM_SIGNED; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
92 |
sampleSizeInBits = 32; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
93 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
94 |
case AuFileFormat.AU_FLOAT: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
95 |
encoding = AudioFormat.Encoding.PCM_FLOAT; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
96 |
sampleSizeInBits = 32; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
97 |
break; |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
98 |
// we don't support these ... |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
99 |
/* case AuFileFormat.AU_DOUBLE: |
2 | 100 |
encoding = new AudioFormat.DOUBLE; |
101 |
sampleSizeInBits = 8; |
|
102 |
break; |
|
103 |
case AuFileFormat.AU_ADPCM_G721: |
|
104 |
encoding = new AudioFormat.G721_ADPCM; |
|
105 |
sampleSizeInBits = 16; |
|
106 |
break; |
|
107 |
case AuFileFormat.AU_ADPCM_G723_3: |
|
108 |
encoding = new AudioFormat.G723_3; |
|
109 |
sampleSize = 24; |
|
110 |
SamplePerUnit = 8; |
|
111 |
break; |
|
112 |
case AuFileFormat.AU_ADPCM_G723_5: |
|
113 |
encoding = new AudioFormat.G723_5; |
|
114 |
sampleSize = 40; |
|
115 |
SamplePerUnit = 8; |
|
116 |
break; |
|
117 |
*/ |
|
36888
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
118 |
default: |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
119 |
// unsupported filetype, throw exception |
0905e6c2d1af
8146239: Support of PCM_FLOAT for the AU file format
serb
parents:
35656
diff
changeset
|
120 |
throw new UnsupportedAudioFileException("not a valid AU file"); |
2 | 121 |
} |
38399
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
122 |
// now seek past the header |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
123 |
dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE); |
2 | 124 |
|
35656 | 125 |
final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels); |
2 | 126 |
//$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files |
38399
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
127 |
//$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
128 |
long frameLength = AudioSystem.NOT_SPECIFIED; |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
129 |
long byteLength = AudioSystem.NOT_SPECIFIED; |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
130 |
if (dataSize != AuFileFormat.UNKNOWN_SIZE) { |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
131 |
frameLength = dataSize / frameSize; |
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
132 |
byteLength = dataSize + headerSize; |
2 | 133 |
} |
35656 | 134 |
final AudioFormat format = new AudioFormat(encoding, sampleRate, |
135 |
sampleSizeInBits, channels, |
|
136 |
frameSize, sampleRate, true); |
|
38399
bd91ce346b5b
6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents:
36888
diff
changeset
|
137 |
return new AuFileFormat(Type.AU, byteLength, format, frameLength); |
2 | 138 |
} |
139 |
} |