Source for gnu.javax.sound.sampled.AU.AUReader

   1: /* AUReader.java -- Read AU files.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.sound.sampled.AU;
  39: 
  40: import javax.sound.sampled.AudioFormat;
  41: import javax.sound.sampled.AudioSystem;
  42: import javax.sound.sampled.AudioFileFormat;
  43: import javax.sound.sampled.AudioInputStream;
  44: import javax.sound.sampled.UnsupportedAudioFileException;
  45: import javax.sound.sampled.spi.AudioFileReader;
  46: import java.io.File;
  47: import java.io.IOException;
  48: import java.io.BufferedInputStream;
  49: import java.io.InputStream;
  50: import java.io.DataInputStream;
  51: import java.io.FileInputStream;
  52: import java.net.URL;
  53: import java.nio.ByteBuffer;
  54: 
  55: public class AUReader extends AudioFileReader
  56: {
  57:   private static class AUHeader
  58:   {
  59:     // Magic number identifying the file. '.snd' 
  60:     private static final int MAGIC = 0x2e736e64;
  61:     
  62:     public static final int SIZE = 24; // size of the header
  63:     
  64:     // Encoding types
  65:     public static final int ULAW = 1; // 8-bit u-law
  66:     public static final int PCM8 = 2; // 8-bit PCM
  67:     public static final int PCM16 = 3; // 16-bit PCM
  68:     public static final int PCM24 = 4; // 24-bit PCM
  69:     public static final int PCM32 = 5; // 32-bit PCM
  70:     public static final int IEEE32 = 6; // 32-bit IEEE f.p.
  71:     public static final int IEEE64 = 7; // 64-bit IEEE f.p.
  72:     public static final int G721 = 23; 
  73:     public static final int G722 = 24; 
  74:     public static final int G723 = 25; 
  75:     public static final int G723_5BIT = 26; 
  76:     public static final int ALAW = 27; // 8-bit a-law
  77: 
  78:     // Header data.
  79:     public int headerSize;
  80:     public int fileSize; // this value may not be set.
  81:     public int encoding;
  82:     public int sampleRate;
  83:     public int channels;
  84:     public int sampleSizeInBits;
  85: 
  86:     public AUHeader(InputStream stream)
  87:       throws IOException, UnsupportedAudioFileException
  88:     {
  89:       byte[] hdr = new byte[24];
  90:       stream.read( hdr );
  91:       ByteBuffer buf = ByteBuffer.wrap(hdr);
  92: 
  93:       if( buf.getInt() != MAGIC )
  94:     throw new UnsupportedAudioFileException("Not an AU format audio file.");
  95:       headerSize = buf.getInt(); 
  96:       fileSize = buf.getInt(); 
  97:       encoding = buf.getInt(); 
  98:       sampleRate = buf.getInt();
  99:       channels = buf.getInt(); 
 100: 
 101:       switch(encoding)
 102:     {
 103:     case ULAW: 
 104:     case PCM8: 
 105:     case ALAW: 
 106:       sampleSizeInBits = 8;
 107:       break;
 108:     case PCM16:
 109:       sampleSizeInBits = 16;
 110:       break;
 111:     case PCM24:
 112:       sampleSizeInBits = 24;
 113:       break;
 114:     case PCM32:
 115:       sampleSizeInBits = 32;
 116:       break;
 117:     default:   // other types exist but are not supported. Yet.
 118:       throw new UnsupportedAudioFileException("Unsupported encoding.");
 119:     }
 120:     }
 121: 
 122:   public AudioFormat getAudioFormat()
 123:     {
 124:       AudioFormat.Encoding encType = AudioFormat.Encoding.PCM_SIGNED;
 125:       if(encoding == 1)
 126:     encType = AudioFormat.Encoding.ULAW;
 127:       if(encoding == 27)
 128:     encType = AudioFormat.Encoding.ALAW;
 129:       
 130:       return new AudioFormat(encType, 
 131:                  (float)sampleRate, 
 132:                  sampleSizeInBits, 
 133:                  channels, 
 134:                  (sampleSizeInBits >> 3) * channels, 
 135:                  (float)sampleRate, 
 136:                  true);
 137:     }
 138: 
 139:   public AudioFileFormat getAudioFileFormat()
 140:     {
 141:       return new AudioFileFormat(new AUFormatType(), 
 142:                  getAudioFormat(), 
 143:                  AudioSystem.NOT_SPECIFIED);
 144:     }
 145:   }
 146: 
 147:   public static class AUFormatType extends AudioFileFormat.Type
 148:   {
 149:     public AUFormatType()
 150:     {
 151:       super("AU", ".au");
 152:     }
 153:   }
 154: 
 155:   public AudioFileFormat getAudioFileFormat(File file)
 156:     throws IOException, UnsupportedAudioFileException
 157:   {
 158:     return getAudioFileFormat(new FileInputStream(file));
 159:   }
 160: 
 161:   public AudioFileFormat getAudioFileFormat(InputStream stream)
 162:     throws IOException, UnsupportedAudioFileException
 163:   {
 164:     if(!stream.markSupported()) 
 165:       throw new IOException("Stream must support marking.");    
 166: 
 167:     stream.mark(25);
 168:     AUHeader header = new AUHeader(stream);
 169:     stream.reset();
 170: 
 171:     return header.getAudioFileFormat();
 172:   }
 173:   
 174:   public AudioFileFormat getAudioFileFormat(URL url)
 175:     throws IOException, UnsupportedAudioFileException
 176:   {        
 177:     return getAudioFileFormat(new BufferedInputStream(url.openStream()));
 178:   }
 179: 
 180:   public AudioInputStream getAudioInputStream(File file)
 181:     throws IOException, UnsupportedAudioFileException
 182:   {
 183:     InputStream stream = new FileInputStream(file);
 184:     long length = file.length();
 185: 
 186:     AUHeader header = new AUHeader( stream );
 187:     if( header.headerSize > AUHeader.SIZE )
 188:       stream.skip(header.headerSize - AUHeader.SIZE);
 189: 
 190:     length -= header.headerSize;
 191: 
 192:     return new AudioInputStream(stream, header.getAudioFormat(), length);
 193:   }
 194: 
 195:   public AudioInputStream getAudioInputStream(InputStream stream)
 196:     throws IOException, UnsupportedAudioFileException
 197:   {
 198:     AUHeader header = new AUHeader( stream );
 199:     if( header.headerSize > AUHeader.SIZE )
 200:       stream.skip(header.headerSize - AUHeader.SIZE);
 201: 
 202:     return new AudioInputStream(stream, header.getAudioFormat(), 
 203:                 AudioSystem.NOT_SPECIFIED);
 204:   }
 205: 
 206:   public AudioInputStream getAudioInputStream(URL url)
 207:     throws IOException, UnsupportedAudioFileException
 208:   {
 209:     return getAudioInputStream(new BufferedInputStream(url.openStream()));
 210:   }
 211: }