1. /*
  2. * @(#)AudioFormat.java 1.29 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. /**
  9. * <code>AudioFormat</code> is the class that specifies a particular arrangement of data in a sound stream.
  10. * By examing the information stored in the audio format, you can discover how to interpret the bits in the
  11. * binary sound data.
  12. * <p>
  13. * Every data line has an audio format associated with its data stream. The audio format of a source (playback) data line indicates
  14. * what kind of data the data line expects to receive for output. For a target (capture) data line, the audio format specifies the kind
  15. * of the data that can be read from the line.
  16. * Sound files also have audio formats, of course. The <code>{@link AudioFileFormat}</code>
  17. * class encapsulates an <code>AudioFormat</code> in addition to other,
  18. * file-specific information. Similarly, an <code>{@link AudioInputStream}</code> has an
  19. * <code>AudioFormat</code>.
  20. * <p>
  21. * The <code>AudioFormat</code> class accommodates a number of common sound-file encoding techniques, including
  22. * pulse-code modulation (PCM), mu-law encoding, and a-law encoding. These encoding techniques are predefined,
  23. * but service providers can create new encoding types.
  24. * The encoding that a specific format uses is named by its <code>encoding</code> field.
  25. *<p>
  26. * In addition to the encoding, the audio format includes other properties that further specify the exact
  27. * arrangement of the data.
  28. * These include the number of channels, sample rate, sample size, byte order, frame rate, and frame size.
  29. * Sounds may have different numbers of audio channels: one for mono, two for stereo.
  30. * The sample rate measures how many "snapshots" (samples) of the sound pressure are taken per second, per channel.
  31. * (If the sound is stereo rather than mono, two samples are actually measured at each instant of time: one for the left channel,
  32. * and another for the right channel; however, the sample rate still measures the number per channel, so the rate is the same
  33. * regardless of the number of channels. This is the standard use of the term.)
  34. * The sample size indicates how many bits are used to store each snapshot; 8 and 16 are typical values.
  35. * For 16-bit samples (or any other sample size larger than a byte),
  36. * byte order is important; the bytes in each sample are arranged in
  37. * either the "little-endian" or "big-endian" style.
  38. * For encodings like PCM, a frame consists of the set of samples for all channels at a given
  39. * point in time, and so the size of a frame (in bytes) is always equal to the size of a sample (in bytes) times
  40. * the number of channels. However, with some other sorts of encodings a frame can contain
  41. * a bundle of compressed data for a whole series of samples, as well as additional, non-sample
  42. * data. For such encodings, the sample rate and sample size refer to the data after it is decoded into PCM,
  43. * and so they are completely different from the frame rate and frame size.
  44. *
  45. * @author Kara Kytle
  46. * @version 1.29 03/01/23
  47. * @see DataLine#getFormat
  48. * @see AudioInputStream#getFormat
  49. * @see AudioFileFormat
  50. * @see javax.sound.sampled.spi.FormatConversionProvider
  51. * @since 1.3
  52. */
  53. public class AudioFormat {
  54. // ENCODING DEFINES
  55. /**
  56. * Specifies signed, linear PCM data.
  57. */
  58. //public static final Type PCM_SIGNED = new Type("PCM SIGNED");
  59. /**
  60. * Specifies unsigned, linear PCM data.
  61. */
  62. //public static final Type PCM_UNSIGNED = new Type("PCM UNSIGNED");
  63. /**
  64. * Specifies u-law encoded data.
  65. */
  66. //public static final Type ULAW = new Type("ULAW");
  67. /**
  68. * Specifies a-law encoded data.
  69. */
  70. //public static final Type ALAW = new Type("ALAW");
  71. // OTHER DEFINES
  72. // INSTANCE VARIABLES
  73. /**
  74. * The audio encoding technique used by this format.
  75. */
  76. protected Encoding encoding;
  77. /**
  78. * The number of samples played or recorded per second, for sounds that have this format.
  79. */
  80. protected float sampleRate;
  81. /**
  82. * The number of bits in each sample of a sound that has this format.
  83. */
  84. protected int sampleSizeInBits;
  85. /**
  86. * The number of audio channels in this format (1 for mono, 2 for stereo).
  87. */
  88. protected int channels;
  89. /**
  90. * The number of bytes in each frame of a sound that has this format.
  91. */
  92. protected int frameSize;
  93. /**
  94. * The number of frames played or recorded per second, for sounds that have this format.
  95. */
  96. protected float frameRate;
  97. /**
  98. * Indicates whether the audio data is stored in big-endian or little-endian order.
  99. */
  100. protected boolean bigEndian;
  101. /**
  102. * Constructs an <code>AudioFormat</code> with the given parameters.
  103. * The encoding specifies the convention used to represent the data.
  104. * The other parameters are further explained in the {@link AudioFormat
  105. * class description}.
  106. * @param encoding the audio encoding technique
  107. * @param sampleRate the number of samples per second
  108. * @param sampleSizeInBits the number of bits in each sample
  109. * @param channels the number of channels (1 for mono, 2 for stereo, and so on)
  110. * @param frameSize the number of bytes in each frame
  111. * @param frameRate the number of frames per second
  112. * @param bigEndian indicates whether the data for a single sample
  113. * is stored in big-endian byte order (<code>false</code>
  114. * means little-endian)
  115. */
  116. public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
  117. int channels, int frameSize, float frameRate, boolean bigEndian) {
  118. this.encoding = encoding;
  119. this.sampleRate = sampleRate;
  120. this.sampleSizeInBits = sampleSizeInBits;
  121. this.channels = channels;
  122. this.frameSize = frameSize;
  123. this.frameRate = frameRate;
  124. this.bigEndian = bigEndian;
  125. }
  126. /**
  127. * Constructs an <code>AudioFormat</code> with a linear PCM encoding and
  128. * the given parameters. The frame size is set to the number of bytes
  129. * required to contain one sample from each channel, and the frame rate
  130. * is set to the sample rate.
  131. *
  132. * @param sampleRate the number of samples per second
  133. * @param sampleSizeInBits the number of bits in each sample
  134. * @param channels the number of channels (1 for mono, 2 for stereo, and so on)
  135. * @param signed indicates whether the data is signed or unsigned
  136. * @param bigEndian indicates whether the data for a single sample
  137. * is stored in big-endian byte order (<code>false</code>
  138. * means little-endian)
  139. */
  140. public AudioFormat(float sampleRate, int sampleSizeInBits,
  141. int channels, boolean signed, boolean bigEndian) {
  142. this((signed == true ? Encoding.PCM_SIGNED : Encoding.PCM_UNSIGNED),
  143. sampleRate,
  144. sampleSizeInBits,
  145. channels,
  146. (channels == AudioSystem.NOT_SPECIFIED || sampleSizeInBits == AudioSystem.NOT_SPECIFIED)?
  147. AudioSystem.NOT_SPECIFIED:
  148. ((sampleSizeInBits + 7) / 8) * channels,
  149. sampleRate,
  150. bigEndian);
  151. }
  152. /**
  153. * Obtains the type of encoding for sounds in this format.
  154. *
  155. * @return the encoding type
  156. * @see Encoding#PCM_SIGNED
  157. * @see Encoding#PCM_UNSIGNED
  158. * @see Encoding#ULAW
  159. * @see Encoding#ALAW
  160. */
  161. public Encoding getEncoding() {
  162. return encoding;
  163. }
  164. /**
  165. * Obtains the sample rate.
  166. * For compressed formats, the return value is the sample rate of the uncompressed
  167. * audio data.
  168. * When this AudioFormat is used for queries (e.g. {@link
  169. * AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
  170. * AudioSystem.isConversionSupported}) or capabilities (e.g. {@link
  171. * DataLine.Info#getFormats() DataLine.Info.getFormats}), a sample rate of
  172. * <code>AudioSystem.NOT_SPECIFIED</code> means that any sample rate is
  173. * acceptable. <code>AudioSystem.NOT_SPECIFIED</code> is also returned when
  174. * the sample rate is not defined for this audio format.
  175. * @return the number of samples per second,
  176. * or <code>AudioSystem.NOT_SPECIFIED</code>
  177. *
  178. * @see #getFrameRate()
  179. * @see AudioSystem#NOT_SPECIFIED
  180. */
  181. public float getSampleRate() {
  182. return sampleRate;
  183. }
  184. /**
  185. * Obtains the size of a sample.
  186. * For compressed formats, the return value is the sample size of the
  187. * uncompressed audio data.
  188. * When this AudioFormat is used for queries (e.g. {@link
  189. * AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
  190. * AudioSystem.isConversionSupported}) or capabilities (e.g. {@link
  191. * DataLine.Info#getFormats() DataLine.Info.getFormats}), a sample size of
  192. * <code>AudioSystem.NOT_SPECIFIED</code> means that any sample size is
  193. * acceptable. <code>AudioSystem.NOT_SPECIFIED</code> is also returned when
  194. * the sample size is not defined for this audio format.
  195. * @return the number of bits in each sample,
  196. * or <code>AudioSystem.NOT_SPECIFIED</code>
  197. *
  198. * @see #getFrameSize()
  199. * @see AudioSystem#NOT_SPECIFIED
  200. */
  201. public int getSampleSizeInBits() {
  202. return sampleSizeInBits;
  203. }
  204. /**
  205. * Obtains the number of channels.
  206. * When this AudioFormat is used for queries (e.g. {@link
  207. * AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
  208. * AudioSystem.isConversionSupported}) or capabilities (e.g. {@link
  209. * DataLine.Info#getFormats() DataLine.Info.getFormats}), a return value of
  210. * <code>AudioSystem.NOT_SPECIFIED</code> means that any (positive) number of channels is
  211. * acceptable.
  212. * @return The number of channels (1 for mono, 2 for stereo, etc.),
  213. * or <code>AudioSystem.NOT_SPECIFIED</code>
  214. *
  215. * @see AudioSystem#NOT_SPECIFIED
  216. */
  217. public int getChannels() {
  218. return channels;
  219. }
  220. /**
  221. * Obtains the frame size in bytes.
  222. * When this AudioFormat is used for queries (e.g. {@link
  223. * AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
  224. * AudioSystem.isConversionSupported}) or capabilities (e.g. {@link
  225. * DataLine.Info#getFormats() DataLine.Info.getFormats}), a frame size of
  226. * <code>AudioSystem.NOT_SPECIFIED</code> means that any frame size is
  227. * acceptable. <code>AudioSystem.NOT_SPECIFIED</code> is also returned when
  228. * the frame size is not defined for this audio format.
  229. * @return the number of bytes per frame,
  230. * or <code>AudioSystem.NOT_SPECIFIED</code>
  231. *
  232. * @see #getSampleSizeInBits()
  233. * @see AudioSystem#NOT_SPECIFIED
  234. */
  235. public int getFrameSize() {
  236. return frameSize;
  237. }
  238. /**
  239. * Obtains the frame rate in frames per second.
  240. * When this AudioFormat is used for queries (e.g. {@link
  241. * AudioSystem#isConversionSupported(AudioFormat, AudioFormat)
  242. * AudioSystem.isConversionSupported}) or capabilities (e.g. {@link
  243. * DataLine.Info#getFormats() DataLine.Info.getFormats}), a frame rate of
  244. * <code>AudioSystem.NOT_SPECIFIED</code> means that any frame rate is
  245. * acceptable. <code>AudioSystem.NOT_SPECIFIED</code> is also returned when
  246. * the frame rate is not defined for this audio format.
  247. * @return the number of frames per second,
  248. * or <code>AudioSystem.NOT_SPECIFIED</code>
  249. *
  250. * @see #getSampleRate()
  251. * @see AudioSystem#NOT_SPECIFIED
  252. */
  253. public float getFrameRate() {
  254. return frameRate;
  255. }
  256. /**
  257. * Indicates whether the audio data is stored in big-endian or little-endian
  258. * byte order. If the sample size is not more than one byte, the return value is
  259. * irrelevant.
  260. * @return <code>true</code> if the data is stored in big-endian byte order,
  261. * <code>false</code> if little-endian
  262. */
  263. public boolean isBigEndian() {
  264. return bigEndian;
  265. }
  266. /**
  267. * Indicates whether this format matches the one specified. To match,
  268. * two formats must have the same encoding, the same number of channels,
  269. * and the same number of bits per sample and bytes per frame.
  270. * The two formats must also have the same sample rate,
  271. * unless the specified format has the sample rate value <code>AudioSystem.NOT_SPECIFIED</code>,
  272. * which any sample rate will match. The frame rates must
  273. * similarly be equal, unless the specified format has the frame rate
  274. * value <code>AudioSystem.NOT_SPECIFIED</code>. The byte order (big-endian or little-endian)
  275. * must match if the sample size is greater than one byte.
  276. *
  277. * @param format format to test for match
  278. * @return <code>true</code> if this format matches the one specified,
  279. * <code>false</code> otherwise.
  280. */
  281. /*
  282. * $$kk: 04.20.99: i changed the semantics of this.
  283. */
  284. public boolean matches(AudioFormat format) {
  285. if ((format.getEncoding() == getEncoding()) &&
  286. ( (format.getSampleRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getSampleRate() == getSampleRate()) ) &&
  287. (format.getSampleSizeInBits() == getSampleSizeInBits()) &&
  288. (format.getChannels() == getChannels() &&
  289. (format.getFrameSize() == getFrameSize()) &&
  290. ( (format.getFrameRate() == (float)AudioSystem.NOT_SPECIFIED) || (format.getFrameRate() == getFrameRate()) ) &&
  291. ( (format.getSampleSizeInBits() <= 8) || (format.isBigEndian() == isBigEndian()) ) ) )
  292. return true;
  293. return false;
  294. }
  295. /**
  296. * Returns a string that describes the format, such as:
  297. * "PCM SIGNED 22050 Hz 16 bit mono big-endian audio data". The contents of the string
  298. * may vary between implementations of Java Sound.
  299. *
  300. * @return a string that describes the format parameters
  301. */
  302. public String toString() {
  303. return getEncoding() + ", " +
  304. sampleRate + " Hz, " +
  305. sampleSizeInBits + " bit, " +
  306. (channels == 2 ? "stereo, " : "mono, ") +
  307. (sampleSizeInBits > 8 ? ((bigEndian == true ? "big-endian, " : "little-endian, ")) : "") +
  308. "audio data";
  309. }
  310. /**
  311. * The <code>Encoding</code> class names the specific type of data representation
  312. * used for an audio stream. The encoding includes aspects of the
  313. * sound format other than the number of channels, sample rate, sample size,
  314. * frame rate, frame size, and byte order.
  315. * <p>
  316. * One ubiquitous type of audio encoding is pulse-code modulation (PCM),
  317. * which is simply a linear (proportional) representation of the sound
  318. * waveform. With PCM, the number stored in each sample is proportional
  319. * to the instantaneous amplitude of the sound pressure at that point in
  320. * time. The numbers are frequently signed or unsigned integers.
  321. * Besides PCM, other encodings include mu-law and a-law, which are nonlinear
  322. * mappings of the sound amplitude that are often used for recording speech.
  323. * <p>
  324. * You can use a predefined encoding by referring to one of the static
  325. * objects created by this class, such as PCM_SIGNED or
  326. * PCM_UNSIGNED. Service providers can create new encodings, such as
  327. * compressed audio formats or floating-point PCM samples, and make
  328. * these available through the <code>{@link AudioSystem}</code> class.
  329. * <p>
  330. * The <code>Encoding</code> class is static, so that all
  331. * <code>AudioFormat</code> objects that have the same encoding will refer
  332. * to the same object (rather than different instances of the same class).
  333. * This allows matches to be made by checking that two format's encodings
  334. * are equal.
  335. *
  336. * @see AudioFormat
  337. * @see javax.sound.sampled.spi.FormatConversionProvider
  338. *
  339. * @author Kara Kytle
  340. * @version 1.29 03/01/23
  341. * @since 1.3
  342. */
  343. public static class Encoding {
  344. // ENCODING DEFINES
  345. /**
  346. * Specifies signed, linear PCM data.
  347. */
  348. public static final Encoding PCM_SIGNED = new Encoding("PCM_SIGNED");
  349. /**
  350. * Specifies unsigned, linear PCM data.
  351. */
  352. public static final Encoding PCM_UNSIGNED = new Encoding("PCM_UNSIGNED");
  353. /**
  354. * Specifies u-law encoded data.
  355. */
  356. public static final Encoding ULAW = new Encoding("ULAW");
  357. /**
  358. * Specifies a-law encoded data.
  359. */
  360. public static final Encoding ALAW = new Encoding("ALAW");
  361. // INSTANCE VARIABLES
  362. /**
  363. * Encoding name.
  364. */
  365. private String name;
  366. // CONSTRUCTOR
  367. /**
  368. * Constructs a new encoding.
  369. * @param name the name of the new type of encoding
  370. */
  371. protected Encoding(String name) {
  372. this.name = name;
  373. }
  374. // METHODS
  375. /**
  376. * Finalizes the equals method
  377. */
  378. public final boolean equals(Object obj) {
  379. return super.equals(obj);
  380. }
  381. /**
  382. * Finalizes the hashCode method
  383. */
  384. public final int hashCode() {
  385. return super.hashCode();
  386. }
  387. /**
  388. * Provides the <code>String</code> representation of the encoding. This <code>String</code> is
  389. * the same name that was passed to the constructor. For the predefined encodings, the name
  390. * is similar to the encoding's variable (field) name. For example, <code>PCM_SIGNED.toString()</code> returns
  391. * the name "pcm_signed".
  392. *
  393. * @return the encoding name
  394. */
  395. public final String toString() {
  396. return name;
  397. }
  398. } // class Encoding
  399. }