Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_instrument_order(i):
return (i.font_index, i.program, i.bank_index)
self.instruments.sort(key=get_instrument_order)
# And now, restart fluidsynth but for actual synth use
logging.debug("Starting fluidsynth as a synthesizer...")
self.fluidsynth = subprocess.Popen(
popen_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
self.fluidsynth.stdin.write(msg.encode("ascii"))
self.fluidsynth.stdin.flush()
# Find the MIDI port created by fluidsynth and open it
logging.debug("Waiting for fluidsynth MIDI port to show up...")
deadline = time.time() + 5
while time.time() < deadline:
port_names = [p for p in mido.get_output_names() if "griode" in p]
if port_names == []:
time.sleep(0.1)
continue
if len(port_names) > 1:
logging.warning("Found more than one port for griode")
self.synth_port = mido.open_output(port_names[0])
logging.info("Connected to MIDI output {}"
.format(port_names[0]))
break
else:
logging.error("Failed to locate the fluidsynth port!")
exit(1)
self._devicename = device["devicename"]
self._port_in = 0
self._port_out = 0
try:
self.log.debug("Attempting to open midi port `%s`" % self._devicename)
# a device can be input, output or ioport. in the latter case it can also be the other two
# so we first check if we can use it as an ioport
if self._devicename in mido.get_ioport_names():
self._port_in = mido.open_ioport(name=self._devicename, callback=self.callback, autoreset=True)
self._port_out = self._port_in
# otherwise we try to use it separately as input and output
else:
if self._devicename in mido.get_input_names():
self._port_in = mido.open_input(name=self._devicename, callback=self.callback)
if self._devicename in mido.get_output_names():
self._port_out = mido.open_output(name=self._devicename, callback=self.callback, autoreset=True)
except:
self.log.critical("\nCould not open device `%s`" % self._devicename)
self.log.critical("The midi device might be used by another application/not plugged in/have a different name.")
self.log.critical("Please close the device in the other application/plug it in/select the rename option in the device management menu and restart this script.")
self.log.critical("Currently connected devices:")
for name in mido.get_input_names():
self.log.critical(" - %s" % name)
# EIO 5 (Input/output error)
exit(5)
slap = patch.getint('button', 'slap', multiple=True) # slap button
midichannel = patch.getint('midi', 'channel', default=None)
model = patch.getstring('midi', 'model', default='mk1')
# the scale and offset are used to map MIDI values to Redis values
scale_note = patch.getfloat('scale', 'note', default=1./127)
scale_control = patch.getfloat('scale', 'control', default=1./127)
offset_note = patch.getfloat('offset', 'note', default=0)
offset_control = patch.getfloat('offset', 'control', default=0)
# this is only for debugging, check which MIDI devices are accessible
monitor.info('------ INPUT ------')
for port in mido.get_input_names():
monitor.info(port)
monitor.info('------ OUTPUT ------')
for port in mido.get_output_names():
monitor.info(port)
monitor.info('-------------------------')
# on windows the input and output are different, on unix they are the same
# use "input/output" when specified, or otherwise use "device" for both
try:
mididevice_input = patch.getstring('midi', 'input')
mididevice_input = EEGsynth.trimquotes(mididevice_input)
except:
mididevice_input = patch.getstring('midi', 'device') # fallback
mididevice_input = EEGsynth.trimquotes(mididevice_input)
try:
mididevice_output = patch.getstring('midi', 'output')
mididevice_output = EEGsynth.trimquotes(mididevice_output)
except:
mididevice_output = patch.getstring('midi', 'device') # fallback
scale_pitch = patch.getfloat('scale', 'pitch', default=127)
scale_duration = patch.getfloat('scale', 'duration', default=2.0)
offset_velocity = patch.getfloat('offset', 'velocity', default=0)
offset_pitch = patch.getfloat('offset', 'pitch', default=0)
offset_duration = patch.getfloat('offset', 'duration', default=0)
# the output scale and offset are used to map MIDI values to Redis values
output_scale = patch.getfloat('output', 'scale', default=1. / 127)
output_offset = patch.getfloat('output', 'offset', default=0)
# this is only for debugging, check which MIDI devices are accessible
monitor.info('------ INPUT ------')
for port in mido.get_input_names():
monitor.info(port)
monitor.info('------ OUTPUT ------')
for port in mido.get_output_names():
monitor.info(port)
monitor.info('-------------------------')
try:
inputport = mido.open_input(mididevice)
monitor.success('Connected to MIDI input')
except:
raise RuntimeError("cannot connect to MIDI input")
try:
outputport = mido.open_output(mididevice)
monitor.success('Connected to MIDI output')
except:
raise RuntimeError("cannot connect to MIDI output")
# channel 1-16 in the ini file should be mapped to 0-15
"""
Try out the new printer port backend.
It also works with MIDO_BACKEND, so you can do:
$ MIDO_BACKEND=printer python
>>> import mido
>>> mido.get_output_names()
['The Printer Port']
"""
import mido
mido.set_backend('printer')
print('Available outputs: {!r}'.format(mido.get_output_names()))
with mido.open_output() as port:
print('Using {}.'.format(port))
port.send(mido.Message('program_change', program=10))
for i in [1, 2, 3]:
port.send(mido.Message('control_change', control=1, value=i))
def __init__(self):
self.Window = None
self.TextElem = None
# use to get the list of midi ports
self.PortList = mido.get_output_names()
# reverse the list so the last one is first
self.PortList = self.PortList[::-1]
def open_outport(self):
# TODO autoroute midi port to virtual synth possible??
avail_out_ports = mido.get_output_names()
ports_dict = {i: avail_out_ports[i] for i in range(len(avail_out_ports))}
port = None
for i in range(len(avail_out_ports)):
if "Synth input" in ports_dict[i]: # Better way than looking for this string?
port = ports_dict[i]
if port:
self.out_port = mido.open_output(port)
print("Found FLUID Synth and autoconnected!")
else:
self.out_port = mido.open_output("Robot port", virtual=True)
print("Could not find FLUID Synth, created virtual midi port called 'Robot port'")
def getDeviceNames(self):
inputNames = mido.get_input_names()
outputNames = mido.get_output_names()
deviceNames = []
for inputName in inputNames:
cleanName = inputName[:inputName.rfind(' ')]
outputName = next((oName for oName in outputNames if oName.startswith(cleanName)), None)
deviceNames.append((cleanName, inputName, outputName))
return deviceNames
# the list of MIDI commands is the only aspect that is specific to the Volca Keys
# see http://media.aadl.org/files/catalog_guides/1444140_chart.pdf
control_name = ['portamento', 'expression', 'voice', 'octave', 'detune', 'vco_eg_int', 'vcf_cutoff', 'vcf_eg_int', 'lfo_rate',
'lfo_pitch_int', 'lfo_cutoff_int', 'eg_attack', 'eg_decay_release', 'eg_sustain', 'delay_time', 'delay_feedback']
control_code = [5, 11, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53]
note_name = ['C0', 'Db0', 'D0', 'Eb0', 'E0', 'F0', 'Gb0', 'G0', 'Ab0', 'A0', 'Bb0', 'B0', 'C1', 'Db1', 'D1', 'Eb1', 'E1', 'F1', 'Gb1', 'G1', 'Ab1', 'A1', 'Bb1', 'B1', 'C2', 'Db2', 'D2', 'Eb2', 'E2', 'F2', 'Gb2', 'G2', 'Ab2', 'A2', 'Bb2', 'B2', 'C3', 'Db3', 'D3', 'Eb3', 'E3', 'F3', 'Gb3', 'G3', 'Ab3', 'A3', 'Bb3', 'B3', 'C4', 'Db4', 'D4', 'Eb4', 'E4', 'F4', 'Gb4', 'G4', 'Ab4', 'A4', 'Bb4', 'B4', 'C5', 'Db5', 'D5', 'Eb5', 'E5', 'F5',
'Gb5', 'G5', 'Ab5', 'A5', 'Bb5', 'B5', 'C6', 'Db6', 'D6', 'Eb6', 'E6', 'F6', 'Gb6', 'G6', 'Ab6', 'A6', 'Bb6', 'B6', 'C7', 'Db7', 'D7', 'Eb7', 'E7', 'F7', 'Gb7', 'G7', 'Ab7', 'A7', 'Bb7', 'B7', 'C8', 'Db8', 'D8', 'Eb8', 'E8', 'F8', 'Gb8', 'G8', 'Ab8', 'A8', 'Bb8', 'B8', 'C9', 'Db9', 'D9', 'Eb9', 'E9', 'F9', 'Gb9', 'G9', 'Ab9', 'A9', 'Bb9', 'B9', 'C10', 'Db10', 'D10', 'Eb10', 'E10', 'F10', 'Gb10', 'G10', 'Ab10', 'A10', 'Bb10', 'B10']
note_code = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143]
# get the options from the configuration file
debug = patch.getint('general', 'debug')
# this is only for debugging, check which MIDI devices are accessible
monitor.info('------ OUTPUT ------')
for port in mido.get_output_names():
monitor.info(port)
monitor.info('-------------------------')
midichannel = patch.getint('midi', 'channel') - 1 # channel 1-16 get mapped to 0-15
mididevice = patch.getstring('midi', 'device')
mididevice = EEGsynth.trimquotes(mididevice)
mididevice = process.extractOne(mididevice, mido.get_output_names())[0] # select the closest match
try:
outputport = mido.open_output(mididevice)
monitor.success('Connected to MIDI output')
except:
raise RuntimeError("cannot connect to MIDI output")
# the scale and offset are used to map Redis values to MIDI values
scale = patch.getfloat('input', 'scale', default=127)