MidiFileReader.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* MidiFileReader.java -- Read MIDI 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.midi.file;
  32. import java.io.DataInputStream;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.IOException;
  36. import java.io.InputStream;
  37. import java.net.URL;
  38. import javax.sound.midi.InvalidMidiDataException;
  39. import javax.sound.midi.MetaMessage;
  40. import javax.sound.midi.MidiEvent;
  41. import javax.sound.midi.MidiFileFormat;
  42. import javax.sound.midi.MidiMessage;
  43. import javax.sound.midi.Sequence;
  44. import javax.sound.midi.ShortMessage;
  45. import javax.sound.midi.SysexMessage;
  46. import javax.sound.midi.Track;
  47. /**
  48. * A MIDI file reader.
  49. *
  50. * This code reads MIDI file types 0 and 1.
  51. *
  52. * There are many decent documents on the web describing the MIDI file
  53. * format. I didn't bother looking for the official document. If it
  54. * exists, I'm not even sure if it is freely available. We should
  55. * update this comment if we find out anything helpful here.
  56. *
  57. * @author Anthony Green (green@redhat.com)
  58. *
  59. */
  60. public class MidiFileReader extends javax.sound.midi.spi.MidiFileReader
  61. {
  62. /* Get the MidiFileFormat for the given input stream.
  63. * @see javax.sound.midi.spi.MidiFileReader#getMidiFileFormat(java.io.InputStream)
  64. */
  65. public MidiFileFormat getMidiFileFormat(InputStream in)
  66. throws InvalidMidiDataException, IOException
  67. {
  68. DataInputStream din;
  69. if (in instanceof DataInputStream)
  70. din = (DataInputStream) in;
  71. else
  72. din = new DataInputStream(in);
  73. int type, ntracks, division, resolution, bytes;
  74. float divisionType;
  75. if (din.readInt() != 0x4d546864) // "MThd"
  76. throw new InvalidMidiDataException("Invalid MIDI chunk header.");
  77. bytes = din.readInt();
  78. if (bytes < 6)
  79. throw new
  80. InvalidMidiDataException("Invalid MIDI chunk header length: " + bytes);
  81. type = din.readShort();
  82. if (type < 0 || type > 2)
  83. throw new
  84. InvalidMidiDataException("Invalid MIDI file type value: " + type);
  85. ntracks = din.readShort();
  86. if (ntracks <= 0)
  87. throw new
  88. InvalidMidiDataException("Invalid number of MIDI tracks: " + ntracks);
  89. division = din.readShort();
  90. if ((division & 0x8000) != 0)
  91. {
  92. division = -((division >>> 8) & 0xFF);
  93. switch (division)
  94. {
  95. case 24:
  96. divisionType = Sequence.SMPTE_24;
  97. break;
  98. case 25:
  99. divisionType = Sequence.SMPTE_25;
  100. break;
  101. case 29:
  102. divisionType = Sequence.SMPTE_30DROP;
  103. break;
  104. case 30:
  105. divisionType = Sequence.SMPTE_30;
  106. break;
  107. default:
  108. throw new
  109. InvalidMidiDataException("Invalid MIDI frame division type: "
  110. + division);
  111. }
  112. resolution = division & 0xff;
  113. }
  114. else
  115. {
  116. divisionType = Sequence.PPQ;
  117. resolution = division & 0x7fff;
  118. }
  119. // If we haven't read every byte in the header now, just skip the rest.
  120. din.skip(bytes - 6);
  121. return new ExtendedMidiFileFormat(type, divisionType, resolution,
  122. MidiFileFormat.UNKNOWN_LENGTH,
  123. MidiFileFormat.UNKNOWN_LENGTH, ntracks);
  124. }
  125. /* Get the MidiFileFormat from the given URL.
  126. * @see javax.sound.midi.spi.MidiFileReader#getMidiFileFormat(java.net.URL)
  127. */
  128. public MidiFileFormat getMidiFileFormat(URL url)
  129. throws InvalidMidiDataException, IOException
  130. {
  131. InputStream is = url.openStream();
  132. try
  133. {
  134. return getMidiFileFormat(is);
  135. }
  136. finally
  137. {
  138. is.close();
  139. }
  140. }
  141. /* Get the MidiFileFormat from the given file.
  142. * @see javax.sound.midi.spi.MidiFileReader#getMidiFileFormat(java.io.File)
  143. */
  144. public MidiFileFormat getMidiFileFormat(File file)
  145. throws InvalidMidiDataException, IOException
  146. {
  147. InputStream is = new FileInputStream(file);
  148. try
  149. {
  150. return getMidiFileFormat(is);
  151. }
  152. finally
  153. {
  154. is.close();
  155. }
  156. }
  157. /* Get the MIDI Sequence found in this input stream.
  158. * @see javax.sound.midi.spi.MidiFileReader#getSequence(java.io.InputStream)
  159. */
  160. public Sequence getSequence(InputStream is) throws InvalidMidiDataException,
  161. IOException
  162. {
  163. MidiDataInputStream din = new MidiDataInputStream(is);
  164. ExtendedMidiFileFormat mff = (ExtendedMidiFileFormat) getMidiFileFormat(din);
  165. Sequence seq = new Sequence(mff.getDivisionType(), mff.getResolution());
  166. int ntracks = mff.getNumberTracks();
  167. while (ntracks-- > 0)
  168. {
  169. Track track = seq.createTrack();
  170. int Mtrk = din.readInt();
  171. if (Mtrk != 0x4d54726b)
  172. throw new InvalidMidiDataException("Invalid MIDI track header.");
  173. din.readInt(); // length
  174. int runningStatus = -1;
  175. int click = 0;
  176. // Set this to true when we've hit an End of Track meta event.
  177. boolean done = false;
  178. // Read all events.
  179. while (! done)
  180. {
  181. MidiMessage mm;
  182. int dtime = din.readVariableLengthInt();
  183. click += dtime;
  184. int sbyte = din.readUnsignedByte();
  185. if (sbyte < 0xf0)
  186. {
  187. ShortMessage sm;
  188. switch (sbyte & 0xf0)
  189. {
  190. case ShortMessage.NOTE_OFF:
  191. case ShortMessage.NOTE_ON:
  192. case ShortMessage.POLY_PRESSURE:
  193. case ShortMessage.CONTROL_CHANGE:
  194. case ShortMessage.PITCH_BEND:
  195. case ShortMessage.SONG_POSITION_POINTER:
  196. sm = new ShortMessage();
  197. sm.setMessage(sbyte, din.readByte(), din.readByte());
  198. runningStatus = sbyte;
  199. break;
  200. case ShortMessage.PROGRAM_CHANGE:
  201. case ShortMessage.CHANNEL_PRESSURE:
  202. case ShortMessage.SONG_SELECT:
  203. case 0xF5: // FIXME: unofficial bus select. Not in spec??
  204. sm = new ShortMessage();
  205. sm.setMessage(sbyte, din.readByte(), 0);
  206. runningStatus = sbyte;
  207. break;
  208. case ShortMessage.TUNE_REQUEST:
  209. case ShortMessage.END_OF_EXCLUSIVE:
  210. case ShortMessage.TIMING_CLOCK:
  211. case ShortMessage.START:
  212. case ShortMessage.CONTINUE:
  213. case ShortMessage.STOP:
  214. case ShortMessage.ACTIVE_SENSING:
  215. case ShortMessage.SYSTEM_RESET:
  216. sm = new ShortMessage();
  217. sm.setMessage(sbyte, 0, 0);
  218. runningStatus = sbyte;
  219. break;
  220. default:
  221. if (runningStatus != - 1)
  222. {
  223. switch (runningStatus & 0xf0)
  224. {
  225. case ShortMessage.NOTE_OFF:
  226. case ShortMessage.NOTE_ON:
  227. case ShortMessage.POLY_PRESSURE:
  228. case ShortMessage.CONTROL_CHANGE:
  229. case ShortMessage.PITCH_BEND:
  230. case ShortMessage.SONG_POSITION_POINTER:
  231. sm = new ShortMessage();
  232. sm.setMessage(runningStatus, sbyte, din.readByte());
  233. break;
  234. case ShortMessage.PROGRAM_CHANGE:
  235. case ShortMessage.CHANNEL_PRESSURE:
  236. case ShortMessage.SONG_SELECT:
  237. case 0xF5: // FIXME: unofficial bus select. Not in
  238. // spec??
  239. sm = new ShortMessage();
  240. sm.setMessage(runningStatus, sbyte, 0);
  241. continue;
  242. case ShortMessage.TUNE_REQUEST:
  243. case ShortMessage.END_OF_EXCLUSIVE:
  244. case ShortMessage.TIMING_CLOCK:
  245. case ShortMessage.START:
  246. case ShortMessage.CONTINUE:
  247. case ShortMessage.STOP:
  248. case ShortMessage.ACTIVE_SENSING:
  249. case ShortMessage.SYSTEM_RESET:
  250. sm = new ShortMessage();
  251. sm.setMessage(runningStatus, 0, 0);
  252. continue;
  253. default:
  254. throw new
  255. InvalidMidiDataException("Invalid Short MIDI Event: "
  256. + sbyte);
  257. }
  258. }
  259. else
  260. throw new
  261. InvalidMidiDataException("Invalid Short MIDI Event: "
  262. + sbyte);
  263. }
  264. mm = sm;
  265. }
  266. else if (sbyte == 0xf0 || sbyte == 0xf7)
  267. {
  268. // System Exclusive event
  269. int slen = din.readVariableLengthInt();
  270. byte sysex[] = new byte[slen];
  271. din.readFully(sysex);
  272. SysexMessage sm = new SysexMessage();
  273. sm.setMessage(sbyte, sysex, slen);
  274. mm = sm;
  275. runningStatus = - 1;
  276. }
  277. else if (sbyte == 0xff)
  278. {
  279. // Meta Message
  280. byte mtype = din.readByte();
  281. int mlen = din.readVariableLengthInt();
  282. byte meta[] = new byte[mlen];
  283. din.readFully(meta);
  284. MetaMessage metam = new MetaMessage();
  285. metam.setMessage(mtype, meta, mlen);
  286. mm = metam;
  287. if (mtype == 0x2f) // End of Track
  288. done = true;
  289. runningStatus = - 1;
  290. }
  291. else
  292. {
  293. throw new InvalidMidiDataException("Invalid status byte: "
  294. + sbyte);
  295. }
  296. track.add(new MidiEvent(mm, click));
  297. }
  298. }
  299. return seq;
  300. }
  301. /* Get the MIDI Sequence found at the given URL.
  302. * @see javax.sound.midi.spi.MidiFileReader#getSequence(java.net.URL)
  303. */
  304. public Sequence getSequence(URL url) throws InvalidMidiDataException,
  305. IOException
  306. {
  307. InputStream is = url.openStream();
  308. try
  309. {
  310. return getSequence(is);
  311. }
  312. finally
  313. {
  314. is.close();
  315. }
  316. }
  317. /* Get the MIDI Sequence found in the given file.
  318. * @see javax.sound.midi.spi.MidiFileReader#getSequence(java.io.File)
  319. */
  320. public Sequence getSequence(File file) throws InvalidMidiDataException,
  321. IOException
  322. {
  323. InputStream is = new FileInputStream(file);
  324. try
  325. {
  326. return getSequence(is);
  327. }
  328. finally
  329. {
  330. is.close();
  331. }
  332. }
  333. }