forked from critterandguitari/ETC_Mother
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_in_test.py
93 lines (66 loc) · 1.92 KB
/
midi_in_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""Contains an example of midi input, and a separate example of midi output.
By default it runs the output example.
python midi.py --output
python midi.py --input
"""
import sys
import os
import time
import pygame
import pygame.midi
from pygame.locals import *
def parse_midi(midi):
etc_ch = 1
for msg in midi:
midi_msg = msg[0]
msg_status = int(midi_msg[0])
msg_channel = msg_status & 0xf
msg_type = (msg_status >> 4) & 0xf
print "channel: " + str(msg_channel) + " type: " + str(msg_type)
# global message clock tick
if (msg_status == 248) :
print "new clock"
# global message clock start
if (msg_status == 250) :
print "new start"
# channel messages
if (msg_channel == etc_ch) :
if (msg_type == 0xB) :
print "new CC"
if (msg_type == 0x8) :
print "new note off"
if (msg_type == 0x9) :
print "new note on"
if (msg_type == 0xC) :
print "new pgm"
def _print_device_info():
for i in range( pygame.midi.get_count() ):
r = pygame.midi.get_device_info(i)
(interf, name, input, output, opened) = r
in_out = ""
if input:
in_out = "(input)"
if output:
in_out = "(output)"
print ("%2i: interface :%s:, name :%s:, opened :%s: %s" %
(i, interf, name, opened, in_out))
#def input_main(device_id = None):
#pygame.init()
pygame.midi.init()
_print_device_info()
device_id = None
if device_id is None:
input_id = pygame.midi.get_default_input_id()
else:
input_id = device_id
print ("using input_id :%s:" % input_id)
i = pygame.midi.Input( input_id )
going = True
while going:
if i.poll():
midi_events = i.read(100)
parse_midi(midi_events)
time.sleep(.03)
del i
pygame.midi.quit()