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