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; 33 import javax.sound.sampled.AudioFileFormat.Type; 34 import javax.sound.sampled.AudioFormat; 35 import javax.sound.sampled.AudioSystem; 36 import javax.sound.sampled.UnsupportedAudioFileException; 37 38 /** 39 * AU file reader. 40 * 41 * @author Kara Kytle 42 * @author Jan Borgersen 43 * @author Florian Bomers 44 */ 45 public final class AuFileReader extends SunFileReader { 46 47 @Override 48 public AudioFileFormat getAudioFileFormatImpl(final InputStream stream) 49 throws UnsupportedAudioFileException, IOException { 50 final DataInputStream dis = new DataInputStream(stream); 51 final int magic = dis.readInt(); 52 53 if (magic != AuFileFormat.AU_SUN_MAGIC) { 54 // not AU, throw exception 55 throw new UnsupportedAudioFileException("not an AU file"); 56 } 57 58 final int headerSize = dis.readInt(); 59 final int dataSize = dis.readInt(); 60 final int auType = dis.readInt(); 61 final int sampleRate = dis.readInt(); 62 final int channels = dis.readInt(); 63 if (channels <= 0) { 64 throw new UnsupportedAudioFileException("Invalid number of channels"); 65 } 66 67 final int sampleSizeInBits; 68 final AudioFormat.Encoding encoding; 69 switch (auType) { 70 case AuFileFormat.AU_ULAW_8: 71 encoding = AudioFormat.Encoding.ULAW; 72 sampleSizeInBits = 8; 73 break; 74 case AuFileFormat.AU_ALAW_8: 75 encoding = AudioFormat.Encoding.ALAW; 76 sampleSizeInBits = 8; 77 break; 78 case AuFileFormat.AU_LINEAR_8: 79 // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned* 103 break; 104 case AuFileFormat.AU_ADPCM_G721: 105 encoding = new AudioFormat.G721_ADPCM; 106 sampleSizeInBits = 16; 107 break; 108 case AuFileFormat.AU_ADPCM_G723_3: 109 encoding = new AudioFormat.G723_3; 110 sampleSize = 24; 111 SamplePerUnit = 8; 112 break; 113 case AuFileFormat.AU_ADPCM_G723_5: 114 encoding = new AudioFormat.G723_5; 115 sampleSize = 40; 116 SamplePerUnit = 8; 117 break; 118 */ 119 default: 120 // unsupported filetype, throw exception 121 throw new UnsupportedAudioFileException("not a valid AU file"); 122 } 123 124 final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels); 125 //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files 126 final int length; 127 if (dataSize < 0) { 128 length = AudioSystem.NOT_SPECIFIED; 129 } else { 130 //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED 131 length = dataSize / frameSize; 132 } 133 // now seek past the header 134 dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE); 135 final AudioFormat format = new AudioFormat(encoding, sampleRate, 136 sampleSizeInBits, channels, 137 frameSize, sampleRate, true); 138 return new AuFileFormat(Type.AU, dataSize + headerSize, format, length); 139 } 140 } | 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 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* 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 } |