1. /*
  2. * @(#)AudioFileFormat.java 1.23 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.sampled;
  8. import java.io.File;
  9. import java.io.OutputStream;
  10. import java.io.IOException;
  11. import java.util.Collections;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. /**
  15. * An instance of the <code>AudioFileFormat</code> class describes
  16. * an audio file, including the file type, the file's length in bytes,
  17. * the length in sample frames of the audio data contained in the file,
  18. * and the format of the audio data.
  19. * <p>
  20. * The <code>{@link AudioSystem}</code> class includes methods for determining the format
  21. * of an audio file, obtaining an audio input stream from an audio file, and
  22. * writing an audio file from an audio input stream.
  23. *
  24. * <p>An <code>AudioFileFormat</code> object can
  25. * include a set of properties. A property is a pair of key and value:
  26. * the key is of type <code>String</code>, the associated property
  27. * value is an arbitrary object.
  28. * Properties specify additional informational
  29. * meta data (like a author, copyright, or file duration).
  30. * Properties are optional information, and file reader and file
  31. * writer implementations are not required to provide or
  32. * recognize properties.
  33. *
  34. * <p>The following table lists some common properties that should
  35. * be used in implementations:
  36. *
  37. * <table border=1>
  38. * <tr>
  39. * <th>Property key</th>
  40. * <th>Value type</th>
  41. * <th>Description</th>
  42. * </tr>
  43. * <tr>
  44. * <td>"duration"</td>
  45. * <td>{@link java.lang.Long Long}</td>
  46. * <td>playback duration of the file in microseconds</td>
  47. * </tr>
  48. * <tr>
  49. * <td>"author"</td>
  50. * <td>{@link java.lang.String String}</td>
  51. * <td>name of the author of this file</td>
  52. * </tr>
  53. * <tr>
  54. * <td>"title"</td>
  55. * <td>{@link java.lang.String String}</td>
  56. * <td>title of this file</td>
  57. * </tr>
  58. * <tr>
  59. * <td>"copyright"</td>
  60. * <td>{@link java.lang.String String}</td>
  61. * <td>copyright message</td>
  62. * </tr>
  63. * <tr>
  64. * <td>"date"</td>
  65. * <td>{@link java.util.Date Date}</td>
  66. * <td>date of the recording or release</td>
  67. * </tr>
  68. * <tr>
  69. * <td>"comment"</td>
  70. * <td>{@link java.lang.String String}</td>
  71. * <td>an arbitrary text</td>
  72. * </tr>
  73. * </table>
  74. *
  75. *
  76. * @author David Rivas
  77. * @author Kara Kytle
  78. * @author Florian Bomers
  79. * @version 1.23 03/12/19
  80. * @see AudioInputStream
  81. * @since 1.3
  82. */
  83. public class AudioFileFormat {
  84. // INSTANCE VARIABLES
  85. /**
  86. * File type.
  87. */
  88. private Type type;
  89. /**
  90. * File length in bytes
  91. */
  92. private int byteLength;
  93. /**
  94. * Format of the audio data contained in the file.
  95. */
  96. private AudioFormat format;
  97. /**
  98. * Audio data length in sample frames
  99. */
  100. private int frameLength;
  101. /** The set of properties */
  102. private HashMap<String, Object> properties;
  103. /**
  104. * Constructs an audio file format object.
  105. * This protected constructor is intended for use by providers of file-reading
  106. * services when returning information about an audio file or about supported audio file
  107. * formats.
  108. * @param type the type of the audio file
  109. * @param byteLength the length of the file in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
  110. * @param format the format of the audio data contained in the file
  111. * @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>
  112. *
  113. * @see #getType
  114. */
  115. protected AudioFileFormat(Type type, int byteLength, AudioFormat format, int frameLength) {
  116. this.type = type;
  117. this.byteLength = byteLength;
  118. this.format = format;
  119. this.frameLength = frameLength;
  120. this.properties = null;
  121. }
  122. /**
  123. * Constructs an audio file format object.
  124. * This public constructor may be used by applications to describe the
  125. * properties of a requested audio file.
  126. * @param type the type of the audio file
  127. * @param format the format of the audio data contained in the file
  128. * @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>
  129. */
  130. public AudioFileFormat(Type type, AudioFormat format, int frameLength) {
  131. this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);
  132. }
  133. /**
  134. * Construct an audio file format object with a set of
  135. * defined properties.
  136. * This public constructor may be used by applications to describe the
  137. * properties of a requested audio file. The properties map
  138. * will be copied to prevent any changes to it.
  139. *
  140. * @param type the type of the audio file
  141. * @param format the format of the audio data contained in the file
  142. * @param frameLength the audio data length in sample frames, or
  143. * <code>AudioSystem.NOT_SPECIFIED</code>
  144. * @param properties a <code>Map<String,Object></code> object
  145. * with properties
  146. *
  147. * @since 1.5
  148. */
  149. public AudioFileFormat(Type type, AudioFormat format,
  150. int frameLength, Map<String, Object> properties) {
  151. this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);
  152. this.properties = new HashMap<String, Object>(properties);
  153. }
  154. /**
  155. * Obtains the audio file type, such as <code>WAVE</code> or <code>AU</code>.
  156. * @return the audio file type
  157. *
  158. * @see Type#WAVE
  159. * @see Type#AU
  160. * @see Type#AIFF
  161. * @see Type#AIFC
  162. * @see Type#SND
  163. */
  164. public Type getType() {
  165. return type;
  166. }
  167. /**
  168. * Obtains the size in bytes of the entire audio file (not just its audio data).
  169. * @return the audio file length in bytes
  170. * @see AudioSystem#NOT_SPECIFIED
  171. */
  172. public int getByteLength() {
  173. return byteLength;
  174. }
  175. /**
  176. * Obtains the format of the audio data contained in the audio file.
  177. * @return the audio data format
  178. */
  179. public AudioFormat getFormat() {
  180. return format;
  181. }
  182. /**
  183. * Obtains the length of the audio data contained in the file, expressed in sample frames.
  184. * @return the number of sample frames of audio data in the file
  185. * @see AudioSystem#NOT_SPECIFIED
  186. */
  187. public int getFrameLength() {
  188. return frameLength;
  189. }
  190. /**
  191. * Obtain an unmodifiable map of properties.
  192. * The concept of properties is further explained in
  193. * the {@link AudioFileFormat class description}.
  194. *
  195. * @return a <code>Map<String,Object></code> object containing
  196. * all properties. If no properties are recognized, an empty map is
  197. * returned.
  198. *
  199. * @see #getProperty(String)
  200. * @since 1.5
  201. */
  202. public Map<String,Object> properties() {
  203. Map<String,Object> ret;
  204. if (properties == null) {
  205. ret = new HashMap<String,Object>(0);
  206. } else {
  207. ret = (Map<String,Object>) (properties.clone());
  208. }
  209. return (Map<String,Object>) Collections.unmodifiableMap(ret);
  210. }
  211. /**
  212. * Obtain the property value specified by the key.
  213. * The concept of properties is further explained in
  214. * the {@link AudioFileFormat class description}.
  215. *
  216. * <p>If the specified property is not defined for a
  217. * particular file format, this method returns
  218. * <code>null</code>.
  219. *
  220. * @param key the key of the desired property
  221. * @return the value of the property with the specified key,
  222. * or <code>null</code> if the property does not exist.
  223. *
  224. * @see #properties
  225. * @since 1.5
  226. */
  227. public Object getProperty(String key) {
  228. if (properties == null) {
  229. return null;
  230. }
  231. return properties.get(key);
  232. }
  233. /**
  234. * Provides a string representation of the file format.
  235. * @return the file format as a string
  236. */
  237. public String toString() {
  238. StringBuffer buf = new StringBuffer();
  239. //$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException
  240. if (type != null) {
  241. buf.append(type.toString() + " (." + type.getExtension() + ") file");
  242. } else {
  243. buf.append("unknown file format");
  244. }
  245. if (byteLength != AudioSystem.NOT_SPECIFIED) {
  246. buf.append(", byte length: " + byteLength);
  247. }
  248. buf.append(", data format: " + format);
  249. if (frameLength != AudioSystem.NOT_SPECIFIED) {
  250. buf.append(", frame length: " + frameLength);
  251. }
  252. return new String(buf);
  253. }
  254. /**
  255. * An instance of the <code>Type</code> class represents one of the
  256. * standard types of audio file. Static instances are provided for the
  257. * common types.
  258. */
  259. public static class Type {
  260. // FILE FORMAT TYPE DEFINES
  261. /**
  262. * Specifies a WAVE file.
  263. */
  264. public static final Type WAVE = new Type("WAVE", "wav");
  265. /**
  266. * Specifies an AU file.
  267. */
  268. public static final Type AU = new Type("AU", "au");
  269. /**
  270. * Specifies an AIFF file.
  271. */
  272. public static final Type AIFF = new Type("AIFF", "aif");
  273. /**
  274. * Specifies an AIFF-C file.
  275. */
  276. public static final Type AIFC = new Type("AIFF-C", "aifc");
  277. /**
  278. * Specifies a SND file.
  279. */
  280. public static final Type SND = new Type("SND", "snd");
  281. // INSTANCE VARIABLES
  282. /**
  283. * File type name.
  284. */
  285. private final String name;
  286. /**
  287. * File type extension.
  288. */
  289. private final String extension;
  290. // CONSTRUCTOR
  291. /**
  292. * Constructs a file type.
  293. * @param name the string that names the file type
  294. * @param extension the string that commonly marks the file type
  295. * without leading dot.
  296. */
  297. public Type(String name, String extension) {
  298. this.name = name;
  299. this.extension = extension;
  300. }
  301. // METHODS
  302. /**
  303. * Finalizes the equals method
  304. */
  305. public final boolean equals(Object obj) {
  306. if (toString() == null) {
  307. return (obj != null) && (obj.toString() == null);
  308. }
  309. if (obj instanceof Type) {
  310. return toString().equals(obj.toString());
  311. }
  312. return false;
  313. }
  314. /**
  315. * Finalizes the hashCode method
  316. */
  317. public final int hashCode() {
  318. if (toString() == null) {
  319. return 0;
  320. }
  321. return toString().hashCode();
  322. }
  323. /**
  324. * Provides the file type's name as the <code>String</code> representation
  325. * of the file type.
  326. * @return the file type's name
  327. */
  328. public final String toString() {
  329. return name;
  330. }
  331. /**
  332. * Obtains the common file name extension for this file type.
  333. * @return file type extension
  334. */
  335. public String getExtension() {
  336. return extension;
  337. }
  338. } // class Type
  339. } // class AudioFileFormat