jdk/src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java
author amenkov
Wed, 13 May 2009 13:52:52 +0400
changeset 3451 22d903fd3c5e
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6657625: RmfFileReader/StandardMidiFileWriter.types are public mutable statics (findbugs) Reviewed-by: hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3451
22d903fd3c5e 6657625: RmfFileReader/StandardMidiFileWriter.types are public mutable statics (findbugs)
amenkov
parents: 2
diff changeset
     2
 * Copyright 1999-2009 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.IllegalArgumentException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.midi.MidiFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.midi.InvalidMidiDataException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.sound.midi.MidiEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.sound.midi.MetaMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.sound.midi.MidiMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.sound.midi.Sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.sound.midi.ShortMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.sound.midi.SysexMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.sound.midi.Track;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.sound.midi.spi.MidiFileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * MIDI file writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class StandardMidiFileWriter extends MidiFileWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int MThd_MAGIC = 0x4d546864;  // 'MThd'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final int MTrk_MAGIC = 0x4d54726b;  // 'MTrk'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final int ONE_BYTE   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final int TWO_BYTE   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int SYSEX      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int META       = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int ERROR      = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final int IGNORE     = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final int MIDI_TYPE_0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int MIDI_TYPE_1 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int bufferSize = 16384;  // buffersize for write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private DataOutputStream tddos;               // data output stream for track writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * MIDI parser types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
