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.

691 lines
17KB

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