WAVReader.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* WAVReader.java -- Read WAV files.
  2. Copyright (C) 2006, 2012 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.sound.sampled.WAV;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.io.DataInputStream;
  36. import java.io.FileInputStream;
  37. import java.net.URL;
  38. import javax.sound.sampled.AudioFormat;
  39. import javax.sound.sampled.AudioFileFormat;
  40. import javax.sound.sampled.AudioInputStream;
  41. import javax.sound.sampled.UnsupportedAudioFileException;
  42. import javax.sound.sampled.spi.AudioFileReader;
  43. /**
  44. * A WAV file reader.
  45. *
  46. * This code reads WAV files.
  47. *
  48. * There are many decent documents on the web describing the WAV file
  49. * format. I didn't bother looking for the official document. If it
  50. * exists, I'm not even sure if it is freely available. We should
  51. * update this comment if we find out anything helpful here. I used
  52. * http://www.sonicspot.com/guide/wavefiles.html
  53. *
  54. * @author Anthony Green (green@redhat.com)
  55. *
  56. */
  57. public class WAVReader extends AudioFileReader
  58. {
  59. private static long readUnsignedIntLE (DataInputStream is)
  60. throws IOException
  61. {
  62. byte[] buf = new byte[4];
  63. is.readFully(buf);
  64. return (buf[0] & 0xFF
  65. | ((buf[1] & 0xFF) << 8)
  66. | ((buf[2] & 0xFF) << 16)
  67. | ((buf[3] & 0xFF) << 24));
  68. }
  69. private static short readUnsignedShortLE (DataInputStream is)
  70. throws IOException
  71. {
  72. byte[] buf = new byte[2];
  73. is.readFully(buf);
  74. return (short) (buf[0] & 0xFF
  75. | ((buf[1] & 0xFF) << 8));
  76. }
  77. /* Get an AudioFileFormat from the given File.
  78. * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.io.File)
  79. */
  80. public AudioFileFormat getAudioFileFormat(File file)
  81. throws UnsupportedAudioFileException, IOException
  82. {
  83. InputStream is = new FileInputStream(file);
  84. try
  85. {
  86. return getAudioFileFormat(is);
  87. }
  88. finally
  89. {
  90. is.close();
  91. }
  92. }
  93. /* Get an AudioFileFormat from the given InputStream.
  94. * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.io.InputStream)
  95. */
  96. public AudioFileFormat getAudioFileFormat(InputStream in)
  97. throws UnsupportedAudioFileException, IOException
  98. {
  99. DataInputStream din;
  100. if (in instanceof DataInputStream)
  101. din = (DataInputStream) in;
  102. else
  103. din = new DataInputStream(in);
  104. if (din.readInt() != 0x52494646) // "RIFF"
  105. throw new UnsupportedAudioFileException("Invalid WAV chunk header.");
  106. // Read the length of this RIFF thing.
  107. readUnsignedIntLE(din);
  108. if (din.readInt() != 0x57415645) // "WAVE"
  109. throw new UnsupportedAudioFileException("Invalid WAV chunk header.");
  110. boolean foundFmt = false;
  111. boolean foundData = false;
  112. short compressionCode = 0, numberChannels = 0, bitsPerSample = 0;
  113. long sampleRate = 0, bytesPerSecond = 0;
  114. long chunkLength = 0;
  115. while (! foundData)
  116. {
  117. int chunkId = din.readInt();
  118. chunkLength = readUnsignedIntLE(din);
  119. switch (chunkId)
  120. {
  121. case 0x666D7420: // "fmt "
  122. foundFmt = true;
  123. compressionCode = readUnsignedShortLE(din);
  124. numberChannels = readUnsignedShortLE(din);
  125. sampleRate = readUnsignedIntLE(din);
  126. bytesPerSecond = readUnsignedIntLE(din);
  127. readUnsignedShortLE(din); // blockAlign
  128. bitsPerSample = readUnsignedShortLE(din);
  129. din.skip(chunkLength - 16);
  130. break;
  131. case 0x66616374: // "fact"
  132. // FIXME: hold compression format dependent data.
  133. din.skip(chunkLength);
  134. break;
  135. case 0x64617461: // "data"
  136. if (! foundFmt)
  137. throw new UnsupportedAudioFileException("This implementation requires WAV fmt chunks precede data chunks.");
  138. foundData = true;
  139. break;
  140. default:
  141. // Unrecognized chunk. Skip it.
  142. din.skip(chunkLength);
  143. }
  144. }
  145. AudioFormat.Encoding encoding;
  146. switch (compressionCode)
  147. {
  148. case 1: // PCM/uncompressed
  149. if (bitsPerSample <= 8)
  150. encoding = AudioFormat.Encoding.PCM_UNSIGNED;
  151. else
  152. encoding = AudioFormat.Encoding.PCM_SIGNED;
  153. break;
  154. default:
  155. throw new UnsupportedAudioFileException("Unrecognized WAV compression code: 0x"
  156. + Integer.toHexString(compressionCode));
  157. }
  158. return new AudioFileFormat (AudioFileFormat.Type.WAVE,
  159. new AudioFormat(encoding,
  160. (float) sampleRate,
  161. bitsPerSample,
  162. numberChannels,
  163. ((bitsPerSample + 7) / 8) * numberChannels,
  164. (float) bytesPerSecond, false),
  165. (int) chunkLength);
  166. }
  167. /* Get an AudioFileFormat from the given URL.
  168. * @see javax.sound.sampled.spi.AudioFileReader#getAudioFileFormat(java.net.URL)
  169. */
  170. public AudioFileFormat getAudioFileFormat(URL url)
  171. throws UnsupportedAudioFileException, IOException
  172. {
  173. InputStream is = url.openStream();
  174. try
  175. {
  176. return getAudioFileFormat(is);
  177. }
  178. finally
  179. {
  180. is.close();
  181. }
  182. }
  183. /* Get an AudioInputStream from the given File.
  184. * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.io.File)
  185. */
  186. public AudioInputStream getAudioInputStream(File file)
  187. throws UnsupportedAudioFileException, IOException
  188. {
  189. return getAudioInputStream(new FileInputStream(file));
  190. }
  191. /* Get an AudioInputStream from the given InputStream.
  192. * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.io.InputStream)
  193. */
  194. public AudioInputStream getAudioInputStream(InputStream stream)
  195. throws UnsupportedAudioFileException, IOException
  196. {
  197. AudioFileFormat aff = getAudioFileFormat(stream);
  198. return new AudioInputStream(stream, aff.getFormat(), (long) aff.getFrameLength());
  199. }
  200. /* Get an AudioInputStream from the given URL.
  201. * @see javax.sound.sampled.spi.AudioFileReader#getAudioInputStream(java.net.URL)
  202. */
  203. public AudioInputStream getAudioInputStream(URL url)
  204. throws UnsupportedAudioFileException, IOException
  205. {
  206. return getAudioInputStream(url.openStream());
  207. }
  208. }