1 /*
2  * Copyright (c) 1999, 2019, 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.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
getAudioFileFormatImpl(final InputStream stream)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;
87                 break;
88             case AuFileFormat.AU_LINEAR_16:
89                 encoding = AudioFormat.Encoding.PCM_SIGNED;
90                 sampleSizeInBits = 16;
91                 break;
92             case AuFileFormat.AU_LINEAR_24:
93                 encoding = AudioFormat.Encoding.PCM_SIGNED;
94                 sampleSizeInBits = 24;
95                 break;
96             case AuFileFormat.AU_LINEAR_32:
97                 encoding = AudioFormat.Encoding.PCM_SIGNED;
98                 sampleSizeInBits = 32;
99                 break;
100             case AuFileFormat.AU_FLOAT:
101                 encoding = AudioFormat.Encoding.PCM_FLOAT;
102                 sampleSizeInBits = 32;
103                 break;
104             case AuFileFormat.AU_DOUBLE:
105                 encoding = AudioFormat.Encoding.PCM_FLOAT;
106                 sampleSizeInBits = 64;
107                 break;
108             // we don't support these ...
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 }
155