This repository was archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmidi_input.py
More file actions
182 lines (136 loc) · 4.63 KB
/
midi_input.py
File metadata and controls
182 lines (136 loc) · 4.63 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
"""
midi_input.py
An input method for simplesoundboard to work with midi pad triggers such as the Akai MPD18
Adapted from the pygame midi.py example.
First, open the MPD18 editor and load the Chromatic preset, then press Commit-Upload to set the pads
Adjust SERVER to be the hostname (and optionally port number) for your soundboard
Adjust TAGMAP to be the names of effect tags you want to trigger.
Adjust DEFAULT to trigger a tag for an unknown pad value
Usage:
python midi.py --input
"""
import sys
import os
import pygame
import pygame.midi
from pygame.locals import *
import urllib
SERVER = 'john-htpc'
#for future use. For now, leave URLROOT blank.
URLROOT = ''
chromatic = range(36,81) # Values for the "Chromatic" preset, 36 through 80
PADMAP = chromatic
TAGMAP = ['Jock Jams',
'Big Save',
'Blowout',
'Close Game',
'Comeback',
'Fire Extinguished',
'Fuckup',
'Game Over',
'Nice Goal',
'On Fire',
'One-Timer',
'Waiting',
'Windowshopping',
'Wrongful Goal',
]
DEFAULT = 'Jock Jams'
def print_device_info():
pygame.midi.init()
_print_device_info()
pygame.midi.quit()
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.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post
pygame.midi.init()
_print_device_info()
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 )
pygame.display.set_mode((1,1))
going = True
while going:
events = event_get()
for e in events:
if e.type in [QUIT]:
going = False
if e.type in [KEYDOWN]:
going = False
if e.type in [pygame.midi.MIDIIN]:
#
# THIS IS WHERE ALL THE GOOD SHIT GOES
#
if e.dict['status']==144: #144 is key down
print 'key '+str(e.dict['data1'])+' pressed'
try:
tagToPlay = TAGMAP[PADMAP.index(e.dict['data1'])]
print 'play tag ' + tagToPlay
except Exception, error:
#raise e
print error
print 'no tag defined for that key, lets play a Jock Jam instead'
tagToPlay = DEFAULT
urlToFetch='http://'+SERVER+URLROOT+'/play/tag/'+urllib.quote(tagToPlay)
print urlToFetch
#response=urllib.urlopen(urlToFetch)
if i.poll():
midi_events = i.read(10)
# convert them into pygame events.
midi_evs = pygame.midi.midis2events(midi_events, i.device_id)
for m_e in midi_evs:
event_post( m_e )
del i
pygame.midi.quit()
def usage():
print ("--input [device_id] : Midi message logger")
print ("--output [device_id] : Midi piano keyboard")
print ("--list : list available midi devices")
def main(mode='output', device_id=None):
"""Run a Midi example
Arguments:
mode - if 'output' run a midi keyboard output example
'input' run a midi event logger input example
'list' list available midi devices
(default 'output')
device_id - midi device number; if None then use the default midi input or
output device for the system
"""
if mode == 'input':
input_main(device_id)
elif mode == 'output':
output_main(device_id)
elif mode == 'list':
print_device_info()
else:
raise ValueError("Unknown mode option '%s'" % mode)
if __name__ == '__main__':
try:
device_id = int( sys.argv[-1] )
except:
device_id = None
if "--input" in sys.argv or "-i" in sys.argv:
input_main(device_id)
elif "--output" in sys.argv or "-o" in sys.argv:
output_main(device_id)
elif "--list" in sys.argv or "-l" in sys.argv:
print_device_info()
else:
usage()