def connect(name='Synth input'): for dev in range(pypm.CountDevices()): interf, p_name, inp, outp, opened = pypm.GetDeviceInfo(dev) if (outp == 1) and name in p_name: port = pypm.Output(dev, 0) return port return None
def run(self): self.running = True pypm.Initialize() self.in_device = pypm.Input(self.in_device_id) self.out_device = pypm.Output(self.out_device_id) self.clear() # try: # while self.running: # self.read_buttons() # finally: # print("Shutting down.") # self.in_device.Close() # self.out_device.Close() # pypm.Terminate() # # return try: while self.running: self.set_pattern(0) time.sleep(self.wait) self.set_pattern(1) time.sleep(self.wait) finally: print("Shutting down.") self.clear() self.in_device.Close() self.out_device.Close() pypm.Terminate()
def __init__(self, device_out=0, channel_out=1, device_in=0, channel_in=1): ioflow.TWidget.__init__(self, name="midi", label="Midi Widget", category="test") pypm.Initialize() self.device_out = int(device_out) self.channel_out = int(channel_out) self.device_in = int(device_in) self.channel_in = int(channel_in) self._midi_out = pypm.Output(self.device_out, 20) self.add_pad( ioflow.TPad(label="Midi Note", value=0, flow="in", min=0, max=127, listener=self.play_note)) self.add_pad( ioflow.TPad(label="Volume Control", value=0, flow="in", min=0, max=127, listener=self.volume_control))
def set_device(self, device): """set output midi device """ #check if device exist dev_list = [ self.midi_device_list[i][0] for i in range(len(self.midi_device_list)) ] if device in dev_list: self.midi_device = device if self.midi_out is not None: del self.midi_out # delete old midi device if present # Initializing midi input stream self.midi_out = pypm.Output(self.midi_device, 0) if VERBOSE: line = " Midi device out: " + str(self.get_device_info()[1]) print line return True else: print "OUTPUT: Invalid midi device selected" print dev_list return False
def initialize(self): pypm.Initialize() midiOutput = self.selectFirebox() if (midiOutput >= 0): self.MidiOut = pypm.Output(midiOutput, latency) return True else: print "Couldn't select Firebox." return False
def __init__(self, in_device_id, out_device_id): print "Opening devices:" self.in_device = pypm.Input(in_device_id) print "\tin: {0}, {1}".format(in_device_id, self.in_device) self.out_device = pypm.Output(out_device_id) print "\tin: {0}, {1}".format(out_device_id, self.out_device) self.write_queue = []
def __init__(self): pypm.Initialize() self.midi = None self.debug = False for n in range(pypm.CountDevices()): info = pypm.GetDeviceInfo(n) name = info[1] isOutput = info[3] if name == MIDIOUT_DEFAULT and isOutput == 1: self.midi = pypm.Output(n, 1) print "Found MIDI output (%s)" % name
def getOutput(cls, dev): """ Get output with devive number 'dev' - dev may also be string matching the target device. If the output was previously loaded this will return the cached device. """ no = dev if isinstance(dev, basestring): no = cls.deviceMap[dev]['output'] key = ('output', no) if key not in cls._channels: cls._channels[key] = pypm.Output(no) return cls._channels[key]
def find_device(): count = pypm.CountDevices() print 'Found {0} devices'.format(count) in_dev, out_dev = (None, None) for i in range(count): info = pypm.GetDeviceInfo(i) print info if info[1] == 'Launchpad Mini': if info[2] == 1: print 'Opening input {0}'.format(i) in_dev = pypm.Input(i) if info[3] == 1: print 'Opening output {0}'.format(i) out_dev = pypm.Output(i) return (in_dev, out_dev)
def TestOutput(): latency = int(raw_input("Type latency: ")) print PrintDevices(OUTPUT) dev = int(raw_input("Type output number: ")) MidiOut = pypm.Output(dev, latency) print "Midi Output opened with ", latency, " latency" dummy = raw_input("ready to send program 1 change... (type RETURN):") MidiOut.Write([[[0xc0, 0, 0], pypm.Time()]]) dummy = raw_input("ready to note-on... (type RETURN):") MidiOut.Write([[[0x90, 60, 100], pypm.Time()]]) dummy = raw_input("read to note-off... (type RETURN):") MidiOut.Write([[[0x90, 60, 0], pypm.Time()]]) dummy = raw_input("ready to note-on (short form)... (type RETURN):") MidiOut.WriteShort(0x90, 60, 100) dummy = raw_input("ready to note-off (short form)... (type RETURN):") MidiOut.WriteShort(0x90, 60, 0) print print "chord will arpeggiate if latency > 0" dummy = raw_input("ready to chord-on/chord-off... (type RETURN):") chord = [60, 67, 76, 83, 90] ChordList = [] MidiTime = pypm.Time() for i in range(len(chord)): ChordList.append([[0x90, chord[i], 100], MidiTime + 1000 * i]) MidiOut.Write(ChordList) while pypm.Time() < MidiTime + 1000 + len(chord) * 1000: pass ChordList = [] # seems a little odd that they don't update MidiTime here... for i in range(len(chord)): ChordList.append([[0x90, chord[i], 0], MidiTime + 1000 * i]) MidiOut.Write(ChordList) print("Sending SysEx messages...") # sending with timestamp = 0 should be the same as sending with # timestamp = pypm.Time() dummy = raw_input( "ready to send a SysEx string with timestamp = 0 ... (type RETURN):") MidiOut.WriteSysEx( 0, '\xF0\x7D\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\xF7') dummy = raw_input( "ready to send a SysEx list with timestamp = pypm.Time() ... (type RETURN):" ) MidiOut.WriteSysEx(pypm.Time(), [0xF0, 0x7D, 0x10, 0x11, 0x12, 0x13, 0xF7]) dummy = raw_input("ready to close and terminate... (type RETURN):") del MidiOut
def main(): a = ScorePhrase() a.addEvent(SE(60, 127, 0)) a.addEvent(SE(55, 127, 9)) a.addEvent(SE(52, 127, 18)) a.addEvent(SE(57, 127, 27)) a.addEvent(SE(59, 127, 33)) a.addEvent(SE(57, 127, 39)) a.addEvent(SE(56, 127, 42)) track1 = ScoreTrack() track1.addPhrase(a, 0) track1.setMidiChannel(1) track1.setMidiTranspose(12) player = Player() player.addTrack(track1) #initialize midi pypm.Initialize() m = pypm.Output(0, 0) m.WriteShort(0xB0, 99, 1) m.WriteShort(0xB0, 74, 51) m.WriteShort(0xB0, 11, 82) m.WriteShort(0xB0, 75, 80) m.WriteShort(0xB0, 77, 72) m.WriteShort(0xB0, 71, 73) bpm = 120 deltat = 60000.0 / bpm / 4 / 4 print deltat # for 2 bars for b in range(2): # for all 48 PPQN events for t in range(0, 47, 1): # for all tracks in the player for z in player.tracks: events = z.getMidiAt(b, t) if (events != None): print("midi events on track [ %s ] at %d,%d: %s") % ( z.getName(), b, t, events) n = events[0] if (n != None): m.WriteShort(n[0], n[1], n[2]) msleep(deltat)
def test(): latency = 200 #latency is in ms, 0 means ignore timestamps dev = 0 MidiOut = pypm.Output(dev, latency) tempo = 100 dur = 60.0 / tempo / 2 notes = [60, 63, 67] n = 0 t = pypm.Time() #current time in ms while n < 10: t += 2 * dur * 1000 playChord(MidiOut, [notes[0] - 12], 1, 127, dur, t) playChord(MidiOut, notes, 0, 60, dur, t) n += 1 while pypm.Time() < t + 2 * dur * 1000: pass del MidiOut
def send_real(send_conn, outdev, latency, debug=False): signal.signal(signal.SIGINT,signal.SIG_IGN) MidiOut = pypm.Output(outdev, latency) while True: try: timestamp, msg = send_conn.recv() except EOFError, e: break if timestamp < 0: break if not msg or len(msg) < 2: print "Malformed message", timestamp, [ hex(b) for b in msg ] continue # sysex if msg[0] == 0xF0 and msg[-1] == 0xF7: if debug: print "Sending SysEx:", timestamp, [ hex(b) for b in msg ] MidiOut.WriteSysEx( timestamp, msg) else: print "Trying to send non-sysex message" pass
def __init__(self): self.outd = pypm.Output(0, 0) self.ind = pypm.Input(1) #@TODO: select from dropdown. self.pyCallback = lambda a, b, c: None # do nothing self.echo = True
def __init__(self, idIn, idOut): self._midiIn = pypm.Input(idIn) self._midiOut = pypm.Output(idOut, 0)
def build_device(self, **kwargs): return pyportmidi.Output(self.dev_info.index, 1)
def __init__(self, pattern=Pattern()): pypm.Initialize() self.bpm = 120 self.pattern = pattern self.output = pypm.Output(pypm.GetDefaultOutputDeviceID(), LATENCY)
# init pyportmide pypm.Initialize() # init curses stdscr = curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(1) # open midi out PrintDevices(OUTPUT) #dev = int(raw_input("Type output number: ")) dev = 8 #print "Using Device:", dev MidiOut = pypm.Output(dev, 100) # call choose Choose([["Diatonic Melody", DiatonicMelody], ["Random Key", RandomKey]]) DiatonicMelody() # uninit curses curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() # uninit pyportmidi del MidiOut pypm.Terminate()
def main2(): p = Player() a = AutomationTrack() s = ScoreTrack() s1 = ScorePhrase() s2 = ScorePhrase() s.addPhrase(s1, 0) s.addPhrase(s2, 1) s.setMidiTranspose(-12) # lead thing b = 12 * 6 s1.addEvent(SE(b, 127, 0)) s1.addEvent(SE(b, 127, 6)) s1.addEvent(SE(b + 7, 127, 12)) s1.addEvent(SE(b + 7, 127, 18)) s1.addEvent(SE(b + 9, 127, 24)) s1.addEvent(SE(b + 9, 127, 30)) s1.addEvent(SE(b + 7, 127, 36)) s2.addEvent(SE(b + 5, 127, 0)) s2.addEvent(SE(b + 5, 127, 6)) s2.addEvent(SE(b + 4, 127, 12)) s2.addEvent(SE(b + 4, 127, 18)) s2.addEvent(SE(b + 2, 127, 24)) s2.addEvent(SE(b + 2, 127, 30)) s2.addEvent(SE(b + 0, 127, 36)) # automations a1 = AutomationPhrase() a.setMidiChannel(1) a.setCC(64) a1.addEvent(AE(0, 0)) a1.addEvent(AE(127, 24)) a.addPhrase(a1, 0) a.addPhrase(a1, 1) p.addTrack(a) p.addTrack(s) s.getLastBar() #p.debug_ShowMidiDevices() #return pypm.Initialize() m = pypm.Output(0, 0) m.WriteShort(0xB0, 99, 1) #m.WriteShort(0xB0, 52, 1) #m.WriteShort(0xB0, 71, 64) #m.WriteShort(0xB0, 76, 1) print "a=", a.getMidiAt(0, 0) print "s=", s.getMidiAt(0, 0) print "a=", a.getMidiAt(0, 24) print "s=", s.getMidiAt(0, 24) # for 2 bars for b in range(2): # for all 48 PPQN events for t in range(0, 47, 6): # for all tracks in the player for z in p.tracks: events = z.getMidiAt(b, t) if (events != None): print("midi events on track [ %s ] at %d,%d: %s") % ( z.getName(), b, t, events) n = events[0] if (n != None): m.WriteShort(n[0], n[1], n[2]) msleep(200)
def __init__(self, input_device, output_device): self.input_device = input_device self.output_device = output_device self.I = pypm.Input(self.input_device) self.O = pypm.Output(self.output_device) self.reading = -1
def __init__(self, port_number): super(MidiTest, self).__init__() pypm.Initialize() self.output = pypm.Output(port_number, 0)
if size == 0: if MidiData[0][0][1] == 18: bar_size = MidiData[0][0][2] / 6 else: bar_size = size DrawBar(MidiOut, input_pos, bar_size) # main code begins here pypm.Initialize( ) # always call this first, or OS may crash when you try to open a stream PrintDevices(OUTPUT) dev = int(raw_input("Type output number: ")) MidiOut = pypm.Output(dev, 0) PrintDevices(INPUT) dev = int(raw_input("Type input number: ")) MidiIn = pypm.Input(dev) # turn off internal LED finger tracking SetLEDsIgnoreTouch(MidiOut) # set initial column SendColumn(MidiOut, 45) # turn on pressure output EnablePressureOutput(MidiOut) demo_choice = int( raw_input(""" Choose a demo: 1) Cursor tracks finger position 2) Cursor size adjusts based on pressure
def SysexTest( deviceInput, deviceOutput, queryBlockAddress, latency ): #midiInput = None midiInput = pypm.Input( int( deviceInput ), 1024 ) midiOutput = pypm.Output( int( deviceOutput ), latency ) ################################################## # Cycle through buttons # for i in range(1,100): # msg = SysexMessage( ord('s'), 0, 1, [ i % 4 ] ) # midiOutput.WriteSysEx( 0, str( bytearray( msg ) ) ) # time.sleep(0.01) #''' ################################################## # Set ICB #msg = SysexMessage( ord('i'), 67, 16, [ 0x00, 0x00, 0x41, 0x41, 0x41, 0x00, 0x08, 0x00, 0x00, 0xC4, 0x48, 0x55, 0x4D, 0x41, 0x4E, 0x4D ] ) #midiOutput.WriteSysEx( 0, str( bytearray( msg ) ) ) ################################################## # Get ICB msg = SysexMessage( ord('r'), queryBlockAddress, 1, [ ord('i') ] ) # Switch control key to B #msg = SysexMessage( ord('s'), 0, 1, [ 1 ] ) # Get Instrument Control Block at address 66 #msg = SysexMessage( ord('r'), 66, 1, [ ord('i') ] ) # Get VCF parameters #msg = SysexMessage( ord('r'), queryBlockAddress, 1, [ ord('v') ] ) # Get Amplitude envelope at address 65 #msg = SysexMessage( ord('r'), 65, 1, [ BlockIdentifier.AMPL ] ) # Get fixed wavetable #msg = SysexMessage( ord('r'), queryBlockAddress, 1, [ BlockIdentifier.FIXWAVE ] ) # Get relative wavetable #msg = SysexMessage( ord('r'), queryBlockAddress, 1, [ BlockIdentifier.RELWAVE ] ) # Change VCF parameters at address 64 #msg = SysexMessage( ord('v'), 64, 10, [ 3, 0, 11, 149, 232, 232, 0, 49, 0, 49 ] ) # Change Amplitude envelope at address 65 ''' msg = SysexMessage( ord('a'), 65, 44, [ 0x20, 0xb1, 0x0a, 0x20, 0x00, 0x00, 0xef, 0x00, 0x00, 0xc1, 0x00, 0x00, 0x85, 0x00, 0x00, 0xa2, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0x20, 0xdf, 0x4b, 0x20, 0x07, 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] ) # ''' # Test #msg = SysexMessage( ord('f'), 65, 32, [ 0x11 ] * 32 ) # Change Frequency envelope at address 65 #msg = SysexMessage( BlockIdentifier.FREQ, 65, 16, [ 0x02,0xFF, 0xA0,0x00, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 ] ) ################################################## # Print output message print 'Out:' print ' '.join('0x%02X ' % b for b in msg) #print ' '.join((bin(b)[2:]).zfill(8) for b in msg) # Write message to MIDI midiOutput.WriteSysEx( 0, str( bytearray( msg ) ) ) # Poll for MIDI input readTimeout = 0.5 if midiInput: readTimeStart = time.time() readReady = True msg = [] while readReady: if not midiInput.Poll(): # Check timeout if (time.time() - readTimeStart) > readTimeout: # Timeout exceeded, abort reading readReady = False else: # Read data MidiData = midiInput.Read(1024) # Append data to buffer for m in MidiData: for n in m[0]: msg.append( n ) if msg: # Parse SysEx header headerSize = 9 if msg[0] == 0xF0 and msg[1] == 0x25 and msg[2] == 0x01 and len( msg ) > headerSize: # Wersi MK1 SysEx message # # 11110000: MIDI SysEx identifier # 00100101: Wersi identifier # 00000001: MK1 identifier # 011NXXXX: Block identifier (hi/lo) # 010NXXXX: Block address (hi/lo) # 001NXXXX: Block length (hi/lo) # 000NXXXX: Data (hi/lo) blockIdentifier = ConvertFromNibbles( [ msg[3], msg[4] ] )[0] blockAddress = ConvertFromNibbles( [ msg[5], msg[6] ] )[0] blockLength = ConvertFromNibbles( [ msg[7], msg[8] ] )[0] data = msg[9:] # Always one byte missing :( # Probably due to a crappy MIDI SysEx ROM implementation in the Wersi. if (len( data ) / 2) >= ( blockLength - 1 ): print "* Received Wersi MK1 SysEx message, length %u" % len(msg) print "Block identifier: %s (%u)" % ( chr( blockIdentifier ), blockIdentifier ) print "Block address: %u (0x%02X)" % ( blockAddress, blockAddress ) print "Block length: %u bytes" % ( blockLength ) print "Data (got %u bytes):" % ( len( data ) / 2 ) print ' '.join('0x%02X ' % b for b in ConvertFromNibbles( data )) print ' '.join('(%s) ' % (chr( b ) if b >= 32 else '?') for b in ConvertFromNibbles( data )) print ' '.join('%08u' % b for b in ConvertFromNibbles( data )) print ' '.join((bin(b)[2:]).zfill(8) for b in ConvertFromNibbles( data )) else: print "Received message was too short (got %u, expected %u). Try again." % ( (len( data ) / 2), blockLength - 1 ) del midiInput # Cleanup del midiOutput