Exemple #1
0
        /// <summary>
        /// Writes a variable-length quantity (VLQ) into this file's byte array, resizing the byte array if necessary.
        /// </summary>
        /// <param name="offset">Offset into the byte array at which the VLQ is written.</param>
        /// <param name="vlq">The VLQ to write.</param>
        /// <param name="length">Length of quantity that the VLQ is replacing (for resizing purposes).</param>
        public void WriteVLQ(int offset, int vlq, int length)
        {
            /* Resize the byte array (if necessary). */
            int i, n = Midi.SizeVLQ(vlq) - length;

            if (n != 0)
            {
                this.Resize(n, offset + length, 0);
            }

            /* For the simple (and common) case of a single-byte VLQ, take a shortcut. */
            if (vlq < 0x80)
            {
                this.Bytes[offset] = (byte)vlq; return;
            }

            /* The VLQ is stored as 7 bits per byte (most significant byte first).  All bytes except the last have the
             * Most Significant Bit (MSB) set, and the last byte has the MSB clear.  The easiest way to accomplish this
             * is to write the VLQ "backwards" (least significant byte first) to a temporary byte array, then reverse it.
             */
            for (n = 0, i = vlq; i > 0; i >>= 7)
            {
                this.Bytes[offset + n] = (byte)((i % 0x100) & 0x7F);
                if (n > 0)
                {
                    this.Bytes[offset + n] |= 0x80;
                }
                ++n;
            }
            Array.Reverse(this.Bytes, offset, n);
        }
        /***********
        * Methods *
        ***********/

        #region Public Methods

        /// <summary>Returns the number of bytes required to store an item of this type.</summary>
        public static int SizeItem(int deltaTime, bool runningStatus, MidiMessageType messageType)
        {
            return(Midi.SizeVLQ(deltaTime) + (runningStatus ? 0 : 1) + (MidiChannelEvent.HasData2(messageType) ? 2 : 1));
        }
Exemple #3
0
        /***********
        * Methods *
        ***********/

        #region Public Methods

        /// <summary>Returns the number of bytes required to store an item of this type.</summary>
        public static int SizeItem(int deltaTime, int dataLength)
        {
            return(Midi.SizeVLQ(deltaTime) + 2 + Midi.SizeVLQ(dataLength) + dataLength);
        }