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