|
1 /* |
|
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.media.sound; |
|
27 |
|
28 import java.io.DataInputStream; |
|
29 import java.io.File; |
|
30 import java.io.FileInputStream; |
|
31 import java.io.InputStream; |
|
32 import java.io.IOException; |
|
33 import java.io.EOFException; |
|
34 import java.io.BufferedInputStream; |
|
35 import java.net.URL; |
|
36 |
|
37 import javax.sound.midi.MidiFileFormat; |
|
38 import javax.sound.midi.InvalidMidiDataException; |
|
39 import javax.sound.midi.MetaMessage; |
|
40 import javax.sound.midi.MidiEvent; |
|
41 import javax.sound.midi.MidiMessage; |
|
42 import javax.sound.midi.Sequence; |
|
43 import javax.sound.midi.SysexMessage; |
|
44 import javax.sound.midi.Track; |
|
45 import javax.sound.midi.spi.MidiFileReader; |
|
46 |
|
47 |
|
48 |
|
49 /** |
|
50 * MIDI file reader. |
|
51 * |
|
52 * @author Kara Kytle |
|
53 * @author Jan Borgersen |
|
54 * @author Florian Bomers |
|
55 */ |
|
56 |
|
57 public final class StandardMidiFileReader extends MidiFileReader { |
|
58 |
|
59 private static final int MThd_MAGIC = 0x4d546864; // 'MThd' |
|
60 |
|
61 private static final int bisBufferSize = 1024; // buffer size in buffered input streams |
|
62 |
|
63 public MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException { |
|
64 return getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, null); |
|
65 } |
|
66 |
|
67 // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length |
|
68 private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException { |
|
69 int maxReadLength = 16; |
|
70 int duration = MidiFileFormat.UNKNOWN_LENGTH; |
|
71 DataInputStream dis; |
|
72 |
|
73 if (stream instanceof DataInputStream) { |
|
74 dis = (DataInputStream) stream; |
|
75 } else { |
|
76 dis = new DataInputStream(stream); |
|
77 } |
|
78 if (smfParser == null) { |
|
79 dis.mark(maxReadLength); |
|
80 } else { |
|
81 smfParser.stream = dis; |
|
82 } |
|
83 |
|
84 int type; |
|
85 int numtracks; |
|
86 float divisionType; |
|
87 int resolution; |
|
88 |
|
89 try { |
|
90 int magic = dis.readInt(); |
|
91 if( !(magic == MThd_MAGIC) ) { |
|
92 // not MIDI |
|
93 throw new InvalidMidiDataException("not a valid MIDI file"); |
|
94 } |
|
95 |
|
96 // read header length |
|
97 int bytesRemaining = dis.readInt() - 6; |
|
98 type = dis.readShort(); |
|
99 numtracks = dis.readShort(); |
|
100 int timing = dis.readShort(); |
|
101 |
|
102 // decipher the timing code |
|
103 if (timing > 0) { |
|
104 // tempo based timing. value is ticks per beat. |
|
105 divisionType = Sequence.PPQ; |
|
106 resolution = timing; |
|
107 } else { |
|
108 // SMPTE based timing. first decipher the frame code. |
|
109 int frameCode = -1 * (timing >> 8); |
|
110 switch(frameCode) { |
|
111 case 24: |
|
112 divisionType = Sequence.SMPTE_24; |
|
113 break; |
|
114 case 25: |
|
115 divisionType = Sequence.SMPTE_25; |
|
116 break; |
|
117 case 29: |
|
118 divisionType = Sequence.SMPTE_30DROP; |
|
119 break; |
|
120 case 30: |
|
121 divisionType = Sequence.SMPTE_30; |
|
122 break; |
|
123 default: |
|
124 throw new InvalidMidiDataException("Unknown frame code: " + frameCode); |
|
125 } |
|
126 // now determine the timing resolution in ticks per frame. |
|
127 resolution = timing & 0xFF; |
|
128 } |
|
129 if (smfParser != null) { |
|
130 // remainder of this chunk |
|
131 dis.skip(bytesRemaining); |
|
132 smfParser.tracks = numtracks; |
|
133 } |
|
134 } finally { |
|
135 // if only reading the file format, reset the stream |
|
136 if (smfParser == null) { |
|
137 dis.reset(); |
|
138 } |
|
139 } |
|
140 MidiFileFormat format = new MidiFileFormat(type, divisionType, resolution, fileLength, duration); |
|
141 return format; |
|
142 } |
|
143 |
|
144 |
|
145 public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException { |
|
146 InputStream urlStream = url.openStream(); // throws IOException |
|
147 BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize ); |
|
148 MidiFileFormat fileFormat = null; |
|
149 try { |
|
150 fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException |
|
151 } finally { |
|
152 bis.close(); |
|
153 } |
|
154 return fileFormat; |
|
155 } |
|
156 |
|
157 |
|
158 public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException { |
|
159 FileInputStream fis = new FileInputStream(file); // throws IOException |
|
160 BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize); |
|
161 |
|
162 // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length |
|
163 long length = file.length(); |
|
164 if (length > Integer.MAX_VALUE) { |
|
165 length = MidiFileFormat.UNKNOWN_LENGTH; |
|
166 } |
|
167 MidiFileFormat fileFormat = null; |
|
168 try { |
|
169 fileFormat = getMidiFileFormatFromStream(bis, (int) length, null); |
|
170 } finally { |
|
171 bis.close(); |
|
172 } |
|
173 return fileFormat; |
|
174 } |
|
175 |
|
176 |
|
177 public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException { |
|
178 SMFParser smfParser = new SMFParser(); |
|
179 MidiFileFormat format = getMidiFileFormatFromStream(stream, |
|
180 MidiFileFormat.UNKNOWN_LENGTH, |
|
181 smfParser); |
|
182 |
|
183 // must be MIDI Type 0 or Type 1 |
|
184 if ((format.getType() != 0) && (format.getType() != 1)) { |
|
185 throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType()); |
|
186 } |
|
187 |
|
188 // construct the sequence object |
|
189 Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution()); |
|
190 |
|
191 // for each track, go to the beginning and read the track events |
|
192 for (int i = 0; i < smfParser.tracks; i++) { |
|
193 if (smfParser.nextTrack()) { |
|
194 smfParser.readTrack(sequence.createTrack()); |
|
195 } else { |
|
196 break; |
|
197 } |
|
198 } |
|
199 return sequence; |
|
200 } |
|
201 |
|
202 |
|
203 |
|
204 public Sequence getSequence(URL url) throws InvalidMidiDataException, IOException { |
|
205 InputStream is = url.openStream(); // throws IOException |
|
206 is = new BufferedInputStream(is, bisBufferSize); |
|
207 Sequence seq = null; |
|
208 try { |
|
209 seq = getSequence(is); |
|
210 } finally { |
|
211 is.close(); |
|
212 } |
|
213 return seq; |
|
214 } |
|
215 |
|
216 |
|
217 public Sequence getSequence(File file) throws InvalidMidiDataException, IOException { |
|
218 InputStream is = new FileInputStream(file); // throws IOException |
|
219 is = new BufferedInputStream(is, bisBufferSize); |
|
220 Sequence seq = null; |
|
221 try { |
|
222 seq = getSequence(is); |
|
223 } finally { |
|
224 is.close(); |
|
225 } |
|
226 return seq; |
|
227 } |
|
228 } |
|
229 |
|
230 //============================================================================================================= |
|
231 |
|
232 /** |
|
233 * State variables during parsing of a MIDI file |
|
234 */ |
|
235 final class SMFParser { |
|
236 private static final int MTrk_MAGIC = 0x4d54726b; // 'MTrk' |
|
237 |
|
238 // set to true to not allow corrupt MIDI files tombe loaded |
|
239 private static final boolean STRICT_PARSER = false; |
|
240 |
|
241 private static final boolean DEBUG = false; |
|
242 |
|
243 int tracks; // number of tracks |
|
244 DataInputStream stream; // the stream to read from |
|
245 |
|
246 private int trackLength = 0; // remaining length in track |
|
247 private byte[] trackData = null; |
|
248 private int pos = 0; |
|
249 |
|
250 SMFParser() { |
|
251 } |
|
252 |
|
253 private int readUnsigned() throws IOException { |
|
254 return trackData[pos++] & 0xFF; |
|
255 } |
|
256 |
|
257 private void read(byte[] data) throws IOException { |
|
258 System.arraycopy(trackData, pos, data, 0, data.length); |
|
259 pos += data.length; |
|
260 } |
|
261 |
|
262 private long readVarInt() throws IOException { |
|
263 long value = 0; // the variable-lengh int value |
|
264 int currentByte = 0; |
|
265 do { |
|
266 currentByte = trackData[pos++] & 0xFF; |
|
267 value = (value << 7) + (currentByte & 0x7F); |
|
268 } while ((currentByte & 0x80) != 0); |
|
269 return value; |
|
270 } |
|
271 |
|
272 private int readIntFromStream() throws IOException { |
|
273 try { |
|
274 return stream.readInt(); |
|
275 } catch (EOFException eof) { |
|
276 throw new EOFException("invalid MIDI file"); |
|
277 } |
|
278 } |
|
279 |
|
280 boolean nextTrack() throws IOException, InvalidMidiDataException { |
|
281 int magic; |
|
282 trackLength = 0; |
|
283 do { |
|
284 // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection |
|
285 if (stream.skipBytes(trackLength) != trackLength) { |
|
286 if (!STRICT_PARSER) { |
|
287 return false; |
|
288 } |
|
289 throw new EOFException("invalid MIDI file"); |
|
290 } |
|
291 magic = readIntFromStream(); |
|
292 trackLength = readIntFromStream(); |
|
293 } while (magic != MTrk_MAGIC); |
|
294 if (!STRICT_PARSER) { |
|
295 if (trackLength < 0) { |
|
296 return false; |
|
297 } |
|
298 } |
|
299 // now read track in a byte array |
|
300 trackData = new byte[trackLength]; |
|
301 try { |
|
302 // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection |
|
303 stream.readFully(trackData); |
|
304 } catch (EOFException eof) { |
|
305 if (!STRICT_PARSER) { |
|
306 return false; |
|
307 } |
|
308 throw new EOFException("invalid MIDI file"); |
|
309 } |
|
310 pos = 0; |
|
311 return true; |
|
312 } |
|
313 |
|
314 private boolean trackFinished() { |
|
315 return pos >= trackLength; |
|
316 } |
|
317 |
|
318 void readTrack(Track track) throws IOException, InvalidMidiDataException { |
|
319 try { |
|
320 // reset current tick to 0 |
|
321 long tick = 0; |
|
322 |
|
323 // reset current status byte to 0 (invalid value). |
|
324 // this should cause us to throw an InvalidMidiDataException if we don't |
|
325 // get a valid status byte from the beginning of the track. |
|
326 int status = 0; |
|
327 boolean endOfTrackFound = false; |
|
328 |
|
329 while (!trackFinished() && !endOfTrackFound) { |
|
330 MidiMessage message; |
|
331 |
|
332 int data1 = -1; // initialize to invalid value |
|
333 int data2 = 0; |
|
334 |
|
335 // each event has a tick delay and then the event data. |
|
336 |
|
337 // first read the delay (a variable-length int) and update our tick value |
|
338 tick += readVarInt(); |
|
339 |
|
340 // check for new status |
|
341 int byteValue = readUnsigned(); |
|
342 |
|
343 if (byteValue >= 0x80) { |
|
344 status = byteValue; |
|
345 } else { |
|
346 data1 = byteValue; |
|
347 } |
|
348 |
|
349 switch (status & 0xF0) { |
|
350 case 0x80: |
|
351 case 0x90: |
|
352 case 0xA0: |
|
353 case 0xB0: |
|
354 case 0xE0: |
|
355 // two data bytes |
|
356 if (data1 == -1) { |
|
357 data1 = readUnsigned(); |
|
358 } |
|
359 data2 = readUnsigned(); |
|
360 message = new FastShortMessage(status | (data1 << 8) | (data2 << 16)); |
|
361 break; |
|
362 case 0xC0: |
|
363 case 0xD0: |
|
364 // one data byte |
|
365 if (data1 == -1) { |
|
366 data1 = readUnsigned(); |
|
367 } |
|
368 message = new FastShortMessage(status | (data1 << 8)); |
|
369 break; |
|
370 case 0xF0: |
|
371 // sys-ex or meta |
|
372 switch(status) { |
|
373 case 0xF0: |
|
374 case 0xF7: |
|
375 // sys ex |
|
376 int sysexLength = (int) readVarInt(); |
|
377 byte[] sysexData = new byte[sysexLength]; |
|
378 read(sysexData); |
|
379 |
|
380 SysexMessage sysexMessage = new SysexMessage(); |
|
381 sysexMessage.setMessage(status, sysexData, sysexLength); |
|
382 message = sysexMessage; |
|
383 break; |
|
384 |
|
385 case 0xFF: |
|
386 // meta |
|
387 int metaType = readUnsigned(); |
|
388 int metaLength = (int) readVarInt(); |
|
389 |
|
390 byte[] metaData = new byte[metaLength]; |
|
391 read(metaData); |
|
392 |
|
393 MetaMessage metaMessage = new MetaMessage(); |
|
394 metaMessage.setMessage(metaType, metaData, metaLength); |
|
395 message = metaMessage; |
|
396 if (metaType == 0x2F) { |
|
397 // end of track means it! |
|
398 endOfTrackFound = true; |
|
399 } |
|
400 break; |
|
401 default: |
|
402 throw new InvalidMidiDataException("Invalid status byte: " + status); |
|
403 } // switch sys-ex or meta |
|
404 break; |
|
405 default: |
|
406 throw new InvalidMidiDataException("Invalid status byte: " + status); |
|
407 } // switch |
|
408 track.add(new MidiEvent(message, tick)); |
|
409 } // while |
|
410 } catch (ArrayIndexOutOfBoundsException e) { |
|
411 if (DEBUG) e.printStackTrace(); |
|
412 // fix for 4834374 |
|
413 throw new EOFException("invalid MIDI file"); |
|
414 } |
|
415 } |
|
416 |
|
417 } |