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