/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* MIDI file reader.
*
* @author Kara Kytle
* @author Jan Borgersen
* @author Florian Bomers
*/
public MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException {
}
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException {
int maxReadLength = 16;
if (stream instanceof DataInputStream) {
} else {
}
} else {
}
int type;
int numtracks;
float divisionType;
int resolution;
try {
if( !(magic == MThd_MAGIC) ) {
// not MIDI
throw new InvalidMidiDataException("not a valid MIDI file");
}
// read header length
// decipher the timing code
if (timing > 0) {
// tempo based timing. value is ticks per beat.
resolution = timing;
} else {
// SMPTE based timing. first decipher the frame code.
switch(frameCode) {
case 24:
break;
case 25:
break;
case 29:
break;
case 30:
break;
default:
}
// now determine the timing resolution in ticks per frame.
}
// remainder of this chunk
}
} finally {
// if only reading the file format, reset the stream
}
}
return format;
}
try {
} finally {
}
return fileFormat;
}
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
}
try {
} finally {
}
return fileFormat;
}
// must be MIDI Type 0 or Type 1
}
// construct the sequence object
// for each track, go to the beginning and read the track events
} else {
break;
}
}
return sequence;
}
try {
} finally {
}
return seq;
}
try {
} finally {
}
return seq;
}
}
//=============================================================================================================
/**
* State variables during parsing of a MIDI file
*/
final class SMFParser {
// set to true to not allow corrupt MIDI files tombe loaded
private static final boolean STRICT_PARSER = false;
private static final boolean DEBUG = false;
SMFParser() {
}
}
}
int currentByte = 0;
do {
return value;
}
try {
} catch (EOFException eof) {
throw new EOFException("invalid MIDI file");
}
}
int magic;
trackLength = 0;
do {
// $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
if (!STRICT_PARSER) {
return false;
}
throw new EOFException("invalid MIDI file");
}
magic = readIntFromStream();
} while (magic != MTrk_MAGIC);
if (!STRICT_PARSER) {
if (trackLength < 0) {
return false;
}
}
// now read track in a byte array
trackData = new byte[trackLength];
try {
// $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
} catch (EOFException eof) {
if (!STRICT_PARSER) {
return false;
}
throw new EOFException("invalid MIDI file");
}
pos = 0;
return true;
}
private boolean trackFinished() {
return pos >= trackLength;
}
try {
// reset current tick to 0
long tick = 0;
// reset current status byte to 0 (invalid value).
// this should cause us to throw an InvalidMidiDataException if we don't
// get a valid status byte from the beginning of the track.
int status = 0;
boolean endOfTrackFound = false;
while (!trackFinished() && !endOfTrackFound) {
int data2 = 0;
// each event has a tick delay and then the event data.
// first read the delay (a variable-length int) and update our tick value
tick += readVarInt();
// check for new status
int byteValue = readUnsigned();
if (byteValue >= 0x80) {
} else {
}
switch (status & 0xF0) {
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xE0:
// two data bytes
if (data1 == -1) {
data1 = readUnsigned();
}
data2 = readUnsigned();
break;
case 0xC0:
case 0xD0:
// one data byte
if (data1 == -1) {
data1 = readUnsigned();
}
break;
case 0xF0:
// sys-ex or meta
switch(status) {
case 0xF0:
case 0xF7:
// sys ex
int sysexLength = (int) readVarInt();
byte[] sysexData = new byte[sysexLength];
break;
case 0xFF:
// meta
int metaType = readUnsigned();
int metaLength = (int) readVarInt();
byte[] metaData = new byte[metaLength];
if (metaType == 0x2F) {
// end of track means it!
endOfTrackFound = true;
}
break;
default:
} // switch sys-ex or meta
break;
default:
} // switch
} // while
} catch (ArrayIndexOutOfBoundsException e) {
if (DEBUG) e.printStackTrace();
// fix for 4834374
throw new EOFException("invalid MIDI file");
}
}
}