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)
| 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) |
no outgoing calls
no test coverage detected