Skip to content

Instantly share code, notes, and snippets.

@eni23
Created March 4, 2020 19:09
Show Gist options
  • Save eni23/3a74e1df771b5ab36b4e7bb8b32014a6 to your computer and use it in GitHub Desktop.
Save eni23/3a74e1df771b5ab36b4e7bb8b32014a6 to your computer and use it in GitHub Desktop.
pygame-midi
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pygame
import pygame.midi
MIDI_DEVICE_NAME_MATCH = "nanoPAD"
# set up pygame
pygame.init()
pygame.midi.init()
# list all midi devices
device_idx = False
for x in range( 0, pygame.midi.get_count() ):
interf, name, is_in, is_out, is_open = pygame.midi.get_device_info(x)
if is_in:
match = str(name).lower().find(MIDI_DEVICE_NAME_MATCH.lower())
if match>-1:
device_idx = x
if not device_idx:
print("Device not found")
sys.exit(1)
# open a specific midi device
inp = pygame.midi.Input(device_idx)
# run the event loop
while True:
if inp.poll():
data = inp.read(1000)
data = data[0][0]
key_id = data[1]
key_val = data[2]
state = True if data[0] == 144 else False
state_human = "on" if state else "off"
base_pct = key_val - 64
pct = round(base_pct / 0.63 )
print("key={0} data={1} state={2} base={3}, pct={4}".format(
key_id,
key_val,
state_human,
base_pct,
pct
))
pygame.time.wait(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment