libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / sound / sampled / AudioFormat.java
blob1f31c9929d14f7968a1a3f6b5b70148b5659e487
1 /* An audio format
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)
9 any later version.
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
19 02110-1301 USA.
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.
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 gnu.java.lang.CPStringBuilder;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.Map;
47 /**
48 * This class describes an audio format, including its encoding,
49 * the number of channels, its frame rate, etc.
50 * @since 1.3
52 public class AudioFormat
54 /**
55 * This describes a given audio format encoding.
56 * @since 1.3
58 public static class Encoding
60 /** The ALAW encoding. */
61 public static final Encoding ALAW = new Encoding("alaw");
63 /** The signed PCM encoding. */
64 public static final Encoding PCM_SIGNED = new Encoding("pcm_signed");
66 /** The unsigned PCM encoding. */
67 public static final Encoding PCM_UNSIGNED = new Encoding("pcm_unsigned");
69 /** The ULAW encoding. */
70 public static final Encoding ULAW = new Encoding("ulaw");
72 private String name;
74 /**
75 * Create a new encoding descriptor, given its name.
76 * @param name the name
78 public Encoding(String name)
80 this.name = name;
83 public final boolean equals(Object o)
85 return super.equals(o);
88 public final int hashCode()
90 return super.hashCode();
93 /**
94 * Return the name of this encoding.
96 public final String toString()
98 return name;
103 * True if the audio data is stored big-endian.
105 protected boolean bigEndian;
108 * The number of channels of data in this format.
110 protected int channels;
113 * The encoding of this format.
115 protected Encoding encoding;
118 * The frame rate of this format. This is the number of frames
119 * per second.
121 protected float frameRate;
124 * The number of bytes per frame in this format.
126 protected int frameSize;
129 * The number of samples per second.
131 protected float sampleRate;
134 * The number of bits in each sample.
136 protected int sampleSizeInBits;
138 private Map<String, Object> properties;
141 * Create a new audio format, given various attributes of it.
142 * The properties map for this format will be empty.
144 * @param encoding the encoding for this format
145 * @param sampleRate the sample rate
146 * @param sampleSizeInBits the sample size, in bits
147 * @param channels the number of channels
148 * @param frameSize the frame size, in bytes
149 * @param frameRate the frame rate, in frames per second
150 * @param bigEndian true if the data is stored big-endian
152 public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
153 int channels, int frameSize, float frameRate,
154 boolean bigEndian)
156 this.encoding = encoding;
157 this.sampleRate = sampleRate;
158 this.sampleSizeInBits = sampleSizeInBits;
159 this.channels = channels;
160 this.frameSize = frameSize;
161 this.frameRate = frameRate;
162 this.bigEndian = bigEndian;
163 this.properties = Collections.<String, Object> emptyMap();
167 * Create a new audio format, given various attributes of it.
168 * The properties map is copied by this constructor, so changes
169 * to the argument Map will not affect the new object.
171 * @param encoding the encoding for this format
172 * @param sampleRate the sample rate
173 * @param sampleSizeInBits the sample size, in bits
174 * @param channels the number of channels
175 * @param frameSize the frame size, in bytes
176 * @param frameRate the frame rate, in frames per second
177 * @param bigEndian true if the data is stored big-endian
178 * @param properties a map describing properties of this format
180 public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
181 int channels, int frameSize, float frameRate,
182 boolean bigEndian, Map<String, Object> properties)
184 this.encoding = encoding;
185 this.sampleRate = sampleRate;
186 this.sampleSizeInBits = sampleSizeInBits;
187 this.channels = channels;
188 this.frameSize = frameSize;
189 this.frameRate = frameRate;
190 this.bigEndian = bigEndian;
191 this.properties = Collections.unmodifiableMap(new HashMap<String, Object>(properties));
195 * Create a new PCM-based audio format, given various attributes of it.
196 * The encoding will either be Encoding#PCM_SIGNED or Encoding#PCM_UNSIGNED.
197 * The frame size for this format will be derived from the sample size in
198 * bits and the number of channels, unless one of those is
199 * AudioSystem#NOT_SPECIFIED. The frame rate will be the same as the sample
200 * rate, and the properties map will be empty.
202 * @param sampleRate the sample rate
203 * @param sampleSizeInBits the sample size, in bits
204 * @param channels the number of channels
205 * @param signed true if this is a signed encoding
206 * @param bigEndian true if the data is stored big-endian
208 public AudioFormat(float sampleRate, int sampleSizeInBits,
209 int channels, boolean signed, boolean bigEndian)
211 this.encoding = signed ? Encoding.PCM_SIGNED : Encoding.PCM_UNSIGNED;
212 this.sampleRate = sampleRate;
213 this.sampleSizeInBits = sampleSizeInBits;
214 this.channels = channels;
215 // It isn't clear whether channels can be NOT_SPECIFIED.
216 if (sampleSizeInBits == AudioSystem.NOT_SPECIFIED
217 || channels == AudioSystem.NOT_SPECIFIED)
218 this.frameSize = AudioSystem.NOT_SPECIFIED;
219 else
220 this.frameSize = (sampleSizeInBits + 7) / 8 * channels;
221 this.frameRate = sampleRate;
222 this.bigEndian = bigEndian;
223 this.properties = Collections.<String, Object> emptyMap();
227 * Return the number of channels in this format.
229 public int getChannels()
231 return channels;
235 * Return the encoding of this format.
237 public Encoding getEncoding()
239 return encoding;
243 * Return the frame rate of this format.
245 public float getFrameRate()
247 return frameRate;
251 * Return the frame size of this format.
253 public int getFrameSize()
255 return frameSize;
259 * Given a key, return a property associated with this format;
260 * or null if this property is not set.
261 * @param key the name of the property
262 * @return the value of the property, or null if the property is not set
264 public Object getProperty(String key)
266 return properties.get(key);
270 * Return the sample rate of this format.
272 public float getSampleRate()
274 return sampleRate;
278 * Return the sample size of this format, in bits.
280 public int getSampleSizeInBits()
282 return sampleSizeInBits;
286 * Return true if this format is big endian, false otherwise.
287 * This only matters for formats whose sample size is greater than
288 * one byte.
290 public boolean isBigEndian()
292 return bigEndian;
296 * Return true if this audio format matches another.
297 * @param fmt the format to match against
298 * @return true if they match, false otherwise
300 public boolean matches(AudioFormat fmt)
302 if (! encoding.equals(fmt.encoding)
303 || channels != fmt.channels
304 || sampleSizeInBits != fmt.sampleSizeInBits
305 || frameSize != fmt.frameSize)
306 return false;
307 if (sampleRate != AudioSystem.NOT_SPECIFIED
308 && fmt.sampleRate != AudioSystem.NOT_SPECIFIED
309 && sampleRate != fmt.sampleRate)
310 return false;
311 if (frameRate != AudioSystem.NOT_SPECIFIED
312 && fmt.frameRate != AudioSystem.NOT_SPECIFIED
313 && frameRate != fmt.frameRate)
314 return false;
315 if (sampleSizeInBits > 8)
316 return bigEndian == fmt.bigEndian;
317 return true;
321 * Return a read-only Map holding the properties associated with
322 * this format.
324 public Map<String, Object> properties()
326 return properties;
330 * Return a description of this format.
332 public String toString()
334 CPStringBuilder result = new CPStringBuilder();
336 // usually at least encoding should be somewhat specified
337 result.append(encoding);
339 if (sampleRate != AudioSystem.NOT_SPECIFIED)
341 result.append(" ");
342 result.append(sampleRate);
343 result.append(" Hz");
346 if (sampleSizeInBits != AudioSystem.NOT_SPECIFIED)
348 result.append(" ");
349 result.append(sampleSizeInBits);
350 result.append(" bits");
353 if (channels != AudioSystem.NOT_SPECIFIED)
355 result.append(" ");
356 result.append(channels);
357 result.append(" channel");
358 if (channels > 1) result.append("s");
361 if (sampleSizeInBits > 8)
362 result.append(bigEndian ? " big endian" : " little endian");
364 return result.toString();