1 /*
   2  * Copyright (c) 1999, 2015, 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;
  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 AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
  48             throws UnsupportedAudioFileException, IOException {
  49         boolean bigendian  = false;
  50         int headerSize     = -1;
  51         int dataSize       = -1;
  52         int encoding_local = -1;
  53         int sampleRate     = -1;
  54         int frameRate      = -1;
  55         int frameSize      = -1;
  56         int channels       = -1;
  57         final int sampleSizeInBits;
  58         int length = 0;
  59         int nread = 0;
  60         AudioFormat.Encoding encoding = null;
  61 
  62         DataInputStream dis = new DataInputStream( stream );
  63 
  64         final int magic = dis.readInt(); nread += 4;
  65 
  66         if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
  67             (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
  68 
  69             // not AU, throw exception
  70             throw new UnsupportedAudioFileException("not an AU file");
  71         }
  72 
  73         if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
  74             bigendian = true;        // otherwise little-endian
  75         }
  76 
  77         headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
  78         dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
  79         encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
  80         sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
  81         channels       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
  82         if (channels <= 0) {
  83             throw new UnsupportedAudioFileException("Invalid number of channels");
  84         }
  85 
  86         frameRate = sampleRate;
  87 
  88         switch (encoding_local) {
  89         case AuFileFormat.AU_ULAW_8:
  90             encoding = AudioFormat.Encoding.ULAW;
  91             sampleSizeInBits = 8;
  92             break;
  93         case AuFileFormat.AU_ALAW_8:
  94             encoding = AudioFormat.Encoding.ALAW;
  95             sampleSizeInBits = 8;
  96             break;
  97         case AuFileFormat.AU_LINEAR_8:
  98             // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
  99             encoding = AudioFormat.Encoding.PCM_SIGNED;
 100             sampleSizeInBits = 8;
 101             break;
 102         case AuFileFormat.AU_LINEAR_16:
 103             encoding = AudioFormat.Encoding.PCM_SIGNED;
 104             sampleSizeInBits = 16;
 105             break;
 106         case AuFileFormat.AU_LINEAR_24:
 107             encoding = AudioFormat.Encoding.PCM_SIGNED;
 108 
 109             sampleSizeInBits = 24;
 110             break;
 111         case AuFileFormat.AU_LINEAR_32:
 112             encoding = AudioFormat.Encoding.PCM_SIGNED;
 113 
 114             sampleSizeInBits = 32;
 115             break;
 116             // $jb: 03.19.99: we don't support these ...
 117             /*          case AuFileFormat.AU_FLOAT:
 118                         encoding = new AudioFormat.FLOAT;
 119                         sampleSizeInBits = 32;
 120                         break;
 121                         case AuFileFormat.AU_DOUBLE:
 122                         encoding = new AudioFormat.DOUBLE;
 123                         sampleSizeInBits = 8;
 124                         break;
 125                         case AuFileFormat.AU_ADPCM_G721:
 126                         encoding = new AudioFormat.G721_ADPCM;
 127                         sampleSizeInBits = 16;
 128                         break;
 129                         case AuFileFormat.AU_ADPCM_G723_3:
 130                         encoding = new AudioFormat.G723_3;
 131                         sampleSize = 24;
 132                         SamplePerUnit = 8;
 133                         break;
 134                         case AuFileFormat.AU_ADPCM_G723_5:
 135                         encoding = new AudioFormat.G723_5;
 136                         sampleSize = 40;
 137                         SamplePerUnit = 8;
 138                         break;
 139             */
 140         default:
 141                 // unsupported filetype, throw exception
 142                 throw new UnsupportedAudioFileException("not a valid AU file");
 143         }
 144 
 145         frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
 146         //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
 147         if( dataSize < 0 ) {
 148             length = AudioSystem.NOT_SPECIFIED;
 149         } else {
 150             //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
 151             length = dataSize / frameSize;
 152         }
 153         // now seek past the header
 154         dis.skipBytes(headerSize - nread);
 155         AudioFormat format = new AudioFormat(encoding, sampleRate,
 156                                              sampleSizeInBits, channels,
 157                                              frameSize, (float) frameRate,
 158                                              bigendian);
 159         return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize,
 160                                 format, length);
 161     }
 162 }