0N/A/*
6321N/A * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.media.sound;
0N/A
0N/Aimport java.io.DataInputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.EOFException;
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.net.URL;
0N/A
0N/Aimport javax.sound.midi.MidiFileFormat;
0N/Aimport javax.sound.midi.InvalidMidiDataException;
0N/Aimport javax.sound.midi.MetaMessage;
0N/Aimport javax.sound.midi.MidiEvent;
0N/Aimport javax.sound.midi.MidiMessage;
0N/Aimport javax.sound.midi.Sequence;
0N/Aimport javax.sound.midi.SysexMessage;
0N/Aimport javax.sound.midi.Track;
0N/Aimport javax.sound.midi.spi.MidiFileReader;
0N/A
0N/A
0N/A
0N/A/**
0N/A * MIDI file reader.
0N/A *
0N/A * @author Kara Kytle
0N/A * @author Jan Borgersen
0N/A * @author Florian Bomers
0N/A */
0N/A
6321N/Apublic final class StandardMidiFileReader extends MidiFileReader {
0N/A
0N/A private static final int MThd_MAGIC = 0x4d546864; // 'MThd'
0N/A
0N/A private static final int bisBufferSize = 1024; // buffer size in buffered input streams
0N/A
0N/A public MidiFileFormat getMidiFileFormat(InputStream stream) throws InvalidMidiDataException, IOException {
0N/A return getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, null);
0N/A }
0N/A
0N/A // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
0N/A private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException {
0N/A int maxReadLength = 16;
0N/A int duration = MidiFileFormat.UNKNOWN_LENGTH;
0N/A DataInputStream dis;
0N/A
0N/A if (stream instanceof DataInputStream) {
0N/A dis = (DataInputStream) stream;
0N/A } else {
0N/A dis = new DataInputStream(stream);
0N/A }
0N/A if (smfParser == null) {
0N/A dis.mark(maxReadLength);
0N/A } else {
0N/A smfParser.stream = dis;
0N/A }
0N/A
0N/A int type;
0N/A int numtracks;
0N/A float divisionType;
0N/A int resolution;
0N/A
0N/A try {
0N/A int magic = dis.readInt();
0N/A if( !(magic == MThd_MAGIC) ) {
0N/A // not MIDI
0N/A throw new InvalidMidiDataException("not a valid MIDI file");
0N/A }
0N/A
0N/A // read header length
0N/A int bytesRemaining = dis.readInt() - 6;
0N/A type = dis.readShort();
0N/A numtracks = dis.readShort();
0N/A int timing = dis.readShort();
0N/A
0N/A // decipher the timing code
0N/A if (timing > 0) {
0N/A // tempo based timing. value is ticks per beat.
0N/A divisionType = Sequence.PPQ;
0N/A resolution = timing;
0N/A } else {
0N/A // SMPTE based timing. first decipher the frame code.
3632N/A int frameCode = -1 * (timing >> 8);
0N/A switch(frameCode) {
0N/A case 24:
0N/A divisionType = Sequence.SMPTE_24;
0N/A break;
0N/A case 25:
0N/A divisionType = Sequence.SMPTE_25;
0N/A break;
0N/A case 29:
0N/A divisionType = Sequence.SMPTE_30DROP;
0N/A break;
0N/A case 30:
0N/A divisionType = Sequence.SMPTE_30;
0N/A break;
0N/A default:
0N/A throw new InvalidMidiDataException("Unknown frame code: " + frameCode);
0N/A }
0N/A // now determine the timing resolution in ticks per frame.
0N/A resolution = timing & 0xFF;
0N/A }
0N/A if (smfParser != null) {
0N/A // remainder of this chunk
0N/A dis.skip(bytesRemaining);
0N/A smfParser.tracks = numtracks;
0N/A }
0N/A } finally {
0N/A // if only reading the file format, reset the stream
0N/A if (smfParser == null) {
0N/A dis.reset();
0N/A }
0N/A }
0N/A MidiFileFormat format = new MidiFileFormat(type, divisionType, resolution, fileLength, duration);
0N/A return format;
0N/A }
0N/A
0N/A
0N/A public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
0N/A InputStream urlStream = url.openStream(); // throws IOException
0N/A BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize );
0N/A MidiFileFormat fileFormat = null;
0N/A try {
0N/A fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException
0N/A } finally {
0N/A bis.close();
0N/A }
0N/A return fileFormat;
0N/A }
0N/A
0N/A
0N/A public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException {
0N/A FileInputStream fis = new FileInputStream(file); // throws IOException
0N/A BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize);
0N/A
0N/A // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length
0N/A long length = file.length();
0N/A if (length > Integer.MAX_VALUE) {
0N/A length = MidiFileFormat.UNKNOWN_LENGTH;
0N/A }
0N/A MidiFileFormat fileFormat = null;
0N/A try {
0N/A fileFormat = getMidiFileFormatFromStream(bis, (int) length, null);
0N/A } finally {
0N/A bis.close();
0N/A }
0N/A return fileFormat;
0N/A }
0N/A
0N/A
0N/A public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException {
0N/A SMFParser smfParser = new SMFParser();
0N/A MidiFileFormat format = getMidiFileFormatFromStream(stream,
0N/A MidiFileFormat.UNKNOWN_LENGTH,
0N/A smfParser);
0N/A
0N/A // must be MIDI Type 0 or Type 1
0N/A if ((format.getType() != 0) && (format.getType() != 1)) {
0N/A throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType());
0N/A }
0N/A
0N/A // construct the sequence object
0N/A Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution());
0N/A
0N/A // for each track, go to the beginning and read the track events
0N/A for (int i = 0; i < smfParser.tracks; i++) {
0N/A if (smfParser.nextTrack()) {
0N/A smfParser.readTrack(sequence.createTrack());
0N/A } else {
0N/A break;
0N/A }
0N/A }
0N/A return sequence;
0N/A }
0N/A
0N/A
0N/A
0N/A public Sequence getSequence(URL url) throws InvalidMidiDataException, IOException {
0N/A InputStream is = url.openStream(); // throws IOException
0N/A is = new BufferedInputStream(is, bisBufferSize);
0N/A Sequence seq = null;
0N/A try {
0N/A seq = getSequence(is);
0N/A } finally {
0N/A is.close();
0N/A }
0N/A return seq;
0N/A }
0N/A
0N/A
0N/A public Sequence getSequence(File file) throws InvalidMidiDataException, IOException {
0N/A InputStream is = new FileInputStream(file); // throws IOException
0N/A is = new BufferedInputStream(is, bisBufferSize);
0N/A Sequence seq = null;
0N/A try {
0N/A seq = getSequence(is);
0N/A } finally {
0N/A is.close();
0N/A }
0N/A return seq;
0N/A }
0N/A}
0N/A
0N/A//=============================================================================================================
0N/A
0N/A/**
0N/A * State variables during parsing of a MIDI file
0N/A */
6321N/Afinal class SMFParser {
0N/A private static final int MTrk_MAGIC = 0x4d54726b; // 'MTrk'
0N/A
0N/A // set to true to not allow corrupt MIDI files tombe loaded
0N/A private static final boolean STRICT_PARSER = false;
0N/A
0N/A private static final boolean DEBUG = false;
0N/A
0N/A int tracks; // number of tracks
0N/A DataInputStream stream; // the stream to read from
0N/A
0N/A private int trackLength = 0; // remaining length in track
0N/A private byte[] trackData = null;
0N/A private int pos = 0;
0N/A
6321N/A SMFParser() {
0N/A }
0N/A
0N/A private int readUnsigned() throws IOException {
0N/A return trackData[pos++] & 0xFF;
0N/A }
0N/A
0N/A private void read(byte[] data) throws IOException {
0N/A System.arraycopy(trackData, pos, data, 0, data.length);
0N/A pos += data.length;
0N/A }
0N/A
0N/A private long readVarInt() throws IOException {
0N/A long value = 0; // the variable-lengh int value
0N/A int currentByte = 0;
0N/A do {
0N/A currentByte = trackData[pos++] & 0xFF;
0N/A value = (value << 7) + (currentByte & 0x7F);
0N/A } while ((currentByte & 0x80) != 0);
0N/A return value;
0N/A }
0N/A
0N/A private int readIntFromStream() throws IOException {
0N/A try {
0N/A return stream.readInt();
0N/A } catch (EOFException eof) {
0N/A throw new EOFException("invalid MIDI file");
0N/A }
0N/A }
0N/A
0N/A boolean nextTrack() throws IOException, InvalidMidiDataException {
0N/A int magic;
0N/A trackLength = 0;
0N/A do {
0N/A // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
0N/A if (stream.skipBytes(trackLength) != trackLength) {
0N/A if (!STRICT_PARSER) {
0N/A return false;
0N/A }
0N/A throw new EOFException("invalid MIDI file");
0N/A }
0N/A magic = readIntFromStream();
0N/A trackLength = readIntFromStream();
0N/A } while (magic != MTrk_MAGIC);
0N/A if (!STRICT_PARSER) {
0N/A if (trackLength < 0) {
0N/A return false;
0N/A }
0N/A }
0N/A // now read track in a byte array
0N/A trackData = new byte[trackLength];
0N/A try {
0N/A // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
0N/A stream.readFully(trackData);
0N/A } catch (EOFException eof) {
0N/A if (!STRICT_PARSER) {
0N/A return false;
0N/A }
0N/A throw new EOFException("invalid MIDI file");
0N/A }
0N/A pos = 0;
0N/A return true;
0N/A }
0N/A
0N/A private boolean trackFinished() {
0N/A return pos >= trackLength;
0N/A }
0N/A
0N/A void readTrack(Track track) throws IOException, InvalidMidiDataException {
0N/A try {
0N/A // reset current tick to 0
0N/A long tick = 0;
0N/A
0N/A // reset current status byte to 0 (invalid value).
0N/A // this should cause us to throw an InvalidMidiDataException if we don't
0N/A // get a valid status byte from the beginning of the track.
0N/A int status = 0;
0N/A boolean endOfTrackFound = false;
0N/A
0N/A while (!trackFinished() && !endOfTrackFound) {
0N/A MidiMessage message;
0N/A
0N/A int data1 = -1; // initialize to invalid value
0N/A int data2 = 0;
0N/A
0N/A // each event has a tick delay and then the event data.
0N/A
0N/A // first read the delay (a variable-length int) and update our tick value
0N/A tick += readVarInt();
0N/A
0N/A // check for new status
0N/A int byteValue = readUnsigned();
0N/A
0N/A if (byteValue >= 0x80) {
0N/A status = byteValue;
0N/A } else {
0N/A data1 = byteValue;
0N/A }
0N/A
0N/A switch (status & 0xF0) {
0N/A case 0x80:
0N/A case 0x90:
0N/A case 0xA0:
0N/A case 0xB0:
0N/A case 0xE0:
0N/A // two data bytes
0N/A if (data1 == -1) {
0N/A data1 = readUnsigned();
0N/A }
0N/A data2 = readUnsigned();
0N/A message = new FastShortMessage(status | (data1 << 8) | (data2 << 16));
0N/A break;
0N/A case 0xC0:
0N/A case 0xD0:
0N/A // one data byte
0N/A if (data1 == -1) {
0N/A data1 = readUnsigned();
0N/A }
0N/A message = new FastShortMessage(status | (data1 << 8));
0N/A break;
0N/A case 0xF0:
0N/A // sys-ex or meta
0N/A switch(status) {
0N/A case 0xF0:
0N/A case 0xF7:
0N/A // sys ex
0N/A int sysexLength = (int) readVarInt();
0N/A byte[] sysexData = new byte[sysexLength];
0N/A read(sysexData);
0N/A
0N/A SysexMessage sysexMessage = new SysexMessage();
0N/A sysexMessage.setMessage(status, sysexData, sysexLength);
0N/A message = sysexMessage;
0N/A break;
0N/A
0N/A case 0xFF:
0N/A // meta
0N/A int metaType = readUnsigned();
0N/A int metaLength = (int) readVarInt();
0N/A
0N/A byte[] metaData = new byte[metaLength];
0N/A read(metaData);
0N/A
0N/A MetaMessage metaMessage = new MetaMessage();
0N/A metaMessage.setMessage(metaType, metaData, metaLength);
0N/A message = metaMessage;
0N/A if (metaType == 0x2F) {
0N/A // end of track means it!
0N/A endOfTrackFound = true;
0N/A }
0N/A break;
0N/A default:
0N/A throw new InvalidMidiDataException("Invalid status byte: " + status);
0N/A } // switch sys-ex or meta
0N/A break;
0N/A default:
0N/A throw new InvalidMidiDataException("Invalid status byte: " + status);
0N/A } // switch
0N/A track.add(new MidiEvent(message, tick));
0N/A } // while
0N/A } catch (ArrayIndexOutOfBoundsException e) {
0N/A if (DEBUG) e.printStackTrace();
0N/A // fix for 4834374
0N/A throw new EOFException("invalid MIDI file");
0N/A }
0N/A }
0N/A
0N/A}