1. /*
  2. * @(#)AudioFileFormat.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 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. /**
  12. * An instance of the <code>AudioFileFormat</code> class describes
  13. * an audio file, including the file type, the file's length in bytes,
  14. * the length in sample frames of the audio data contained in the file,
  15. * and the format of the audio data.
  16. * <p>
  17. * The <code>{@link AudioSystem}</code> class includes methods for determining the format
  18. * of an audio file, obtaining an audio input stream from an audio file, and
  19. * writing an audio file from an audio input stream.
  20. *
  21. * @author David Rivas
  22. * @author Kara Kytle
  23. * @version 1.19 03/01/23
  24. * @see AudioInputStream
  25. * @since 1.3
  26. */
  27. public class AudioFileFormat {
  28. // INSTANCE VARIABLES
  29. /**
  30. * File type.
  31. */
  32. private Type type;
  33. /**
  34. * File length in bytes
  35. */
  36. private int byteLength;
  37. /**
  38. * Format of the audio data contained in the file.
  39. */
  40. private AudioFormat format;
  41. /**
  42. * Audio data length in sample frames
  43. */
  44. private int frameLength;
  45. /**
  46. * Constructs an audio file format object.
  47. * This protected constructor is intended for use by providers of file-reading
  48. * services when returning information about an audio file or about supported audio file
  49. * formats.
  50. * @param type the type of the audio file
  51. * @param byteLength the length of the file in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
  52. * @param format the format of the audio data contained in the file
  53. * @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>
  54. *
  55. * @see #getType
  56. */
  57. protected AudioFileFormat(Type type, int byteLength, AudioFormat format, int frameLength) {
  58. this.type = type;
  59. this.byteLength = byteLength;
  60. this.format = format;
  61. this.frameLength = frameLength;
  62. }
  63. /**
  64. * Constructs an audio file format object.
  65. * This public constructor may be used by applications to describe the
  66. * properties of a requested audio file.
  67. * @param type the type of the audio file
  68. * @param format the format of the audio data contained in the file
  69. * @param frameLength the audio data length in sample frames, or <code>AudioSystem.NOT_SPECIFIED</code>
  70. */
  71. public AudioFileFormat(Type type, AudioFormat format, int frameLength) {
  72. this(type,AudioSystem.NOT_SPECIFIED,format,frameLength);
  73. }
  74. /**
  75. * Obtains the audio file type, such as <code>WAVE</code> or <code>AU</code>.
  76. * @return the audio file type
  77. *
  78. * @see Type#WAVE
  79. * @see Type#AU
  80. * @see Type#AIFF
  81. * @see Type#AIFC
  82. * @see Type#SND
  83. */
  84. public Type getType() {
  85. return type;
  86. }
  87. /**
  88. * Obtains the size in bytes of the entire audio file (not just its audio data).
  89. * @return the audio file length in bytes
  90. * @see AudioSystem#NOT_SPECIFIED
  91. */
  92. public int getByteLength() {
  93. return byteLength;
  94. }
  95. /**
  96. * Obtains the format of the audio data contained in the audio file.
  97. * @return the audio data format
  98. */
  99. public AudioFormat getFormat() {
  100. return format;
  101. }
  102. /**
  103. * Obtains the length of the audio data contained in the file, expressed in sample frames.
  104. * @return the number of sample frames of audio data in the file
  105. * @see AudioSystem#NOT_SPECIFIED
  106. */
  107. public int getFrameLength() {
  108. return frameLength;
  109. }
  110. /**
  111. * Provides a string representation of the file format.
  112. * @return the file format as a string
  113. */
  114. public String toString() {
  115. StringBuffer buf = new StringBuffer();
  116. //$$fb2002-11-01: fix for 4672864: AudioFileFormat.toString() throws unexpected NullPointerException
  117. if (type != null) {
  118. buf.append(type.toString() + " (." + type.getExtension() + ") file");
  119. } else {
  120. buf.append("unknown file format");
  121. }
  122. if (byteLength != AudioSystem.NOT_SPECIFIED) {
  123. buf.append(", byte length: " + byteLength);
  124. }
  125. buf.append(", data format: " + format);
  126. if (frameLength != AudioSystem.NOT_SPECIFIED) {
  127. buf.append(", frame length: " + frameLength);
  128. }
  129. return new String(buf);
  130. }
  131. /**
  132. * An instance of the <code>Type</code> class represents one of the
  133. * standard types of audio file. Static instances are provided for the
  134. * common types.
  135. */
  136. public static class Type {
  137. // FILE FORMAT TYPE DEFINES
  138. /**
  139. * Specifies a WAVE file.
  140. */
  141. public static final Type WAVE = new Type("WAVE", "wav");
  142. /**
  143. * Specifies an AU file.
  144. */
  145. public static final Type AU = new Type("AU", "au");
  146. /**
  147. * Specifies an AIFF file.
  148. */
  149. public static final Type AIFF = new Type("AIFF", "aif");
  150. /**
  151. * Specifies an AIFF-C file.
  152. */
  153. public static final Type AIFC = new Type("AIFF-C", "aifc");
  154. /**
  155. * Specifies a SND file.
  156. */
  157. public static final Type SND = new Type("SND", "snd");
  158. // INSTANCE VARIABLES
  159. /**
  160. * File type name.
  161. */
  162. private final String name;
  163. /**
  164. * File type extension.
  165. */
  166. private final String extension;
  167. // CONSTRUCTOR
  168. /**
  169. * Constructs a file type.
  170. * @param name the string that names the file type
  171. * @param extension the string that commonly marks the file type
  172. */
  173. protected Type(String name, String extension) {
  174. this.name = name;
  175. this.extension = extension;
  176. }
  177. // METHODS
  178. /**
  179. * Finalizes the equals method
  180. */
  181. public final boolean equals(Object obj) {
  182. return super.equals(obj);
  183. }
  184. /**
  185. * Finalizes the hashCode method
  186. */
  187. public final int hashCode() {
  188. return super.hashCode();
  189. }
  190. /**
  191. * Provides the file type's name as the <code>String</code> representation
  192. * of the file type.
  193. * @return the file type's name
  194. */
  195. public final String toString() {
  196. return name;
  197. }
  198. /**
  199. * Obtains the common file name extension for this file type.
  200. * @return file type extension
  201. */
  202. public String getExtension() {
  203. return extension;
  204. }
  205. } // class Type
  206. } // class AudioFileFormat