3451
22d903fd3c5e 6657625: RmfFileReader/StandardMidiFileWriter.types are public mutable statics (findbugs)
amenkov
parents: 2
diff changeset
    85
    private static final int types[] = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        MIDI_TYPE_0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        MIDI_TYPE_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public int[] getMidiFileTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int[] localArray = new int[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.arraycopy(types, 0, localArray, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return localArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Obtains the file types that this provider can write from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * sequence specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param sequence the sequence for which midi file type support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * is queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return array of file types.  If no file types are supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * returns an array of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int[] getMidiFileTypes(Sequence sequence){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int typesArray[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Track tracks[] = sequence.getTracks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if( tracks.length==1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            typesArray = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            typesArray[0] = MIDI_TYPE_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            typesArray[1] = MIDI_TYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            typesArray = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            typesArray[0] = MIDI_TYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return typesArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean isFileTypeSupported(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for(int i=0; i<types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if( type == types[i] ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public int write(Sequence in, int type, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        byte [] buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        long bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if( !isFileTypeSupported(type,in) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new IllegalArgumentException("Could not write MIDI file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // First get the fileStream from this sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        InputStream fileStream = getFileStream(type,in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (fileStream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException("Could not write MIDI file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        buffer = new byte[bufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Done....return bytesWritten
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return (int) bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public int write(Sequence in, int type, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        FileOutputStream fos = new FileOutputStream(out); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int bytesWritten = write( in, type, fos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private InputStream getFileStream(int type, Sequence sequence) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        Track tracks[] = sequence.getTracks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int bytesBuilt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int headerLength = 14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int timeFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        float divtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        PipedOutputStream   hpos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        DataOutputStream    hdos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        PipedInputStream    headerStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        InputStream         trackStreams [] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        InputStream         trackStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        InputStream fStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // Determine the filetype to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if( type==MIDI_TYPE_0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (tracks.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if( type==MIDI_TYPE_1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (tracks.length < 1) { // $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if(tracks.length==1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                type = MIDI_TYPE_0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else if(tracks.length>1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                type = MIDI_TYPE_1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // Now build the file one track at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Note that above we made sure that MIDI_TYPE_0 only happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // if tracks.length==1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        trackStreams = new InputStream[tracks.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int trackCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for(int i=0; i<tracks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                trackStreams[trackCount] = writeTrack( tracks[i], type );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                trackCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if(Printer.err) Printer.err("Exception in write: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            //bytesBuilt += trackStreams[i].getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        // Now seqence the track streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if( trackCount == 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            trackStream = trackStreams[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else if( trackCount > 1 ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            trackStream = trackStreams[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            for(int i=1; i<tracks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                // fix for 5048381: NullPointerException when saving a MIDI sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                // don't include failed track streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (trackStreams[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    trackStream = new SequenceInputStream( trackStream, trackStreams[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new IllegalArgumentException("invalid MIDI data in sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Now build the header...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        hpos = new PipedOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        hdos = new DataOutputStream(hpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        headerStream = new PipedInputStream(hpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // Write the magic number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        hdos.writeInt( MThd_MAGIC );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // Write the header length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        hdos.writeInt( headerLength - 8 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // Write the filetype
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if(type==MIDI_TYPE_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            hdos.writeShort( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // MIDI_TYPE_1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            hdos.writeShort( 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // Write the number of tracks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        hdos.writeShort( (short) trackCount );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        // Determine and write the timing format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        divtype = sequence.getDivisionType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if( divtype == Sequence.PPQ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            timeFormat = sequence.getResolution();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else if( divtype == Sequence.SMPTE_24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            timeFormat = (24<<8) * -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            timeFormat += (sequence.getResolution() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        } else if( divtype == Sequence.SMPTE_25) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            timeFormat = (25<<8) * -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            timeFormat += (sequence.getResolution() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else if( divtype == Sequence.SMPTE_30DROP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            timeFormat = (29<<8) * -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            timeFormat += (sequence.getResolution() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } else if( divtype == Sequence.SMPTE_30) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            timeFormat = (30<<8) * -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            timeFormat += (sequence.getResolution() & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // $$jb: 04.08.99: What to really do here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        hdos.writeShort( timeFormat );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // now construct an InputStream to become the FileStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        fStream = new SequenceInputStream(headerStream, trackStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        hdos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        length = bytesBuilt + headerLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return fStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns ONE_BYTE, TWO_BYTE, SYSEX, META,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * ERROR, or IGNORE (i.e. invalid for a MIDI file)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private int getType(int byteValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if ((byteValue & 0xF0) == 0xF0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            switch(byteValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            case 0xF0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            case 0xF7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return SYSEX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            case 0xFF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return META;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return IGNORE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        switch(byteValue & 0xF0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        case 0x80:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        case 0x90:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        case 0xA0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        case 0xB0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        case 0xE0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return TWO_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        case 0xC0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case 0xD0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return ONE_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private final static long mask = 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private int writeVarInt(long value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int len = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int shift=63; // number of bitwise left-shifts of mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // first screen out leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // then write actual values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        while (shift > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            tddos.writeByte((int) (((value & (mask << shift)) >> shift) | 0x80));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        tddos.writeByte((int) (value & mask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private InputStream writeTrack( Track track, int type ) throws IOException, InvalidMidiDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int lastBytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int size = track.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        PipedOutputStream thpos = new PipedOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        DataOutputStream  thdos = new DataOutputStream(thpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        PipedInputStream  thpis = new PipedInputStream(thpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        ByteArrayOutputStream tdbos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        tddos = new DataOutputStream(tdbos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        ByteArrayInputStream tdbis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        SequenceInputStream  fStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        long currentTick = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        long deltaTick = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        long eventTick = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int runningStatus = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // -----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Write each event in the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // -----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for(int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            MidiEvent event = track.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            int eventtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            int metatype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            int data1, data2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            byte data[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            ShortMessage shortMessage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            MetaMessage  metaMessage  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            SysexMessage sysexMessage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // get the tick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // $$jb: this gets easier if we change all system-wide time to delta ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            eventTick = event.getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            deltaTick = event.getTick() - currentTick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            currentTick = event.getTick();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // get the status byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            status = event.getMessage().getStatus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            eventtype = getType( status );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            switch( eventtype ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            case ONE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                shortMessage = (ShortMessage) event.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                data1 = shortMessage.getData1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                bytesWritten += writeVarInt( deltaTick );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if(status!=runningStatus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    runningStatus=status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    tddos.writeByte(status);  bytesWritten += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                tddos.writeByte(data1);   bytesWritten += 1;
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 TWO_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                shortMessage = (ShortMessage) event.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                data1 = shortMessage.getData1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                data2 = shortMessage.getData2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                bytesWritten += writeVarInt( deltaTick );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                if(status!=runningStatus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    runningStatus=status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    tddos.writeByte(status);  bytesWritten += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                tddos.writeByte(data1);   bytesWritten += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                tddos.writeByte(data2);   bytesWritten += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            case SYSEX:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                sysexMessage = (SysexMessage) event.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                length     = sysexMessage.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                data       = sysexMessage.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                bytesWritten += writeVarInt( deltaTick );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // $$jb: 04.08.99: always write status for sysex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                runningStatus=status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                tddos.writeByte( data[0] ); bytesWritten += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                // $$jb: 10.18.99: we don't maintain length in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // the message data for SysEx (it is not transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                // over the line), so write the calculated length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                // minus the status byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                bytesWritten += writeVarInt( (data.length-1) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // $$jb: 10.18.99: now write the rest of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                tddos.write(data, 1, (data.length-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                bytesWritten += (data.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            case META:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                metaMessage = (MetaMessage) event.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                length    = metaMessage.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                data      = metaMessage.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                bytesWritten += writeVarInt( deltaTick );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                // $$jb: 10.18.99: getMessage() returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                // entire valid midi message for a file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // including the status byte and the var-length-int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // length value, so we can just write the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                // here.  note that we must _always_ write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // status byte, regardless of runningStatus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                runningStatus=status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                tddos.write( data, 0, data.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                bytesWritten += data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            case IGNORE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                // ignore this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            case ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // ignore this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                throw new InvalidMidiDataException("internal file writer error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        // End write each event in the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // ---------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // Build Track header now that we know length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        thdos.writeInt(MTrk_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        thdos.writeInt(bytesWritten);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        bytesWritten += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // Now sequence them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        tdbis = new ByteArrayInputStream( tdbos.toByteArray() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        fStream = new SequenceInputStream(thpis,tdbis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        thdos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        tddos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return fStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
}