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.DataOutputStream;
0N/Aimport java.io.PipedInputStream;
0N/Aimport java.io.PipedOutputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.SequenceInputStream;
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/A
0N/Aimport javax.sound.midi.InvalidMidiDataException;
0N/Aimport javax.sound.midi.MidiEvent;
0N/Aimport javax.sound.midi.MetaMessage;
0N/Aimport javax.sound.midi.Sequence;
0N/Aimport javax.sound.midi.ShortMessage;
0N/Aimport javax.sound.midi.SysexMessage;
0N/Aimport javax.sound.midi.Track;
0N/Aimport javax.sound.midi.spi.MidiFileWriter;
0N/A
0N/A
0N/A/**
0N/A * MIDI file writer.
0N/A *
0N/A * @author Kara Kytle
0N/A * @author Jan Borgersen
0N/A */
6321N/Apublic final class StandardMidiFileWriter extends MidiFileWriter {
0N/A
0N/A private static final int MThd_MAGIC = 0x4d546864; // 'MThd'
0N/A private static final int MTrk_MAGIC = 0x4d54726b; // 'MTrk'
0N/A
0N/A private static final int ONE_BYTE = 1;
0N/A private static final int TWO_BYTE = 2;
0N/A private static final int SYSEX = 3;
0N/A private static final int META = 4;
0N/A private static final int ERROR = 5;
0N/A private static final int IGNORE = 6;
0N/A
0N/A private static final int MIDI_TYPE_0 = 0;
0N/A private static final int MIDI_TYPE_1 = 1;
0N/A
0N/A private static final int bufferSize = 16384; // buffersize for write
0N/A private DataOutputStream tddos; // data output stream for track writing
0N/A
0N/A
0N/A
0N/A /**
0N/A * MIDI parser types
0N/A */
1504N/A private static final int types[] = {
0N/A MIDI_TYPE_0,
0N/A MIDI_TYPE_1
0N/A };
0N/A
0N/A
0N/A /**
0N/A * new
0N/A */
0N/A public int[] getMidiFileTypes() {
0N/A int[] localArray = new int[types.length];
0N/A System.arraycopy(types, 0, localArray, 0, types.length);
0N/A return localArray;
0N/A }
0N/A
0N/A /**
0N/A * Obtains the file types that this provider can write from the
0N/A * sequence specified.
0N/A * @param sequence the sequence for which midi file type support
0N/A * is queried
0N/A * @return array of file types. If no file types are supported,
0N/A * returns an array of length 0.
0N/A */
0N/A public int[] getMidiFileTypes(Sequence sequence){
0N/A int typesArray[];
0N/A Track tracks[] = sequence.getTracks();
0N/A
0N/A if( tracks.length==1 ) {
0N/A typesArray = new int[2];
0N/A typesArray[0] = MIDI_TYPE_0;
0N/A typesArray[1] = MIDI_TYPE_1;
0N/A } else {
0N/A typesArray = new int[1];
0N/A typesArray[0] = MIDI_TYPE_1;
0N/A }
0N/A
0N/A return typesArray;
0N/A }
0N/A
0N/A public boolean isFileTypeSupported(int type) {
0N/A for(int i=0; i<types.length; i++) {
0N/A if( type == types[i] ) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A public int write(Sequence in, int type, OutputStream out) throws IOException {
0N/A byte [] buffer = null;
0N/A
0N/A int bytesRead = 0;
0N/A long bytesWritten = 0;
0N/A
0N/A if( !isFileTypeSupported(type,in) ) {
0N/A throw new IllegalArgumentException("Could not write MIDI file");
0N/A }
0N/A // First get the fileStream from this sequence
0N/A InputStream fileStream = getFileStream(type,in);
0N/A if (fileStream == null) {
0N/A throw new IllegalArgumentException("Could not write MIDI file");
0N/A }
0N/A buffer = new byte[bufferSize];
0N/A
0N/A while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
0N/A out.write( buffer, 0, (int)bytesRead );
0N/A bytesWritten += bytesRead;
0N/A }
0N/A // Done....return bytesWritten
0N/A return (int) bytesWritten;
0N/A }
0N/A
0N/A public int write(Sequence in, int type, File out) throws IOException {
0N/A FileOutputStream fos = new FileOutputStream(out); // throws IOException
0N/A int bytesWritten = write( in, type, fos );
0N/A fos.close();
0N/A return bytesWritten;
0N/A }
0N/A
0N/A //=================================================================================
0N/A
0N/A
0N/A private InputStream getFileStream(int type, Sequence sequence) throws IOException {
0N/A Track tracks[] = sequence.getTracks();
0N/A int bytesBuilt = 0;
0N/A int headerLength = 14;
0N/A int length = 0;
0N/A int timeFormat;
0N/A float divtype;
0N/A
0N/A PipedOutputStream hpos = null;
0N/A DataOutputStream hdos = null;
0N/A PipedInputStream headerStream = null;
0N/A
0N/A InputStream trackStreams [] = null;
0N/A InputStream trackStream = null;
0N/A InputStream fStream = null;
0N/A
0N/A // Determine the filetype to write
0N/A if( type==MIDI_TYPE_0 ) {
0N/A if (tracks.length != 1) {
0N/A return null;
0N/A }
0N/A } else if( type==MIDI_TYPE_1 ) {
0N/A if (tracks.length < 1) { // $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
0N/A return null;
0N/A }
0N/A } else {
0N/A if(tracks.length==1) {
0N/A type = MIDI_TYPE_0;
0N/A } else if(tracks.length>1) {
0N/A type = MIDI_TYPE_1;
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A // Now build the file one track at a time
0N/A // Note that above we made sure that MIDI_TYPE_0 only happens
0N/A // if tracks.length==1
0N/A
0N/A trackStreams = new InputStream[tracks.length];
0N/A int trackCount = 0;
0N/A for(int i=0; i<tracks.length; i++) {
0N/A try {
0N/A trackStreams[trackCount] = writeTrack( tracks[i], type );
0N/A trackCount++;
0N/A } catch (InvalidMidiDataException e) {
0N/A if(Printer.err) Printer.err("Exception in write: " + e.getMessage());
0N/A }
0N/A //bytesBuilt += trackStreams[i].getLength();
0N/A }
0N/A
0N/A // Now seqence the track streams
0N/A if( trackCount == 1 ) {
0N/A trackStream = trackStreams[0];
0N/A } else if( trackCount > 1 ){
0N/A trackStream = trackStreams[0];
0N/A for(int i=1; i<tracks.length; i++) {
0N/A // fix for 5048381: NullPointerException when saving a MIDI sequence
0N/A // don't include failed track streams
0N/A if (trackStreams[i] != null) {
0N/A trackStream = new SequenceInputStream( trackStream, trackStreams[i]);
0N/A }
0N/A }
0N/A } else {
0N/A throw new IllegalArgumentException("invalid MIDI data in sequence");
0N/A }
0N/A
0N/A // Now build the header...
0N/A hpos = new PipedOutputStream();
0N/A hdos = new DataOutputStream(hpos);
0N/A headerStream = new PipedInputStream(hpos);
0N/A
0N/A // Write the magic number
0N/A hdos.writeInt( MThd_MAGIC );
0N/A
0N/A // Write the header length
0N/A hdos.writeInt( headerLength - 8 );
0N/A
0N/A // Write the filetype
0N/A if(type==MIDI_TYPE_0) {
0N/A hdos.writeShort( 0 );
0N/A } else {
0N/A // MIDI_TYPE_1
0N/A hdos.writeShort( 1 );
0N/A }
0N/A
0N/A // Write the number of tracks
0N/A hdos.writeShort( (short) trackCount );
0N/A
0N/A // Determine and write the timing format
0N/A divtype = sequence.getDivisionType();
0N/A if( divtype == Sequence.PPQ ) {
0N/A timeFormat = sequence.getResolution();
0N/A } else if( divtype == Sequence.SMPTE_24) {
0N/A timeFormat = (24<<8) * -1;
0N/A timeFormat += (sequence.getResolution() & 0xFF);
0N/A } else if( divtype == Sequence.SMPTE_25) {
0N/A timeFormat = (25<<8) * -1;
0N/A timeFormat += (sequence.getResolution() & 0xFF);
0N/A } else if( divtype == Sequence.SMPTE_30DROP) {
0N/A timeFormat = (29<<8) * -1;
0N/A timeFormat += (sequence.getResolution() & 0xFF);
0N/A } else if( divtype == Sequence.SMPTE_30) {
0N/A timeFormat = (30<<8) * -1;
0N/A timeFormat += (sequence.getResolution() & 0xFF);
0N/A } else {
0N/A // $$jb: 04.08.99: What to really do here?
0N/A return null;
0N/A }
0N/A hdos.writeShort( timeFormat );
0N/A
0N/A // now construct an InputStream to become the FileStream
0N/A fStream = new SequenceInputStream(headerStream, trackStream);
0N/A hdos.close();
0N/A
0N/A length = bytesBuilt + headerLength;
0N/A return fStream;
0N/A }
0N/A
0N/A /**
0N/A * Returns ONE_BYTE, TWO_BYTE, SYSEX, META,
0N/A * ERROR, or IGNORE (i.e. invalid for a MIDI file)
0N/A */
0N/A private int getType(int byteValue) {
0N/A if ((byteValue & 0xF0) == 0xF0) {
0N/A switch(byteValue) {
0N/A case 0xF0:
0N/A case 0xF7:
0N/A return SYSEX;
0N/A case 0xFF:
0N/A return META;
0N/A }
0N/A return IGNORE;
0N/A }
0N/A
0N/A switch(byteValue & 0xF0) {
0N/A case 0x80:
0N/A case 0x90:
0N/A case 0xA0:
0N/A case 0xB0:
0N/A case 0xE0:
0N/A return TWO_BYTE;
0N/A case 0xC0:
0N/A case 0xD0:
0N/A return ONE_BYTE;
0N/A }
0N/A return ERROR;
0N/A }
0N/A
0N/A private final static long mask = 0x7F;
0N/A
0N/A private int writeVarInt(long value) throws IOException {
0N/A int len = 1;
0N/A int shift=63; // number of bitwise left-shifts of mask
0N/A // first screen out leading zeros
0N/A while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
0N/A // then write actual values
0N/A while (shift > 0) {
0N/A tddos.writeByte((int) (((value & (mask << shift)) >> shift) | 0x80));
0N/A shift-=7;
0N/A len++;
0N/A }
0N/A tddos.writeByte((int) (value & mask));
0N/A return len;
0N/A }
0N/A
0N/A private InputStream writeTrack( Track track, int type ) throws IOException, InvalidMidiDataException {
0N/A int bytesWritten = 0;
0N/A int lastBytesWritten = 0;
0N/A int size = track.size();
0N/A PipedOutputStream thpos = new PipedOutputStream();
0N/A DataOutputStream thdos = new DataOutputStream(thpos);
0N/A PipedInputStream thpis = new PipedInputStream(thpos);
0N/A
0N/A ByteArrayOutputStream tdbos = new ByteArrayOutputStream();
0N/A tddos = new DataOutputStream(tdbos);
0N/A ByteArrayInputStream tdbis = null;
0N/A
0N/A SequenceInputStream fStream = null;
0N/A
0N/A long currentTick = 0;
0N/A long deltaTick = 0;
0N/A long eventTick = 0;
0N/A int runningStatus = -1;
0N/A
0N/A // -----------------------------
0N/A // Write each event in the track
0N/A // -----------------------------
0N/A for(int i=0; i<size; i++) {
0N/A MidiEvent event = track.get(i);
0N/A
0N/A int status;
0N/A int eventtype;
0N/A int metatype;
0N/A int data1, data2;
0N/A int length;
0N/A byte data[] = null;
0N/A ShortMessage shortMessage = null;
0N/A MetaMessage metaMessage = null;
0N/A SysexMessage sysexMessage = null;
0N/A
0N/A // get the tick
0N/A // $$jb: this gets easier if we change all system-wide time to delta ticks
0N/A eventTick = event.getTick();
0N/A deltaTick = event.getTick() - currentTick;
0N/A currentTick = event.getTick();
0N/A
0N/A // get the status byte
0N/A status = event.getMessage().getStatus();
0N/A eventtype = getType( status );
0N/A
0N/A switch( eventtype ) {
0N/A case ONE_BYTE:
0N/A shortMessage = (ShortMessage) event.getMessage();
0N/A data1 = shortMessage.getData1();
0N/A bytesWritten += writeVarInt( deltaTick );
0N/A
0N/A if(status!=runningStatus) {
0N/A runningStatus=status;
0N/A tddos.writeByte(status); bytesWritten += 1;
0N/A }
0N/A tddos.writeByte(data1); bytesWritten += 1;
0N/A break;
0N/A
0N/A case TWO_BYTE:
0N/A shortMessage = (ShortMessage) event.getMessage();
0N/A data1 = shortMessage.getData1();
0N/A data2 = shortMessage.getData2();
0N/A
0N/A bytesWritten += writeVarInt( deltaTick );
0N/A if(status!=runningStatus) {
0N/A runningStatus=status;
0N/A tddos.writeByte(status); bytesWritten += 1;
0N/A }
0N/A tddos.writeByte(data1); bytesWritten += 1;
0N/A tddos.writeByte(data2); bytesWritten += 1;
0N/A break;
0N/A
0N/A case SYSEX:
0N/A sysexMessage = (SysexMessage) event.getMessage();
0N/A length = sysexMessage.getLength();
0N/A data = sysexMessage.getMessage();
0N/A bytesWritten += writeVarInt( deltaTick );
0N/A
0N/A // $$jb: 04.08.99: always write status for sysex
0N/A runningStatus=status;
0N/A tddos.writeByte( data[0] ); bytesWritten += 1;
0N/A
0N/A // $$jb: 10.18.99: we don't maintain length in
0N/A // the message data for SysEx (it is not transmitted
0N/A // over the line), so write the calculated length
0N/A // minus the status byte
0N/A bytesWritten += writeVarInt( (data.length-1) );
0N/A
0N/A // $$jb: 10.18.99: now write the rest of the
0N/A // message
0N/A tddos.write(data, 1, (data.length-1));
0N/A bytesWritten += (data.length-1);
0N/A break;
0N/A
0N/A case META:
0N/A metaMessage = (MetaMessage) event.getMessage();
0N/A length = metaMessage.getLength();
0N/A data = metaMessage.getMessage();
0N/A bytesWritten += writeVarInt( deltaTick );
0N/A
0N/A // $$jb: 10.18.99: getMessage() returns the
0N/A // entire valid midi message for a file,
0N/A // including the status byte and the var-length-int
0N/A // length value, so we can just write the data
0N/A // here. note that we must _always_ write the
0N/A // status byte, regardless of runningStatus.
0N/A runningStatus=status;
0N/A tddos.write( data, 0, data.length );
0N/A bytesWritten += data.length;
0N/A break;
0N/A
0N/A case IGNORE:
0N/A // ignore this event
0N/A break;
0N/A
0N/A case ERROR:
0N/A // ignore this event
0N/A break;
0N/A
0N/A default:
0N/A throw new InvalidMidiDataException("internal file writer error");
0N/A }
0N/A }
0N/A // ---------------------------------
0N/A // End write each event in the track
0N/A // ---------------------------------
0N/A
0N/A // Build Track header now that we know length
0N/A thdos.writeInt(MTrk_MAGIC);
0N/A thdos.writeInt(bytesWritten);
0N/A bytesWritten += 8;
0N/A
0N/A // Now sequence them
0N/A tdbis = new ByteArrayInputStream( tdbos.toByteArray() );
0N/A fStream = new SequenceInputStream(thpis,tdbis);
0N/A thdos.close();
0N/A tddos.close();
0N/A
0N/A return fStream;
0N/A }
0N/A}