Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

carla_control.py 28KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla Backend code (OSC stuff)
  4. # Copyright (C) 2011-2019 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. # ----------------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt5.QtCore import QEventLoop
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Custom)
  22. import ui_carla_osc_connect
  23. from carla_host import *
  24. # ------------------------------------------------------------------------------------------------------------
  25. # Imports (liblo)
  26. from liblo import (
  27. Address,
  28. AddressError,
  29. ServerError,
  30. Server,
  31. make_method,
  32. send as lo_send,
  33. TCP as LO_TCP,
  34. UDP as LO_UDP,
  35. )
  36. from random import random
  37. # ------------------------------------------------------------------------------------------------------------
  38. DEBUG = False
  39. # ----------------------------------------------------------------------------------------------------------------------
  40. # OSC connect Dialog
  41. class ConnectDialog(QDialog):
  42. def __init__(self, parent):
  43. QDialog.__init__(self, parent)
  44. self.ui = ui_carla_osc_connect.Ui_Dialog()
  45. self.ui.setupUi(self)
  46. self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
  47. # Mke PlainTextEdit background the same color as the window background
  48. palette = self.ui.te_reported_hint.palette()
  49. palette.setColor(QPalette.Base, palette.color(QPalette.Background))
  50. self.ui.te_reported_hint.setPalette(palette)
  51. # -------------------------------------------------------------------------------------------------------------
  52. # Load settings
  53. self.loadSettings()
  54. # -------------------------------------------------------------------------------------------------------------
  55. # Set-up connections
  56. self.finished.connect(self.slot_saveSettings)
  57. self.ui.rb_reported_auto.clicked.connect(self.slot_reportedAutoClicked)
  58. self.ui.rb_reported_custom.clicked.connect(self.slot_reportedCustomClicked)
  59. self.ui.le_host.textChanged.connect(self.slot_hostChanged)
  60. self.ui.le_reported_host.textChanged.connect(self.slot_reportedHostChanged)
  61. # -----------------------------------------------------------------------------------------------------------------
  62. def getResult(self):
  63. return (self.ui.le_host.text(),
  64. self.ui.le_reported_host.text() if self.ui.rb_reported_custom.isChecked() else "",
  65. self.ui.sb_tcp_port.value(),
  66. self.ui.sb_udp_port.value())
  67. def checkIfButtonBoxShouldBeEnabled(self, host, reportedHostAutomatic, reportedHost):
  68. enabled = len(host) > 0
  69. if enabled and not reportedHostAutomatic:
  70. enabled = len(reportedHost) > 0
  71. self.ui.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enabled)
  72. def loadSettings(self):
  73. settings = QSettings("falkTX", "CarlaOSCConnect")
  74. if settings.value("ReportedHostAutomatic", True, type=bool):
  75. self.ui.rb_reported_custom.setChecked(False)
  76. self.ui.rb_reported_auto.setChecked(True)
  77. else:
  78. self.ui.rb_reported_auto.setChecked(False)
  79. self.ui.rb_reported_custom.setChecked(True)
  80. self.ui.le_host.setText(settings.value("Host", "127.0.0.1", type=str))
  81. self.ui.le_reported_host.setText(settings.value("ReportedHost", "", type=str))
  82. self.ui.sb_tcp_port.setValue(settings.value("TCPPort", CARLA_DEFAULT_OSC_TCP_PORT_NUMBER, type=int))
  83. self.ui.sb_udp_port.setValue(settings.value("UDPPort", CARLA_DEFAULT_OSC_UDP_PORT_NUMBER, type=int))
  84. self.checkIfButtonBoxShouldBeEnabled(self.ui.le_host.text(),
  85. self.ui.rb_reported_auto.isChecked(),
  86. self.ui.le_reported_host.text())
  87. # ------------------------------------------------------------------------------------------------------------------
  88. @pyqtSlot(bool)
  89. def slot_reportedAutoClicked(self, clicked):
  90. self.checkIfButtonBoxShouldBeEnabled(self.ui.le_host.text(),
  91. clicked,
  92. self.ui.le_reported_host.text())
  93. @pyqtSlot(bool)
  94. def slot_reportedCustomClicked(self, clicked):
  95. self.checkIfButtonBoxShouldBeEnabled(self.ui.le_host.text(),
  96. not clicked,
  97. self.ui.le_reported_host.text())
  98. @pyqtSlot(str)
  99. def slot_hostChanged(self, text):
  100. self.checkIfButtonBoxShouldBeEnabled(text,
  101. self.ui.rb_reported_auto.isChecked(),
  102. self.ui.le_reported_host.text())
  103. @pyqtSlot(str)
  104. def slot_reportedHostChanged(self, text):
  105. self.checkIfButtonBoxShouldBeEnabled(self.ui.le_host.text(),
  106. self.ui.rb_reported_auto.isChecked(),
  107. text)
  108. @pyqtSlot()
  109. def slot_saveSettings(self):
  110. settings = QSettings("falkTX", "CarlaOSCConnect")
  111. settings.setValue("Host", self.ui.le_host.text())
  112. settings.setValue("ReportedHost", self.ui.le_reported_host.text())
  113. settings.setValue("TCPPort", self.ui.sb_tcp_port.value())
  114. settings.setValue("UDPPort", self.ui.sb_udp_port.value())
  115. settings.setValue("ReportedHostAutomatic", self.ui.rb_reported_auto.isChecked())
  116. # ------------------------------------------------------------------------------------------------------------------
  117. def done(self, r):
  118. QDialog.done(self, r)
  119. self.close()
  120. # ------------------------------------------------------------------------------------------------------------
  121. # Host OSC object
  122. class CarlaHostOSC(CarlaHostQtPlugin):
  123. def __init__(self):
  124. CarlaHostQtPlugin.__init__(self)
  125. self.lo_server_tcp = None
  126. self.lo_server_udp = None
  127. self.lo_target_tcp = None
  128. self.lo_target_udp = None
  129. self.lo_target_tcp_name = ""
  130. self.lo_target_udp_name = ""
  131. self.resetPendingMessages()
  132. # -------------------------------------------------------------------
  133. def resetPendingMessages(self):
  134. self.lastMessageId = 1
  135. self.pendingMessages = []
  136. self.responses = {}
  137. def printAndReturnError(self, error):
  138. print(error)
  139. self.fLastError = error
  140. return False
  141. def sendMsg(self, lines):
  142. if len(lines) < 1:
  143. return self.printAndReturnError("not enough arguments")
  144. method = lines.pop(0)
  145. if method == "set_engine_option":
  146. return True
  147. if self.lo_target_tcp is None:
  148. return self.printAndReturnError("lo_target_tcp is None")
  149. if self.lo_target_tcp_name is None:
  150. return self.printAndReturnError("lo_target_tcp_name is None")
  151. if method in ("clear_engine_xruns",
  152. "cancel_engine_action",
  153. #"load_file",
  154. #"load_project",
  155. #"save_project",
  156. #"clear_project_filename",
  157. "patchbay_connect",
  158. "patchbay_disconnect",
  159. "patchbay_refresh",
  160. "transport_play",
  161. "transport_pause",
  162. "transport_bpm",
  163. "transport_relocate",
  164. "add_plugin",
  165. "remove_plugin",
  166. "remove_all_plugins",
  167. "rename_plugin",
  168. "clone_plugin",
  169. "replace_plugin",
  170. "switch_plugins",
  171. #"load_plugin_state",
  172. #"save_plugin_state",
  173. ):
  174. path = "/ctrl/" + method
  175. needResp = True
  176. elif method in (#"set_option",
  177. "set_active",
  178. "set_drywet",
  179. "set_volume",
  180. "set_balance_left",
  181. "set_balance_right",
  182. "set_panning",
  183. #"set_ctrl_channel",
  184. "set_parameter_value",
  185. "set_parameter_midi_channel",
  186. "set_parameter_midi_cc",
  187. "set_program",
  188. "set_midi_program",
  189. #"set_custom_data",
  190. #"set_chunk_data",
  191. #"prepare_for_save",
  192. #"reset_parameters",
  193. #"randomize_parameters",
  194. ):
  195. pluginId = lines.pop(0)
  196. needResp = False
  197. path = "/%s/%i/%s" % (self.lo_target_tcp_name, pluginId, method)
  198. elif method == "send_midi_note":
  199. pluginId = lines.pop(0)
  200. needResp = False
  201. channel, note, velocity = lines
  202. if velocity:
  203. path = "/%s/%i/note_on" % (self.lo_target_tcp_name, pluginId)
  204. else:
  205. path = "/%s/%i/note_off" % (self.lo_target_tcp_name, pluginId)
  206. lines.pop(2)
  207. else:
  208. return self.printAndReturnError("invalid method '%s'" % method)
  209. if len(self.pendingMessages) != 0:
  210. return self.printAndReturnError("A previous operation is still pending, please wait")
  211. args = [int(line) if isinstance(line, bool) else line for line in lines]
  212. #print(path, args)
  213. if not needResp:
  214. lo_send(self.lo_target_tcp, path, *args)
  215. return True
  216. messageId = self.lastMessageId
  217. self.lastMessageId += 1
  218. self.pendingMessages.append(messageId)
  219. lo_send(self.lo_target_tcp, path, messageId, *args)
  220. while messageId in self.pendingMessages:
  221. QApplication.processEvents(QEventLoop.AllEvents, 100)
  222. error = self.responses.pop(messageId)
  223. if not error:
  224. return True
  225. self.fLastError = error
  226. return False
  227. def sendMsgAndSetError(self, lines):
  228. return self.sendMsg(lines)
  229. # -------------------------------------------------------------------
  230. def engine_init(self, driverName, clientName):
  231. return self.lo_target_tcp is not None
  232. def engine_close(self):
  233. return True
  234. def engine_idle(self):
  235. return
  236. def is_engine_running(self):
  237. return self.lo_target_tcp is not None
  238. def set_engine_about_to_close(self):
  239. return
  240. # ---------------------------------------------------------------------------------------------------------------------
  241. # OSC Control server
  242. class CarlaControlServerTCP(Server):
  243. def __init__(self, host, rhost):
  244. Server.__init__(self, proto=LO_TCP)
  245. if False:
  246. host = CarlaHostOSC()
  247. self.host = host
  248. self.rhost = rhost
  249. def idle(self):
  250. self.fReceivedMsgs = False
  251. while self.recv(0) and self.fReceivedMsgs:
  252. pass
  253. def getFullURL(self):
  254. if self.rhost:
  255. return "osc.tcp://%s:%i/ctrl" % (self.rhost, self.get_port())
  256. return "%sctrl" % self.get_url()
  257. @make_method('/ctrl/cb', 'iiiiifs')
  258. def carla_cb(self, path, args):
  259. if DEBUG: print(path, args)
  260. self.fReceivedMsgs = True
  261. action, pluginId, value1, value2, value3, valuef, valueStr = args
  262. self.host._setViaCallback(action, pluginId, value1, value2, value3, valuef, valueStr)
  263. engineCallback(self.host, action, pluginId, value1, value2, value3, valuef, valueStr)
  264. @make_method('/ctrl/info', 'iiiihiisssssss')
  265. def carla_info(self, path, args):
  266. if DEBUG: print(path, args)
  267. self.fReceivedMsgs = True
  268. (
  269. pluginId, type_, category, hints, uniqueId, optsAvail, optsEnabled,
  270. name, filename, iconName, realName, label, maker, copyright,
  271. ) = args
  272. hints &= ~PLUGIN_HAS_CUSTOM_UI
  273. pinfo = {
  274. 'type': type_,
  275. 'category': category,
  276. 'hints': hints,
  277. 'optionsAvailable': optsAvail,
  278. 'optionsEnabled': optsEnabled,
  279. 'uniqueId': uniqueId,
  280. 'filename': filename,
  281. 'name': name,
  282. 'label': label,
  283. 'maker': maker,
  284. 'copyright': copyright,
  285. 'iconName': iconName
  286. }
  287. self.host._set_pluginInfoUpdate(pluginId, pinfo)
  288. self.host._set_pluginRealName(pluginId, realName)
  289. @make_method('/ctrl/ports', 'iiiiiiii')
  290. def carla_ports(self, path, args):
  291. if DEBUG: print(path, args)
  292. self.fReceivedMsgs = True
  293. pluginId, audioIns, audioOuts, midiIns, midiOuts, paramIns, paramOuts, paramTotal = args
  294. self.host._set_audioCountInfo(pluginId, {'ins': audioIns, 'outs': audioOuts})
  295. self.host._set_midiCountInfo(pluginId, {'ins': midiOuts, 'outs': midiOuts})
  296. self.host._set_parameterCountInfo(pluginId, paramTotal, {'ins': paramIns, 'outs': paramOuts})
  297. @make_method('/ctrl/param', 'iiiiiissfffffff')
  298. def carla_param(self, path, args):
  299. if DEBUG: print(path, args)
  300. self.fReceivedMsgs = True
  301. (
  302. pluginId, paramId, type_, hints, midiChan, midiCC, name, unit,
  303. def_, min_, max_, step, stepSmall, stepLarge, value
  304. ) = args
  305. hints &= ~(PARAMETER_USES_SCALEPOINTS | PARAMETER_USES_CUSTOM_TEXT)
  306. paramInfo = {
  307. 'name': name,
  308. 'symbol': "",
  309. 'unit': unit,
  310. 'scalePointCount': 0,
  311. }
  312. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  313. paramData = {
  314. 'type': type_,
  315. 'hints': hints,
  316. 'index': paramId,
  317. 'rindex': -1,
  318. 'midiCC': midiCC,
  319. 'midiChannel': midiChan,
  320. }
  321. self.host._set_parameterData(pluginId, paramId, paramData)
  322. paramRanges = {
  323. 'def': def_,
  324. 'min': min_,
  325. 'max': max_,
  326. 'step': step,
  327. 'stepSmall': stepSmall,
  328. 'stepLarge': stepLarge,
  329. }
  330. self.host._set_parameterRangesUpdate(pluginId, paramId, paramRanges)
  331. self.host._set_parameterValue(pluginId, paramId, value)
  332. @make_method('/ctrl/count', 'iiiiii')
  333. def carla_count(self, path, args):
  334. if DEBUG: print(path, args)
  335. self.fReceivedMsgs = True
  336. pluginId, pcount, mpcount, cdcount, cp, cmp = args
  337. self.host._set_programCount(pluginId, pcount)
  338. self.host._set_midiProgramCount(pluginId, mpcount)
  339. self.host._set_customDataCount(pluginId, cdcount)
  340. self.host._set_pluginInfoUpdate(pluginId, { 'programCurrent': cp, 'midiProgramCurrent': cmp })
  341. @make_method('/ctrl/pcount', 'iii')
  342. def carla_pcount(self, path, args):
  343. if DEBUG: print(path, args)
  344. self.fReceivedMsgs = True
  345. pluginId, pcount, mpcount = args
  346. self.host._set_programCount(pluginId, pcount)
  347. self.host._set_midiProgramCount(pluginId, mpcount)
  348. @make_method('/ctrl/prog', 'iis')
  349. def carla_prog(self, path, args):
  350. if DEBUG: print(path, args)
  351. self.fReceivedMsgs = True
  352. pluginId, progId, progName = args
  353. self.host._set_programName(pluginId, progId, progName)
  354. @make_method('/ctrl/mprog', 'iiiis')
  355. def carla_mprog(self, path, args):
  356. if DEBUG: print(path, args)
  357. self.fReceivedMsgs = True
  358. pluginId, midiProgId, bank, program, name = args
  359. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  360. @make_method('/ctrl/cdata', 'iisss')
  361. def carla_cdata(self, path, args):
  362. if DEBUG: print(path, args)
  363. self.fReceivedMsgs = True
  364. pluginId, index, type_, key, value = args
  365. self.host._set_customData(pluginId, index, { 'type': type_, 'key': key, 'value': value })
  366. @make_method('/ctrl/iparams', 'ifffffff')
  367. def carla_iparams(self, path, args):
  368. if DEBUG: print(path, args)
  369. self.fReceivedMsgs = True
  370. pluginId, active, drywet, volume, balLeft, balRight, pan, ctrlChan = args
  371. self.host._set_internalValue(pluginId, PARAMETER_ACTIVE, active)
  372. self.host._set_internalValue(pluginId, PARAMETER_DRYWET, drywet)
  373. self.host._set_internalValue(pluginId, PARAMETER_VOLUME, volume)
  374. self.host._set_internalValue(pluginId, PARAMETER_BALANCE_LEFT, balLeft)
  375. self.host._set_internalValue(pluginId, PARAMETER_BALANCE_RIGHT, balRight)
  376. self.host._set_internalValue(pluginId, PARAMETER_PANNING, pan)
  377. self.host._set_internalValue(pluginId, PARAMETER_CTRL_CHANNEL, ctrlChan)
  378. @make_method('/ctrl/resp', 'is')
  379. def carla_resp(self, path, args):
  380. if DEBUG: print(path, args)
  381. self.fReceivedMsgs = True
  382. messageId, error = args
  383. self.host.responses[messageId] = error
  384. self.host.pendingMessages.remove(messageId)
  385. @make_method('/ctrl/exit', '')
  386. def carla_exit(self, path, args):
  387. if DEBUG: print(path, args)
  388. self.fReceivedMsgs = True
  389. #self.host.lo_target_tcp = None
  390. self.host.QuitCallback.emit()
  391. @make_method('/ctrl/exit-error', 's')
  392. def carla_exit_error(self, path, args):
  393. if DEBUG: print(path, args)
  394. self.fReceivedMsgs = True
  395. error, = args
  396. self.host.lo_target_tcp = None
  397. self.host.QuitCallback.emit()
  398. self.host.ErrorCallback.emit(error)
  399. @make_method(None, None)
  400. def fallback(self, path, args):
  401. print("ControlServerTCP::fallback(\"%s\") - unknown message, args =" % path, args)
  402. self.fReceivedMsgs = True
  403. # ---------------------------------------------------------------------------------------------------------------------
  404. class CarlaControlServerUDP(Server):
  405. def __init__(self, host, rhost):
  406. Server.__init__(self, proto=LO_UDP)
  407. if False:
  408. host = CarlaHostOSC()
  409. self.host = host
  410. self.rhost = rhost
  411. def idle(self):
  412. self.fReceivedMsgs = False
  413. while self.recv(0) and self.fReceivedMsgs:
  414. pass
  415. def getFullURL(self):
  416. if self.rhost:
  417. return "osc.udp://%s:%i/ctrl" % (self.rhost, self.get_port())
  418. return "%sctrl" % self.get_url()
  419. @make_method('/ctrl/runtime', 'fiihiiif')
  420. def carla_runtime(self, path, args):
  421. self.fReceivedMsgs = True
  422. load, xruns, playing, frame, bar, beat, tick, bpm = args
  423. self.host._set_runtime_info(load, xruns)
  424. self.host._set_transport(bool(playing), frame, bar, beat, tick, bpm)
  425. @make_method('/ctrl/param', 'iif')
  426. def carla_param_fixme(self, path, args):
  427. self.fReceivedMsgs = True
  428. pluginId, paramId, paramValue = args
  429. self.host._set_parameterValue(pluginId, paramId, paramValue)
  430. @make_method('/ctrl/peaks', 'iffff')
  431. def carla_peaks(self, path, args):
  432. self.fReceivedMsgs = True
  433. pluginId, in1, in2, out1, out2 = args
  434. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  435. @make_method(None, None)
  436. def fallback(self, path, args):
  437. print("ControlServerUDP::fallback(\"%s\") - unknown message, args =" % path, args)
  438. self.fReceivedMsgs = True
  439. # ---------------------------------------------------------------------------------------------------------------------
  440. # Main Window
  441. class HostWindowOSC(HostWindow):
  442. def __init__(self, host, oscAddr):
  443. HostWindow.__init__(self, host, True)
  444. self.host = host
  445. if False:
  446. # kdevelop likes this :)
  447. host = CarlaHostOSC()
  448. self.host = host
  449. # ----------------------------------------------------------------------------------------------------
  450. # Connect actions to functions
  451. self.ui.act_file_connect.triggered.connect(self.slot_fileConnect)
  452. self.ui.act_file_refresh.triggered.connect(self.slot_fileRefresh)
  453. # ----------------------------------------------------------------------------------------------------
  454. # Final setup
  455. if oscAddr:
  456. QTimer.singleShot(0, self.connectOsc)
  457. def connectOsc(self, addrTCP = None, addrUDP = None, rhost = None):
  458. if addrTCP is not None:
  459. self.fOscAddressTCP = addrTCP
  460. if addrUDP is not None:
  461. self.fOscAddressUDP = addrUDP
  462. if rhost is not None:
  463. self.fOscReportedHost = rhost
  464. lo_target_tcp_name = self.fOscAddressTCP.rsplit("/", 1)[-1]
  465. lo_target_udp_name = self.fOscAddressUDP.rsplit("/", 1)[-1]
  466. err = None
  467. try:
  468. lo_target_tcp = Address(self.fOscAddressTCP)
  469. lo_server_tcp = CarlaControlServerTCP(self.host, self.fOscReportedHost)
  470. lo_send(lo_target_tcp, "/register", lo_server_tcp.getFullURL())
  471. lo_target_udp = Address(self.fOscAddressUDP)
  472. lo_server_udp = CarlaControlServerUDP(self.host, self.fOscReportedHost)
  473. lo_send(lo_target_udp, "/register", lo_server_udp.getFullURL())
  474. except AddressError as e:
  475. err = e
  476. except OSError as e:
  477. err = e
  478. except:
  479. err = Exception()
  480. if err is not None:
  481. fullError = self.tr("Failed to connect to the Carla instance.")
  482. if len(err.args) > 0:
  483. fullError += " %s\n%s\n" % (self.tr("Error was:"), err.args[0])
  484. fullError += "\n"
  485. fullError += self.tr("Make sure the remote Carla is running and the URL and Port are correct.") + "\n"
  486. fullError += self.tr("If it still does not work, check your current device and the remote's firewall.")
  487. CustomMessageBox(self,
  488. QMessageBox.Warning,
  489. self.tr("Error"),
  490. self.tr("Connection failed"),
  491. fullError,
  492. QMessageBox.Ok,
  493. QMessageBox.Ok)
  494. return
  495. self.host.lo_server_tcp = lo_server_tcp
  496. self.host.lo_target_tcp = lo_target_tcp
  497. self.host.lo_target_tcp_name = lo_target_tcp_name
  498. self.host.lo_server_udp = lo_server_udp
  499. self.host.lo_target_udp = lo_target_udp
  500. self.host.lo_target_udp_name = lo_target_udp_name
  501. self.ui.act_file_refresh.setEnabled(True)
  502. self.startTimers()
  503. def disconnectOsc(self):
  504. self.killTimers()
  505. self.unregister()
  506. self.removeAllPlugins()
  507. patchcanvas.clear()
  508. self.ui.act_file_refresh.setEnabled(False)
  509. # --------------------------------------------------------------------------------------------------------
  510. def unregister(self):
  511. if self.host.lo_server_tcp is not None:
  512. if self.host.lo_target_tcp is not None:
  513. try:
  514. lo_send(self.host.lo_target_tcp, "/unregister", self.host.lo_server_tcp.getFullURL())
  515. except:
  516. pass
  517. self.host.lo_target_tcp = None
  518. while self.host.lo_server_tcp.recv(0):
  519. pass
  520. #self.host.lo_server_tcp.free()
  521. self.host.lo_server_tcp = None
  522. if self.host.lo_server_udp is not None:
  523. if self.host.lo_target_udp is not None:
  524. try:
  525. lo_send(self.host.lo_target_udp, "/unregister", self.host.lo_server_udp.getFullURL())
  526. except:
  527. pass
  528. self.host.lo_target_udp = None
  529. while self.host.lo_server_udp.recv(0):
  530. pass
  531. #self.host.lo_server_udp.free()
  532. self.host.lo_server_udp = None
  533. self.host.lo_target_tcp_name = ""
  534. self.host.lo_target_udp_name = ""
  535. # --------------------------------------------------------------------------------------------------------
  536. # Timers
  537. def idleFast(self):
  538. HostWindow.idleFast(self)
  539. if self.host.lo_server_tcp is not None:
  540. self.host.lo_server_tcp.idle()
  541. else:
  542. self.disconnectOsc()
  543. if self.host.lo_server_udp is not None:
  544. self.host.lo_server_udp.idle()
  545. else:
  546. self.disconnectOsc()
  547. # --------------------------------------------------------------------------------------------------------
  548. def removeAllPlugins(self):
  549. self.host.fPluginsInfo = {}
  550. HostWindow.removeAllPlugins(self)
  551. # --------------------------------------------------------------------------------------------------------
  552. def loadSettings(self, firstTime):
  553. settings = HostWindow.loadSettings(self, firstTime)
  554. self.fOscAddressTCP = settings.value("RemoteAddressTCP", "osc.tcp://127.0.0.1:22752/Carla", type=str)
  555. self.fOscAddressUDP = settings.value("RemoteAddressUDP", "osc.udp://127.0.0.1:22752/Carla", type=str)
  556. self.fOscReportedHost = settings.value("RemoteReportedHost", "", type=str)
  557. def saveSettings(self):
  558. settings = HostWindow.saveSettings(self)
  559. if self.fOscAddressTCP:
  560. settings.setValue("RemoteAddressTCP", self.fOscAddressTCP)
  561. if self.fOscAddressUDP:
  562. settings.setValue("RemoteAddressUDP", self.fOscAddressUDP)
  563. if self.fOscReportedHost:
  564. settings.setValue("RemoteReportedHost", self.fOscReportedHost)
  565. # --------------------------------------------------------------------------------------------------------
  566. @pyqtSlot()
  567. def slot_fileConnect(self):
  568. dialog = ConnectDialog(self)
  569. if not dialog.exec_():
  570. return
  571. host, rhost, tcpPort, udpPort = dialog.getResult()
  572. self.disconnectOsc()
  573. self.connectOsc("osc.tcp://%s:%i/Carla" % (host, tcpPort),
  574. "osc.udp://%s:%i/Carla" % (host, udpPort),
  575. rhost)
  576. @pyqtSlot()
  577. def slot_fileRefresh(self):
  578. if None in (self.host.lo_server_tcp, self.host.lo_server_udp, self.host.lo_target_tcp, self.host.lo_target_udp):
  579. return
  580. lo_send(self.host.lo_target_udp, "/unregister", self.host.lo_server_udp.getFullURL())
  581. while self.host.lo_server_udp.recv(0):
  582. pass
  583. #self.host.lo_server_udp.free()
  584. lo_send(self.host.lo_target_tcp, "/unregister", self.host.lo_server_tcp.getFullURL())
  585. while self.host.lo_server_tcp.recv(0):
  586. pass
  587. #self.host.lo_server_tcp.free()
  588. self.removeAllPlugins()
  589. patchcanvas.clear()
  590. self.host.lo_server_tcp = CarlaControlServerTCP(self.host, self.fOscReportedHost)
  591. self.host.lo_server_udp = CarlaControlServerUDP(self.host, self.fOscReportedHost)
  592. try:
  593. lo_send(self.host.lo_target_tcp, "/register", self.host.lo_server_tcp.getFullURL())
  594. except:
  595. self.disconnectOsc()
  596. return
  597. try:
  598. lo_send(self.host.lo_target_udp, "/register", self.host.lo_server_udp.getFullURL())
  599. except:
  600. self.disconnectOsc()
  601. return
  602. # --------------------------------------------------------------------------------------------------------
  603. @pyqtSlot()
  604. def slot_handleSIGTERM(self):
  605. print("Got SIGTERM -> Closing now")
  606. self.host.pendingMessages = []
  607. self.close()
  608. @pyqtSlot()
  609. def slot_handleQuitCallback(self):
  610. self.disconnectOsc()
  611. HostWindow.slot_handleQuitCallback(self)
  612. # --------------------------------------------------------------------------------------------------------
  613. def closeEvent(self, event):
  614. self.killTimers()
  615. self.unregister()
  616. HostWindow.closeEvent(self, event)
  617. # ------------------------------------------------------------------------------------------------------------