Collection of tools useful for audio production
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.

718 lines
18KB

  1. /*
  2. * Carla Plugin bridge code
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #ifdef BUILD_BRIDGE_PLUGIN
  18. #include "carla_bridge_client.h"
  19. #include "carla_plugin.h"
  20. #include <QtCore/QDir>
  21. #include <QtCore/QFile>
  22. #include <QtCore/QTimerEvent>
  23. #include <QtGui/QApplication>
  24. #include <QtGui/QDialog>
  25. #include <QtGui/QVBoxLayout>
  26. #ifdef Q_OS_UNIX
  27. #include <signal.h>
  28. #endif
  29. static int qargc = 0;
  30. static char** qargv = nullptr;
  31. bool qcloseNow = false;
  32. #if defined(Q_OS_UNIX)
  33. void closeSignalHandler(int)
  34. {
  35. qcloseNow = true;
  36. }
  37. #elif defined(Q_OS_WIN)
  38. BOOL WINAPI closeSignalHandler(DWORD dwCtrlType)
  39. {
  40. if (dwCtrlType == CTRL_C_EVENT)
  41. {
  42. qcloseNow = true;
  43. return TRUE;
  44. }
  45. return FALSE;
  46. }
  47. #endif
  48. void initSignalHandler()
  49. {
  50. #if defined(Q_OS_UNIX)
  51. struct sigaction sint, sterm;
  52. sint.sa_handler = closeSignalHandler;
  53. sigemptyset(&sint.sa_mask);
  54. sint.sa_flags |= SA_RESTART;
  55. sigaction(SIGINT, &sint, 0);
  56. sterm.sa_handler = closeSignalHandler;
  57. sigemptyset(&sterm.sa_mask);
  58. sterm.sa_flags |= SA_RESTART;
  59. sigaction(SIGTERM, &sterm, 0);
  60. #elif defined(Q_OS_WIN)
  61. SetConsoleCtrlHandler(closeSignalHandler, TRUE);
  62. #endif
  63. }
  64. CARLA_BRIDGE_START_NAMESPACE
  65. // -------------------------------------------------------------------------
  66. // client
  67. class CarlaPluginClient : public CarlaClient
  68. {
  69. public:
  70. CarlaPluginClient(CarlaToolkit* const toolkit)
  71. : CarlaClient(toolkit)
  72. {
  73. engine = nullptr;
  74. plugin = nullptr;
  75. }
  76. ~CarlaPluginClient()
  77. {
  78. }
  79. void setStuff(CarlaBackend::CarlaEngine* const engine_, CarlaBackend::CarlaPlugin* const plugin_)
  80. {
  81. Q_ASSERT(engine_);
  82. Q_ASSERT(plugin_);
  83. engine = engine_;
  84. plugin = plugin_;
  85. }
  86. // ---------------------------------------------------------------------
  87. // processing
  88. void setParameter(const int32_t rindex, const double value)
  89. {
  90. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  91. Q_ASSERT(plugin);
  92. if (! plugin)
  93. return;
  94. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  95. }
  96. void setProgram(const uint32_t index)
  97. {
  98. qDebug("CarlaPluginClient::setProgram(%i)", index);
  99. Q_ASSERT(engine);
  100. Q_ASSERT(plugin);
  101. Q_ASSERT(index < plugin->programCount());
  102. if (! (plugin && engine))
  103. return;
  104. if (index >= plugin->programCount())
  105. return;
  106. plugin->setProgram(index, true, true, false, true);
  107. double value;
  108. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  109. {
  110. value = plugin->getParameterValue(i);
  111. engine->osc_send_bridge_set_parameter_value(i, value);
  112. engine->osc_send_bridge_set_default_value(i, value);
  113. }
  114. }
  115. void setMidiProgram(const uint32_t index)
  116. {
  117. qDebug("CarlaPluginClient::setMidiProgram(%i)", index);
  118. Q_ASSERT(engine);
  119. Q_ASSERT(plugin);
  120. if (! (plugin && engine))
  121. return;
  122. plugin->setMidiProgram(index, true, true, false, true);
  123. double value;
  124. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  125. {
  126. value = plugin->getParameterValue(i);
  127. engine->osc_send_bridge_set_parameter_value(i, value);
  128. engine->osc_send_bridge_set_default_value(i, value);
  129. }
  130. }
  131. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  132. {
  133. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  134. Q_ASSERT(plugin);
  135. Q_ASSERT(velo > 0);
  136. if (! plugin)
  137. return;
  138. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  139. }
  140. void noteOff(const uint8_t channel, const uint8_t note)
  141. {
  142. qDebug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  143. Q_ASSERT(plugin);
  144. if (! plugin)
  145. return;
  146. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  147. }
  148. // ---------------------------------------------------------------------
  149. // plugin
  150. void saveNow()
  151. {
  152. qDebug("CarlaPluginClient::saveNow()");
  153. Q_ASSERT(plugin);
  154. Q_ASSERT(engine);
  155. if (! (plugin && engine))
  156. return;
  157. plugin->prepareForSave();
  158. for (uint32_t i=0; i < plugin->customDataCount(); i++)
  159. {
  160. const CarlaBackend::CustomData* const cdata = plugin->customData(i);
  161. engine->osc_send_bridge_set_custom_data(CarlaBackend::getCustomDataTypeString(cdata->type), cdata->key, cdata->value);
  162. }
  163. if (plugin->hints() & CarlaBackend::PLUGIN_USES_CHUNKS)
  164. {
  165. void* data = nullptr;
  166. int32_t dataSize = plugin->chunkData(&data);
  167. if (data && dataSize >= 4)
  168. {
  169. QString filePath;
  170. filePath = QDir::tempPath();
  171. #ifdef Q_OS_WIN
  172. filePath += "\\.CarlaChunk_";
  173. #else
  174. filePath += "/.CarlaChunk_";
  175. #endif
  176. filePath += plugin->name();
  177. QFile file(filePath);
  178. if (file.open(QIODevice::WriteOnly))
  179. {
  180. QByteArray chunk((const char*)data, dataSize);
  181. file.write(chunk);
  182. file.close();
  183. engine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  184. }
  185. }
  186. }
  187. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  188. }
  189. void setCustomData(const char* const type, const char* const key, const char* const value)
  190. {
  191. qDebug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  192. Q_ASSERT(plugin);
  193. if (! plugin)
  194. return;
  195. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  196. }
  197. void setChunkData(const char* const filePath)
  198. {
  199. qDebug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  200. Q_ASSERT(plugin);
  201. if (! plugin)
  202. return;
  203. Q_UNUSED(filePath);
  204. #if 0
  205. nextChunkFilePath = strdup(filePath);
  206. while (nextChunkFilePath)
  207. carla_msleep(25);
  208. #endif
  209. }
  210. // ---------------------------------------------------------------------
  211. // idle
  212. void idle()
  213. {
  214. Q_ASSERT(plugin);
  215. if (! plugin)
  216. return;
  217. plugin->idleGui();
  218. }
  219. void showGui(const bool yesNo)
  220. {
  221. qDebug("CarlaPluginClient::showGui(%s)", bool2str(yesNo));
  222. Q_ASSERT(plugin);
  223. if (! plugin)
  224. return;
  225. plugin->showGui(yesNo);
  226. }
  227. // ---------------------------------------------------------------------
  228. // callback
  229. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  230. {
  231. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  232. if (! engine)
  233. return;
  234. switch (action)
  235. {
  236. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  237. engine->osc_send_bridge_set_parameter_value(value1, value3);
  238. break;
  239. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  240. engine->osc_send_bridge_set_program(value1);
  241. break;
  242. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  243. engine->osc_send_bridge_set_midi_program(value1);
  244. break;
  245. case CarlaBackend::CALLBACK_NOTE_ON:
  246. {
  247. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  248. //osc_send_midi(mdata);
  249. break;
  250. }
  251. case CarlaBackend::CALLBACK_NOTE_OFF:
  252. {
  253. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  254. //osc_send_midi(mdata);
  255. break;
  256. }
  257. case CarlaBackend::CALLBACK_SHOW_GUI:
  258. if (value1 == 0)
  259. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  260. break;
  261. case CarlaBackend::CALLBACK_RESIZE_GUI:
  262. if (m_toolkit)
  263. m_toolkit->resize(value1, value2);
  264. break;
  265. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  266. //if (CARLA_PLUGIN)
  267. //{
  268. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  269. // {
  270. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  271. // }
  272. //}
  273. break;
  274. case CarlaBackend::CALLBACK_QUIT:
  275. //quequeMessage(MESSAGE_QUIT, 0, 0, 0.0);
  276. break;
  277. default:
  278. break;
  279. }
  280. Q_UNUSED(value3);
  281. }
  282. // ---------------------------------------------------------------------
  283. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  284. {
  285. Q_ASSERT(ptr);
  286. if (! ptr)
  287. return;
  288. CarlaPluginClient* const client = (CarlaPluginClient*)ptr;
  289. client->handleCallback(action, value1, value2, value3);
  290. }
  291. private:
  292. CarlaBackend::CarlaEngine* engine;
  293. CarlaBackend::CarlaPlugin* plugin;
  294. };
  295. // -------------------------------------------------------------------------
  296. // toolkit
  297. class BridgeApplication : public QApplication
  298. {
  299. public:
  300. BridgeApplication()
  301. : QApplication(qargc, qargv)
  302. {
  303. msgTimer = 0;
  304. m_client = nullptr;
  305. }
  306. void exec(CarlaPluginClient* const client)
  307. {
  308. m_client = client;
  309. msgTimer = startTimer(50);
  310. QApplication::exec();
  311. }
  312. void killMsgTimer()
  313. {
  314. Q_ASSERT(msgTimer != 0);
  315. killTimer(msgTimer);
  316. msgTimer = 0;
  317. }
  318. protected:
  319. void timerEvent(QTimerEvent* const event)
  320. {
  321. if (qcloseNow)
  322. return quit();
  323. if (event->timerId() == msgTimer)
  324. {
  325. if (m_client)
  326. {
  327. m_client->idle();
  328. if (! m_client->runMessages())
  329. {
  330. msgTimer = 0;
  331. return;
  332. }
  333. }
  334. }
  335. QApplication::timerEvent(event);
  336. }
  337. private:
  338. int msgTimer;
  339. CarlaPluginClient* m_client;
  340. };
  341. class CarlaToolkitPlugin : public CarlaToolkit
  342. {
  343. public:
  344. CarlaToolkitPlugin()
  345. : CarlaToolkit("carla-bridge-plugin")
  346. {
  347. qDebug("CarlaToolkitPlugin::CarlaToolkitPlugin()");
  348. app = nullptr;
  349. dialog = nullptr;
  350. m_resizable = false;
  351. }
  352. ~CarlaToolkitPlugin()
  353. {
  354. qDebug("CarlaToolkitPlugin::~CarlaToolkitPlugin()");
  355. Q_ASSERT(! app);
  356. }
  357. void init()
  358. {
  359. qDebug("CarlaToolkitPlugin::init()");
  360. Q_ASSERT(! app);
  361. app = new BridgeApplication;
  362. }
  363. void exec(CarlaClient* const client, const bool showGui)
  364. {
  365. qDebug("CarlaToolkitPlugin::exec(%p)", client);
  366. Q_ASSERT(app);
  367. Q_ASSERT(client);
  368. m_client = client;
  369. if (showGui)
  370. {
  371. show();
  372. }
  373. else
  374. {
  375. m_client->sendOscUpdate();
  376. m_client->sendOscBridgeUpdate();
  377. app->setQuitOnLastWindowClosed(false);
  378. }
  379. app->exec((CarlaPluginClient*)client);
  380. }
  381. void quit()
  382. {
  383. qDebug("CarlaToolkitPlugin::quit()");
  384. Q_ASSERT(app);
  385. if (dialog)
  386. {
  387. dialog->close();
  388. delete dialog;
  389. dialog = nullptr;
  390. }
  391. if (app)
  392. {
  393. app->killMsgTimer();
  394. if (! app->closingDown())
  395. app->quit();
  396. delete app;
  397. app = nullptr;
  398. }
  399. }
  400. void show()
  401. {
  402. qDebug("CarlaToolkitPlugin::show()");
  403. if (m_client)
  404. ((CarlaPluginClient*)m_client)->showGui(true);
  405. if (dialog)
  406. dialog->show();
  407. }
  408. void hide()
  409. {
  410. qDebug("CarlaToolkitPlugin::hide()");
  411. if (dialog)
  412. dialog->hide();
  413. if (m_client)
  414. ((CarlaPluginClient*)m_client)->showGui(false);
  415. }
  416. void resize(int width, int height)
  417. {
  418. qDebug("CarlaToolkitPlugin::resize(%i, %i)", width, height);
  419. Q_ASSERT(dialog);
  420. if (! dialog)
  421. return;
  422. if (m_resizable)
  423. dialog->resize(width, height);
  424. else
  425. dialog->setFixedSize(width, height);
  426. }
  427. // ---------------------------------------------------------------------
  428. void createWindow(const char* const pluginName, const bool createLayout, const bool resizable)
  429. {
  430. qDebug("CarlaToolkitPlugin::createWindow(%s, %s, %s)", pluginName, bool2str(createLayout), bool2str(resizable));
  431. Q_ASSERT(pluginName);
  432. m_resizable = resizable;
  433. dialog = new QDialog(nullptr);
  434. resize(10, 10);
  435. if (createLayout)
  436. {
  437. QVBoxLayout* const layout = new QVBoxLayout(dialog);
  438. dialog->setContentsMargins(0, 0, 0, 0);
  439. dialog->setLayout(layout);
  440. }
  441. dialog->setWindowTitle(QString("%1 (GUI)").arg(pluginName));
  442. #ifdef Q_OS_WIN
  443. if (! resizable)
  444. dialog->setWindowFlags(dialog->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  445. #endif
  446. }
  447. QDialog* getWindowHandle() const
  448. {
  449. return dialog;
  450. }
  451. // ---------------------------------------------------------------------
  452. private:
  453. BridgeApplication* app;
  454. QDialog* dialog;
  455. bool m_resizable;
  456. };
  457. CarlaToolkit* CarlaToolkit::createNew(const char* const)
  458. {
  459. return new CarlaToolkitPlugin;
  460. }
  461. // -------------------------------------------------------------------------
  462. CARLA_BRIDGE_END_NAMESPACE
  463. int main(int argc, char* argv[])
  464. {
  465. if (argc != 6)
  466. {
  467. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  468. return 1;
  469. }
  470. qargc = argc;
  471. qargv = argv;
  472. const char* const oscUrl = argv[1];
  473. const char* const stype = argv[2];
  474. const char* const filename = argv[3];
  475. const char* name = argv[4];
  476. const char* const label = argv[5];
  477. const bool useOsc = strcmp(oscUrl, "null");
  478. if (strcmp(name, "(none)") == 0)
  479. name = nullptr;
  480. CarlaBackend::PluginType itype;
  481. if (strcmp(stype, "LADSPA") == 0)
  482. itype = CarlaBackend::PLUGIN_LADSPA;
  483. else if (strcmp(stype, "DSSI") == 0)
  484. itype = CarlaBackend::PLUGIN_DSSI;
  485. else if (strcmp(stype, "LV2") == 0)
  486. itype = CarlaBackend::PLUGIN_LV2;
  487. else if (strcmp(stype, "VST") == 0)
  488. itype = CarlaBackend::PLUGIN_VST;
  489. else
  490. {
  491. itype = CarlaBackend::PLUGIN_NONE;
  492. qWarning("Invalid plugin type '%s'", stype);
  493. return 1;
  494. }
  495. // Init toolkit
  496. CarlaBridge::CarlaToolkitPlugin toolkit;
  497. toolkit.init();
  498. // Init client
  499. CarlaBridge::CarlaPluginClient client(&toolkit);
  500. // Init OSC
  501. if (useOsc && ! client.oscInit(oscUrl))
  502. {
  503. toolkit.quit();
  504. return -1;
  505. }
  506. // Init backend engine
  507. CarlaBackend::CarlaEngineJack engine;
  508. engine.setCallback(client.callback, &client);
  509. // bridge client <-> engine
  510. client.registerOscEngine(&engine);
  511. // Init engine
  512. QString engName = QString("%1 (master)").arg(name ? name : label);
  513. engName.truncate(engine.maxClientNameSize());
  514. if (! engine.init(engName.toUtf8().constData()))
  515. {
  516. qWarning("Bridge engine failed to start, error was:\n%s", CarlaBackend::getLastError());
  517. engine.close();
  518. toolkit.quit();
  519. return 2;
  520. }
  521. /// Init plugin
  522. short id = engine.addPlugin(itype, filename, name, label);
  523. int ret;
  524. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  525. {
  526. CarlaBackend::CarlaPlugin* const plugin = engine.getPlugin(id);
  527. client.setStuff(&engine, plugin);
  528. // create window if needed
  529. bool guiResizable;
  530. CarlaBackend::GuiType guiType;
  531. plugin->getGuiInfo(&guiType, &guiResizable);
  532. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  533. {
  534. toolkit.createWindow(plugin->name(), (guiType == CarlaBackend::GUI_INTERNAL_QT4), guiResizable);
  535. plugin->setGuiData(toolkit.getWindowHandle());
  536. }
  537. if (! useOsc)
  538. plugin->setActive(true, false, false);
  539. ret = 0;
  540. }
  541. else
  542. {
  543. qWarning("Plugin failed to load, error was:\n%s", CarlaBackend::getLastError());
  544. ret = 1;
  545. }
  546. if (ret == 0)
  547. {
  548. initSignalHandler();
  549. toolkit.exec(&client, !useOsc);
  550. }
  551. engine.removeAllPlugins();
  552. engine.close();
  553. if (useOsc)
  554. {
  555. // Close OSC
  556. client.sendOscExiting();
  557. client.oscClose();
  558. // toolkit can't be closed manually, only by host
  559. }
  560. else
  561. {
  562. // Close toolkit
  563. toolkit.quit();
  564. }
  565. return ret;
  566. }
  567. #endif // BUILD_BRIDGE_PLUGIN