This patch makes sure that every track in a MIDI file ends with an End
Of Track meta message.

I tested this with JFugue, a package for music programming
(http://www.jfugue.org).  The JFugue examples use this SPI code to
generate MIDI files, which appear to play correctly.

I'm checking it in. 

AG


2006-07-02  Anthony Green  <[EMAIL PROTECTED]>

        * gnu/javax/sound/midi/file/MidiFileWriter.java (writeTrack): Make
        sure that every track written ends with an End Of Track meta
        message.



Index: gnu/javax/sound/midi/file/MidiFileWriter.java
===================================================================
RCS file: 
/sources/classpath/classpath/gnu/javax/sound/midi/file/MidiFileWriter.java,v
retrieving revision 1.1
diff -u -r1.1 MidiFileWriter.java
--- gnu/javax/sound/midi/file/MidiFileWriter.java       2 Jul 2006 18:15:50 
-0000       1.1
+++ gnu/javax/sound/midi/file/MidiFileWriter.java       2 Jul 2006 23:25:03 
-0000
@@ -44,6 +44,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import javax.sound.midi.MetaMessage;
 import javax.sound.midi.MidiEvent;
 import javax.sound.midi.Sequence;
 import javax.sound.midi.Track;
@@ -161,8 +162,22 @@
        pme = me;
        i++;
       } 
-    // FIXME: if the last event isn't an end of track.. write that.
-    return trackLength + 8;
+
+    // We're done if the last event was an End of Track meta message.
+    if (pme != null && (pme.getMessage() instanceof MetaMessage))
+      {
+       MetaMessage mm = (MetaMessage) pme.getMessage();
+       if (mm.getType() == 0x2f) // End of Track message
+         return trackLength + 8;
+      }
+
+    // Write End of Track meta message
+    dos.writeVariableLengthInt(0); // Delta time of 0
+    dos.writeByte(0xff); // Meta Message
+    dos.writeByte(0x2f); // End of Track message
+    dos.writeVariableLengthInt(0); // Length of 0
+
+    return trackLength + 8 + 4;
   }
 
   /* Write a Sequence to a file.



Reply via email to