2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package javax.sound.sampled;
41 import java.util.Collections;
42 import java.util.HashMap;
46 * This class describes an audio format, including its encoding,
47 * the number of channels, its frame rate, etc.
50 public class AudioFormat
53 * This describes a given audio format encoding.
56 public static class Encoding
58 /** The ALAW encoding. */
59 public static final Encoding ALAW = new Encoding("alaw");
61 /** The signed PCM encoding. */
62 public static final Encoding PCM_SIGNED = new Encoding("pcm_signed");
64 /** The unsigned PCM encoding. */
65 public static final Encoding PCM_UNSIGNED = new Encoding("pcm_unsigned");
67 /** The ULAW encoding. */
68 public static final Encoding ULAW = new Encoding("ulaw");
73 * Create a new encoding descriptor, given its name.
74 * @param name the name
76 public Encoding(String name)
81 public final boolean equals(Object o)
83 return super.equals(o);
86 public final int hashCode()
88 return super.hashCode();
92 * Return the name of this encoding.
94 public final String toString()
101 * True if the audio data is stored big-endian.
103 protected boolean bigEndian;
106 * The number of channels of data in this format.
108 protected int channels;
111 * The encoding of this format.
113 protected Encoding encoding;
116 * The frame rate of this format. This is the number of frames
119 protected float frameRate;
122 * The number of bytes per frame in this format.
124 protected int frameSize;
127 * The number of samples per second.
129 protected float sampleRate;
132 * The number of bits in each sample.
134 protected int sampleSizeInBits;
136 private Map properties;
139 * Create a new audio format, given various attributes of it.
140 * The properties map for this format will be empty.
142 * @param encoding the encoding for this format
143 * @param sampleRate the sample rate
144 * @param sampleSizeInBits the sample size, in bits
145 * @param channels the number of channels
146 * @param frameSize the frame size, in bytes
147 * @param frameRate the frame rate, in frames per second
148 * @param bigEndian true if the data is stored big-endian
150 public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
151 int channels, int frameSize, float frameRate,
154 this.encoding = encoding;
155 this.sampleRate = sampleRate;
156 this.sampleSizeInBits = sampleSizeInBits;
157 this.channels = channels;
158 this.frameSize = frameSize;
159 this.frameRate = frameRate;
160 this.bigEndian = bigEndian;
161 this.properties = Collections.EMPTY_MAP;
165 * Create a new audio format, given various attributes of it.
166 * The properties map is copied by this constructor, so changes
167 * to the argument Map will not affect the new object.
169 * @param encoding the encoding for this format
170 * @param sampleRate the sample rate
171 * @param sampleSizeInBits the sample size, in bits
172 * @param channels the number of channels
173 * @param frameSize the frame size, in bytes
174 * @param frameRate the frame rate, in frames per second
175 * @param bigEndian true if the data is stored big-endian
176 * @param properties a map describing properties of this format
178 public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
179 int channels, int frameSize, float frameRate,
180 boolean bigEndian, Map<String, Object> properties)
182 this.encoding = encoding;
183 this.sampleRate = sampleRate;
184 this.sampleSizeInBits = sampleSizeInBits;
185 this.channels = channels;
186 this.frameSize = frameSize;
187 this.frameRate = frameRate;
188 this.bigEndian = bigEndian;
189 this.properties = Collections.unmodifiableMap(new HashMap(properties));
193 * Create a new PCM-based audio format, given various attributes of it.
194 * The encoding will either be Encoding#PCM_SIGNED or Encoding#PCM_UNSIGNED.
195 * The frame size for this format will be derived from the sample size in
196 * bits and the number of channels, unless one of those is
197 * AudioSystem#NOT_SPECIFIED. The frame rate will be the same as the sample
198 * rate, and the properties map will be empty.
200 * @param sampleRate the sample rate
201 * @param sampleSizeInBits the sample size, in bits
202 * @param channels the number of channels
203 * @param signed true if this is a signed encoding
204 * @param bigEndian true if the data is stored big-endian
206 public AudioFormat(float sampleRate, int sampleSizeInBits,
207 int channels, boolean signed, boolean bigEndian)
209 this.encoding = signed ? Encoding.PCM_SIGNED : Encoding.PCM_UNSIGNED;
210 this.sampleRate = sampleRate;
211 this.sampleSizeInBits = sampleSizeInBits;
212 this.channels = channels;
213 // It isn't clear whether channels can be NOT_SPECIFIED.
214 if (sampleSizeInBits == AudioSystem.NOT_SPECIFIED
215 || channels == AudioSystem.NOT_SPECIFIED)
216 this.frameSize = AudioSystem.NOT_SPECIFIED;
218 this.frameSize = (sampleSizeInBits + 7) / 8 * channels;
219 this.frameRate = sampleRate;
220 this.bigEndian = bigEndian;
221 this.properties = Collections.EMPTY_MAP;
225 * Return the number of channels in this format.
227 public int getChannels()
233 * Return the encoding of this format.
235 public Encoding getEncoding()
241 * Return the frame rate of this format.
243 public float getFrameRate()
249 * Return the frame size of this format.
251 public int getFrameSize()
257 * Given a key, return a property associated with this format;
258 * or null if this property is not set.
259 * @param key the name of the property
260 * @return the value of the property, or null if the property is not set
262 public Object getProperty(String key)
264 return properties.get(key);
268 * Return the sample rate of this format.
270 public float getSampleRate()
276 * Return the sample size of this format, in bits.
278 public int getSampleSizeInBits()
280 return sampleSizeInBits;
284 * Return true if this format is big endian, false otherwise.
285 * This only matters for formats whose sample size is greater than
288 public boolean isBigEndian()
294 * Return true if this audio format matches another.
295 * @param fmt the format to match against
296 * @return true if they match, false otherwise
298 public boolean matches(AudioFormat fmt)
300 if (! encoding.equals(fmt.encoding)
301 || channels != fmt.channels
302 || sampleSizeInBits != fmt.sampleSizeInBits
303 || frameSize != fmt.frameSize)
305 if (sampleRate != AudioSystem.NOT_SPECIFIED
306 && fmt.sampleRate != AudioSystem.NOT_SPECIFIED
307 && sampleRate != fmt.sampleRate)
309 if (frameRate != AudioSystem.NOT_SPECIFIED
310 && fmt.frameRate != AudioSystem.NOT_SPECIFIED
311 && frameRate != fmt.frameRate)
313 if (sampleSizeInBits > 8)
314 return bigEndian == fmt.bigEndian;
319 * Return a read-only Map holding the properties associated with
322 public Map<String, Object> properties()
328 * Return a description of this format.
330 public String toString()
332 StringBuffer result = new StringBuffer();
333 result.append(encoding);
335 result.append(sampleRate);
336 result.append(" Hz ");
337 result.append(sampleSizeInBits);
338 result.append(" bits ");
339 result.append(channels);
340 result.append(" channels");
341 if (sampleSizeInBits > 8)
342 result.append(bigEndian ? " big endian" : " little endian");
343 return result.toString();