Friday, January 10, 2025

python to convert midi to keystrokes

Groovin in G had a tutorial about hacks in renoise. He had one where he used a midi to keystroke tool to set an lfo to cycle through samples that triggers a keystroke to switch between samples. I looked into it and Bome's midi translator is kind of expensive so heres a python script that does the same thing


import mido
import pyautogui as pg

# Replace 'INPUTPORT' with your MIDI input port name, e.g., 'CASIO USB-MIDI:CASIO USB-MIDI MIDI 1 16:0'
input_port = 'CASIO USB-MIDI:CASIO USB-MIDI MIDI 1 16:0'

# MIDI note 36 and 37 correspond to the notes you want to trigger the hotkeys for
NOTE36 = 36  # Alt + Down Arrow
NOTE37 = 37  # Alt + Up Arrow

# Open the MIDI input port
with mido.open_input(input_port) as inport:
    for msg in inport:
        # Check for "note on" message (status byte 144-159 for "note on")
        if msg.type == 'note_on':
            if msg.note == NOTE36:  # Check if the pressed note is MIDI note 36
                print(f"MIDI note 36 pressed: {msg}")
                # Send Alt + Down Arrow
                pg.hotkey('alt', 'down')  # Simulate Alt + Down Arrow

            elif msg.note == NOTE37:  # Check if the pressed note is MIDI note 37
                print(f"MIDI note 37 pressed: {msg}")
                # Send Alt + Up Arrow
                pg.hotkey('alt', 'up')  # Simulate Alt + Up Arrow

No comments:

Post a Comment