< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/StandardMidiFileWriter.java

Print this page




  30 import java.io.DataOutputStream;
  31 import java.io.File;
  32 import java.io.FileOutputStream;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.OutputStream;
  36 import java.io.PipedInputStream;
  37 import java.io.PipedOutputStream;
  38 import java.io.SequenceInputStream;
  39 import java.util.Objects;
  40 
  41 import javax.sound.midi.InvalidMidiDataException;
  42 import javax.sound.midi.MetaMessage;
  43 import javax.sound.midi.MidiEvent;
  44 import javax.sound.midi.Sequence;
  45 import javax.sound.midi.ShortMessage;
  46 import javax.sound.midi.SysexMessage;
  47 import javax.sound.midi.Track;
  48 import javax.sound.midi.spi.MidiFileWriter;
  49 
  50 
  51 /**
  52  * MIDI file writer.
  53  *
  54  * @author Kara Kytle
  55  * @author Jan Borgersen
  56  */
  57 public final class StandardMidiFileWriter extends MidiFileWriter {
  58 
  59     private static final int MThd_MAGIC = 0x4d546864;  // 'MThd'
  60     private static final int MTrk_MAGIC = 0x4d54726b;  // 'MTrk'
  61 
  62     private static final int ONE_BYTE   = 1;
  63     private static final int TWO_BYTE   = 2;
  64     private static final int SYSEX      = 3;
  65     private static final int META       = 4;
  66     private static final int ERROR      = 5;
  67     private static final int IGNORE     = 6;
  68 
  69     private static final int MIDI_TYPE_0 = 0;
  70     private static final int MIDI_TYPE_1 = 1;
  71 
  72     private static final int bufferSize = 16384;  // buffersize for write
  73     private DataOutputStream tddos;               // data output stream for track writing
  74 
  75 
  76 
  77     /**
  78      * MIDI parser types
  79      */
  80     private static final int types[] = {
  81         MIDI_TYPE_0,
  82         MIDI_TYPE_1
  83     };
  84 
  85 
  86     /**
  87      * new
  88      */
  89     public int[] getMidiFileTypes() {
  90         int[] localArray = new int[types.length];
  91         System.arraycopy(types, 0, localArray, 0, types.length);
  92         return localArray;
  93     }
  94 
  95     /**
  96      * Obtains the file types that this provider can write from the
  97      * sequence specified.
  98      * @param sequence the sequence for which midi file type support
  99      * is queried
 100      * @return array of file types.  If no file types are supported,
 101      * returns an array of length 0.
 102      */

 103     public int[] getMidiFileTypes(Sequence sequence){
 104         int typesArray[];
 105         Track tracks[] = sequence.getTracks();
 106 
 107         if( tracks.length==1 ) {
 108             typesArray = new int[2];
 109             typesArray[0] = MIDI_TYPE_0;
 110             typesArray[1] = MIDI_TYPE_1;
 111         } else {
 112             typesArray = new int[1];
 113             typesArray[0] = MIDI_TYPE_1;
 114         }
 115 
 116         return typesArray;
 117     }
 118 

 119     public int write(Sequence in, int type, OutputStream out) throws IOException {
 120         Objects.requireNonNull(out);
 121         if (!isFileTypeSupported(type, in)) {
 122             throw new IllegalArgumentException("Could not write MIDI file");
 123         }
 124         byte [] buffer = null;
 125 
 126         int bytesRead = 0;
 127         long bytesWritten = 0;
 128 
 129         // First get the fileStream from this sequence
 130         InputStream fileStream = getFileStream(type,in);
 131         if (fileStream == null) {
 132             throw new IllegalArgumentException("Could not write MIDI file");
 133         }
 134         buffer = new byte[bufferSize];
 135 
 136         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 137             out.write( buffer, 0, bytesRead );
 138             bytesWritten += bytesRead;
 139         }
 140         // Done....return bytesWritten
 141         return (int) bytesWritten;
 142     }
 143 

 144     public int write(Sequence in, int type, File out) throws IOException {
 145         Objects.requireNonNull(in);
 146         FileOutputStream fos = new FileOutputStream(out); // throws IOException
 147         int bytesWritten = write( in, type, fos );
 148         fos.close();
 149         return bytesWritten;
 150     }
 151 
 152     //=================================================================================
 153 
 154 
 155     private InputStream getFileStream(int type, Sequence sequence) throws IOException {
 156         Track tracks[] = sequence.getTracks();
 157         int bytesBuilt = 0;
 158         int headerLength = 14;
 159         int length = 0;
 160         int timeFormat;
 161         float divtype;
 162 
 163         PipedOutputStream   hpos = null;
 164         DataOutputStream    hdos = null;
 165         PipedInputStream    headerStream = null;
 166 
 167         InputStream         trackStreams [] = null;
 168         InputStream         trackStream = null;
 169         InputStream fStream = null;
 170 
 171         // Determine the filetype to write
 172         if( type==MIDI_TYPE_0 ) {
 173             if (tracks.length != 1) {
 174                 return null;




  30 import java.io.DataOutputStream;
  31 import java.io.File;
  32 import java.io.FileOutputStream;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.OutputStream;
  36 import java.io.PipedInputStream;
  37 import java.io.PipedOutputStream;
  38 import java.io.SequenceInputStream;
  39 import java.util.Objects;
  40 
  41 import javax.sound.midi.InvalidMidiDataException;
  42 import javax.sound.midi.MetaMessage;
  43 import javax.sound.midi.MidiEvent;
  44 import javax.sound.midi.Sequence;
  45 import javax.sound.midi.ShortMessage;
  46 import javax.sound.midi.SysexMessage;
  47 import javax.sound.midi.Track;
  48 import javax.sound.midi.spi.MidiFileWriter;
  49 

  50 /**
  51  * MIDI file writer.
  52  *
  53  * @author Kara Kytle
  54  * @author Jan Borgersen
  55  */
  56 public final class StandardMidiFileWriter extends MidiFileWriter {
  57 
  58     private static final int MThd_MAGIC = 0x4d546864;  // 'MThd'
  59     private static final int MTrk_MAGIC = 0x4d54726b;  // 'MTrk'
  60 
  61     private static final int ONE_BYTE   = 1;
  62     private static final int TWO_BYTE   = 2;
  63     private static final int SYSEX      = 3;
  64     private static final int META       = 4;
  65     private static final int ERROR      = 5;
  66     private static final int IGNORE     = 6;
  67 
  68     private static final int MIDI_TYPE_0 = 0;
  69     private static final int MIDI_TYPE_1 = 1;
  70 
  71     private static final int bufferSize = 16384;  // buffersize for write
  72     private DataOutputStream tddos;               // data output stream for track writing
  73 


  74     /**
  75      * MIDI parser types.
  76      */
  77     private static final int types[] = {
  78         MIDI_TYPE_0,
  79         MIDI_TYPE_1
  80     };
  81 
  82     @Override



  83     public int[] getMidiFileTypes() {
  84         int[] localArray = new int[types.length];
  85         System.arraycopy(types, 0, localArray, 0, types.length);
  86         return localArray;
  87     }
  88 
  89     /**
  90      * Obtains the file types that this provider can write from the
  91      * sequence specified.
  92      * @param sequence the sequence for which midi file type support
  93      * is queried
  94      * @return array of file types.  If no file types are supported,
  95      * returns an array of length 0.
  96      */
  97     @Override
  98     public int[] getMidiFileTypes(Sequence sequence){
  99         int typesArray[];
 100         Track tracks[] = sequence.getTracks();
 101 
 102         if( tracks.length==1 ) {
 103             typesArray = new int[2];
 104             typesArray[0] = MIDI_TYPE_0;
 105             typesArray[1] = MIDI_TYPE_1;
 106         } else {
 107             typesArray = new int[1];
 108             typesArray[0] = MIDI_TYPE_1;
 109         }
 110 
 111         return typesArray;
 112     }
 113 
 114     @Override
 115     public int write(Sequence in, int type, OutputStream out) throws IOException {
 116         Objects.requireNonNull(out);
 117         if (!isFileTypeSupported(type, in)) {
 118             throw new IllegalArgumentException("Could not write MIDI file");
 119         }
 120         byte [] buffer = null;
 121 
 122         int bytesRead = 0;
 123         long bytesWritten = 0;
 124 
 125         // First get the fileStream from this sequence
 126         InputStream fileStream = getFileStream(type,in);
 127         if (fileStream == null) {
 128             throw new IllegalArgumentException("Could not write MIDI file");
 129         }
 130         buffer = new byte[bufferSize];
 131 
 132         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 133             out.write( buffer, 0, bytesRead );
 134             bytesWritten += bytesRead;
 135         }
 136         // Done....return bytesWritten
 137         return (int) bytesWritten;
 138     }
 139 
 140     @Override
 141     public int write(Sequence in, int type, File out) throws IOException {
 142         Objects.requireNonNull(in);
 143         FileOutputStream fos = new FileOutputStream(out); // throws IOException
 144         int bytesWritten = write( in, type, fos );
 145         fos.close();
 146         return bytesWritten;
 147     }
 148 
 149     //=================================================================================
 150 

 151     private InputStream getFileStream(int type, Sequence sequence) throws IOException {
 152         Track tracks[] = sequence.getTracks();
 153         int bytesBuilt = 0;
 154         int headerLength = 14;
 155         int length = 0;
 156         int timeFormat;
 157         float divtype;
 158 
 159         PipedOutputStream   hpos = null;
 160         DataOutputStream    hdos = null;
 161         PipedInputStream    headerStream = null;
 162 
 163         InputStream         trackStreams [] = null;
 164         InputStream         trackStream = null;
 165         InputStream fStream = null;
 166 
 167         // Determine the filetype to write
 168         if( type==MIDI_TYPE_0 ) {
 169             if (tracks.length != 1) {
 170                 return null;


< prev index next >