MCPcopy Create free account
hub / github.com/bspaans/python-mingus / int_to_varbyte

Method int_to_varbyte

mingus/midi/midi_track.py:267–285  ·  view source on GitHub ↗

Convert an integer into a variable length byte. How it works: the bytes are stored in big-endian (significant bit first), the highest bit of the byte (mask 0x80) is set when there are more bytes following. The remaining 7 bits (mask 0x7F) are used to store the value.

(self, value)

Source from the content-addressed store, hash-verified

265 return b"\x00" + META_EVENT + TRACK_NAME + l + name.encode("ascii")
266
267 def int_to_varbyte(self, value):
268 """Convert an integer into a variable length byte.
269
270 How it works: the bytes are stored in big-endian (significant bit
271 first), the highest bit of the byte (mask 0x80) is set when there
272 are more bytes following. The remaining 7 bits (mask 0x7F) are used
273 to store the value.
274 """
275 # Warning: bit kung-fu ahead. The length of the integer in bytes
276 length = int(log(max(value, 1), 0x80)) + 1
277
278 # Remove the highest bit and move the bits to the right if length > 1
279 bytes = [value >> i * 7 & 0x7F for i in range(length)]
280 bytes.reverse()
281
282 # Set the first bit on every one but the last bit.
283 for i in range(len(bytes) - 1):
284 bytes[i] = bytes[i] | 0x80
285 return pack("%sB" % len(bytes), *bytes)

Callers 3

play_BarMethod · 0.95
set_deltatimeMethod · 0.95
track_name_eventMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected