def test_pitchwheel(self): """Check if pitchwheel type check and encoding is working.""" msg = Message('pitchwheel', pitch=mido.messages.MIN_PITCHWHEEL) bytes = msg.bytes() self.assertTrue(bytes[1] == bytes[2] == 0) msg = Message('pitchwheel', pitch=mido.messages.MAX_PITCHWHEEL) bytes = msg.bytes() self.assertTrue(bytes[1] == bytes[2] == 127)
def test_encode_and_parse(self): """Encode a message and then parse it. Should return the same message. """ msg1 = Message('note_on') msg2 = mido.parse(msg1.bytes()) self.assertTrue(msg1 == msg2)
def test_encode_and_parse_all(self): """Encode and then parse all message types. This checks mostly for errors in the parser. """ p = mido.Parser() for spec in mido.messages.get_message_specs(): if spec.type == 'sysex_end': # This is considered a part of 'sysex_start'. continue msg = Message(spec.type) p.feed(msg.bytes()) outmsg = p.get_message() self.assertTrue(outmsg is not True) self.assertTrue(outmsg.type == spec.type)
def callback(msg: mido.Message): global grpc_client global msgQueue send_msg = grpc_midi_pb2.Sysex() send_msg.data = bytes(msg.bytes()) if grpc_client is None: # Add msg to queue and continue if send_msg is not None: msgQueue.put(send_msg) if grpc_client is not None: # Try sending message directly try: logger.info("BT RECEIVE -> GRPC SEND") logger.debug("BT RECEIVE -> GRPC SEND {}".format(msg)) grpc_client.SendMidi(send_msg) except Exception as e: logger.error("Error sending message.. {}".format(e)) grpc_client = None if send_msg is not None: msgQueue.put(send_msg)
def send(self, msg: mido.Message): if self._updateValueCallback is None: logger.debug("No subscription?") return timestamp = [0x80, 0x80] # default timestamp if self._lastTimestampMillis is not None and self._lastTimestampMidi is not None: # Kind of synchronize to sender's time by taking the timestamp from incoming messages and adding the diff in local # system time millis curTime = int(round(time.time() * 1000)) diffTime = curTime - self._lastTimestampMillis oldMidiTime = midi_timestamp.toSysTime(self._lastTimestampMidi) newMidiTime = oldMidiTime + diffTime timestamp = midi_timestamp.toMidiTime(newMidiTime) # Apply status bytes timestamp[0] = timestamp[0] | 0x80 timestamp[1] = timestamp[1] | 0x80 bytes = timestamp + msg.bytes() if msg.type == 'sysex': if bytes[-1] != 0xF7: logger.debug("Missing sysex end byte") bytes = bytes + [0xF7] # Last sysex byte must by preceded by timestamp # Append timestamp bytes = bytes[:-1] + [timestamp[1]] + [bytes[-1]] splitMsg = [] if self._maxValueSize is not None and len(bytes) > self._maxValueSize: # logger.debug("split {}".format([hex(c) for c in bytes])) splitMsg = self._splitSysexBytes(bytes, self._maxValueSize) else: splitMsg = [bytes] for msg in splitMsg: # time.sleep(0.01) logger.debug("Writing {} to MIDI-BLE".format([hex(c) for c in msg])) self._updateValueCallback(msg)
from mido import Message msg = Message('note_on', note=60) print(msg) msg = msg.copy(note=100, velocity=127) print(msg) msg2 = Message('note_on', note=100, velocity=3, time=6.2) print(msg2) print(msg2.bytes()) print(msg2.hex()) msg3 = Message.from_bytes([0x90, 0x42, 0x60]) print(msg3) print(msg3.dict()) print(msg3.is_meta)
def test_sysex(self): original = Message('sysex', data=(1, 2, 3, 4, 5)) parsed = mido.parse(original.bytes()) self.assertTrue(original == parsed)
def test_pitchwheel_encode_parse(self): """Encode and parse pitchwheel with value=0.""" a = Message('pitchwheel', pitch=0) b = mido.parse(a.bytes()) self.assertTrue(a == b)
def send(self, msg: mido.Message): logger.debug("MIDI-GRPC Receive {}".format(msg)) grpc_msg = audioled_controller.grpc_midi_pb2.Sysex() grpc_msg.data = bytes(msg.bytes()) self._queue.put(grpc_msg)