001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sound.sampled;
019:
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: public class AudioFormat {
025: public static class Encoding {
026:
027: public static final Encoding ALAW = new Encoding("ALAW"); //$NON-NLS-1$
028:
029: public static final Encoding PCM_SIGNED = new Encoding(
030: "PCM_SIGNED"); //$NON-NLS-1$
031:
032: public static final Encoding PCM_UNSIGNED = new Encoding(
033: "PCM_UNSIGNED"); //$NON-NLS-1$
034:
035: public static final Encoding ULAW = new Encoding("ULAW"); //$NON-NLS-1$
036:
037: private String name;
038:
039: public Encoding(String name) {
040: this .name = name;
041: }
042:
043: @Override
044: public final boolean equals(Object another) {
045: if (this == another) {
046: return true;
047: }
048:
049: if (another == null || !(another instanceof Encoding)) {
050: return false;
051: }
052:
053: Encoding obj = (Encoding) another;
054: return name == null ? obj.name == null : name
055: .equals(obj.name);
056: }
057:
058: @Override
059: public final int hashCode() {
060: return name == null ? 0 : name.hashCode();
061: }
062:
063: @Override
064: public final String toString() {
065: return name;
066: }
067: }
068:
069: protected boolean bigEndian;
070:
071: protected int channels;
072:
073: protected Encoding encoding;
074:
075: protected float frameRate;
076:
077: protected int frameSize;
078:
079: protected float sampleRate;
080:
081: protected int sampleSizeInBits;
082:
083: private HashMap<String, Object> prop;
084:
085: public AudioFormat(AudioFormat.Encoding encoding, float sampleRate,
086: int sampleSizeInBits, int channels, int frameSize,
087: float frameRate, boolean bigEndian) {
088:
089: this .encoding = encoding;
090: this .sampleRate = sampleRate;
091: this .sampleSizeInBits = sampleSizeInBits;
092: this .channels = channels;
093: this .frameSize = frameSize;
094: this .frameRate = frameRate;
095: this .bigEndian = bigEndian;
096:
097: }
098:
099: public AudioFormat(AudioFormat.Encoding encoding, float sampleRate,
100: int sampleSizeInBits, int channels, int frameSize,
101: float frameRate, boolean bigEndian,
102: Map<String, Object> properties) {
103:
104: this .encoding = encoding;
105: this .sampleRate = sampleRate;
106: this .sampleSizeInBits = sampleSizeInBits;
107: this .channels = channels;
108: this .frameSize = frameSize;
109: this .frameRate = frameRate;
110: this .bigEndian = bigEndian;
111: prop = new HashMap<String, Object>();
112: prop.putAll(properties);
113:
114: }
115:
116: public AudioFormat(float sampleRate, int sampleSizeInBits,
117: int channels, boolean signed, boolean bigEndian) {
118:
119: this .encoding = (signed ? Encoding.PCM_SIGNED
120: : Encoding.PCM_UNSIGNED);
121: this .sampleRate = sampleRate;
122: this .sampleSizeInBits = sampleSizeInBits;
123: this .channels = channels;
124: this .frameSize = sampleSizeInBits >> 3;
125: if ((sampleSizeInBits & 0x7) != 0) {
126: this .frameSize++;
127: }
128: this .frameSize *= channels;
129: this .frameRate = sampleRate;
130: this .bigEndian = bigEndian;
131:
132: }
133:
134: public Encoding getEncoding() {
135: return encoding;
136: }
137:
138: public float getSampleRate() {
139: return sampleRate;
140: }
141:
142: public int getSampleSizeInBits() {
143: return sampleSizeInBits;
144: }
145:
146: public int getChannels() {
147: return channels;
148: }
149:
150: public int getFrameSize() {
151: return frameSize;
152: }
153:
154: public float getFrameRate() {
155: return frameRate;
156: }
157:
158: public boolean isBigEndian() {
159: return bigEndian;
160: }
161:
162: public Map<String, Object> properties() {
163: if (prop != null) {
164: return Collections.unmodifiableMap(prop);
165: } else {
166: return Collections.emptyMap();
167: }
168: }
169:
170: public Object getProperty(String key) {
171: if (prop == null) {
172: return null;
173: }
174: return prop.get(key);
175: }
176:
177: public boolean matches(AudioFormat format) {
178: if (!encoding.equals(format.getEncoding())
179: || channels != format.getChannels()
180: || sampleSizeInBits != format.getSampleSizeInBits()
181: || frameSize != format.getFrameSize()) {
182: return false;
183: }
184: if (format.getSampleRate() != AudioSystem.NOT_SPECIFIED
185: && sampleRate != format.getSampleRate()) {
186: return false;
187: }
188:
189: if (format.getFrameRate() != AudioSystem.NOT_SPECIFIED
190: && frameRate != format.getFrameRate()) {
191: return false;
192: }
193:
194: if ((sampleSizeInBits > 8)
195: && (bigEndian != format.isBigEndian())) {
196: return false;
197: }
198: return true;
199:
200: }
201:
202: public String toString() {
203:
204: String ch;
205: switch (channels) {
206: case 1:
207: ch = "mono,"; //$NON-NLS-1$
208: break;
209: case 2:
210: ch = "stereo,"; //$NON-NLS-1$
211: default:
212: ch = channels + " channels, "; //$NON-NLS-1$
213: }
214:
215: return encoding
216: + " " + sampleRate + " Hz, " + sampleSizeInBits + " bit, " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
217: + ch + frameSize
218: + " bytes/frame, " + frameRate + " frames/second"; //$NON-NLS-1$ //$NON-NLS-2$
219: }
220:
221: }
|