Example #1
0
        private void write(BinaryWriterBE binaryWriter)
        {
            binaryWriter.Write(0x4D546864);
            binaryWriter.Write(6);
            binaryWriter.Write(type);
            binaryWriter.Write(Convert.ToInt16(tracks.Count));
            binaryWriter.Write(PPQ);

            for (int i = 0; i < tracks.Count; i++)
            {
                tracks[i].writeTrack(binaryWriter, (byte)i);
            }
        }
Example #2
0
        internal void writeTrack(BinaryWriterBE binaryWriter, byte track)
        {
            binaryWriter.Write(0x4D54726B);

            binaryWriter.Write(0);
            long lenPos = binaryWriter.BaseStream.Position;

            for (int i = 0; i < messages.Count; i++)
            {
                switch (messages[i].GetKey())
                {
                case Keys.VLV:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.NoteOff:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.NoteOn:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.AfterTouch:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.Controller:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.Patch:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.Pressure:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.PitchBend:
                    messages[i].Write(binaryWriter, track);
                    break;

                case Keys.Meta:
                    messages[i].Write(binaryWriter, 0x0F);
                    break;
                }
            }

            if (messages[messages.Count - 1].GetKey() != Keys.Meta)
            {
                throw new Exception("Track does not end with an end command");
            }

            long endPos = binaryWriter.BaseStream.Position;

            int trackLength = (int)binaryWriter.BaseStream.Position - (int)lenPos;

            binaryWriter.BaseStream.Position = lenPos - 4;
            binaryWriter.Write(trackLength);

            binaryWriter.BaseStream.Position = endPos;
        }