View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *  
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package entagged.audioformats;
20  
21  import java.util.Enumeration;
22  import java.util.Hashtable;
23  
24  /***
25   * This class represents a structure for storing and retrieving information
26   * about the codec respectively the encoding parameters.<br>
27   * Most of the parameters are available for nearly each audio format. Some
28   * others would result in standard values.<br>
29   * <b>Consider:</b> None of the setter methods will actually affect the audio
30   * file. This is just a structure for retrieving information, not manipulating
31   * the audio file.<br>
32   * 
33   * @author Raphael Slinckx
34   */
35  public class EncodingInfo {
36  
37  	/***
38  	 * The key for the Bitrate.({@link Integer})<br>
39  	 * 
40  	 * @see #content
41  	 */
42  	public final static String FIELD_BITRATE = "BITRATE";
43  
44  	/***
45  	 * The key for the number of audio channels.({@link Integer})<br>
46  	 * 
47  	 * @see #content
48  	 */
49  	public final static String FIELD_CHANNEL = "CHANNB";
50  
51  	/***
52  	 * The key for the extra encoding information.({@link String})<br>
53  	 * 
54  	 * @see #content
55  	 */
56  	public final static String FIELD_INFOS = "INFOS";
57  
58  	/***
59  	 * The key for the audio clip duration in seconds. ({@link java.lang.Float})<br>
60  	 * 
61  	 * @see #content
62  	 */
63  	public final static String FIELD_LENGTH = "LENGTH";
64  
65  	/***
66  	 * The key for the audio sample rate in &quot;Hz&quot;. ({@link Integer})<br>
67  	 * 
68  	 * @see #content
69  	 */
70  	public final static String FIELD_SAMPLERATE = "SAMPLING";
71  
72  	/***
73  	 * The key for the audio type.({@link String})<br>
74  	 * 
75  	 * @see #content
76  	 */
77  	public final static String FIELD_TYPE = "TYPE";
78  
79  	/***
80  	 * The key for the VBR flag. ({@link Boolean})<br>
81  	 * 
82  	 * @see #content
83  	 */
84  	public final static String FIELD_VBR = "VBR";
85  
86  	/***
87  	 * This table containts the parameters.<br>
88  	 */
89  	private Hashtable content;
90  
91  	/***
92  	 * Creates an instance with emtpy values.<br>
93  	 */
94  	public EncodingInfo() {
95  		content = new Hashtable(6);
96  		content.put(FIELD_BITRATE, new Integer(-1));
97  		content.put(FIELD_CHANNEL, new Integer(-1));
98  		content.put(FIELD_TYPE, "");
99  		content.put(FIELD_INFOS, "");
100 		content.put(FIELD_SAMPLERATE, new Integer(-1));
101 		content.put(FIELD_LENGTH, new Float(-1));
102 		content.put(FIELD_VBR, new Boolean(true));
103 	}
104 
105 	/***
106 	 * This method returns the bitrate of the represented audio clip in
107 	 * &quot;Kbps&quot;.<br>
108 	 * 
109 	 * @return The bitrate in Kbps.
110 	 */
111 	public int getBitrate() {
112 		return ((Integer) content.get(FIELD_BITRATE)).intValue();
113 	}
114 
115 	/***
116 	 * This method returns the number of audio channels the clip contains.<br>
117 	 * (The stereo, mono thing).
118 	 * 
119 	 * @return The number of channels. (2 for stereo, 1 for mono)
120 	 */
121 	public int getChannelNumber() {
122 		return ((Integer) content.get(FIELD_CHANNEL)).intValue();
123 	}
124 
125 	/***
126 	 * Returns the encoding type.
127 	 * 
128 	 * @return The encoding type
129 	 */
130 	public String getEncodingType() {
131 		return (String) content.get(FIELD_TYPE);
132 	}
133 
134 	/***
135 	 * This method returns some extra information about the encoding.<br>
136 	 * This may not contain anything for some audio formats.<br>
137 	 * 
138 	 * @return Some extra information.
139 	 */
140 	public String getExtraEncodingInfos() {
141 		return (String) content.get(FIELD_INFOS);
142 	}
143 
144 	/***
145 	 * This method returns the duration of the represented audio clip in
146 	 * seconds.<br>
147 	 * 
148 	 * @see #getPreciseLength()
149 	 * @return The duration in seconds.
150 	 */
151 	public int getLength() {
152 		return (int) getPreciseLength();
153 	}
154 
155 	/***
156 	 * This method returns the duration of the represented audio clip in seconds
157 	 * (single-precision).<br>
158 	 * 
159 	 * @see #getLength()
160 	 * @return The duration in seconds.
161 	 */
162 	public float getPreciseLength() {
163 		return ((Float) content.get(FIELD_LENGTH)).floatValue();
164 	}
165 
166 	/***
167 	 * This method returns the sample rate, the audio clip was encoded with.<br>
168 	 * 
169 	 * @return Sample rate of the audio clip in &quot;Hz&quot;.
170 	 */
171 	public int getSamplingRate() {
172 		return ((Integer) content.get(FIELD_SAMPLERATE)).intValue();
173 	}
174 
175 	/***
176 	 * This method returns <code>true</code>, if the audio file is encoded
177 	 * with &quot;Variable Bitrate&quot;.<br>
178 	 * 
179 	 * @return <code>true</code> if audio clip is encoded with VBR.
180 	 */
181 	public boolean isVbr() {
182 		return ((Boolean) content.get(FIELD_VBR)).booleanValue();
183 	}
184 
185 	/***
186 	 * This Method sets the bitrate in &quot;Kbps&quot;.<br>
187 	 * 
188 	 * @param bitrate
189 	 *            bitrate in kbps.
190 	 */
191 	public void setBitrate(int bitrate) {
192 		content.put(FIELD_BITRATE, new Integer(bitrate));
193 	}
194 
195 	/***
196 	 * Sets the number of channels.
197 	 * 
198 	 * @param chanNb
199 	 *            number of channels (2 for stereo, 1 for mono).
200 	 */
201 	public void setChannelNumber(int chanNb) {
202 		content.put(FIELD_CHANNEL, new Integer(chanNb));
203 	}
204 
205 	/***
206 	 * Sets the type of the encoding.<br>
207 	 * This is a bit format specific.<br>
208 	 * eg:Layer I/II/III
209 	 * 
210 	 * @param encodingType
211 	 *            Encoding type.
212 	 */
213 	public void setEncodingType(String encodingType) {
214 		content.put(FIELD_TYPE, encodingType);
215 	}
216 
217 	/***
218 	 * A string contianing anything else that might be interesting
219 	 * 
220 	 * @param infos
221 	 *            Extra information.
222 	 */
223 	public void setExtraEncodingInfos(String infos) {
224 		content.put(FIELD_INFOS, infos);
225 	}
226 
227 	/***
228 	 * This method sets the audio duration of the represented clip.<br>
229 	 * 
230 	 * @param length
231 	 *            The duration of the audio clip in seconds.
232 	 */
233 	public void setLength(int length) {
234 		content.put(FIELD_LENGTH, new Float(length));
235 	}
236 
237 	/***
238 	 * This method sets the audio duration of the represented clip.<br>
239 	 * 
240 	 * @param seconds
241 	 *            The duration of the audio clip in seconds (single-precision).
242 	 */
243 	public void setPreciseLength(float seconds) {
244 		content.put(FIELD_LENGTH, new Float(seconds));
245 	}
246 
247 	/***
248 	 * Sets the Sampling rate in &quot;Hz&quot;<br>
249 	 * 
250 	 * @param samplingRate
251 	 *            Sample rate.
252 	 */
253 	public void setSamplingRate(int samplingRate) {
254 		content.put(FIELD_SAMPLERATE, new Integer(samplingRate));
255 	}
256 
257 	/***
258 	 * Sets the VBR flag for the represented audio clip.<br>
259 	 * 
260 	 * @param b
261 	 *            <code>true</code> if VBR.
262 	 */
263 	public void setVbr(boolean b) {
264 		content.put(FIELD_VBR, new Boolean(b));
265 	}
266 
267 	/***
268 	 * Pretty prints this encoding info
269 	 * 
270 	 * @see java.lang.Object#toString()
271 	 */
272 	public String toString() {
273 		StringBuffer out = new StringBuffer(50);
274 		out.append("Encoding infos content:\n");
275 		Enumeration en = content.keys();
276 		while (en.hasMoreElements()) {
277 			Object key = en.nextElement();
278 			Object val = content.get(key);
279 			out.append("\t");
280 			out.append(key);
281 			out.append(" : ");
282 			out.append(val);
283 			out.append("\n");
284 		}
285 		return out.toString().substring(0, out.length() - 1);
286 	}
287 }