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.

649 lines
16KB

  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. engine = engine_;
  81. plugin = plugin_;
  82. }
  83. // ---------------------------------------------------------------------
  84. // processing
  85. void setParameter(const int32_t rindex, const double value)
  86. {
  87. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  88. Q_ASSERT(plugin);
  89. if (! plugin)
  90. return;
  91. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  92. }
  93. void setProgram(const uint32_t index)
  94. {
  95. qDebug("CarlaPluginClient::setProgram(%i)", index);
  96. Q_ASSERT(plugin && index < plugin->programCount());
  97. if (! plugin)
  98. return;
  99. if (index >= plugin->programCount())
  100. return;
  101. plugin->setProgram(index, true, true, false, true);
  102. }
  103. void setMidiProgram(const uint32_t bank, const uint32_t program)
  104. {
  105. qDebug("CarlaPluginClient::setMidiProgram(%i, %i)", bank, program);
  106. Q_ASSERT(plugin);
  107. if (! plugin)
  108. return;
  109. plugin->setMidiProgramById(bank, program, true, true, false, true);
  110. }
  111. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  112. {
  113. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  114. Q_ASSERT(plugin);
  115. Q_ASSERT(velo > 0);
  116. if (! plugin)
  117. return;
  118. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  119. }
  120. void noteOff(const uint8_t channel, const uint8_t note)
  121. {
  122. qDebug("CarlaPluginClient::noteOn(%i, %i)", channel, note);
  123. Q_ASSERT(plugin);
  124. if (! plugin)
  125. return;
  126. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  127. }
  128. // ---------------------------------------------------------------------
  129. // plugin
  130. void saveNow()
  131. {
  132. Q_ASSERT(plugin);
  133. if (! plugin)
  134. return;
  135. plugin->prepareForSave();
  136. #if 0
  137. for (uint32_t i=0; i < CARLA_PLUGIN->customDataCount(); i++)
  138. {
  139. const CustomData* const cdata = CARLA_PLUGIN->customData(i);
  140. osc_send_bridge_custom_data(customdatatype2str(cdata->type), cdata->key, cdata->value);
  141. }
  142. if (CARLA_PLUGIN->hints() & PLUGIN_USES_CHUNKS)
  143. {
  144. void* data = nullptr;
  145. int32_t dataSize = CARLA_PLUGIN->chunkData(&data);
  146. if (data && dataSize >= 4)
  147. {
  148. QString filePath;
  149. filePath += "/tmp/.CarlaChunk_";
  150. filePath += CARLA_PLUGIN->name();
  151. QFile file(filePath);
  152. if (file.open(QIODevice::WriteOnly))
  153. {
  154. QByteArray chunk((const char*)data, dataSize);
  155. file.write(chunk);
  156. file.close();
  157. osc_send_bridge_chunk_data(filePath.toUtf8().constData());
  158. }
  159. }
  160. }
  161. osc_send_configure(CARLA_BRIDGE_MSG_SAVED, "");
  162. #endif
  163. }
  164. void setCustomData(const char* const type, const char* const key, const char* const value)
  165. {
  166. Q_ASSERT(plugin);
  167. if (! plugin)
  168. return;
  169. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  170. }
  171. void setChunkData(const char* const filePath)
  172. {
  173. Q_ASSERT(plugin);
  174. if (! plugin)
  175. return;
  176. Q_UNUSED(filePath);
  177. #if 0
  178. nextChunkFilePath = strdup(filePath);
  179. while (nextChunkFilePath)
  180. carla_msleep(25);
  181. #endif
  182. }
  183. // ---------------------------------------------------------------------
  184. // idle
  185. void idle()
  186. {
  187. Q_ASSERT(plugin);
  188. if (! plugin)
  189. return;
  190. plugin->idleGui();
  191. //plugin->showGui(true);
  192. }
  193. void showGui(const bool yesNo)
  194. {
  195. qDebug("CarlaPluginClient::showGui(%s)", bool2str(yesNo));
  196. Q_ASSERT(plugin);
  197. if (! plugin)
  198. return;
  199. plugin->showGui(yesNo);
  200. }
  201. // ---------------------------------------------------------------------
  202. // callback
  203. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  204. {
  205. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  206. // FIXME - those OSC calls should be on the engine
  207. switch (action)
  208. {
  209. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  210. engine->osc_send_bridge_set_parameter_value(value1, value3);
  211. break;
  212. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  213. engine->osc_send_bridge_set_program(value1);
  214. break;
  215. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  216. engine->osc_send_bridge_set_midi_program(value1);
  217. break;
  218. case CarlaBackend::CALLBACK_NOTE_ON:
  219. {
  220. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  221. //osc_send_midi(mdata);
  222. break;
  223. }
  224. case CarlaBackend::CALLBACK_NOTE_OFF:
  225. {
  226. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  227. //osc_send_midi(mdata);
  228. break;
  229. }
  230. case CarlaBackend::CALLBACK_SHOW_GUI:
  231. //if (value1 == 0)
  232. //sendOscConfigure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  233. //quequeMessage(MESSAGE_QUIT, 0, 0, 0.0);
  234. break;
  235. case CarlaBackend::CALLBACK_RESIZE_GUI:
  236. qDebug("resize callback-------------------------------------------------------------------------------");
  237. quequeMessage(MESSAGE_RESIZE_GUI, value1, value2, 0.0);
  238. //if (m_toolkit)
  239. // m_toolkit->resize(value1, value2);
  240. break;
  241. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  242. //if (CARLA_PLUGIN)
  243. //{
  244. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  245. // {
  246. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  247. // }
  248. //}
  249. break;
  250. case CarlaBackend::CALLBACK_QUIT:
  251. //quequeMessage(MESSAGE_QUIT, 0, 0, 0.0);
  252. break;
  253. default:
  254. break;
  255. }
  256. Q_UNUSED(value1);
  257. Q_UNUSED(value2);
  258. Q_UNUSED(value3);
  259. }
  260. // ---------------------------------------------------------------------
  261. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  262. {
  263. Q_ASSERT(ptr);
  264. if (! ptr)
  265. return;
  266. CarlaPluginClient* const client = (CarlaPluginClient*)ptr;
  267. client->handleCallback(action, value1, value2, value3);
  268. }
  269. private:
  270. CarlaBackend::CarlaEngine* engine;
  271. CarlaBackend::CarlaPlugin* plugin;
  272. };
  273. // -------------------------------------------------------------------------
  274. // toolkit
  275. class BridgeApplication : public QApplication
  276. {
  277. public:
  278. BridgeApplication()
  279. : QApplication(qargc, qargv)
  280. {
  281. msgTimer = 0;
  282. m_client = nullptr;
  283. }
  284. void exec(CarlaPluginClient* const client)
  285. {
  286. m_client = client;
  287. msgTimer = startTimer(50);
  288. QApplication::exec();
  289. }
  290. protected:
  291. void timerEvent(QTimerEvent* const event)
  292. {
  293. if (qcloseNow)
  294. return quit();
  295. if (event->timerId() == msgTimer)
  296. {
  297. if (m_client)
  298. {
  299. m_client->idle();
  300. if (! m_client->runMessages())
  301. killTimer(msgTimer);
  302. }
  303. }
  304. QApplication::timerEvent(event);
  305. }
  306. private:
  307. int msgTimer;
  308. CarlaPluginClient* m_client;
  309. };
  310. class CarlaToolkitPlugin : public CarlaToolkit
  311. {
  312. public:
  313. CarlaToolkitPlugin()
  314. : CarlaToolkit("carla-bridge-plugin")
  315. {
  316. qDebug("CarlaToolkitPlugin::CarlaToolkitPlugin()");
  317. app = nullptr;
  318. dialog = nullptr;
  319. }
  320. ~CarlaToolkitPlugin()
  321. {
  322. qDebug("CarlaToolkitPlugin::~CarlaToolkitPlugin()");
  323. Q_ASSERT(! app);
  324. }
  325. void init()
  326. {
  327. qDebug("CarlaToolkitPlugin::init()");
  328. Q_ASSERT(! app);
  329. app = new BridgeApplication;
  330. }
  331. void exec(CarlaClient* const client, const bool showGui)
  332. {
  333. qDebug("CarlaToolkitPlugin::exec(%p)", client);
  334. Q_ASSERT(app);
  335. Q_ASSERT(client);
  336. m_client = client;
  337. if (showGui)
  338. {
  339. show();
  340. }
  341. else
  342. {
  343. m_client->sendOscUpdate();
  344. m_client->sendOscBridgeUpdate();
  345. }
  346. app->exec((CarlaPluginClient*)client);
  347. }
  348. void quit()
  349. {
  350. qDebug("CarlaToolkitPlugin::quit()");
  351. Q_ASSERT(app);
  352. if (dialog)
  353. {
  354. dialog->close();
  355. delete dialog;
  356. dialog = nullptr;
  357. }
  358. if (app)
  359. {
  360. if (! app->closingDown())
  361. app->quit();
  362. delete app;
  363. app = nullptr;
  364. }
  365. }
  366. void show()
  367. {
  368. qDebug("CarlaToolkitPlugin::show()");
  369. if (m_client)
  370. ((CarlaPluginClient*)m_client)->showGui(true);
  371. if (dialog)
  372. dialog->show();
  373. }
  374. void hide()
  375. {
  376. qDebug("CarlaToolkitPlugin::hide()");
  377. if (m_client)
  378. ((CarlaPluginClient*)m_client)->showGui(false);
  379. if (dialog)
  380. dialog->show();
  381. }
  382. void resize(int width, int height)
  383. {
  384. qDebug("CarlaToolkitPlugin::resize(%i, %i)", width, height);
  385. Q_ASSERT(dialog);
  386. if (dialog)
  387. dialog->setFixedSize(width, height);
  388. }
  389. // ---------------------------------------------------------------------
  390. void createWindow(const char* const pluginName, const bool createLayout)
  391. {
  392. dialog = new QDialog(nullptr);
  393. dialog->resize(10, 10);
  394. if (createLayout)
  395. {
  396. QVBoxLayout* const layout = new QVBoxLayout(dialog);
  397. dialog->setContentsMargins(0, 0, 0, 0);
  398. dialog->setLayout(layout);
  399. }
  400. dialog->setWindowTitle(QString("%1 (GUI)").arg(pluginName));
  401. }
  402. CarlaBackend::GuiDataHandle getWindowHandle() const
  403. {
  404. return dialog;
  405. }
  406. // ---------------------------------------------------------------------
  407. private:
  408. BridgeApplication* app;
  409. QDialog* dialog;
  410. };
  411. CarlaToolkit* CarlaToolkit::createNew(const char* const)
  412. {
  413. return new CarlaToolkitPlugin;
  414. }
  415. // -------------------------------------------------------------------------
  416. CARLA_BRIDGE_END_NAMESPACE
  417. int main(int argc, char* argv[])
  418. {
  419. if (argc != 6)
  420. {
  421. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  422. return 1;
  423. }
  424. qargc = argc;
  425. qargv = argv;
  426. const char* const oscUrl = argv[1];
  427. const char* const stype = argv[2];
  428. const char* const filename = argv[3];
  429. const char* name = argv[4];
  430. const char* const label = argv[5];
  431. const bool useOsc = strcmp(oscUrl, "null");
  432. if (strcmp(name, "(none)") == 0)
  433. name = nullptr;
  434. CarlaBackend::PluginType itype;
  435. if (strcmp(stype, "LADSPA") == 0)
  436. itype = CarlaBackend::PLUGIN_LADSPA;
  437. else if (strcmp(stype, "DSSI") == 0)
  438. itype = CarlaBackend::PLUGIN_DSSI;
  439. else if (strcmp(stype, "LV2") == 0)
  440. itype = CarlaBackend::PLUGIN_LV2;
  441. else if (strcmp(stype, "VST") == 0)
  442. itype = CarlaBackend::PLUGIN_VST;
  443. else
  444. {
  445. itype = CarlaBackend::PLUGIN_NONE;
  446. qWarning("Invalid plugin type '%s'", stype);
  447. return 1;
  448. }
  449. // Init toolkit
  450. CarlaBridge::CarlaToolkitPlugin toolkit;
  451. toolkit.init();
  452. // Init client
  453. CarlaBridge::CarlaPluginClient client(&toolkit);
  454. // Init OSC
  455. if (useOsc && ! client.oscInit(oscUrl))
  456. {
  457. toolkit.quit();
  458. return -1;
  459. }
  460. // Init backend engine
  461. CarlaBackend::CarlaEngineJack engine;
  462. engine.setCallback(client.callback, &client);
  463. // bridge client <-> engine
  464. client.registerOscEngine(&engine);
  465. // Init engine
  466. QString engName = QString("%1 (master)").arg(name ? name : label);
  467. engName.truncate(engine.maxClientNameSize());
  468. if (! engine.init(engName.toUtf8().constData()))
  469. {
  470. qWarning("Bridge engine failed to start, error was:\n%s", CarlaBackend::getLastError());
  471. engine.close();
  472. toolkit.quit();
  473. return 2;
  474. }
  475. /// Init plugin
  476. short id = engine.addPlugin(itype, filename, name, label);
  477. int ret;
  478. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  479. {
  480. CarlaBackend::CarlaPlugin* const plugin = engine.getPlugin(id);
  481. client.setStuff(&engine, plugin);
  482. // create window if needed
  483. bool guiResizable;
  484. CarlaBackend::GuiType guiType;
  485. plugin->getGuiInfo(&guiType, &guiResizable);
  486. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  487. {
  488. toolkit.createWindow(plugin->name(), (guiType == CarlaBackend::GUI_INTERNAL_QT4));
  489. plugin->setGuiData(toolkit.getWindowHandle());
  490. }
  491. if (! useOsc)
  492. plugin->setActive(true, false, false);
  493. ret = 0;
  494. }
  495. else
  496. {
  497. qWarning("Plugin failed to load, error was:\n%s", CarlaBackend::getLastError());
  498. ret = 1;
  499. }
  500. if (ret == 0)
  501. {
  502. initSignalHandler();
  503. toolkit.exec(&client, !useOsc);
  504. }
  505. engine.removeAllPlugins();
  506. engine.close();
  507. // Close OSC
  508. if (useOsc)
  509. {
  510. client.sendOscExiting();
  511. client.oscClose();
  512. }
  513. // Close toolkit
  514. toolkit.quit();
  515. return ret;
  516. }
  517. #endif // BUILD_BRIDGE_PLUGIN