data[3] = False sleep(0.01) def set_mode(m=0): global mode mode = m def test(): open_port("/dev/tty.usbmodem1d1141") open_port("/dev/tty.usbserial-LI5AE6AA") while count_leds() < 4: add_led(0, 0, 0, 0) add_led(1, 7, 7, 7) add_led(0, 7, 7, 7) add_led(0, 8, 8, 8) while count_leds() < 21: add_led(0, 0, 0, 0) for n in range(0, 21): reset_led(n) melody_in.set_callback(melody_callback) drum_in.set_callback(drum_callback) start_new_thread(midi_input_runner, ()) start_new_thread(pir_input_runner, ()) start_new_thread(light_runner, ())
class Device: MIDI_IN = None MIDI_OUT = None MIDI_PORT_COUNT = 0 IS_FIRST_CONNECTION = True UPDATE_LIST = [] KEY_BINDINGS = {} PULSE_AUDIO = PulseAudioHelper() XORG = XorgHelper() def __init__(self, name='Generic Device', regex=None): self.NAME = name self.DEVICE_REGEX = regex self.UPDATE_LIST.append(self.update_midi_connection_on_reconnect) def __enter__(self): self.MIDI_IN = MidiIn() self.MIDI_OUT = MidiOut() return self def __exit__(self, _type, _value, _traceback): del self.MIDI_IN, self.MIDI_OUT def __repr__(self): return self.NAME def update(self, ELAPSED_TIME): for update in self.UPDATE_LIST: update(ELAPSED_TIME) def send_signal(self, channel, key, payload): self.MIDI_OUT.send_message([channel, key, payload]) def update_midi_connection_on_reconnect(self, _elapsed_time): current_port_count = len([ x for x in self.MIDI_OUT.get_ports() if not re.search(r'[rR]t[Mm]idi', x) ]) if current_port_count != self.MIDI_PORT_COUNT: self.MIDI_PORT_COUNT = current_port_count self.activate() def activate(self): if self.DEVICE_REGEX is not None: self.activate_ports() def activate_ports(self): activated = False self.MIDI_IN.close_port() self.MIDI_OUT.close_port() if not self.IS_FIRST_CONNECTION: print(f'{self} disconnected!') sleep(1.0) for midi in (self.MIDI_IN, self.MIDI_OUT): port_number = None for i, port_name in enumerate(midi.get_ports()): if re.search(self.DEVICE_REGEX, port_name): port_number = i break if port_number is not None: midi.open_port(port_number) activated = True if activated: self.IS_FIRST_CONNECTION = False print(f'{self} connected!') self.MIDI_IN.set_callback(self.input_callback) def input_callback(self, midi_signal_in, *_args, **_kwargs): byte_signal = midi_signal_in[0] key = str(byte_signal[1]) if key in self.KEY_BINDINGS.keys(): self.KEY_BINDINGS[key](byte_signal) else: self.animation_callback(byte_signal) def animation_callback(self, byte_signal): print(f'{self}:{byte_signal}')
print("OUT ports:", out_ports) def call_obs_api(command='pause-toggle'): url = f'http://localhost:28000/{command}' print("Calling:", url) response = requests.get(url).json() print("OBS RESPONSE:", response) def __callback(msg_data, data): print("GOT DATA") note_in = Message.from_bytes(msg_data[0]) # print(msg_data, data, Message.from_bytes(msg_data[0])) real_channel = note_in.channel + 1 if hasattr(note_in, 'channel') else 'null' print(f'{note_in} / real_channel={real_channel}') if note_in.type == 'control_change' and note_in.control == 65 and note_in.value == 0: print("Special key pressed!") # threading.Thread(target=call_obs_api, kwargs=dict(command='pause-toggle'), daemon=True).start() gevent.spawn_later(0.1, call_obs_api) for index in range(len(in_ports)): my_in = MidiIn().open_port(index) my_in.set_callback(__callback) while True: print("HI") gevent.sleep(1.0) gevent.spawn_later(0.1, call_obs_api)