def midiOutput() : mididev = config.mididev midiConn = -1 lastError = 0 while True : try : midiConn = os.open(config.mididev, os.O_WRONLY) while 1 : midiMsg = outq.get(True) display = "" for i in midiMsg : display = display + "0x%x " % i stats.midiout(display) os.write(midiConn, bytearray(midiMsg)) except Exception as e : stats.error(e) print("**> Midi Output Exception cought: %s" % e) if lastError + 5 > time.time() : print("Waiting before trying again....") time.sleep(5) lastError = time.time() except BaseException : print("Keyboard / base interrupt received, stopping deamon...") return 1 finally : # close stuff we need to try : if midiConn != -1 : os.close(midiConn) except : pass
def midiOutput(): mididev = config.mididev midiConn = -1 lastError = 0 while True: try: midiConn = os.open(config.mididev, os.O_WRONLY) while 1: midiMsg = outq.get(True) display = "" for i in midiMsg: display = display + "0x%x " % i stats.midiout(display) os.write(midiConn, bytearray(midiMsg)) except Exception as e: stats.error(e) print("**> Midi Output Exception cought: %s" % e) if lastError + 5 > time.time(): print("Waiting before trying again....") time.sleep(5) lastError = time.time() except BaseException: print("Keyboard / base interrupt received, stopping deamon...") return 1 finally: # close stuff we need to try: if midiConn != -1: os.close(midiConn) except: pass
def midiInput(): lastError = 0 while 1: try: midiIn = os.open(config.mididev, os.O_RDONLY) sysexBuffer = [] while 1: midiMsg = os.read(midiIn, 1024) toSend = [] for i in midiMsg: i = ord(i) if len(sysexBuffer) > 0: sysexBuffer.append(i) if i == 0xF7: toSend.extend(sysexBuffer) sysexBuffer = [] elif i == 0xF0: sysexBuffer.append(i) else: toSend.append(i) if len(toSend) == 0: continue stats.midiin(toSend) outq.put_nowait(toSend) if config.sendport != 0: oscMsg = "/midi/" for i in toSend: oscMsg = oscMsg + "%x " % i # OSC string must be filled with nulls, multiples of 4 footer = len(oscMsg) % 4 if footer == 0: footer = 4 oscMsg = oscMsg + ("\0" * footer) networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) networkConn.sendto(oscMsg, (config.sendhost, config.sendport)) except Exception as e: stats.error(e) print("**> Midi Input Exception cought: %s" % e) if lastError + 4 > time.time(): print("Waiting before trying again....") time.sleep(4) lastError = time.time() except BaseException: return 1 finally: try: os.close(midiIn) except: pass
def midiInput() : lastError = 0 while 1 : try : midiIn = os.open(config.mididev, os.O_RDONLY) sysexBuffer = [] while 1 : midiMsg = os.read(midiIn, 1024); toSend = [] for i in midiMsg : i = ord(i) if len(sysexBuffer) > 0 : sysexBuffer.append(i) if i == 0xF7 : toSend.extend(sysexBuffer) sysexBuffer = [] elif i == 0xF0 : sysexBuffer.append(i) else : toSend.append(i) if len(toSend) == 0 : continue stats.midiin(toSend); outq.put_nowait(toSend) if config.sendport != 0 : oscMsg = "/midi/" for i in toSend : oscMsg = oscMsg + "%x " % i # OSC string must be filled with nulls, multiples of 4 footer = len(oscMsg) % 4 if footer == 0 : footer = 4 oscMsg = oscMsg + ("\0" * footer) networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) networkConn.sendto(oscMsg, (config.sendhost, config.sendport)) except Exception as e : stats.error(e) print("**> Midi Input Exception cought: %s" % e) if lastError + 4 > time.time() : print("Waiting before trying again....") time.sleep(4) lastError = time.time() except BaseException : return 1 finally : try : os.close(midiIn) except : pass
def oscInput(): pilink_debug = "OSCinput: " networkConn = -1 lastError = 0 while 1: try: networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) networkConn.bind(("0.0.0.0", config.receiveport)) print("PILINK: LISTENING UDP PORT %d" % config.receiveport) while 1: midiMsg = [] # send empty midi message msg = networkConn.recvfrom(4096)[0] try: parsedMsg = [] oscParser(msg, parsedMsg) #stats.oscin(str(parsedMsg)) for i in parsedMsg: midiMsg.extend(osc2Midi(i)) except Exception as e: stats.error(e) exc_type, exc_value, exc_traceback = sys.exc_info() print( str( traceback.format_exception(exc_type, exc_value, exc_traceback))) if len(midiMsg) != 0: outq.put_nowait(midiMsg) except Exception as e: stats.error(e) print("PILINK: **> Exception cought: %s" % e) if lastError + 5 > time.time(): print("PILINK: Waiting before trying again....") time.sleep(5) lastError = time.time() except BaseException: print( "PILINK: Keyboard / base interrupt received, stopping deamon..." ) return 1 finally: try: if networkConn != -1: networkConn.close() except: pass
def midiOutput(): pilink_debug = "MIDIout: " midiConn = -1 lastError = 0 while True: try: midiConn = rtmidi.MidiOut() #neu available_ports = midiConn.get_ports() #neu print("PILINK: --> midioutports: ") #neu-test for i in available_ports: #neu-test print("PILINK: --> " + i) #neu-test if available_ports: #neu midiConn.open_port(config.rtmidi_PORT) #neu else: #neu midiConn.open_virtual_port("My virtual output") #neu while 1: midiMsg = outq.get(True) display = "" for i in midiMsg: display = display + "0x%x " % i #stats.midiout(display) # chan Note Vel midiConn.send_message(midiMsg) #neu except Exception as e: stats.error(e) print("PILINK: **> Midi Output Exception cought: %s" % e) if lastError + 5 > time.time(): print("PILINK: Waiting before trying again....") time.sleep(5) lastError = time.time() except BaseException: print( "PILINK: Keyboard / base interrupt received, stopping deamon..." ) return 1 finally: # close stuff we need to try: if midiConn != -1: del midiConn #neu except: pass
def oscInput() : networkConn = -1 lastError = 0 while 1 : try : networkConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) networkConn.bind(("0.0.0.0", config.receiveport)) print("LISTENING UDP PORT %d" % config.receiveport) while 1 : midiMsg = [] # send empty midi message msg = networkConn.recvfrom(4096)[0] try : parsedMsg = [] oscParser(msg, parsedMsg) stats.oscin(str(parsedMsg)) for i in parsedMsg : midiMsg.extend(osc2Midi(i)) except Exception as e : stats.error(e) exc_type, exc_value, exc_traceback = sys.exc_info() print(str(traceback.format_exception(exc_type, exc_value, exc_traceback))) if len(midiMsg) != 0 : outq.put_nowait(midiMsg) except Exception as e : stats.error(e) print("**> Exception cought: %s" % e) if lastError + 5 > time.time() : print("Waiting before trying again....") time.sleep(5) lastError = time.time() except BaseException : print("Keyboard / base interrupt received, stopping deamon...") return 1 finally : try : if networkConn != - 1 : networkConn.close() except : pass
def osc2Midi(msg) : oscToken = msg[0].split("/")[1:] token = oscToken.pop(0) midiMsg = [] # missing link protocol if token == 'midi' : token = oscToken.pop(0) if token == 'z' : while len(oscToken) != 0 : token = oscToken.pop(0) midiMsg.extend(parseMidiMsg(token, msg[1:])) else : midiMsg = parseMidiMsg(token, msg[1:]) # TouchOSC and Reaktor protocol elif token.isdigit() : chl = int(token) if chl >= 0 and chl < 16 : if len(oscToken) == 0 : return midiMsg else : token = oscToken.pop() # Reaktor vs TouchOSC protocol if token.find('_') >= 0 : token.replace('_', ' ') msgToken = token.split() action = msgToken.pop() ### TODO add more OSC actions here if action == 'note' : midiMsg = [ ( '0x90' + chl ), int(msgToken[0]), int(msgToken[1]) ] else : stats.error("Unknown OSC action %s" % action) return midiMsg
def osc2Midi(msg): oscToken = msg[0].split("/")[1:] token = oscToken.pop(0) midiMsg = [] # missing link protocol if token == 'midi': token = oscToken.pop(0) if token == 'z': while len(oscToken) != 0: token = oscToken.pop(0) midiMsg.extend(parseMidiMsg(token, msg[1:])) else: midiMsg = parseMidiMsg(token, msg[1:]) # TouchOSC and Reaktor protocol elif token.isdigit(): chl = int(token) if chl >= 0 and chl < 16: if len(oscToken) == 0: return midiMsg else: token = oscToken.pop() # Reaktor vs TouchOSC protocol if token.find('_') >= 0: token.replace('_', ' ') msgToken = token.split() action = msgToken.pop() ### TODO add more OSC actions here if action == 'note': midiMsg = [('0x90' + chl), int(msgToken[0]), int(msgToken[1])] else: stats.error("Unknown OSC action %s" % action) return midiMsg
# single bead move new_x0 = old_x0 new_x1 = old_x1 + np.random.randn() * opt_sig(omega, tau, lam) path[islice] = new_x0 path[(islice + 1) % nslice] = new_x1 # calculate action change new_action = action(path, omega, beta, lam) prob = np.exp(-(new_action - old_action)) # action = -ln_rho # accept or recject if np.random.rand() < prob: naccept += 1 else: path[islice] = old_x0 path[(islice + 1) % nslice] = old_x1 # end if prob nmove += 1 # end for islice # end for istep # end for isample np.savetxt('pos.dat', pos) pos2 = pos[nequil:] * pos[nequil:] x2eq = x2[nequil:] accept = float(naccept) / nmove #float(naccept)/nsample/nslice/block_size print(omega, beta, sig, accept, corr(pos2), np.mean(x2eq), error(x2eq)) # end __main__
return args.omega, args.beta, args.lam, args.sig, args.nequil, args.nsample, args.use_opt_sig, args.block_size # end def if __name__ == '__main__': omega, beta, lam, sig, nequil, nsample, use_opt_sig, block_size = parse_inputs( ) # calculate optimal step size if use_opt_sig: sig = opt_sig(omega, beta, lam) # end if accept, x2 = sample_1d_harmonic_analytic(ln_rho, omega, beta, lam, sig, block_size) x2eq = x2[nequil:] fmt = "{omega:6.2f} {beta:4.2f} {sig:4.2f} {accept:3.2f} {corr:3.1f} {x2:4.6f} {x2e:4.6f}" output = fmt.format(omega=omega, beta=beta, sig=sig, accept=accept, corr=corr(x2eq), x2=np.mean(x2eq), x2e=error(x2eq)) print(output) #print( omega,beta,sig,accept,corr(x2eq),np.mean(x2eq),error(x2eq) ) np.savetxt('ax2.dat', x2) # end __main__
prob = np.exp(-(new_action - old_action)) if np.random.rand() < prob: naccept += 1 old_action = new_action else: path[islice] -= move # end if # end for islice # end for istep # end for isample print(float(naccept) / nsample / block_size / (nslice - 1)) np.savetxt('action.dat', action_arr) acteq = action_arr[nequil:] act_mean = np.mean(acteq) act_error = error(acteq) path_mean = np.mean(all_paths, axis=1) path_sig = np.std(all_paths, axis=1) fig, ax = plt.subplots(1, 1) ax.set_xlabel('x', fontsize=16) ax.set_ylabel('t', fontsize=16) ax.errorbar(path_mean, tau * np.arange(nslice), xerr=path_sig / np.sqrt(nsample - nequil), fmt='.-', label='sampled path') ax.plot(path0, tau * np.arange(nslice), c='k', label='classical path') #ax.annotate(xy=(-0.5,3),s='true=%3.2f'%action0) #ax.annotate(xy=(0.5,3),s='link=%3.2f+-%3.2f'%(act_mean,act_error)) print(action0, act_mean, '+-', act_error)