import usb import alsaseq mpd16 = None idVendor = 0x09e8 idProduct = 0x0062 for bus in usb.busses(): for device in bus.devices: if device.idVendor == idVendor and device.idProduct == idProduct: mpd16 = device break if mpd16 is None: raise ValueError, "No MPD16 found" conf = mpd16.configurations[0] iface = conf.interfaces[0][0] midiept = None for ept in iface.endpoints: if ept.address == 0x82: midiept = ept break if midiept is None: raise ValueError, "MIDI endpoint not found" dev = mpd16.open() dev.setConfiguration(mpd16.configurations[0]) dev.claimInterface(0) dev.setAltInterface(0) #dev.reset() #print "MPD16", dir(mpd16) #print "Config", dir(conf) #print "Iface", dir(iface) #print dev.getString(2, 200) #print midiept.maxPacketSize alsaseq.client("MPD16", 0, 1, False) while True: data = dev.bulkRead(midiept.address, midiept.maxPacketSize, 0) if len(data) == 2: continue pos = 0 while pos < len(data): elen = data[pos] - 32 edata = data[pos + 1 : pos + 1 + elen] channel = edata[0] & 0x0F if edata[0] >= 0x90 and edata[0] <= 0x9F: if edata[2] > 0: alsaseq.output( (alsaseq.SND_SEQ_EVENT_NOTEON, 1, 0, 253, (0, 0), (0, 0), (0, 0), (channel, edata[1], edata[2], 0, 0))) else: alsaseq.output( (alsaseq.SND_SEQ_EVENT_NOTEOFF, 1, 0, 253, (0, 0), (0, 0), (0, 0), (channel, edata[1], 0, 0, 0))) if edata[0] >= 0xA0 and edata[0] <= 0xAF: alsaseq.output( (alsaseq.SND_SEQ_EVENT_KEYPRESS, 1, 0, 253, (0, 0), (0, 0), (0, 0), (channel, edata[1], edata[2], 0, 0))) if edata[0] >= 0xB0 and edata[0] <= 0xBF: alsaseq.output( (alsaseq.SND_SEQ_EVENT_CONTROLLER, 1, 0, 253, (0, 0), (0, 0), (0, 0), (channel, 0, 0, 0, edata[1], edata[2]))) pos += 1 + elen dev.releaseInterface()