1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/* An audio format
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.sound.sampled;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* This class describes an audio format, including its encoding,
* the number of channels, its frame rate, etc.
* @since 1.3
*/
public class AudioFormat
{
/**
* This describes a given audio format encoding.
* @since 1.3
*/
public static class Encoding
{
/** The ALAW encoding. */
public static final Encoding ALAW = new Encoding("alaw");
/** The signed PCM encoding. */
public static final Encoding PCM_SIGNED = new Encoding("pcm_signed");
/** The unsigned PCM encoding. */
public static final Encoding PCM_UNSIGNED = new Encoding("pcm_unsigned");
/** The ULAW encoding. */
public static final Encoding ULAW = new Encoding("ulaw");
private String name;
/**
* Create a new encoding descriptor, given its name.
* @param name the name
*/
public Encoding(String name)
{
this.name = name;
}
public final boolean equals(Object o)
{
return super.equals(o);
}
public final int hashCode()
{
return super.hashCode();
}
/**
* Return the name of this encoding.
*/
public final String toString()
{
return name;
}
}
/**
* True if the audio data is stored big-endian.
*/
protected boolean bigEndian;
/**
* The number of channels of data in this format.
*/
protected int channels;
/**
* The encoding of this format.
*/
protected Encoding encoding;
/**
* The frame rate of this format. This is the number of frames
* per second.
*/
protected float frameRate;
/**
* The number of bytes per frame in this format.
*/
protected int frameSize;
/**
* The number of samples per second.
*/
protected float sampleRate;
/**
* The number of bits in each sample.
*/
protected int sampleSizeInBits;
private Map<String, Object> properties;
/**
* Create a new audio format, given various attributes of it.
* The properties map for this format will be empty.
*
* @param encoding the encoding for this format
* @param sampleRate the sample rate
* @param sampleSizeInBits the sample size, in bits
* @param channels the number of channels
* @param frameSize the frame size, in bytes
* @param frameRate the frame rate, in frames per second
* @param bigEndian true if the data is stored big-endian
*/
public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
int channels, int frameSize, float frameRate,
boolean bigEndian)
{
this.encoding = encoding;
this.sampleRate = sampleRate;
this.sampleSizeInBits = sampleSizeInBits;
this.channels = channels;
this.frameSize = frameSize;
this.frameRate = frameRate;
this.bigEndian = bigEndian;
this.properties = Collections.<String, Object> emptyMap();
}
/**
* Create a new audio format, given various attributes of it.
* The properties map is copied by this constructor, so changes
* to the argument Map will not affect the new object.
*
* @param encoding the encoding for this format
* @param sampleRate the sample rate
* @param sampleSizeInBits the sample size, in bits
* @param channels the number of channels
* @param frameSize the frame size, in bytes
* @param frameRate the frame rate, in frames per second
* @param bigEndian true if the data is stored big-endian
* @param properties a map describing properties of this format
*/
public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
int channels, int frameSize, float frameRate,
boolean bigEndian, Map<String, Object> properties)
{
this.encoding = encoding;
this.sampleRate = sampleRate;
this.sampleSizeInBits = sampleSizeInBits;
this.channels = channels;
this.frameSize = frameSize;
this.frameRate = frameRate;
this.bigEndian = bigEndian;
this.properties = Collections.unmodifiableMap(new HashMap<String, Object>(properties));
}
/**
* Create a new PCM-based audio format, given various attributes of it.
* The encoding will either be Encoding#PCM_SIGNED or Encoding#PCM_UNSIGNED.
* The frame size for this format will be derived from the sample size in
* bits and the number of channels, unless one of those is
* AudioSystem#NOT_SPECIFIED. The frame rate will be the same as the sample
* rate, and the properties map will be empty.
*
* @param sampleRate the sample rate
* @param sampleSizeInBits the sample size, in bits
* @param channels the number of channels
* @param signed true if this is a signed encoding
* @param bigEndian true if the data is stored big-endian
*/
public AudioFormat(float sampleRate, int sampleSizeInBits,
int channels, boolean signed, boolean bigEndian)
{
this.encoding = signed ? Encoding.PCM_SIGNED : Encoding.PCM_UNSIGNED;
this.sampleRate = sampleRate;
this.sampleSizeInBits = sampleSizeInBits;
this.channels = channels;
// It isn't clear whether channels can be NOT_SPECIFIED.
if (sampleSizeInBits == AudioSystem.NOT_SPECIFIED
|| channels == AudioSystem.NOT_SPECIFIED)
this.frameSize = AudioSystem.NOT_SPECIFIED;
else
this.frameSize = (sampleSizeInBits + 7) / 8 * channels;
this.frameRate = sampleRate;
this.bigEndian = bigEndian;
this.properties = Collections.<String, Object> emptyMap();
}
/**
* Return the number of channels in this format.
*/
public int getChannels()
{
return channels;
}
/**
* Return the encoding of this format.
*/
public Encoding getEncoding()
{
return encoding;
}
/**
* Return the frame rate of this format.
*/
public float getFrameRate()
{
return frameRate;
}
/**
* Return the frame size of this format.
*/
public int getFrameSize()
{
return frameSize;
}
/**
* Given a key, return a property associated with this format;
* or null if this property is not set.
* @param key the name of the property
* @return the value of the property, or null if the property is not set
*/
public Object getProperty(String key)
{
return properties.get(key);
}
/**
* Return the sample rate of this format.
*/
public float getSampleRate()
{
return sampleRate;
}
/**
* Return the sample size of this format, in bits.
*/
public int getSampleSizeInBits()
{
return sampleSizeInBits;
}
/**
* Return true if this format is big endian, false otherwise.
* This only matters for formats whose sample size is greater than
* one byte.
*/
public boolean isBigEndian()
{
return bigEndian;
}
/**
* Return true if this audio format matches another.
* @param fmt the format to match against
* @return true if they match, false otherwise
*/
public boolean matches(AudioFormat fmt)
{
if (! encoding.equals(fmt.encoding)
|| channels != fmt.channels
|| sampleSizeInBits != fmt.sampleSizeInBits
|| frameSize != fmt.frameSize)
return false;
if (sampleRate != AudioSystem.NOT_SPECIFIED
&& fmt.sampleRate != AudioSystem.NOT_SPECIFIED
&& sampleRate != fmt.sampleRate)
return false;
if (frameRate != AudioSystem.NOT_SPECIFIED
&& fmt.frameRate != AudioSystem.NOT_SPECIFIED
&& frameRate != fmt.frameRate)
return false;
if (sampleSizeInBits > 8)
return bigEndian == fmt.bigEndian;
return true;
}
/**
* Return a read-only Map holding the properties associated with
* this format.
*/
public Map<String, Object> properties()
{
return properties;
}
/**
* Return a description of this format.
*/
public String toString()
{
StringBuffer result = new StringBuffer();
result.append(encoding);
result.append(" ");
result.append(sampleRate);
result.append(" Hz ");
result.append(sampleSizeInBits);
result.append(" bits ");
result.append(channels);
result.append(" channels");
if (sampleSizeInBits > 8)
result.append(bigEndian ? " big endian" : " little endian");
return result.toString();
}
}
|