def setNote(self, note=60, velocity=100, duration=0.2, channel=1): self.channel = channel self.midinote = note self.velocity = velocity self.note_length = duration self.channel = channel midiMsg = rtmidi.MidiMessage() #set MIDI message params self.note_on = midiMsg.noteOn(self.channel, self.midinote, self.velocity) #NOTE_OFF equals NOTE_ON with velocity 0 self.note_off = midiMsg.noteOn(self.channel, self.midinote, 0) #Update info string self.infostr = self.id + " " + str(self.note_on) + "\n" + str( self.note_off) self.infostr += "\n" + "Duration: " + str(self.note_length) self.infostr += "\n" + "Neural Network Parameters:\n" self.infostr += "Activation level: " + str(self.activation) + "\n" self.infostr += "Activation counter increase: " + str( self.addToCounter) + "\n" self.infostr += "Activation threshold: " + str( self.threshold) + "\n\n\n" print("Note data parameter change: " + str(self)) return
def sendMidi(name, event, *args): if name not in outputs: return m = None if 'note' in event and len(args) == 3: args = [int(round(x)) for x in args] if args[2] > 0: m = rtmidi.MidiMessage.noteOn(*args) else: m = rtmidi.MidiMessage.noteOff(args[0], args[1]) elif 'control' in event and len(args) == 3: args = [int(round(x)) for x in args] m = rtmidi.MidiMessage.controllerEvent(*args) elif 'program' in event and len(args) == 2: args = [int(round(x)) for x in args] m = rtmidi.MidiMessage.programChange(*args) elif 'pitch' in event and len(args) == 2: args = [int(round(x)) for x in args] m = rtmidi.MidiMessage.pitchWheel(*args) elif 'sysex' in event: # rtmidi.MidiMessage(unhexlify('f0 7e 7f 06 01 f7')) creates a sysex message # from hex MIDI data string 'f0 7e 7f 06 01 f7'. # We expect all args to be hex strings! args[0] may contain placeholders of # the form 'nn' that are replaced using args[1..N] to create the final message. midiBytes = args[0].replace(' ', '') i = 1 for m in sysexRegex.finditer(midiBytes): midiBytes = midiBytes[:m.start()] + args[i].replace( ' ', '') + midiBytes[m.end():] i += 1 if debug: ipcSend('log', 'Sysex string: %s' % midiBytes) m = rtmidi.MidiMessage(unhexlify(midiBytes)) if m is None: ipcSend( 'log', 'ERROR: MIDI: could not convert osc args to midi message (%s %s)' % (event, " ".join([str(x) for x in args]))) else: outputs[name].sendMessage(m) if debug: ipcSend('log', 'MIDI sent: %s' % m)
def __init__(self, note=60, velocity=100, duration=0.2, id="NNote", channel=1, activation=0.0, addToCounter=0.0001, threshold=1.0): self.id = id self.infostr = "" self.channel = channel self.note_length = duration self.velocity = velocity self.midinote = note self.midiout = midiout #MIDI settings: # Velocity and duration will be set by the NN eventually. midiMsg = rtmidi.MidiMessage() # set MIDI message params self.note_on = midiMsg.noteOn(self.channel, self.midinote, self.velocity) self.note_off = midiMsg.noteOn(self.channel, self.midinote, 0) #NN settings: self.activation = activation #Initial activation level. self.addToCounter = addToCounter #Activation increase per call to Connection.run(). self.threshold = threshold #Activation threshold, which, when reached, results to activation set to 0.0 and NNote.bang() is called. self.infostr += self.id + " " + str(self.note_on) + "\n" + str( self.note_off) self.infostr += "\n" + "Duration: " + str(self.note_length) self.infostr += "\n" + "Neural Network Parameters:\n" self.infostr += "Activation level: " + str(self.activation) + "\n" self.infostr += "Activation counter increase: " + str( self.addToCounter) + "\n" self.infostr += "Activation threshold: " + str( self.threshold) + "\n\n\n" print("Constructed a neuron: " + str(self))
def interface(port, action, syx=False): ''' Interface for midi send/receive sysex action 'receive' saves file at 'receive_cache' action 'send' send syx return True when done ''' port = find_midi_port(port) if port: if action == 'receive': # wait in number of seconds # and/or midi messages waiting = 10 received = False md = rtmidi.RtMidiIn() md.openPort(port) md.ignoreTypes(False, True, True) while waiting and not received: msg = md.getMessage(1000) if msg and validate_syx(msg): save_syx(msg) received = True break waiting -= 1 md.closePort() return received elif action == 'send': if not syx: return False else: md = rtmidi.RtMidiOut() md.openPort(port) msg = rtmidi.MidiMessage(syx) if validate_syx(msg): md.sendMessage(msg) return True else: print('MIDI port not valid') return False
def output(self, mess): self.midiout.sendMessage(rtmidi.MidiMessage(bytes(mess)))
cmd.append('rtmidi') msg = ' '.join(cmd) print('ERROR: Please install the rtmidi module from pyrtmidi using:') print(msg) if os.path.isdir('micronux/rtmidi'): from .rtmidi.rtmidi import _rtmidi as rtmidi else: try: import rtmidi except ImportError: install_rtmidi() exit() try: test = rtmidi.MidiMessage() except: install_rtmidi(vendor=True) exit() # End of workaround receive_cache = os.path.normpath('programs/cache/received.syx') send_ready = True def list_midi_ports(): ''' List all midi ports, return a dict { name : address } ''' md = rtmidi.RtMidiOut() port_list = {} for port in range(md.getPortCount()): name = md.getPortName(port)
def _send_cc(self, port_name, cc, value): self._log.debug('Sending CC ({}, {}, {}) to {}'.format( 1, cc, value, port_name)) msg = rtmidi.MidiMessage().controllerEvent(1, cc, value) self._midi[port_name].sendMessage(msg)
def send(self, *args): print("CREATE: %s" % args) m = rtmidi.MidiMessage(*args) self.dev.sendMessage(m)
def stopReceiving(self): self.midiin.closePort() self.running = False receiver = MidiReceiver() receiver.start() # Transmitting time.sleep(0.1) print() print('Now transmitting messages...') 3, # Send messages m = rtmidi.MidiMessage() messages = ( ('isAftertouch', rtmidi.MidiMessage().aftertouchChange(1, 10, 100)), ('isAftertouch', rtmidi.MidiMessage().aftertouchChange(2, 20, 127)), (None, rtmidi.MidiMessage().allControllersOff(1)), (None, rtmidi.MidiMessage().allControllersOff(2)), ('isAllNotesOff', rtmidi.MidiMessage().allNotesOff(1)), ('isAllNotesOff', rtmidi.MidiMessage().allNotesOff(2)), ('isAllSoundOff', rtmidi.MidiMessage().allSoundOff(1)), ('isAllSoundOff', rtmidi.MidiMessage().allSoundOff(2)), ('isChannelPressure', rtmidi.MidiMessage().channelPressureChange(1, 50)), ('isChannelPressure', rtmidi.MidiMessage().channelPressureChange(2, 100)), ('isController', rtmidi.MidiMessage().controllerEvent(1, 10, 100)), ('isController', rtmidi.MidiMessage().controllerEvent(2, 11, 127)), # ('isSysEx', rtmidi.MidiMessage().createSysExMessage( ??? )), (None, rtmidi.MidiMessage().endOfTrack()),
def play_note(self, key): note = self.key_mappings[key] msg_on = rtmidi.MidiMessage().noteOn(self.channel, note, self.velocity) msg_off = rtmidi.MidiMessage().noteOff(self.channel, note) self.out.sendMessage(msg_on) self.out.sendMessage(msg_off)