Source for javax.sound.sampled.AudioFileFormat

   1: /* Audio file format
   2:    Copyright (C) 2005 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: 
  39: package javax.sound.sampled;
  40: 
  41: import java.util.Collections;
  42: import java.util.HashMap;
  43: import java.util.Map;
  44: 
  45: /**
  46:  * This describes an audio file, including information about its length,
  47:  * the format of the audio data, and other things.
  48:  * @since 1.3
  49:  */
  50: public class AudioFileFormat
  51: {
  52:   /**
  53:    * An instance of this type describes a standard audio file format.
  54:    * @since 1.3
  55:    */
  56:   public static class Type
  57:   {
  58:     // This is kind of goofy since there are multiple extensions for
  59:     // some of these.
  60: 
  61:     /** The AIFC format.  */
  62:     public static final Type AIFC = new Type("AIFC", "aifc");
  63: 
  64:     /** The AIFF format.  */
  65:     public static final Type AIFF = new Type("AIFF", "aiff");
  66: 
  67:     /** The AU format.  */
  68:     public static final Type AU = new Type("AU", "au");
  69: 
  70:     /** The SND format.  */
  71:     public static final Type SND = new Type("SND", "snd");
  72: 
  73:     /** The WAVE format.  */
  74:     public static final Type WAVE = new Type ("WAVE", "wav");
  75: 
  76:     private String name;
  77:     private String extension;
  78: 
  79:     /**
  80:      * Create a new Type given its name and file extension.
  81:      * The file extension does not include the ".".
  82:      * @param name the type's name
  83:      * @param extension the file extension
  84:      */
  85:     public Type(String name, String extension)
  86:     {
  87:       this.name = name;
  88:       this.extension = extension;
  89:     }
  90: 
  91:     public final boolean equals(Object o)
  92:     {
  93:       if (! (o instanceof Type))
  94:         return false;
  95:       Type other = (Type) o;
  96:       return name.equals(other.name) && extension.equals(other.extension);
  97:     }
  98: 
  99:     public final int hashCode()
 100:     {
 101:       return name.hashCode() + extension.hashCode();
 102:     }
 103: 
 104:     /**
 105:      * Return the extension associated with this Type.
 106:      */
 107:     public String getExtension()
 108:     {
 109:       return extension;
 110:     }
 111: 
 112:     /**
 113:      * Return the name of this Type.
 114:      */
 115:     public final String toString()
 116:     {
 117:       return name;
 118:     }
 119:   }
 120: 
 121:   private int byteLength;
 122:   private AudioFormat format;
 123:   private Type type;
 124:   private int frameLength;
 125:   private Map<String, Object> properties;
 126: 
 127:   /**
 128:    * Create a new AudioFileFormat given the type, the format, and the
 129:    * frame length.  The new object will have an unspecified byte length,
 130:    * and an empty properties map.
 131:    * @param type the type
 132:    * @param fmt the format
 133:    * @param frameLen the frame length
 134:    */
 135:   public AudioFileFormat(Type type, AudioFormat fmt, int frameLen)
 136:   {
 137:     this.byteLength = AudioSystem.NOT_SPECIFIED;
 138:     this.format = fmt;
 139:     this.type = type;
 140:     this.frameLength = frameLen;
 141:     this.properties = Collections.<String, Object> emptyMap();
 142:   }
 143: 
 144:   /**
 145:    * Create a new AudioFileFormat given the type, the format, the
 146:    * frame length, and some properties.  The new object will have an
 147:    * unspecified byte length.  A copy of the properties argument will
 148:    * be made, so changes to the map passed in will not affect the
 149:    * new AudioFileFormat.
 150:    * @param type the type
 151:    * @param fmt the format
 152:    * @param frameLen the frame length
 153:    * @param properties the properties
 154:    */
 155:   public AudioFileFormat(Type type, AudioFormat fmt, int frameLen,
 156:                          Map<String, Object> properties)
 157:   {
 158:     this.byteLength = AudioSystem.NOT_SPECIFIED;
 159:     this.format = fmt;
 160:     this.type = type;
 161:     this.frameLength = frameLen;
 162:     this.properties = Collections.unmodifiableMap(new HashMap<String, Object>(properties));
 163:   }
 164: 
 165:   /**
 166:    * Create a new AudioFileFormat given the type, the byte length, the format,
 167:    * and the frame length.  The new object will have an empty properties map.
 168:    * @param type the type
 169:    * @param byteLen the byte length
 170:    * @param fmt the format
 171:    * @param frameLen the frame length
 172:    */
 173:   protected AudioFileFormat(Type type, int byteLen, AudioFormat fmt,
 174:                             int frameLen)
 175:   {
 176:     this.byteLength = byteLen;
 177:     this.format = fmt;
 178:     this.type = type;
 179:     this.frameLength = frameLen;
 180:     this.properties = Collections.<String, Object> emptyMap();
 181:   }
 182: 
 183:   /**
 184:    * Return the byte length of this file format.
 185:    */
 186:   public int getByteLength()
 187:   {
 188:     return byteLength;
 189:   }
 190: 
 191:   /**
 192:    * Return the AudioFormat associated with this file format.
 193:    */
 194:   public AudioFormat getFormat()
 195:   {
 196:     return format;
 197:   }
 198: 
 199:   /**
 200:    * Return the frame length of this file format.
 201:    */
 202:   public int getFrameLength()
 203:   {
 204:     return frameLength;
 205:   }
 206: 
 207:   /**
 208:    * Return the value of a property defined in this format.
 209:    * @param key the property name
 210:    * @return the value of the property, or null if the property is not defined
 211:    */
 212:   public Object getProperty(String key)
 213:   {
 214:     return properties.get(key);
 215:   }
 216: 
 217:   /**
 218:    * Return the Type associated with this file format.
 219:    */
 220:   public Type getType()
 221:   {
 222:     return type;
 223:   }
 224: 
 225:   /**
 226:    * Return the properties associated with this format, as a Map.
 227:    * The returned Map is unmodifiable.
 228:    */
 229:   public Map<String, Object> properties()
 230:   {
 231:     return properties;
 232:   }
 233: 
 234:   /**
 235:    * Return a description of this AudioFileFormat.
 236:    */
 237:   public String toString()
 238:   {
 239:     return ("byteLength=" + byteLength + "; format=" + format
 240:             + "; type=" + type + "; frameLength=" + frameLength);
 241:   }
 242: }