def start(self): while True: if alsaseq.inputpending(): (etype, # enum snd_seq_event_type flags, tag, queue, timestamp, source, destination, data) = alsaseq.input() sec, msec = timestamp sclient, sport = source dclient, dport = destination event = self.events[etype] if etype in self.events else etype if event in ['controller', 'pgmchange', 'chanpress', 'pitchbend']: channel, _, _, _, *params = data else: channel, *params = data print('{} event, channel {}, params {}, timestamp {}' .format(event, channel, params, timestamp)) if event in self.binds: self.binds[event](*params[:2])
def main(): se = ServoEvent() parser = MusicParser() alsaseq.client('RoboWhistle PassThrough', 1, 1, False) # Set up a new ALSA channel alsaseq.connectfrom( 1, 129, 0) # Midi file input needs to be sent in to channel 129 # Backup ALSA channel to run on port 128. Can use either a virtual synth (e.g. Timidity or a physical MIDI keyboard) alsaseq.connectto(1, 128, 0) srv = ServoEvent() srv.ResetServoEvent() #To enter FreePlay mode provide any argument when running the program; if left blank AutoPlay will proceed automaticlly if len(sys.argv) is 2: print "Entering FreePlay Mode" play = FreePlay() play.ManualPlay() else: print str(len(sys.argv)) while 1: if alsaseq.inputpending(): # ALSA queue event = alsaseq.input() # Pop event from top of the queue eventPitch = event[7][ 1] # Pitch of the note that needs to be played parsedNote = parser.AnalyseSingleNote(eventPitch % 12) if parsedNote is None: alsaseq.output( event ) # Event is unplayable by the whistle, forward it to the synthesiser else: se.PlayNoteEvent(parsedNote) # Pass item for playing
def play(self): while self.is_running: # check for quit in pygame for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: self.interrupt = not self.interrupt print("toggle interrupt") if event.key == pygame.K_ESCAPE: self.is_running = False if event.key == pygame.K_RIGHT: self.update_step() if event.key == pygame.K_LEFT: self.update_step(direction="backward") elif event.type == pygame.QUIT: self.is_running = False if alsaseq.inputpending(): event = alsaseq.input() if event[ 0] == 6: # note on event (zumindest bei kleinem midikeyboard) self.reaction_note_on(event) elif event[0] == 7: self.reaction_note_off(event)
def interaction_loop(): """Interaction loop for the box, reads serial, makes predictions, outputs servo and sound.""" global last_received_midi # Start Lever Processing userloc = read_lever() if userloc is not None: if args.verbose: print("Input:", userloc) # Send MIDI to synth. midi_loc = int(userloc * 127) midi_ctl_event = (10, 1, 0, 0, (0, 0), (0, 0), (0, 0), (0, 0, 0, 0, 0, midi_loc)) alsaseq.output(midi_ctl_event) # print("MIDIOUT:", midi_ctl_event) if args.mirror: last_received_midi = midi_loc # Read incoming midi. while(alsaseq.inputpending() > 0): midi_event = alsaseq.input() if midi_event[0] == SND_SEQ_EVENT_CONTROLLER: # just take the controller value last_received_midi = midi_event[7][5] # do something with it if args.verbose: print("Servo:", last_received_midi) # print("MIDI:", midi_event) if args.servo: # Only send to servo if args suggest it. # Only act on most recent message. command_servo(last_received_midi)
def MIDIgen(self): # Try to run using a platform-specific backend if platform.system() == "Linux" and alsa_ok: alsaseq.client("Audionodes", 1, 1, False) while True: if alsaseq.inputpending(): inputData = alsaseq.input() if inputData[0] in (6, 7): # Key f = 440 * (2**((inputData[-1][1] - 48) / 12)) yield { "type": "key", "frequency": f, "velocity": inputData[-1][2], "note": inputData[-1][1] } elif inputData[0] in (10, ): # Sustain yield { "type": "sustain", "velocity": inputData[-1][-1] } time.sleep(0.01) # If such is not available, fall back to PyGame else: pygame.init() midi.init() inputdata = midi.Input(midi.get_default_input_id()) def toDict(event): velocity = event[0][0][2] # Sustain if event[0][0][0] == 176 and event[0][0][1] == 64: return {"velocity": velocity, "type": "sustain"} elif event[0][0][0] == 144: note = event[0][0][1] f = 440 * (2**((note - 48) / 12)) return { "type": "key", "velocity": velocity, "note": note, "frequency": f } while 1: if inputdata.poll(): event = inputdata.read(1) if toDict(event) != None: print(toDict(event)) yield toDict(event) time.sleep(0.01)
def midi_read(self): while alsaseq.inputpending(): event = alsaseq.input() chan = event[7][0] if event[0]==alsaseq.SND_SEQ_EVENT_CONTROLLER and chan==self.midi_chan and self.active_screen=='control': ctrl = event[7][4] val = event[7][5] print ("MIDI CTRL " + str(ctrl) + ", CH" + str(chan) + " => " + str(val)) if ctrl in self.screens['control'].zcontroller_map.keys(): self.screens['control'].zcontroller_map[ctrl].set_value(val,True) if self.polling: top.after(40, self.midi_read)
def run(self): import select alsaseq.client(appinfo.name, self.num_inports, self.num_outports, True) self.start_time = datetime.now() alsaseq.start() log.debug("ALSA sequencer started") alsafd = alsaseq.fd() while not self.join_req: fds_ready = select.select([alsafd], [], [], 0.1) if alsaseq.inputpending(): raw_event = alsaseq.input() new_event = self.create_event(raw_event) self.dispatch_event(new_event)
def midi_read(self): try: while alsaseq.inputpending(): event = alsaseq.input() chan = event[7][0] if event[0]==alsaseq.SND_SEQ_EVENT_CONTROLLER and chan==self.zyngine.get_midi_chan() and self.active_screen=='control': ctrl = event[7][4] val = event[7][5] #print ("MIDI CTRL " + str(ctrl) + ", CH" + str(chan) + " => " + str(val)) if ctrl in self.screens['control'].zcontroller_map.keys(): self.screens['control'].zcontroller_map[ctrl].set_value(val,True) except Exception as err: print("ERROR: zynthian_gui.midi_read() => %s" % err) if self.polling: top.after(40, self.midi_read)
def run(clientName, inPorts, outPorts, controller): curScreen = controller alsaseq.client(clientName, inPorts, outPorts, True) initd = False while True: if not alsaseq.inputpending(): time.sleep(0.001) continue val = alsaseq.input() mtype = val[0] if mtype == _MIDI_CONNECT: __devi = val[7][0] __devo = val[7][2] initd = True elif not initd: continue elif mtype == _MIDI_CC: but = val[7][4] vel = val[7][5] r,c = __b2idx(but) if c == 9: but = PLAY if vel == 0: curScreen.onButtonUp(but, 9 - r, c) else: curScreen.onButtonDown(but, vel, 9 - r, c) elif mtype == _MIDI_ON: but = val[7][1] vel = val[7][2] r,c = __b2idx(but) if vel == 0: curScreen.onButtonUp(but, 9 - r, c) else: curScreen.onButtonDown(GRID, vel, 9 - r, c) elif mtype == _MIDI_OFF: but = val[7][1] vel = val[7][2] r,c = __b2idx(but) curScreen.onButtonUp(GRID, 9 - r, c) elif mtype == _MIDI_MAFTER: vel = val[7][5] curScreen.onMonoAftertouch(vel) elif mtype == _MIDI_PAFTER: but = val[7][1] vel = val[7][2] r,c = __b2idx(but) curScreen.onPolyAftertouch(9 - r, c, vel)
def run(self): import select alsaseq.client(appinfo.name, self.num_inports, self.num_outports, True) self.start_time = datetime.now() alsaseq.start() log.debug("ALSA sequencer started") alsafd = alsaseq.fd() while not self.join_req: fds_ready = select.select([alsafd], [], [], 0.1) if alsaseq.inputpending(): raw_event = alsaseq.input() new_event = self.create_event(raw_event) self.dispatch_event(new_event) alsaseq.stop() alsaseq.close()
def run(self): print ("starting alsa listener") while not sync.terminate.isSet(): if alsaseq.inputpending(): now = time.time() event = alsaseq.input() evtype = event[0] pitch = event[7][1] if evtype == alsaseq.SND_SEQ_EVENT_NOTEON: sync.putEvent(now) else: time.sleep(0.001) continue else: time.sleep(0.001) continue print ("stopping alsa listener")
def Poll(self): if back_end == 'pypm': return self.mDevIn.Poll() elif back_end == 'alsaseq': return alsaseq.inputpending() elif back_end == 'rtmidi': if self.rtmidi_pkt_read_flag: self.rtmidi_pkt = None if not self.rtmidi_pkt: self.rtmidi_pkt = self.mDevIn.get_message() self.rtmidi_pkt_read_flag = False if self.rtmidi_pkt != None: return True elif back_end == 'mididings': print('mididings not implemented') return False return False
def run(self): print "starting alsa listener" while not sync.terminate.isSet(): if alsaseq.inputpending(): event = alsaseq.input() evtype = event[0] pitch = event[7][1] if evtype == alsaseq.SND_SEQ_EVENT_NOTEON: cmd = (PSH_NOTE, pitch) elif evtype == alsaseq.SND_SEQ_EVENT_NOTEOFF: cmd = (POP_NOTE, pitch) elif evtype == alsaseq.SND_SEQ_EVENT_START: cmd = (TRP_START, None) elif evtype == alsaseq.SND_SEQ_EVENT_STOP: cmd = (TRP_STOP, None) elif evtype == alsaseq.SND_SEQ_EVENT_CLOCK: cmd = (TRP_TICK, None) else: continue sync.putCommand(cmd) else: time.sleep(0.005) continue print "stopping alsa listener"
def retrieveinput(): "Retrieve received events." global incoming, waitingforsplit, split p = select.poll() p.register(fd, select.POLLIN) while vivo: p.poll(5000) while alsaseq.inputpending(): entrante = alsaseq.input() nota = entrante[7][1] type = entrante[0] if type in rechazados: continue # discard obnoxious Clavinova events elif type == alsaseq.SND_SEQ_EVENT_ECHO: drums(ritmos[nritmo], tempo, compases) continue ev = alsamidi.modifyevent(entrante, ch=1) if waitingforsplit: split = nota waitingforsplit = 0 print(nota) continue # discard note if not split: alsaseq.output(entrante) alsaseq.output(ev) incoming.append(ev) incoming.append(entrante) elif nota > split: alsaseq.output(entrante) incoming.append(entrante) else: alsaseq.output(ev) incoming.append(ev) print(len(incoming), "incoming") print("Ending retrieveinput()")
def retrieveinput(): 'Retrieve received events.' global incoming, waitingforsplit, split p = select.poll() p.register(fd, select.POLLIN) while vivo: p.poll(5000) while alsaseq.inputpending(): entrante = alsaseq.input() nota = entrante[7][1] type = entrante[0] if type in rechazados: continue # discard obnoxious Clavinova events elif type == alsaseq.SND_SEQ_EVENT_ECHO: drums(ritmos[nritmo], tempo, compases) continue ev = alsamidi.modifyevent(entrante, ch=1) if waitingforsplit: split = nota waitingforsplit = 0 print(nota) continue # discard note if not split: alsaseq.output(entrante) alsaseq.output(ev) incoming.append(ev) incoming.append(entrante) elif nota > split: alsaseq.output(entrante) incoming.append(entrante) else: alsaseq.output(ev) incoming.append(ev) print(len(incoming), 'incoming') print('Ending retrieveinput()')
#!/usr/bin/env python3 # amiditimer.py - Tom Clayton # Connect input and output to alsa midi streams and this program will # send a message out and time how long it takes for it to return. import alsaseq import sys alsaseq.client('Midi Timer', 1, 1, True) # alsaseq event: # (type, flags, tag, queue, time stamp, source, destination, data) # data = (channel, note, velocity, start, duration) out_event = (6, 1, 0, 1, (0, 0), (0, 0), (0, 0), (0, 60, 127, 0, 0)) input("Midi Timer, connect then hit enter to start.") alsaseq.output(out_event) alsaseq.start() while True: if alsaseq.inputpending(): event = alsaseq.input() if event[0] == 6: print(event[4][1] / 1000000, " ms") break
def Poll(self): ' returns True if input event pending, otherwise False if nothing ' return alsaseq.inputpending()
events = [] EV_KEYDOWN = 6 EV_KEYUP = 7 s = serial.Serial("/dev/ttyUSB0", 115200) ch = 1 def sendmsg(chan,note,vel): print "msg going",chan,note onoff = 0x40 if note >= 0 else 0 note = abs(note) s.write(chr(0x80 | chan | onoff)) s.write(chr(0x60 | (note >> 4))) s.write(chr(0x50 | (note & 0xf))) s.write(chr(0x20 | (vel >> 4))) s.write(chr(0x10 | (vel & 0xf))) while 1: if alsaseq.inputpending(): ch = int(open("ch.txt", "r").read()) event = alsaseq.input() evtype = event[0] if evtype == EV_KEYDOWN: sendmsg(ch, event[7][1], event[7][2]) elif evtype == EV_KEYUP: sendmsg(ch, -event[7][1], event[7][2]) else: print event