27
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 import javax.sound.sampled.AudioFileFormat.Type;
33 import javax.sound.sampled.AudioFormat;
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 */
44 public final class AuFileReader extends SunFileReader {
45
46 @Override
47 public StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
48 throws UnsupportedAudioFileException, IOException {
49 final DataInputStream dis = new DataInputStream(stream);
50 final int magic = dis.readInt();
51
52 if (magic != AuFileFormat.AU_SUN_MAGIC) {
53 // not AU, throw exception
54 throw new UnsupportedAudioFileException("not an AU file");
55 }
56
57 final int headerSize = dis.readInt();
58 final long /* unsigned int */ dataSize = dis.readInt() & 0xffffffffL;
59 final int auType = dis.readInt();
60 final int sampleRate = dis.readInt();
61 final int channels = dis.readInt();
62 if (channels <= 0) {
63 throw new UnsupportedAudioFileException("Invalid number of channels");
64 }
65
66 final int sampleSizeInBits;
67 final AudioFormat.Encoding encoding;
68 switch (auType) {
69 case AuFileFormat.AU_ULAW_8:
70 encoding = AudioFormat.Encoding.ULAW;
71 sampleSizeInBits = 8;
72 break;
73 case AuFileFormat.AU_ALAW_8:
74 encoding = AudioFormat.Encoding.ALAW;
75 sampleSizeInBits = 8;
76 break;
77 case AuFileFormat.AU_LINEAR_8:
78 // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
79 encoding = AudioFormat.Encoding.PCM_SIGNED;
80 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 */
118 default:
119 // unsupported filetype, throw exception
120 throw new UnsupportedAudioFileException("not a valid AU file");
121 }
122 // now seek past the header
123 dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
124
125 final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
126 //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
127 //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
128 long frameLength = AudioSystem.NOT_SPECIFIED;
129 long byteLength = AudioSystem.NOT_SPECIFIED;
130 if (dataSize != AuFileFormat.UNKNOWN_SIZE) {
131 frameLength = dataSize / frameSize;
132 byteLength = dataSize + headerSize;
133 }
134 final AudioFormat format = new AudioFormat(encoding, sampleRate,
135 sampleSizeInBits, channels,
136 frameSize, sampleRate, true);
137 return new AuFileFormat(Type.AU, byteLength, format, frameLength);
138 }
139 }
|
27
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 import javax.sound.sampled.AudioFileFormat.Type;
33 import javax.sound.sampled.AudioFormat;
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 */
44 public final class AuFileReader extends SunFileReader {
45
46 @Override
47 StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
48 throws UnsupportedAudioFileException, IOException {
49 final DataInputStream dis = new DataInputStream(stream);
50 final int magic = dis.readInt();
51
52 if (magic != AuFileFormat.AU_SUN_MAGIC) {
53 // not AU, throw exception
54 throw new UnsupportedAudioFileException("not an AU file");
55 }
56
57 final int headerSize = dis.readInt();
58 if (headerSize < AuFileFormat.AU_HEADERSIZE) {
59 throw new UnsupportedAudioFileException("Invalid header size");
60 }
61 final long /* unsigned int */ dataSize = dis.readInt() & 0xffffffffL;
62 final int auType = dis.readInt();
63 final int sampleRate = dis.readInt();
64 if (sampleRate <= 0) {
65 throw new UnsupportedAudioFileException("Invalid sample rate");
66 }
67 final int channels = dis.readInt();
68 if (channels <= 0) {
69 throw new UnsupportedAudioFileException("Invalid number of channels");
70 }
71
72 final int sampleSizeInBits;
73 final AudioFormat.Encoding encoding;
74 switch (auType) {
75 case AuFileFormat.AU_ULAW_8:
76 encoding = AudioFormat.Encoding.ULAW;
77 sampleSizeInBits = 8;
78 break;
79 case AuFileFormat.AU_ALAW_8:
80 encoding = AudioFormat.Encoding.ALAW;
81 sampleSizeInBits = 8;
82 break;
83 case AuFileFormat.AU_LINEAR_8:
84 // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
85 encoding = AudioFormat.Encoding.PCM_SIGNED;
86 sampleSizeInBits = 8;
108 break;
109 case AuFileFormat.AU_ADPCM_G721:
110 encoding = new AudioFormat.G721_ADPCM;
111 sampleSizeInBits = 16;
112 break;
113 case AuFileFormat.AU_ADPCM_G723_3:
114 encoding = new AudioFormat.G723_3;
115 sampleSize = 24;
116 SamplePerUnit = 8;
117 break;
118 case AuFileFormat.AU_ADPCM_G723_5:
119 encoding = new AudioFormat.G723_5;
120 sampleSize = 40;
121 SamplePerUnit = 8;
122 break;
123 */
124 default:
125 // unsupported filetype, throw exception
126 throw new UnsupportedAudioFileException("not a valid AU file");
127 }
128
129 // Skip the variable-length annotation field. The content of this field
130 // is currently undefined by AU specification and is unsupported by
131 // JavaSound, so seek past the header
132 dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
133
134 // Even if the sampleSizeInBits and channels are supported we can get an
135 // unsupported frameSize because of overflow
136 final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
137 if (frameSize <= 0) {
138 throw new UnsupportedAudioFileException("Invalid frame size");
139 }
140
141 //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
142 //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
143 long frameLength = AudioSystem.NOT_SPECIFIED;
144 long byteLength = AudioSystem.NOT_SPECIFIED;
145 if (dataSize != AuFileFormat.UNKNOWN_SIZE) {
146 frameLength = dataSize / frameSize;
147 byteLength = dataSize + headerSize;
148 }
149 final AudioFormat format = new AudioFormat(encoding, sampleRate,
150 sampleSizeInBits, channels,
151 frameSize, sampleRate, true);
152 return new AuFileFormat(Type.AU, byteLength, format, frameLength);
153 }
154 }
|