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.

1108 lines
33KB

  1. /*
  2. * Carla ReWire Plugin
  3. * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN64)
  20. # undef WANT_REWIRE
  21. #endif
  22. #ifdef WANT_REWIRE
  23. #include "CarlaLibUtils.hpp"
  24. #include "CarlaMathUtils.hpp"
  25. // -----------------------------------------------------
  26. CARLA_BACKEND_START_NAMESPACE
  27. #if 0
  28. }
  29. #endif
  30. // -----------------------------------------------------------------------
  31. // ReWire assumes sizeof(long) == 4
  32. typedef int32_t rlong;
  33. typedef uint32_t rulong;
  34. static_assert(sizeof(rlong) == 4, "Incorrect rlong size");
  35. static_assert(sizeof(rulong) == 4, "Incorrect rulong size");
  36. // rewire is not for linux, so for easy testing+development, we can skip this
  37. #ifndef CARLA_OS_LINUX
  38. static_assert(sizeof(long) == 4, "Incorrect long size");
  39. static_assert(sizeof(ulong) == 4, "Incorrect ulong size");
  40. static_assert(sizeof(long) == sizeof(rlong), "long size mismatch");
  41. static_assert(sizeof(ulong) == sizeof(rulong), "ulon size mismatch");
  42. #endif
  43. struct RwOpenInfo {
  44. rulong size1; // 16
  45. rulong size2; // 12
  46. rlong sampleRate;
  47. rlong bufferSize;
  48. };
  49. struct RwDevInfo {
  50. rulong size; // 8288
  51. char name[32];
  52. rlong channelCount; // max limited to 255
  53. char channelNames[256][32];
  54. rulong defaultChannels[8];
  55. rulong stereoPairs[4];
  56. rulong eventBufferSize;
  57. rlong version;
  58. };
  59. struct RwAudioInfo {
  60. rulong size; // 12
  61. rlong sampleRate;
  62. rlong bufferSize;
  63. };
  64. struct RwBusInfo {
  65. rulong size; // 40
  66. rulong channel;
  67. char name[32];
  68. };
  69. struct RwEventInfo {
  70. rulong size; // 132 FIXME?
  71. rulong bus[32];
  72. };
  73. struct RwEvent { // 24
  74. ushort type;
  75. uchar d1, d2;
  76. rulong s1, s2, s3, s4, s5;
  77. };
  78. struct RwEventBuffer {
  79. rulong size1; // 20
  80. rulong size2; // 24 (of RwEvent)
  81. rulong count;
  82. rulong maxCount;
  83. #ifndef CARLA_OS_LINUX // pointers on 64bit linux are size 8, skip this
  84. RwEvent* buf;
  85. #else
  86. rulong buf;
  87. #endif
  88. };
  89. struct RwAudioInInfo {
  90. rulong size; // 1116
  91. RwEventBuffer evBuf;
  92. rulong channels[8];
  93. #ifndef CARLA_OS_LINUX // pointers on 64bit linux are size 8, skip this
  94. float* audioBuf[256];
  95. #else
  96. rulong audioBuf[256];
  97. #endif
  98. rlong tickStart;
  99. rulong frames;
  100. rulong playMode;
  101. rulong tempo; // bpm
  102. rulong signNumerator;
  103. rulong signDenominator;
  104. rlong loopStartPos;
  105. rlong loopEndPos;
  106. rulong loopOn;
  107. };
  108. struct RwAudioOutInfo {
  109. rulong size; // 56
  110. RwEventBuffer evBuf;
  111. rulong channels[8];
  112. };
  113. static_assert(sizeof(RwOpenInfo) == 16, "Incorrect ReWire struct size");
  114. static_assert(sizeof(RwDevInfo) == 8288, "Incorrect ReWire struct size");
  115. static_assert(sizeof(RwAudioInfo) == 12, "Incorrect ReWire struct size");
  116. static_assert(sizeof(RwBusInfo) == 40, "Incorrect ReWire struct size");
  117. static_assert(sizeof(RwEventInfo) == 132, "Incorrect ReWire struct size");
  118. static_assert(sizeof(RwEvent) == 24, "Incorrect ReWire struct size");
  119. static_assert(sizeof(RwEventBuffer) == 20, "Incorrect ReWire struct size");
  120. static_assert(sizeof(RwAudioInInfo) == 1116, "Incorrect ReWire struct size");
  121. static_assert(sizeof(RwAudioOutInfo) == 56, "Incorrect ReWire struct size");
  122. // -----------------------------------------------------------------------
  123. typedef void (*Fn_RWDEFCloseDevice)();
  124. typedef void (*Fn_RWDEFDriveAudio)(RwAudioInInfo* in, RwAudioOutInfo* out);
  125. typedef void (*Fn_RWDEFGetDeviceInfo)(RwDevInfo* info);
  126. typedef void (*Fn_RWDEFGetDeviceNameAndVersion)(long* version, char* name);
  127. typedef void (*Fn_RWDEFGetEventBusInfo)(ushort index, RwBusInfo* info);
  128. typedef void (*Fn_RWDEFGetEventChannelInfo)(void* v1, void* v2);
  129. typedef void (*Fn_RWDEFGetEventControllerInfo)(void* v1, ushort index, void* v2);
  130. typedef void (*Fn_RWDEFGetEventInfo)(RwEventInfo* info);
  131. typedef void (*Fn_RWDEFGetEventNoteInfo)(void* v1, ushort index, void* v2);
  132. typedef void (*Fn_RWDEFIdle)();
  133. typedef char (*Fn_RWDEFIsCloseOK)();
  134. typedef char (*Fn_RWDEFIsPanelAppLaunched)();
  135. typedef int (*Fn_RWDEFLaunchPanelApp)();
  136. typedef int (*Fn_RWDEFOpenDevice)(RwOpenInfo* info);
  137. typedef int (*Fn_RWDEFQuitPanelApp)();
  138. typedef void (*Fn_RWDEFSetAudioInfo)(RwAudioInfo* info);
  139. // -----------------------------------------------------------------------------
  140. struct RewireBridge {
  141. void* lib;
  142. Fn_RWDEFCloseDevice RWDEFCloseDevice;
  143. Fn_RWDEFDriveAudio RWDEFDriveAudio;
  144. Fn_RWDEFGetDeviceInfo RWDEFGetDeviceInfo;
  145. Fn_RWDEFGetDeviceNameAndVersion RWDEFGetDeviceNameAndVersion;
  146. Fn_RWDEFGetEventBusInfo RWDEFGetEventBusInfo;
  147. Fn_RWDEFGetEventChannelInfo RWDEFGetEventChannelInfo;
  148. Fn_RWDEFGetEventControllerInfo RWDEFGetEventControllerInfo;
  149. Fn_RWDEFGetEventInfo RWDEFGetEventInfo;
  150. Fn_RWDEFGetEventNoteInfo RWDEFGetEventNoteInfo;
  151. Fn_RWDEFIdle RWDEFIdle;
  152. Fn_RWDEFIsCloseOK RWDEFIsCloseOK;
  153. Fn_RWDEFIsPanelAppLaunched RWDEFIsPanelAppLaunched;
  154. Fn_RWDEFLaunchPanelApp RWDEFLaunchPanelApp;
  155. Fn_RWDEFOpenDevice RWDEFOpenDevice;
  156. Fn_RWDEFQuitPanelApp RWDEFQuitPanelApp;
  157. Fn_RWDEFSetAudioInfo RWDEFSetAudioInfo;
  158. RewireBridge()
  159. : lib(nullptr),
  160. RWDEFCloseDevice(nullptr),
  161. RWDEFDriveAudio(nullptr),
  162. RWDEFGetDeviceInfo(nullptr),
  163. RWDEFGetDeviceNameAndVersion(nullptr),
  164. RWDEFGetEventBusInfo(nullptr),
  165. RWDEFGetEventChannelInfo(nullptr),
  166. RWDEFGetEventControllerInfo(nullptr),
  167. RWDEFGetEventInfo(nullptr),
  168. RWDEFGetEventNoteInfo(nullptr),
  169. RWDEFIdle(nullptr),
  170. RWDEFIsCloseOK(nullptr),
  171. RWDEFIsPanelAppLaunched(nullptr),
  172. RWDEFLaunchPanelApp(nullptr),
  173. RWDEFOpenDevice(nullptr),
  174. RWDEFQuitPanelApp(nullptr),
  175. RWDEFSetAudioInfo(nullptr) {}
  176. ~RewireBridge()
  177. {
  178. cleanup();
  179. }
  180. int init(const char* const filename)
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(lib == nullptr, -2);
  183. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', -2);
  184. lib = lib_open(filename);
  185. if (lib == nullptr)
  186. return -1;
  187. #define JOIN(a, b) a ## b
  188. #define LIB_SYMBOL(NAME) NAME = (Fn_##NAME)lib_symbol(lib, #NAME);
  189. //if (NAME == nullptr) cleanup(); return -2;
  190. LIB_SYMBOL(RWDEFCloseDevice)
  191. LIB_SYMBOL(RWDEFDriveAudio)
  192. LIB_SYMBOL(RWDEFGetDeviceInfo)
  193. LIB_SYMBOL(RWDEFGetDeviceNameAndVersion)
  194. LIB_SYMBOL(RWDEFGetEventBusInfo)
  195. LIB_SYMBOL(RWDEFGetEventChannelInfo)
  196. LIB_SYMBOL(RWDEFGetEventControllerInfo)
  197. LIB_SYMBOL(RWDEFGetEventInfo)
  198. LIB_SYMBOL(RWDEFGetEventNoteInfo)
  199. LIB_SYMBOL(RWDEFIdle)
  200. LIB_SYMBOL(RWDEFIsCloseOK)
  201. LIB_SYMBOL(RWDEFIsPanelAppLaunched)
  202. LIB_SYMBOL(RWDEFLaunchPanelApp)
  203. LIB_SYMBOL(RWDEFOpenDevice)
  204. LIB_SYMBOL(RWDEFQuitPanelApp)
  205. LIB_SYMBOL(RWDEFSetAudioInfo)
  206. #undef JOIN
  207. #undef LIB_SYMBOL
  208. return 0;
  209. }
  210. void cleanup()
  211. {
  212. if (lib != nullptr)
  213. {
  214. lib_close(lib);
  215. lib = nullptr;
  216. }
  217. RWDEFCloseDevice = nullptr;
  218. RWDEFDriveAudio = nullptr;
  219. RWDEFGetDeviceInfo = nullptr;
  220. RWDEFGetDeviceNameAndVersion = nullptr;
  221. RWDEFGetEventBusInfo = nullptr;
  222. RWDEFGetEventChannelInfo = nullptr;
  223. RWDEFGetEventControllerInfo = nullptr;
  224. RWDEFGetEventInfo = nullptr;
  225. RWDEFGetEventNoteInfo = nullptr;
  226. RWDEFIdle = nullptr;
  227. RWDEFIsCloseOK = nullptr;
  228. RWDEFIsPanelAppLaunched = nullptr;
  229. RWDEFLaunchPanelApp = nullptr;
  230. RWDEFOpenDevice = nullptr;
  231. RWDEFQuitPanelApp = nullptr;
  232. RWDEFSetAudioInfo = nullptr;
  233. }
  234. };
  235. // -----------------------------------------------------
  236. class ReWirePlugin : public CarlaPlugin
  237. {
  238. public:
  239. ReWirePlugin(CarlaEngine* const engine, const uint id)
  240. : CarlaPlugin(engine, id),
  241. fIsOpen(false),
  242. fIsPanelLaunched(false),
  243. fLabel(nullptr)
  244. {
  245. carla_debug("ReWirePlugin::ReWirePlugin(%p, %i)", engine, id);
  246. carla_zeroStruct<RwAudioInInfo>(fRwAudioIn);
  247. fRwAudioIn.size = sizeof(RwAudioInInfo);
  248. carla_zeroStruct<RwAudioOutInfo>(fRwAudioOut);
  249. fRwAudioOut.size = sizeof(RwAudioOutInfo);
  250. }
  251. ~ReWirePlugin() override
  252. {
  253. carla_debug("ReWirePlugin::~ReWirePlugin()");
  254. // close panel
  255. if (fIsPanelLaunched)
  256. {
  257. fRw.RWDEFQuitPanelApp();
  258. fIsPanelLaunched = false;
  259. }
  260. pData->singleMutex.lock();
  261. pData->masterMutex.lock();
  262. if (pData->client != nullptr && pData->client->isActive())
  263. pData->client->deactivate();
  264. if (pData->active)
  265. {
  266. deactivate();
  267. pData->active = false;
  268. }
  269. if (fIsOpen)
  270. {
  271. for (; fRw.RWDEFIsCloseOK() == 0;)
  272. carla_msleep(2);
  273. fRw.RWDEFCloseDevice();
  274. fIsOpen = false;
  275. }
  276. if (fLabel != nullptr)
  277. {
  278. delete[] fLabel;
  279. fLabel = nullptr;
  280. }
  281. clearBuffers();
  282. }
  283. // -------------------------------------------------------------------
  284. // Information (base)
  285. PluginType getType() const noexcept override
  286. {
  287. return PLUGIN_REWIRE;
  288. }
  289. PluginCategory getCategory() const noexcept override
  290. {
  291. return PLUGIN_CATEGORY_SYNTH;
  292. }
  293. // -------------------------------------------------------------------
  294. // Information (count)
  295. // nothing
  296. // -------------------------------------------------------------------
  297. // Information (current data)
  298. int32_t getChunkData(void** const dataPtr) const noexcept override
  299. {
  300. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  301. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr, 0);
  302. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  303. // TODO
  304. return 0;
  305. }
  306. // -------------------------------------------------------------------
  307. // Information (per-plugin data)
  308. uint getOptionsAvailable() const noexcept override
  309. {
  310. uint options = 0x0;
  311. if (getMidiInCount() > 0)
  312. {
  313. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  314. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  315. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  316. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  317. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  318. }
  319. return options;
  320. }
  321. float getParameterValue(const uint32_t parameterId) const noexcept override
  322. {
  323. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  324. // TODO
  325. return 0.0f;
  326. }
  327. void getLabel(char* const strBuf) const noexcept override
  328. {
  329. CARLA_SAFE_ASSERT_RETURN(fLabel != nullptr,)
  330. std::strcpy(strBuf, fLabel);
  331. }
  332. void getRealName(char* const strBuf) const noexcept override
  333. {
  334. CARLA_SAFE_ASSERT_RETURN(fLabel != nullptr,)
  335. std::strcpy(strBuf, fLabel);
  336. }
  337. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  341. // TODO
  342. CarlaPlugin::getParameterName(parameterId, strBuf);
  343. }
  344. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  345. {
  346. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  347. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  348. // TODO
  349. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  350. }
  351. // -------------------------------------------------------------------
  352. // Set data (state)
  353. // nothing
  354. // -------------------------------------------------------------------
  355. // Set data (internal stuff)
  356. // nothing
  357. // -------------------------------------------------------------------
  358. // Set data (plugin-specific stuff)
  359. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  360. {
  361. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  362. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  363. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  364. // TODO
  365. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  366. }
  367. void setChunkData(const char* const stringData) override
  368. {
  369. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  370. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  371. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr,);
  372. // TODO
  373. }
  374. // -------------------------------------------------------------------
  375. // Set ui stuff
  376. void showCustomUI(const bool yesNo) override
  377. {
  378. if (yesNo)
  379. {
  380. if (! fRw.RWDEFIsPanelAppLaunched())
  381. fRw.RWDEFLaunchPanelApp();
  382. fIsPanelLaunched = true;
  383. }
  384. else
  385. {
  386. if (fRw.RWDEFIsPanelAppLaunched())
  387. fRw.RWDEFQuitPanelApp();
  388. fIsPanelLaunched = false;
  389. }
  390. }
  391. void idle() override
  392. {
  393. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  394. fRw.RWDEFIdle();
  395. // check if panel has been closed
  396. if (fIsPanelLaunched && ! fRw.RWDEFIsPanelAppLaunched())
  397. {
  398. // FIXME
  399. //fIsPanelLaunched = true;
  400. //pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  401. // static int counter = 0;
  402. //
  403. // if (counter % 1000)
  404. // {
  405. // carla_stdout("Panel is closed?");
  406. // }
  407. //
  408. // ++counter;
  409. }
  410. CarlaPlugin::idle();
  411. }
  412. // -------------------------------------------------------------------
  413. // Plugin state
  414. void reload() override
  415. {
  416. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  417. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  418. carla_debug("ReWirePlugin::reload() - start");
  419. const EngineProcessMode processMode(pData->engine->getProccessMode());
  420. // Safely disable plugin for reload
  421. const ScopedDisabler sd(this);
  422. if (pData->active)
  423. deactivate();
  424. clearBuffers();
  425. uint32_t aIns, aOuts, mIns, mOuts, params;
  426. aIns = aOuts = 0;
  427. bool needsCtrlIn, needsCtrlOut;
  428. needsCtrlIn = needsCtrlOut = false;
  429. RwDevInfo devInfo;
  430. carla_zeroStruct<RwDevInfo>(devInfo);
  431. devInfo.size = sizeof(RwDevInfo);
  432. fRw.RWDEFGetDeviceInfo(&devInfo);
  433. if (devInfo.channelCount > 0)
  434. aIns = aOuts = static_cast<uint32_t>(devInfo.channelCount);
  435. mIns = mOuts = 0; // TODO, should always be 1
  436. params = 0; // TODO?
  437. if (aIns > 0)
  438. {
  439. pData->audioIn.createNew(aIns);
  440. }
  441. if (aOuts > 0)
  442. {
  443. pData->audioOut.createNew(aOuts);
  444. needsCtrlIn = true;
  445. }
  446. if (params > 0)
  447. {
  448. pData->param.createNew(params, false);
  449. needsCtrlIn = true;
  450. }
  451. const uint portNameSize(pData->engine->getMaxPortNameSize());
  452. CarlaString portName;
  453. // Audio Ins
  454. for (uint32_t j=0; j < aIns; ++j)
  455. {
  456. portName.clear();
  457. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  458. {
  459. portName = pData->name;
  460. portName += ":";
  461. }
  462. portName += "i";
  463. portName += devInfo.channelNames[j];
  464. portName.truncate(portNameSize);
  465. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  466. pData->audioIn.ports[j].rindex = j;
  467. }
  468. // Audio Outs
  469. for (uint32_t j=0; j < aOuts; ++j)
  470. {
  471. portName.clear();
  472. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  473. {
  474. portName = pData->name;
  475. portName += ":";
  476. }
  477. portName += "o";
  478. portName += devInfo.channelNames[j];
  479. portName.truncate(portNameSize);
  480. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  481. pData->audioOut.ports[j].rindex = j;
  482. }
  483. if (needsCtrlIn)
  484. {
  485. portName.clear();
  486. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  487. {
  488. portName = pData->name;
  489. portName += ":";
  490. }
  491. portName += "events-in";
  492. portName.truncate(portNameSize);
  493. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  494. }
  495. if (needsCtrlOut)
  496. {
  497. portName.clear();
  498. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  499. {
  500. portName = pData->name;
  501. portName += ":";
  502. }
  503. portName += "events-out";
  504. portName.truncate(portNameSize);
  505. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  506. }
  507. // plugin hints
  508. pData->hints = 0x0;
  509. pData->hints |= PLUGIN_IS_SYNTH;
  510. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  511. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  512. pData->hints |= PLUGIN_CAN_DRYWET;
  513. if (aOuts > 0)
  514. pData->hints |= PLUGIN_CAN_VOLUME;
  515. if (aOuts >= 2 && aOuts % 2 == 0)
  516. pData->hints |= PLUGIN_CAN_BALANCE;
  517. // extra plugin hints
  518. pData->extraHints = 0x0;
  519. if (mIns > 0)
  520. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  521. if (mOuts > 0)
  522. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  523. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  524. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  525. bufferSizeChanged(pData->engine->getBufferSize());
  526. if (pData->active)
  527. activate();
  528. carla_debug("ReWirePlugin::reload() - end");
  529. }
  530. // -------------------------------------------------------------------
  531. // Plugin processing
  532. void activate() noexcept override
  533. {
  534. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  535. // TODO
  536. }
  537. void deactivate() noexcept override
  538. {
  539. CARLA_SAFE_ASSERT_RETURN(fRw.lib != nullptr,);
  540. // TODO
  541. }
  542. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  543. {
  544. // --------------------------------------------------------------------------------------------------------
  545. // Check if active
  546. if (! pData->active)
  547. {
  548. // disable any output sound
  549. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  550. FLOAT_CLEAR(outBuffer[i], frames);
  551. return;
  552. }
  553. // --------------------------------------------------------------------------------------------------------
  554. // Check if needs reset
  555. if (pData->needsReset)
  556. {
  557. #ifndef BUILD_BRIDGE
  558. if (pData->latency > 0)
  559. {
  560. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  561. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  562. }
  563. #endif
  564. pData->needsReset = false;
  565. }
  566. // --------------------------------------------------------------------------------------------------------
  567. // Set TimeInfo
  568. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  569. //fRwAudioIn.playMode; // ???
  570. //fRwAudioIn.frames = timeInfo.frame; // not sure if buf or tranport frames
  571. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  572. {
  573. double ppqBar = double(timeInfo.bbt.bar - 1) * timeInfo.bbt.beatsPerBar;
  574. double ppqBeat = double(timeInfo.bbt.beat - 1);
  575. double ppqTick = double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat;
  576. // PPQ Pos, ???
  577. fRwAudioIn.tickStart = static_cast<rlong>(ppqBar + ppqBeat + ppqTick);
  578. // Tempo
  579. fRwAudioIn.tempo = static_cast<rulong>(timeInfo.bbt.beatsPerMinute);
  580. // Bars
  581. //fTimeInfo.barStartPos = ppqBar;
  582. // Time Signature
  583. fRwAudioIn.signNumerator = static_cast<rulong>(timeInfo.bbt.beatsPerBar);
  584. fRwAudioIn.signDenominator = static_cast<rulong>(timeInfo.bbt.beatType);
  585. }
  586. else
  587. {
  588. fRwAudioIn.tickStart = 0;
  589. fRwAudioIn.tempo = 120;
  590. fRwAudioIn.signNumerator = 4;
  591. fRwAudioIn.signDenominator = 4;
  592. }
  593. // --------------------------------------------------------------------------------------------------------
  594. // Plugin processing (no events)
  595. {
  596. processSingle(inBuffer, outBuffer, frames, 0);
  597. } // End of Plugin processing (no events)
  598. }
  599. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  600. {
  601. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  602. if (pData->audioIn.count > 0)
  603. {
  604. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  605. }
  606. if (pData->audioOut.count > 0)
  607. {
  608. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  609. }
  610. // --------------------------------------------------------------------------------------------------------
  611. // Try lock, silence otherwise
  612. if (pData->engine->isOffline())
  613. {
  614. pData->singleMutex.lock();
  615. }
  616. else if (! pData->singleMutex.tryLock())
  617. {
  618. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  619. {
  620. for (uint32_t k=0; k < frames; ++k)
  621. outBuffer[i][k+timeOffset] = 0.0f;
  622. }
  623. return false;
  624. }
  625. // --------------------------------------------------------------------------------------------------------
  626. // Set audio buffers
  627. //rulong channels[8];
  628. fRwAudioIn.frames = frames;
  629. #ifndef CARLA_OS_LINUX
  630. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  631. {
  632. fRwAudioIn.audioBuf[i] = outBuffer[i]+timeOffset;
  633. FLOAT_COPY(fRwAudioIn.audioBuf[i], inBuffer[i]+timeOffset, frames);
  634. }
  635. #endif
  636. // --------------------------------------------------------------------------------------------------------
  637. // Run plugin
  638. fRw.RWDEFDriveAudio(&fRwAudioIn, &fRwAudioOut);
  639. #ifndef BUILD_BRIDGE
  640. // --------------------------------------------------------------------------------------------------------
  641. // Post-processing (dry/wet, volume and balance)
  642. {
  643. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  644. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  645. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  646. bool isPair;
  647. float bufValue, oldBufLeft[doBalance ? frames : 1];
  648. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  649. {
  650. // Dry/Wet
  651. if (doDryWet)
  652. {
  653. for (uint32_t k=0; k < frames; ++k)
  654. {
  655. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k+timeOffset];
  656. outBuffer[i][k+timeOffset] = (outBuffer[i][k+timeOffset] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  657. }
  658. }
  659. // Balance
  660. if (doBalance)
  661. {
  662. isPair = (i % 2 == 0);
  663. if (isPair)
  664. {
  665. CARLA_ASSERT(i+1 < pData->audioOut.count);
  666. FLOAT_COPY(oldBufLeft, outBuffer[i]+timeOffset, frames);
  667. }
  668. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  669. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  670. for (uint32_t k=0; k < frames; ++k)
  671. {
  672. if (isPair)
  673. {
  674. // left
  675. outBuffer[i][k+timeOffset] = oldBufLeft[k] * (1.0f - balRangeL);
  676. outBuffer[i][k+timeOffset] += outBuffer[i+1][k+timeOffset] * (1.0f - balRangeR);
  677. }
  678. else
  679. {
  680. // right
  681. outBuffer[i][k+timeOffset] = outBuffer[i][k+timeOffset] * balRangeR;
  682. outBuffer[i][k+timeOffset] += oldBufLeft[k] * balRangeL;
  683. }
  684. }
  685. }
  686. // Volume
  687. if (doVolume)
  688. {
  689. for (uint32_t k=0; k < frames; ++k)
  690. outBuffer[i][k+timeOffset] *= pData->postProc.volume;
  691. }
  692. }
  693. } // End of Post-processing
  694. #endif
  695. // --------------------------------------------------------------------------------------------------------
  696. pData->singleMutex.unlock();
  697. return true;
  698. }
  699. void bufferSizeChanged(const uint32_t newBufferSize) override
  700. {
  701. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  702. carla_debug("ReWirePlugin::bufferSizeChanged(%i)", newBufferSize);
  703. if (pData->active)
  704. deactivate();
  705. RwAudioInfo audioInfo;
  706. audioInfo.size = sizeof(RwAudioInfo);
  707. audioInfo.bufferSize = static_cast<rlong>(newBufferSize);
  708. audioInfo.sampleRate = static_cast<rlong>(pData->engine->getSampleRate());
  709. fRw.RWDEFSetAudioInfo(&audioInfo);
  710. if (pData->active)
  711. activate();
  712. }
  713. void sampleRateChanged(const double newSampleRate) override
  714. {
  715. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  716. carla_debug("ReWirePlugin::sampleRateChanged(%g)", newSampleRate);
  717. if (pData->active)
  718. deactivate();
  719. RwAudioInfo audioInfo;
  720. audioInfo.size = sizeof(RwAudioInfo);
  721. audioInfo.bufferSize = static_cast<rlong>(pData->engine->getBufferSize());
  722. audioInfo.sampleRate = static_cast<rlong>(newSampleRate);
  723. fRw.RWDEFSetAudioInfo(&audioInfo);
  724. if (pData->active)
  725. activate();
  726. }
  727. // -------------------------------------------------------------------
  728. // Plugin buffers
  729. // nothing
  730. // -------------------------------------------------------------------
  731. // Post-poned UI Stuff
  732. // nothing
  733. // -------------------------------------------------------------------
  734. protected:
  735. // TODO
  736. // -------------------------------------------------------------------
  737. public:
  738. bool init(const char* const filename, const char* const name)
  739. {
  740. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  741. // ---------------------------------------------------------------
  742. // first checks
  743. if (pData->client != nullptr)
  744. {
  745. pData->engine->setLastError("Plugin client is already registered");
  746. return false;
  747. }
  748. if (filename == nullptr || filename[0] == '\0')
  749. {
  750. pData->engine->setLastError("null filename");
  751. return false;
  752. }
  753. // ---------------------------------------------------------------
  754. // open DLL
  755. int ret = fRw.init(filename);
  756. if (ret != 0)
  757. {
  758. if (ret == -1)
  759. pData->engine->setLastError(lib_error(filename));
  760. else
  761. pData->engine->setLastError("Not a valid ReWire application");
  762. return false;
  763. }
  764. // ---------------------------------------------------------------
  765. // get info
  766. long version;
  767. char nameBuf[STR_MAX+1];
  768. carla_zeroChar(nameBuf, STR_MAX+1);
  769. fRw.RWDEFGetDeviceNameAndVersion(&version, nameBuf);
  770. if (nameBuf[0] == '\0')
  771. {
  772. pData->engine->setLastError("ReWire application has no name");
  773. return false;
  774. }
  775. fLabel = carla_strdup(nameBuf);
  776. if (name != nullptr && name[0] != '\0')
  777. pData->name = pData->engine->getUniquePluginName(name);
  778. else
  779. pData->name = pData->engine->getUniquePluginName(nameBuf);
  780. pData->filename = carla_strdup(filename);
  781. // ---------------------------------------------------------------
  782. // register client
  783. pData->client = pData->engine->addClient(this);
  784. if (pData->client == nullptr || ! pData->client->isOk())
  785. {
  786. pData->engine->setLastError("Failed to register plugin client");
  787. return false;
  788. }
  789. // ---------------------------------------------------------------
  790. // initialize app
  791. RwOpenInfo info;
  792. info.size1 = sizeof(RwOpenInfo);
  793. info.size2 = 12;
  794. info.bufferSize = static_cast<rlong>(pData->engine->getBufferSize());
  795. info.sampleRate = static_cast<rlong>(pData->engine->getSampleRate());
  796. ret = fRw.RWDEFOpenDevice(&info);
  797. carla_stdout("RW open ret = %i", ret);
  798. // TODO check ret
  799. fIsOpen = true;
  800. // ---------------------------------------------------------------
  801. // load plugin settings
  802. {
  803. // set default options
  804. pData->options = 0x0;
  805. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  806. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  807. // TODO
  808. if (getMidiInCount() > 0)
  809. {
  810. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  811. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  812. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  813. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  814. }
  815. #ifndef BUILD_BRIDGE
  816. // set identifier string
  817. CarlaString identifier("ReWire/");
  818. identifier += fLabel;
  819. pData->identifier = identifier.dup();
  820. // load settings
  821. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  822. // ignore settings, we need this anyway
  823. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  824. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  825. #endif
  826. }
  827. return true;
  828. }
  829. private:
  830. RewireBridge fRw;
  831. RwAudioInInfo fRwAudioIn;
  832. RwAudioOutInfo fRwAudioOut;
  833. bool fIsOpen;
  834. bool fIsPanelLaunched;
  835. const char* fLabel;
  836. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ReWirePlugin)
  837. };
  838. CARLA_BACKEND_END_NAMESPACE
  839. #endif // WANT_REWIRE
  840. // -------------------------------------------------------------------------------------------------------------------
  841. CARLA_BACKEND_START_NAMESPACE
  842. CarlaPlugin* CarlaPlugin::newReWire(const Initializer& init)
  843. {
  844. carla_debug("CarlaPlugin::newReWire({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.uniqueId);
  845. #ifdef WANT_REWIRE
  846. ReWirePlugin* const plugin(new ReWirePlugin(init.engine, init.id));
  847. if (! plugin->init(init.filename, init.name))
  848. {
  849. delete plugin;
  850. return nullptr;
  851. }
  852. plugin->reload();
  853. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  854. {
  855. init.engine->setLastError("Carla's rack mode can only work with Stereo ReWire applications, sorry!");
  856. delete plugin;
  857. return nullptr;
  858. }
  859. return plugin;
  860. #else
  861. init.engine->setLastError("ReWire support not available");
  862. return nullptr;
  863. #endif
  864. }
  865. CARLA_BACKEND_END_NAMESPACE
  866. // -------------------------------------------------------------------------------------------------------------------