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