1 /*
   2  * Copyright (c) 1999, 2016, 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
  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;
  81                 break;
  82             case AuFileFormat.AU_LINEAR_16:
  83                 encoding = AudioFormat.Encoding.PCM_SIGNED;
  84                 sampleSizeInBits = 16;
  85                 break;
  86             case AuFileFormat.AU_LINEAR_24:
  87                 encoding = AudioFormat.Encoding.PCM_SIGNED;
  88                 sampleSizeInBits = 24;
  89                 break;
  90             case AuFileFormat.AU_LINEAR_32:
  91                 encoding = AudioFormat.Encoding.PCM_SIGNED;
  92                 sampleSizeInBits = 32;
  93                 break;
  94             case AuFileFormat.AU_FLOAT:
  95                 encoding = AudioFormat.Encoding.PCM_FLOAT;
  96                 sampleSizeInBits = 32;
  97                 break;
  98             // we don't support these ...
  99             /*          case AuFileFormat.AU_DOUBLE:
 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             */
 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 }