Skip to content

Commit 624ca20

Browse files
committed
added midi events example and filter for ListAll()
1 parent b03c743 commit 624ca20

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

MANIFEST.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
include examples/hello.py
22
include examples/information.py
3+
include examples/launchpad_pressure.py
34
include examples/launchpad_pro.py
5+
include examples/launchpad_rgb.py
46
include examples/launchpad_rgb-fire.py
57
include examples/launchpad_rgb-pulse.py
6-
include examples/launchpad_rgb.py
8+
include examples/midi_events.py
79
include README.md
810
include LICENSE.txt
911
include images/lppro_colorcodes.png

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Successfully tested with Ubuntu 18.04-LTS+. Requires compiling your own PyGame t
7878
- added demo file "launchpad_pressure.py" for pressure sensitivity
7979
- added multiple search names for the X
8080
- updated all rgb-demos to work with the X
81+
- changed ListAll() method to optionally accept a string to query specific devices only
82+
- added a general midi_events.py demo file for better debugging
8183

8284
### CHANGES 2020/05/XX:
8385

@@ -944,7 +946,7 @@ Btw, the fireworks demo will play whenever the Launchpad cannot be enumerated (c
944946

945947
### Utility functions
946948

947-
ListAll()
949+
ListAll( [searchString] )
948950
EventRaw()
949951

950952

@@ -1245,13 +1247,15 @@ Functions requiring a color code have a "...ByCode" naming style.
12451247
RETURN:
12461248

12471249

1248-
### ListAll()
1250+
### ListAll( searchString = '' )
12491251

12501252
Debug function.
1251-
Can be called any time and does not even require Open().
12521253
Prints a list of all detected MIDI devices and addresses.
1254+
Can be called any time and does not even require an opened device.
1255+
The optional <searchString> parameter can be used to return a filtered set of
1256+
device names only. By default, it is set to an empty string, which simply prints everything.
12531257

1254-
PARAMS:
1258+
PARAMS: <searchString> [OPTIONAL] only devices containing this string will be considered
12551259
RETURN:
12561260

12571261

examples/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
!launchpad_rgb.py
99
!launchpad_rgb-pulse.py
1010
!launchpad_pressure.py
11-
11+
!midi_events.py

examples/midi_events.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
#
3+
# Show MIDI events for a device.
4+
#
5+
# Some restrictions apply:
6+
# - This will only work if a device has an in- AND output, because the Launchpad
7+
# classes always open both directions.
8+
# - If multiple devices are attached or the search string returns more than one device,
9+
# only the first one will be openend. Remove devices as necessary.
10+
# - Picking the wrong device might result in no output.
11+
# - Quit with CTRL-C :)
12+
#
13+
#
14+
# FMMT666(ASkr) 7/2013..8/2020
15+
# www.askrprojects.net
16+
17+
import sys
18+
19+
try:
20+
import launchpad_py as launchpad
21+
except ImportError:
22+
try:
23+
import launchpad
24+
except ImportError:
25+
sys.exit("error loading launchpad.py")
26+
27+
28+
29+
def cmdInput( prompt ):
30+
if sys.version_info.major == 2:
31+
inName = str(raw_input( prompt ))
32+
elif sys.version_info.major == 3:
33+
inName = str(input( prompt ))
34+
else:
35+
print('Meh, please consider running this with either Python 2 or 3')
36+
sys.exit(-1)
37+
38+
return inName
39+
40+
41+
def main():
42+
43+
lp = launchpad.Launchpad()
44+
45+
print("===\nAvailable MIDI devices:")
46+
lp.ListAll( )
47+
print("===")
48+
49+
print("Enter a part of the device name, that shall be monitored.")
50+
print("The string needs to cover the in- and output channels of a device!")
51+
print("Otherwise, opening only one input or one output channel will result in an error.")
52+
print("The search string is case-insensitive.")
53+
54+
inName = cmdInput( " search string:" )
55+
56+
while True:
57+
print("===")
58+
lp.ListAll( searchString = inName )
59+
print("===")
60+
61+
inOk = cmdInput( " ENTER to continue or new search string:" )
62+
if inOk == '':
63+
break
64+
else:
65+
inName = inOk
66+
67+
try:
68+
lp.Open( 0, inName )
69+
except:
70+
print("error opening this device")
71+
sys.exit(-1)
72+
73+
print("===")
74+
print("Now hit the keys, turn the knobs or push the buttons:")
75+
76+
while(True):
77+
events = lp.EventRaw()
78+
if events != []:
79+
print( events )
80+
81+
82+
if __name__ == '__main__':
83+
main()

launchpad_py/launchpad.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ def SearchDevices( self, name, output = True, input = True, quiet = True ):
203203

204204
for n in range( midi.get_count() ):
205205
md = midi.get_device_info( n )
206-
if quiet == False:
207-
print(md)
208-
sys.stdout.flush()
209206
if str( md[1].lower() ).find( name.lower() ) >= 0:
207+
if quiet == False:
208+
print('%2d' % ( i ), md)
209+
sys.stdout.flush()
210210
if output == True and md[3] > 0:
211211
ret.append( i )
212212
if input == True and md[2] > 0:
@@ -302,8 +302,8 @@ def Close( self ):
302302
#-------------------------------------------------------------------------------------
303303
#-- prints a list of all devices to the console (for debug)
304304
#-------------------------------------------------------------------------------------
305-
def ListAll( self ):
306-
self.midi.SearchDevices( "*", True, True, False )
305+
def ListAll( self, searchString = '' ):
306+
self.midi.SearchDevices( searchString, True, True, False )
307307

308308

309309
#-------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)