7 class USBMIDIConfiguration
:
8 def __init__(self
, cfg
, ifno
, ifalt
):
13 return "cfg=%d ifno=%d ifalt=%d" % (self
.cfg
, self
.ifno
, self
.ifalt
)
15 return "USBMIDIConfiguration(%d,%d,%d)" % (self
.cfg
, self
.ifno
, self
.ifalt
)
17 class USBMIDIDeviceDescriptor
:
18 def __init__(self
, vendorID
, productID
, interfaces
= None):
19 self
.vendorID
= vendorID
20 self
.productID
= productID
21 if interfaces
is None:
24 self
.interfaces
= interfaces
25 def add_interface(self
, config
, ifno
, ifalt
):
26 self
.interfaces
.append(USBMIDIConfiguration(config
, ifno
, ifalt
))
27 def has_interfaces(self
):
28 return len(self
.interfaces
)
30 return "vid=%04x pid=%04x" % (self
.vendorID
, self
.productID
)
32 return "USBMIDIDeviceDescriptor(0x%04x, 0x%04x, %s)" % (self
.vendorID
, self
.productID
, self
.interfaces
)
35 cin_sizes
= [None, None, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1]
36 def __init__(self
, mididev
, midicfg
, debug
= False):
37 dev
= usb
.core
.find(idVendor
= mididev
.vendorID
, idProduct
= mididev
.productID
)
41 if cfgo
.bConfigurationValue
== midicfg
.cfg
:
43 intf
= cfgo
[(midicfg
.ifno
, midicfg
.ifalt
)]
45 raise ValueError, "Configuration %d not found" % midicfg
.cfg
46 print intf
.bNumEndpoints
51 print "endpoint %x" % ep
.bEndpointAddress
52 if ep
.bEndpointAddress
> 0x80:
56 if self
.epOut
is None:
61 data
= self
.epIn
.read(self
.epIn
.wMaxPacketSize
)
64 return array
.array('B', data
)
65 except usb
.core
.USBError
, e
:
68 def encode(self
, port
, msg
):
70 a
.append(16 * port
+ (msg
[0] >> 4))
74 def write(self
, data
):
75 self
.epOut
.write(data
)
77 def parse(self
, data
):
83 cin
, cable_id
= data
[i
] & 15, data
[i
] >> 4
84 msgs
.append(data
[i
+ 1 : i
+ 1 + KeyRig25
.cin_sizes
[cin
]])
89 def findall(vendorID
= None, productID
= None, debug
= False):
91 devices
= usb
.core
.find(find_all
= True)
93 if vendorID
is not None and dev
.idVendor
!= vendorID
:
95 if productID
is not None and dev
.idProduct
!= productID
:
97 thisdev
= USBMIDIDeviceDescriptor(dev
.idVendor
, dev
.idProduct
)
99 print "Device %04x:%04x, class %d" % (dev
.idVendor
, dev
.idProduct
, dev
.bDeviceClass
)
100 if dev
.bDeviceClass
== 0: # device defined at interface level
103 print "Configuration ", cfg
.bConfigurationValue
106 print "Interface %d alternate-setting %d" % (intf
.bInterfaceNumber
, intf
.bAlternateSetting
)
107 print "Class %d subclass %d" % (intf
.bInterfaceClass
, intf
.bInterfaceSubClass
)
108 if intf
.bInterfaceClass
== 1 and intf
.bInterfaceSubClass
== 3:
110 print "(%d,%d,%d): This is USB MIDI" % (cfg
.bConfigurationValue
, intf
.bInterfaceNumber
, intf
.bAlternateSetting
)
111 thisdev
.add_interface(cfg
.bConfigurationValue
, intf
.bInterfaceNumber
, intf
.bAlternateSetting
)
112 if thisdev
.has_interfaces():
113 dev_list
.append(thisdev
)
118 class KnownUSBMIDI(USBMIDI
):
119 def __init__(self
, vendorID
, productID
):
120 devlist
= USBMIDI
.findall(vendorID
, productID
, debug
= False)
123 USBMIDI
.__init
__(self
, devlist
[0], devlist
[0].interfaces
[0])
125 class KeyRig25(KnownUSBMIDI
):
127 KnownUSBMIDI
.__init
__(self
, vendorID
= 0x763, productID
= 0x115)
129 class XMidi2x2(KnownUSBMIDI
):
131 KnownUSBMIDI
.__init
__(self
, vendorID
= 0x41e, productID
= 0x3f08)
133 class LexiconOmega(KnownUSBMIDI
):
135 KnownUSBMIDI
.__init
__(self
, vendorID
= 0x1210, productID
= 2)
137 print USBMIDI
.findall()
139 xmidi
.write(xmidi
.encode(1, [0x90, 36, 100]))
140 xmidi
.write(xmidi
.encode(1, [0x80, 36, 100]))
143 krig
= LexiconOmega()
147 decoded
= krig
.parse(data
)
148 reencoded
= array
.array('B')
150 reencoded
.extend(xmidi
.encode(1, list(msg
)))
151 xmidi
.write(reencoded
)