def writeVarLength(i):
    """
    Accept an input, and write a MIDI-compatible variable length stream

    The MIDI format is a little strange, and makes use of so-called variable
    length quantities. These quantities are a stream of bytes. If the most
    significant bit is 1, then more bytes follow. If it is zero, then the
    byte in question is the last in the stream
    """
    input = int(i + 0.5)
    output = [0, 0, 0, 0]
    reversed = [0, 0, 0, 0]
    count = 0
    result = input & 127
    output[count] = result
    count = count + 1
    input = input >> 7
    while input > 0:
        result = input & 127
        result = result | 128
        output[count] = result
        count = count + 1
        input = input >> 7
    reversed[0] = output[3]
    reversed[1] = output[2]
    reversed[2] = output[1]
    reversed[3] = output[0]
    return reversed[4 - count:4]
