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.

1613 lines
50KB

  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifndef BUILD_BRIDGE
  19. #include "CarlaBridgeUtils.hpp"
  20. #include "CarlaShmUtils.hpp"
  21. #include "jackbridge/JackBridge.hpp"
  22. #include <ctime>
  23. #include <QtCore/QDir>
  24. #include <QtCore/QFile>
  25. #include <QtCore/QStringList>
  26. #include <QtCore/QTextStream>
  27. #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  28. /* check argument count */ \
  29. if (argc != argcToCompare) \
  30. { \
  31. carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  32. return 1; \
  33. } \
  34. if (argc > 0) \
  35. { \
  36. /* check for nullness */ \
  37. if (! (types && typesToCompare)) \
  38. { \
  39. carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
  40. return 1; \
  41. } \
  42. /* check argument types */ \
  43. if (std::strcmp(types, typesToCompare) != 0) \
  44. { \
  45. carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  46. return 1; \
  47. } \
  48. }
  49. CARLA_BACKEND_START_NAMESPACE
  50. // -------------------------------------------------------------------------------------------------------------------
  51. // Engine Helpers
  52. extern void registerEnginePlugin(CarlaEngine* const engine, const unsigned int id, CarlaPlugin* const plugin);
  53. // -------------------------------------------------------------------------------------------------------------------
  54. shm_t shm_mkstemp(char* const fileBase)
  55. {
  56. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  57. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  58. "0123456789";
  59. const int size = (fileBase != nullptr) ? std::strlen(fileBase) : 0;
  60. shm_t shm;
  61. carla_shm_init(shm);
  62. if (size < 6)
  63. {
  64. errno = EINVAL;
  65. return shm;
  66. }
  67. if (std::strcmp(fileBase + size - 6, "XXXXXX") != 0)
  68. {
  69. errno = EINVAL;
  70. return shm;
  71. }
  72. while (true)
  73. {
  74. for (int c = size - 6; c < size; c++)
  75. {
  76. // Note the -1 to avoid the trailing '\0' in charSet.
  77. fileBase[c] = charSet[std::rand() % (sizeof(charSet) - 1)];
  78. }
  79. shm_t shm = carla_shm_create(fileBase);
  80. if (carla_is_shm_valid(shm) || errno != EEXIST)
  81. return shm;
  82. }
  83. }
  84. // -------------------------------------------------------------------------------------------------------------------
  85. struct BridgeParamInfo {
  86. float value;
  87. CarlaString name;
  88. CarlaString unit;
  89. BridgeParamInfo()
  90. : value(0.0f) {}
  91. };
  92. class BridgePlugin : public CarlaPlugin
  93. {
  94. public:
  95. BridgePlugin(CarlaEngine* const engine, const unsigned int id, const BinaryType btype, const PluginType ptype)
  96. : CarlaPlugin(engine, id),
  97. fBinaryType(btype),
  98. fPluginType(ptype),
  99. fInitiated(false),
  100. fInitError(false),
  101. fSaved(false),
  102. fParams(nullptr)
  103. {
  104. carla_debug("BridgePlugin::BridgePlugin(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  105. kData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_BRIDGE);
  106. fHints |= PLUGIN_IS_BRIDGE;
  107. }
  108. ~BridgePlugin() override
  109. {
  110. carla_debug("BridgePlugin::~BridgePlugin()");
  111. kData->singleMutex.lock();
  112. kData->masterMutex.lock();
  113. if (kData->active)
  114. {
  115. deactivate();
  116. kData->active = false;
  117. }
  118. if (kData->osc.thread.isRunning())
  119. {
  120. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeQuit);
  121. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  122. waitForServer();
  123. }
  124. if (kData->osc.data.target != nullptr)
  125. {
  126. osc_send_hide(&kData->osc.data);
  127. osc_send_quit(&kData->osc.data);
  128. }
  129. kData->osc.data.free();
  130. // Wait a bit first, then force kill
  131. if (kData->osc.thread.isRunning() && ! kData->osc.thread.wait(kData->engine->getOptions().oscUiTimeout))
  132. {
  133. carla_stderr("Failed to properly stop Plugin Bridge thread");
  134. kData->osc.thread.terminate();
  135. }
  136. cleanup();
  137. clearBuffers();
  138. //info.chunk.clear();
  139. }
  140. // -------------------------------------------------------------------
  141. // Information (base)
  142. BinaryType binaryType() const
  143. {
  144. return fBinaryType;
  145. }
  146. PluginType type() const override
  147. {
  148. return fPluginType;
  149. }
  150. PluginCategory category() override
  151. {
  152. return fInfo.category;
  153. }
  154. long uniqueId() const override
  155. {
  156. return fInfo.uniqueId;
  157. }
  158. // -------------------------------------------------------------------
  159. // Information (count)
  160. uint32_t midiInCount() const override
  161. {
  162. return fInfo.mIns;
  163. }
  164. uint32_t midiOutCount() const override
  165. {
  166. return fInfo.mOuts;
  167. }
  168. // -------------------------------------------------------------------
  169. // Information (current data)
  170. int32_t chunkData(void** const dataPtr) override
  171. {
  172. CARLA_ASSERT(fOptions & PLUGIN_OPTION_USE_CHUNKS);
  173. CARLA_ASSERT(dataPtr != nullptr);
  174. #if 0
  175. if (! info.chunk.isEmpty())
  176. {
  177. *dataPtr = info.chunk.data();
  178. return info.chunk.size();
  179. }
  180. #endif
  181. return 0;
  182. }
  183. // -------------------------------------------------------------------
  184. // Information (per-plugin data)
  185. unsigned int availableOptions() override
  186. {
  187. unsigned int options = 0x0;
  188. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  189. options |= PLUGIN_OPTION_USE_CHUNKS;
  190. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  191. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  192. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  193. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  194. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  195. return options;
  196. }
  197. float getParameterValue(const uint32_t parameterId) override
  198. {
  199. CARLA_ASSERT(parameterId < kData->param.count);
  200. return fParams[parameterId].value;
  201. }
  202. void getLabel(char* const strBuf) override
  203. {
  204. std::strncpy(strBuf, (const char*)fInfo.label, STR_MAX);
  205. }
  206. void getMaker(char* const strBuf) override
  207. {
  208. std::strncpy(strBuf, (const char*)fInfo.maker, STR_MAX);
  209. }
  210. void getCopyright(char* const strBuf) override
  211. {
  212. std::strncpy(strBuf, (const char*)fInfo.copyright, STR_MAX);
  213. }
  214. void getRealName(char* const strBuf) override
  215. {
  216. std::strncpy(strBuf, (const char*)fInfo.name, STR_MAX);
  217. }
  218. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  219. {
  220. CARLA_ASSERT(parameterId < kData->param.count);
  221. std::strncpy(strBuf, (const char*)fParams[parameterId].name, STR_MAX);
  222. }
  223. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  224. {
  225. CARLA_ASSERT(parameterId < kData->param.count);
  226. std::strncpy(strBuf, (const char*)fParams[parameterId].unit, STR_MAX);
  227. }
  228. // -------------------------------------------------------------------
  229. // Set data (state)
  230. void prepareForSave() override
  231. {
  232. #if 0
  233. m_saved = false;
  234. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SAVE_NOW, "");
  235. for (int i=0; i < 200; ++i)
  236. {
  237. if (m_saved)
  238. break;
  239. carla_msleep(50);
  240. }
  241. if (! m_saved)
  242. carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
  243. else
  244. carla_debug("BridgePlugin::prepareForSave() - success!");
  245. #endif
  246. }
  247. // -------------------------------------------------------------------
  248. // Set data (internal stuff)
  249. // nothing
  250. // -------------------------------------------------------------------
  251. // Set data (plugin-specific stuff)
  252. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  253. {
  254. CARLA_ASSERT(parameterId < kData->param.count);
  255. const float fixedValue(kData->param.fixValue(parameterId, value));
  256. fParams[parameterId].value = fixedValue;
  257. const bool doLock(sendGui || sendOsc || sendCallback);
  258. if (doLock)
  259. kData->singleMutex.lock();
  260. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetParameter);
  261. rdwr_writeInt(&fShmControl.data->ringBuffer, parameterId);
  262. rdwr_writeFloat(&fShmControl.data->ringBuffer, value);
  263. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  264. if (doLock)
  265. kData->singleMutex.unlock();
  266. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  267. }
  268. void setProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  269. {
  270. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->prog.count));
  271. if (index < -1)
  272. index = -1;
  273. else if (index > static_cast<int32_t>(kData->prog.count))
  274. return;
  275. const bool doLock(sendGui || sendOsc || sendCallback);
  276. if (doLock)
  277. kData->singleMutex.lock();
  278. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetProgram);
  279. rdwr_writeInt(&fShmControl.data->ringBuffer, index);
  280. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  281. if (doLock)
  282. kData->singleMutex.unlock();
  283. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  284. }
  285. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  286. {
  287. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  288. if (index < -1)
  289. index = -1;
  290. else if (index > static_cast<int32_t>(kData->midiprog.count))
  291. return;
  292. const bool doLock(sendGui || sendOsc || sendCallback);
  293. if (doLock)
  294. kData->singleMutex.lock();
  295. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetMidiProgram);
  296. rdwr_writeInt(&fShmControl.data->ringBuffer, index);
  297. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  298. if (doLock)
  299. kData->singleMutex.unlock();
  300. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  301. }
  302. #if 0
  303. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  304. {
  305. CARLA_ASSERT(type);
  306. CARLA_ASSERT(key);
  307. CARLA_ASSERT(value);
  308. if (sendGui)
  309. {
  310. // TODO - if type is chunk|binary, store it in a file and send path instead
  311. QString cData;
  312. cData = type;
  313. cData += "·";
  314. cData += key;
  315. cData += "·";
  316. cData += value;
  317. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  318. }
  319. CarlaPlugin::setCustomData(type, key, value, sendGui);
  320. }
  321. void setChunkData(const char* const stringData) override
  322. {
  323. CARLA_ASSERT(m_hints & PLUGIN_USES_CHUNKS);
  324. CARLA_ASSERT(stringData);
  325. QString filePath;
  326. filePath = QDir::tempPath();
  327. filePath += "/.CarlaChunk_";
  328. filePath += m_name;
  329. filePath = QDir::toNativeSeparators(filePath);
  330. QFile file(filePath);
  331. if (file.open(QIODevice::WriteOnly | QIODevice::Text))
  332. {
  333. QTextStream out(&file);
  334. out << stringData;
  335. file.close();
  336. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CHUNK, filePath.toUtf8().constData());
  337. }
  338. }
  339. #endif
  340. // -------------------------------------------------------------------
  341. // Set gui stuff
  342. void showGui(const bool yesNo) override
  343. {
  344. if (yesNo)
  345. osc_send_show(&kData->osc.data);
  346. else
  347. osc_send_hide(&kData->osc.data);
  348. }
  349. void idleGui() override
  350. {
  351. if (! kData->osc.thread.isRunning())
  352. carla_stderr2("TESTING: Bridge has closed!");
  353. CarlaPlugin::idleGui();
  354. }
  355. // -------------------------------------------------------------------
  356. // Plugin state
  357. void reload() override
  358. {
  359. carla_debug("BridgePlugin::reload() - start");
  360. CARLA_ASSERT(kData->engine != nullptr);
  361. if (kData->engine == nullptr)
  362. return;
  363. const ProcessMode processMode(kData->engine->getProccessMode());
  364. // Safely disable plugin for reload
  365. const ScopedDisabler sd(this);
  366. bool needsCtrlIn, needsCtrlOut;
  367. needsCtrlIn = needsCtrlOut = false;
  368. if (fInfo.aIns > 0)
  369. {
  370. kData->audioIn.createNew(fInfo.aIns);
  371. }
  372. if (fInfo.aOuts > 0)
  373. {
  374. kData->audioOut.createNew(fInfo.aOuts);
  375. needsCtrlIn = true;
  376. }
  377. if (fInfo.mIns > 0)
  378. needsCtrlIn = true;
  379. if (fInfo.mOuts > 0)
  380. needsCtrlOut = true;
  381. const uint portNameSize(kData->engine->maxPortNameSize());
  382. CarlaString portName;
  383. // Audio Ins
  384. for (uint32_t j=0; j < fInfo.aIns; ++j)
  385. {
  386. portName.clear();
  387. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  388. {
  389. portName = fName;
  390. portName += ":";
  391. }
  392. if (fInfo.aIns > 1)
  393. {
  394. portName += "input_";
  395. portName += CarlaString(j+1);
  396. }
  397. else
  398. portName += "input";
  399. portName.truncate(portNameSize);
  400. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  401. kData->audioIn.ports[j].rindex = j;
  402. }
  403. // Audio Outs
  404. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  405. {
  406. portName.clear();
  407. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  408. {
  409. portName = fName;
  410. portName += ":";
  411. }
  412. if (fInfo.aOuts > 1)
  413. {
  414. portName += "output_";
  415. portName += CarlaString(j+1);
  416. }
  417. else
  418. portName += "output";
  419. portName.truncate(portNameSize);
  420. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  421. kData->audioOut.ports[j].rindex = j;
  422. }
  423. if (needsCtrlIn)
  424. {
  425. portName.clear();
  426. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  427. {
  428. portName = fName;
  429. portName += ":";
  430. }
  431. portName += "event-in";
  432. portName.truncate(portNameSize);
  433. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  434. }
  435. if (needsCtrlOut)
  436. {
  437. portName.clear();
  438. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  439. {
  440. portName = fName;
  441. portName += ":";
  442. }
  443. portName += "event-out";
  444. portName.truncate(portNameSize);
  445. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  446. }
  447. bufferSizeChanged(kData->engine->getBufferSize());
  448. reloadPrograms(true);
  449. carla_debug("BridgePlugin::reload() - end");
  450. }
  451. // -------------------------------------------------------------------
  452. // Plugin processing
  453. void activate() override
  454. {
  455. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetParameter);
  456. rdwr_writeInt(&fShmControl.data->ringBuffer, PARAMETER_ACTIVE);
  457. rdwr_writeFloat(&fShmControl.data->ringBuffer, 1.0f);
  458. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  459. waitForServer();
  460. }
  461. void deactivate() override
  462. {
  463. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetParameter);
  464. rdwr_writeInt(&fShmControl.data->ringBuffer, PARAMETER_ACTIVE);
  465. rdwr_writeFloat(&fShmControl.data->ringBuffer, 0.0f);
  466. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  467. waitForServer();
  468. }
  469. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  470. {
  471. uint32_t i/*, k*/;
  472. // --------------------------------------------------------------------------------------------------------
  473. // Check if active
  474. if (! kData->active)
  475. {
  476. // disable any output sound
  477. for (i=0; i < kData->audioOut.count; ++i)
  478. carla_zeroFloat(outBuffer[i], frames);
  479. return;
  480. }
  481. // --------------------------------------------------------------------------------------------------------
  482. // Check if needs reset
  483. if (kData->needsReset)
  484. {
  485. // TODO
  486. kData->needsReset = false;
  487. }
  488. // --------------------------------------------------------------------------------------------------------
  489. // Plugin processing (no events)
  490. //else
  491. {
  492. processSingle(inBuffer, outBuffer, frames);
  493. } // End of Plugin processing (no events)
  494. }
  495. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  496. {
  497. CARLA_ASSERT(frames > 0);
  498. if (frames == 0)
  499. return false;
  500. if (kData->audioIn.count > 0)
  501. {
  502. CARLA_ASSERT(inBuffer != nullptr);
  503. if (inBuffer == nullptr)
  504. return false;
  505. }
  506. if (kData->audioOut.count > 0)
  507. {
  508. CARLA_ASSERT(outBuffer != nullptr);
  509. if (outBuffer == nullptr)
  510. return false;
  511. }
  512. uint32_t i, k;
  513. // --------------------------------------------------------------------------------------------------------
  514. // Try lock, silence otherwise
  515. if (kData->engine->isOffline())
  516. {
  517. kData->singleMutex.lock();
  518. }
  519. else if (! kData->singleMutex.tryLock())
  520. {
  521. for (i=0; i < kData->audioOut.count; ++i)
  522. carla_zeroFloat(outBuffer[i], frames);
  523. return false;
  524. }
  525. // --------------------------------------------------------------------------------------------------------
  526. // Reset audio buffers
  527. for (i=0; i < fInfo.aIns; ++i)
  528. carla_copyFloat(fShmAudioPool.data + (i * frames), inBuffer[i], frames);
  529. // --------------------------------------------------------------------------------------------------------
  530. // Run plugin
  531. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeProcess);
  532. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  533. waitForServer();
  534. for (i=0; i < fInfo.aOuts; ++i)
  535. carla_copyFloat(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), frames);
  536. // --------------------------------------------------------------------------------------------------------
  537. // Post-processing (dry/wet, volume and balance)
  538. {
  539. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) != 0 && kData->postProc.volume != 1.0f;
  540. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  541. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  542. bool isPair;
  543. float bufValue, oldBufLeft[doBalance ? frames : 1];
  544. for (i=0; i < kData->audioOut.count; ++i)
  545. {
  546. // Dry/Wet
  547. if (doDryWet)
  548. {
  549. for (k=0; k < frames; ++k)
  550. {
  551. bufValue = inBuffer[(kData->audioIn.count == 1) ? 0 : i][k];
  552. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  553. }
  554. }
  555. // Balance
  556. if (doBalance)
  557. {
  558. isPair = (i % 2 == 0);
  559. if (isPair)
  560. {
  561. CARLA_ASSERT(i+1 < kData->audioOut.count);
  562. carla_copyFloat(oldBufLeft, outBuffer[i], frames);
  563. }
  564. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  565. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  566. for (k=0; k < frames; ++k)
  567. {
  568. if (isPair)
  569. {
  570. // left
  571. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  572. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  573. }
  574. else
  575. {
  576. // right
  577. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  578. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  579. }
  580. }
  581. }
  582. // Volume
  583. if (doVolume)
  584. {
  585. for (k=0; k < frames; ++k)
  586. outBuffer[i][k] *= kData->postProc.volume;
  587. }
  588. }
  589. } // End of Post-processing
  590. // --------------------------------------------------------------------------------------------------------
  591. kData->singleMutex.unlock();
  592. return true;
  593. }
  594. void bufferSizeChanged(const uint32_t newBufferSize) override
  595. {
  596. resizeAudioPool(newBufferSize);
  597. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetBufferSize);
  598. rdwr_writeInt(&fShmControl.data->ringBuffer, newBufferSize);
  599. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  600. }
  601. void sampleRateChanged(const double newSampleRate) override
  602. {
  603. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetSampleRate);
  604. rdwr_writeFloat(&fShmControl.data->ringBuffer, newSampleRate);
  605. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  606. }
  607. // -------------------------------------------------------------------
  608. // Plugin buffers
  609. void clearBuffers() override
  610. {
  611. if (fParams != nullptr)
  612. {
  613. delete[] fParams;
  614. fParams = nullptr;
  615. }
  616. CarlaPlugin::clearBuffers();
  617. }
  618. // -------------------------------------------------------------------
  619. // Post-poned UI Stuff
  620. // nothing
  621. // -------------------------------------------------------------------
  622. int setOscPluginBridgeInfo(const PluginBridgeInfoType type, const int argc, const lo_arg* const* const argv, const char* const types)
  623. {
  624. carla_debug("setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeInfoType2str(type), argc, argv, types);
  625. switch (type)
  626. {
  627. case kPluginBridgeAudioCount:
  628. {
  629. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  630. const int32_t aIns = argv[0]->i;
  631. const int32_t aOuts = argv[1]->i;
  632. const int32_t aTotal = argv[2]->i;
  633. CARLA_ASSERT(aIns >= 0);
  634. CARLA_ASSERT(aOuts >= 0);
  635. CARLA_ASSERT(aIns + aOuts == aTotal);
  636. fInfo.aIns = aIns;
  637. fInfo.aOuts = aOuts;
  638. break;
  639. // unused
  640. (void)aTotal;
  641. }
  642. case kPluginBridgeMidiCount:
  643. {
  644. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  645. const int32_t mIns = argv[0]->i;
  646. const int32_t mOuts = argv[1]->i;
  647. const int32_t mTotal = argv[2]->i;
  648. CARLA_ASSERT(mIns >= 0);
  649. CARLA_ASSERT(mOuts >= 0);
  650. CARLA_ASSERT(mIns + mOuts == mTotal);
  651. fInfo.mIns = mIns;
  652. fInfo.mOuts = mOuts;
  653. break;
  654. // unused
  655. (void)mTotal;
  656. }
  657. case kPluginBridgeParameterCount:
  658. {
  659. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iii");
  660. const int32_t pIns = argv[0]->i;
  661. const int32_t pOuts = argv[1]->i;
  662. const int32_t pTotal = argv[2]->i;
  663. CARLA_ASSERT(pIns >= 0);
  664. CARLA_ASSERT(pOuts >= 0);
  665. CARLA_ASSERT(pIns + pOuts <= pTotal);
  666. // delete old data
  667. kData->param.clear();
  668. if (fParams != nullptr)
  669. {
  670. delete[] fParams;
  671. fParams = nullptr;
  672. }
  673. CARLA_SAFE_ASSERT_INT2(pTotal < static_cast<int32_t>(kData->engine->getOptions().maxParameters), pTotal, kData->engine->getOptions().maxParameters);
  674. const int32_t count(carla_min<int32_t>(pTotal, kData->engine->getOptions().maxParameters, 0));
  675. if (count > 0)
  676. {
  677. kData->param.createNew(count);
  678. fParams = new BridgeParamInfo[count];
  679. }
  680. break;
  681. // unused
  682. (void)pIns;
  683. (void)pOuts;
  684. }
  685. case kPluginBridgeProgramCount:
  686. {
  687. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  688. const int32_t count = argv[0]->i;
  689. CARLA_ASSERT(count >= 0);
  690. kData->prog.clear();
  691. if (count > 0)
  692. kData->prog.createNew(count);
  693. break;
  694. }
  695. case kPluginBridgeMidiProgramCount:
  696. {
  697. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  698. const int32_t count = argv[0]->i;
  699. CARLA_ASSERT(count >= 0);
  700. kData->midiprog.clear();
  701. if (count > 0)
  702. kData->midiprog.createNew(count);
  703. break;
  704. }
  705. case kPluginBridgePluginInfo:
  706. {
  707. CARLA_BRIDGE_CHECK_OSC_TYPES(7, "iissssh");
  708. const int32_t category = argv[0]->i;
  709. const int32_t hints = argv[1]->i;
  710. const char* const name = (const char*)&argv[2]->s;
  711. const char* const label = (const char*)&argv[3]->s;
  712. const char* const maker = (const char*)&argv[4]->s;
  713. const char* const copyright = (const char*)&argv[5]->s;
  714. const int64_t uniqueId = argv[6]->h;
  715. CARLA_ASSERT(category >= 0);
  716. CARLA_ASSERT(hints >= 0);
  717. CARLA_ASSERT(name != nullptr);
  718. CARLA_ASSERT(label != nullptr);
  719. CARLA_ASSERT(maker != nullptr);
  720. CARLA_ASSERT(copyright != nullptr);
  721. fHints = hints | PLUGIN_IS_BRIDGE;
  722. fInfo.category = static_cast<PluginCategory>(category);
  723. fInfo.uniqueId = uniqueId;
  724. fInfo.name = name;
  725. fInfo.label = label;
  726. fInfo.maker = maker;
  727. fInfo.copyright = copyright;
  728. if (fName.isEmpty())
  729. fName = name;
  730. break;
  731. }
  732. case kPluginBridgeParameterInfo:
  733. {
  734. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  735. const int32_t index = argv[0]->i;
  736. const char* const name = (const char*)&argv[1]->s;
  737. const char* const unit = (const char*)&argv[2]->s;
  738. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->param.count), index, kData->param.count);
  739. CARLA_ASSERT(name != nullptr);
  740. CARLA_ASSERT(unit != nullptr);
  741. if (index >= 0 && static_cast<int32_t>(kData->param.count))
  742. {
  743. fParams[index].name = name;
  744. fParams[index].unit = unit;
  745. }
  746. break;
  747. }
  748. case kPluginBridgeParameterData:
  749. {
  750. CARLA_BRIDGE_CHECK_OSC_TYPES(6, "iiiiii");
  751. const int32_t index = argv[0]->i;
  752. const int32_t type = argv[1]->i;
  753. const int32_t rindex = argv[2]->i;
  754. const int32_t hints = argv[3]->i;
  755. const int32_t channel = argv[4]->i;
  756. const int32_t cc = argv[5]->i;
  757. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->param.count), index, kData->param.count);
  758. CARLA_ASSERT(type >= 0);
  759. CARLA_ASSERT(rindex >= 0);
  760. CARLA_ASSERT(hints >= 0);
  761. CARLA_ASSERT(channel >= 0 && channel < 16);
  762. CARLA_ASSERT(cc >= -1);
  763. if (index >= 0 && static_cast<int32_t>(kData->param.count))
  764. {
  765. kData->param.data[index].type = static_cast<ParameterType>(type);
  766. kData->param.data[index].index = index;
  767. kData->param.data[index].rindex = rindex;
  768. kData->param.data[index].hints = hints;
  769. kData->param.data[index].midiChannel = channel;
  770. kData->param.data[index].midiCC = cc;
  771. }
  772. break;
  773. }
  774. case kPluginBridgeParameterRanges:
  775. {
  776. CARLA_BRIDGE_CHECK_OSC_TYPES(7, "iffffff");
  777. const int32_t index = argv[0]->i;
  778. const float def = argv[1]->f;
  779. const float min = argv[2]->f;
  780. const float max = argv[3]->f;
  781. const float step = argv[4]->f;
  782. const float stepSmall = argv[5]->f;
  783. const float stepLarge = argv[6]->f;
  784. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->param.count), index, kData->param.count);
  785. CARLA_ASSERT(min < max);
  786. CARLA_ASSERT(def >= min);
  787. CARLA_ASSERT(def <= max);
  788. if (index >= 0 && static_cast<int32_t>(kData->param.count))
  789. {
  790. kData->param.ranges[index].def = def;
  791. kData->param.ranges[index].min = min;
  792. kData->param.ranges[index].max = max;
  793. kData->param.ranges[index].step = step;
  794. kData->param.ranges[index].stepSmall = stepSmall;
  795. kData->param.ranges[index].stepLarge = stepLarge;
  796. }
  797. break;
  798. }
  799. case kPluginBridgeProgramInfo:
  800. {
  801. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  802. const int32_t index = argv[0]->i;
  803. const char* const name = (const char*)&argv[1]->s;
  804. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->prog.count), index, kData->prog.count);
  805. CARLA_ASSERT(name != nullptr);
  806. if (index >= 0 && index < static_cast<int32_t>(kData->prog.count))
  807. {
  808. if (kData->prog.names[index] != nullptr)
  809. delete[] kData->prog.names[index];
  810. kData->prog.names[index] = carla_strdup(name);
  811. }
  812. break;
  813. }
  814. case kPluginBridgeMidiProgramInfo:
  815. {
  816. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  817. const int32_t index = argv[0]->i;
  818. const int32_t bank = argv[1]->i;
  819. const int32_t program = argv[2]->i;
  820. const char* const name = (const char*)&argv[3]->s;
  821. CARLA_ASSERT_INT2(index < static_cast<int32_t>(kData->midiprog.count), index, kData->midiprog.count);
  822. CARLA_ASSERT(bank >= 0);
  823. CARLA_ASSERT(program >= 0 && program < 128);
  824. CARLA_ASSERT(name != nullptr);
  825. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count))
  826. {
  827. if (kData->midiprog.data[index].name != nullptr)
  828. delete[] kData->midiprog.data[index].name;
  829. kData->midiprog.data[index].bank = bank;
  830. kData->midiprog.data[index].program = program;
  831. kData->midiprog.data[index].name = carla_strdup(name);
  832. }
  833. break;
  834. }
  835. case kPluginBridgeConfigure:
  836. {
  837. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  838. const char* const key = (const char*)&argv[0]->s;
  839. const char* const value = (const char*)&argv[1]->s;
  840. CARLA_ASSERT(key != nullptr);
  841. CARLA_ASSERT(value != nullptr);
  842. if (key == nullptr || value == nullptr)
  843. break;
  844. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  845. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  846. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  847. fSaved = true;
  848. break;
  849. }
  850. case kPluginBridgeSetParameterValue:
  851. {
  852. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  853. const int32_t index = argv[0]->i;
  854. const float value = argv[1]->f;
  855. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->param.count), index, kData->param.count);
  856. if (index >= 0 && static_cast<int32_t>(kData->param.count))
  857. {
  858. const float fixedValue(kData->param.fixValue(index, value));
  859. fParams[index].value = fixedValue;
  860. CarlaPlugin::setParameterValue(index, fixedValue, false, true, true);
  861. }
  862. break;
  863. }
  864. case kPluginBridgeSetDefaultValue:
  865. {
  866. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  867. const int32_t index = argv[0]->i;
  868. const float value = argv[1]->f;
  869. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->param.count), index, kData->param.count);
  870. if (index >= 0 && static_cast<int32_t>(kData->param.count))
  871. kData->param.ranges[index].def = value;
  872. break;
  873. }
  874. case kPluginBridgeSetProgram:
  875. {
  876. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  877. const int32_t index = argv[0]->i;
  878. CARLA_ASSERT_INT2(index >= 0 && index < static_cast<int32_t>(kData->prog.count), index, kData->prog.count);
  879. setProgram(index, false, true, true);
  880. break;
  881. }
  882. case kPluginBridgeSetMidiProgram:
  883. {
  884. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  885. const int32_t index = argv[0]->i;
  886. CARLA_ASSERT_INT2(index < static_cast<int32_t>(kData->midiprog.count), index, kData->midiprog.count);
  887. setMidiProgram(index, false, true, true);
  888. break;
  889. }
  890. case kPluginBridgeSetCustomData:
  891. {
  892. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  893. const char* const type = (const char*)&argv[0]->s;
  894. const char* const key = (const char*)&argv[1]->s;
  895. const char* const value = (const char*)&argv[2]->s;
  896. CARLA_ASSERT(type != nullptr);
  897. CARLA_ASSERT(key != nullptr);
  898. CARLA_ASSERT(value != nullptr);
  899. setCustomData(type, key, value, false);
  900. break;
  901. }
  902. case kPluginBridgeSetChunkData:
  903. {
  904. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  905. #if 0
  906. const char* const chunkFileChar = (const char*)&argv[0]->s;
  907. CARLA_ASSERT(chunkFileChar);
  908. QString chunkFileStr(chunkFileChar);
  909. #ifndef Q_OS_WIN
  910. // Using Wine, fix temp dir
  911. if (m_binary == BINARY_WIN32 || m_binary == BINARY_WIN64)
  912. {
  913. // Get WINEPREFIX
  914. QString wineDir;
  915. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  916. wineDir = QString(WINEPREFIX);
  917. else
  918. wineDir = QDir::homePath() + "/.wine";
  919. QStringList chunkFileStrSplit1 = chunkFileStr.split(":/");
  920. QStringList chunkFileStrSplit2 = chunkFileStrSplit1.at(1).split("\\");
  921. QString wineDrive = chunkFileStrSplit1.at(0).toLower();
  922. QString wineTMP = chunkFileStrSplit2.at(0);
  923. QString baseName = chunkFileStrSplit2.at(1);
  924. chunkFileStr = wineDir;
  925. chunkFileStr += "/drive_";
  926. chunkFileStr += wineDrive;
  927. chunkFileStr += "/";
  928. chunkFileStr += wineTMP;
  929. chunkFileStr += "/";
  930. chunkFileStr += baseName;
  931. chunkFileStr = QDir::toNativeSeparators(chunkFileStr);
  932. }
  933. #endif
  934. QFile chunkFile(chunkFileStr);
  935. if (chunkFile.open(QIODevice::ReadOnly))
  936. {
  937. info.chunk = chunkFile.readAll();
  938. chunkFile.close();
  939. chunkFile.remove();
  940. }
  941. #endif
  942. break;
  943. }
  944. case kPluginBridgeUpdateNow:
  945. {
  946. fInitiated = true;
  947. break;
  948. }
  949. case kPluginBridgeError:
  950. {
  951. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  952. const char* const error = (const char*)&argv[0]->s;
  953. CARLA_ASSERT(error != nullptr);
  954. kData->engine->setLastError(error);
  955. fInitError = true;
  956. fInitiated = true;
  957. break;
  958. }
  959. }
  960. return 0;
  961. }
  962. // -------------------------------------------------------------------
  963. const void* getExtraStuff() override
  964. {
  965. return fBridgeBinary.isNotEmpty() ? (const char*)fBridgeBinary : nullptr;
  966. }
  967. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  968. {
  969. CARLA_ASSERT(kData->engine != nullptr);
  970. CARLA_ASSERT(kData->client == nullptr);
  971. // ---------------------------------------------------------------
  972. // first checks
  973. if (kData->engine == nullptr)
  974. {
  975. return false;
  976. }
  977. if (kData->client != nullptr)
  978. {
  979. kData->engine->setLastError("Plugin client is already registered");
  980. return false;
  981. }
  982. // ---------------------------------------------------------------
  983. // set info
  984. if (name != nullptr)
  985. fName = kData->engine->getUniquePluginName(name);
  986. fFilename = filename;
  987. fBridgeBinary = bridgeBinary;
  988. // ---------------------------------------------------------------
  989. // SHM Audio Pool
  990. {
  991. char tmpFileBase[60];
  992. std::srand(std::time(NULL));
  993. std::sprintf(tmpFileBase, "/carla-bridge_shm_XXXXXX");
  994. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  995. if (! carla_is_shm_valid(fShmAudioPool.shm))
  996. {
  997. //_cleanup();
  998. carla_stdout("Failed to open or create shared memory file #1");
  999. return false;
  1000. }
  1001. fShmAudioPool.filename = tmpFileBase;
  1002. }
  1003. // ---------------------------------------------------------------
  1004. // SHM Control
  1005. {
  1006. char tmpFileBase[60];
  1007. std::sprintf(tmpFileBase, "/carla-bridge_shc_XXXXXX");
  1008. fShmControl.shm = shm_mkstemp(tmpFileBase);
  1009. if (! carla_is_shm_valid(fShmControl.shm))
  1010. {
  1011. //_cleanup();
  1012. carla_stdout("Failed to open or create shared memory file #2");
  1013. return false;
  1014. }
  1015. fShmControl.filename = tmpFileBase;
  1016. if (! carla_shm_map<BridgeShmControl>(fShmControl.shm, fShmControl.data))
  1017. {
  1018. //_cleanup();
  1019. carla_stdout("Failed to mmap shared memory file");
  1020. return false;
  1021. }
  1022. CARLA_ASSERT(fShmControl.data != nullptr);
  1023. std::memset(fShmControl.data, 0, sizeof(BridgeShmControl));
  1024. std::strcpy(fShmControl.data->ringBuffer.buf, "This thing is actually working!!!!");
  1025. if (sem_init(&fShmControl.data->runServer, 1, 0) != 0)
  1026. {
  1027. //_cleanup();
  1028. carla_stdout("Failed to initialize shared memory semaphore #1");
  1029. return false;
  1030. }
  1031. if (sem_init(&fShmControl.data->runClient, 1, 0) != 0)
  1032. {
  1033. //_cleanup();
  1034. carla_stdout("Failed to initialize shared memory semaphore #2");
  1035. return false;
  1036. }
  1037. }
  1038. // initial values
  1039. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetBufferSize);
  1040. rdwr_writeInt(&fShmControl.data->ringBuffer, kData->engine->getBufferSize());
  1041. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeSetSampleRate);
  1042. rdwr_writeFloat(&fShmControl.data->ringBuffer, kData->engine->getSampleRate());
  1043. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  1044. // register plugin now so we can receive OSC (and wait for it)
  1045. fHints |= PLUGIN_IS_BRIDGE;
  1046. registerEnginePlugin(kData->engine, fId, this);
  1047. // init OSC
  1048. {
  1049. char shmIdStr[12+1] = { 0 };
  1050. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1051. std::strncat(shmIdStr, &fShmControl.filename[fShmControl.filename.length()-6], 6);
  1052. kData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1053. kData->osc.thread.start();
  1054. }
  1055. for (int i=0; i < 200; ++i)
  1056. {
  1057. if (fInitiated || ! kData->osc.thread.isRunning())
  1058. break;
  1059. carla_msleep(50);
  1060. }
  1061. if (! fInitiated)
  1062. {
  1063. // unregister so it gets handled properly
  1064. registerEnginePlugin(kData->engine, fId, nullptr);
  1065. kData->osc.thread.quit();
  1066. if (kData->osc.thread.isRunning())
  1067. kData->osc.thread.terminate();
  1068. kData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1069. return false;
  1070. }
  1071. else if (fInitError)
  1072. {
  1073. // unregister so it gets handled properly
  1074. registerEnginePlugin(kData->engine, fId, nullptr);
  1075. kData->osc.thread.quit();
  1076. if (kData->osc.thread.isRunning())
  1077. kData->osc.thread.terminate();
  1078. // last error was set before
  1079. return false;
  1080. }
  1081. // ---------------------------------------------------------------
  1082. // register client
  1083. kData->client = kData->engine->addClient(this);
  1084. if (kData->client == nullptr || ! kData->client->isOk())
  1085. {
  1086. kData->engine->setLastError("Failed to register plugin client");
  1087. return false;
  1088. }
  1089. return true;
  1090. }
  1091. private:
  1092. const BinaryType fBinaryType;
  1093. const PluginType fPluginType;
  1094. bool fInitiated;
  1095. bool fInitError;
  1096. bool fSaved;
  1097. CarlaString fBridgeBinary;
  1098. struct BridgeAudioPool {
  1099. CarlaString filename;
  1100. float* data;
  1101. size_t size;
  1102. shm_t shm;
  1103. BridgeAudioPool()
  1104. : data(nullptr),
  1105. size(0)
  1106. {
  1107. carla_shm_init(shm);
  1108. }
  1109. } fShmAudioPool;
  1110. struct BridgeControl {
  1111. CarlaString filename;
  1112. BridgeShmControl* data;
  1113. shm_t shm;
  1114. BridgeControl()
  1115. : data(nullptr)
  1116. {
  1117. carla_shm_init(shm);
  1118. }
  1119. } fShmControl;
  1120. struct Info {
  1121. uint32_t aIns, aOuts;
  1122. uint32_t mIns, mOuts;
  1123. PluginCategory category;
  1124. long uniqueId;
  1125. CarlaString name;
  1126. CarlaString label;
  1127. CarlaString maker;
  1128. CarlaString copyright;
  1129. //QByteArray chunk;
  1130. Info()
  1131. : aIns(0),
  1132. aOuts(0),
  1133. mIns(0),
  1134. mOuts(0),
  1135. category(PLUGIN_CATEGORY_NONE),
  1136. uniqueId(0) {}
  1137. } fInfo;
  1138. BridgeParamInfo* fParams;
  1139. void cleanup()
  1140. {
  1141. if (fShmAudioPool.filename.isNotEmpty())
  1142. fShmAudioPool.filename.clear();
  1143. if (fShmControl.filename.isNotEmpty())
  1144. fShmControl.filename.clear();
  1145. if (fShmAudioPool.data != nullptr)
  1146. {
  1147. carla_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data, fShmAudioPool.size);
  1148. fShmAudioPool.data = nullptr;
  1149. }
  1150. fShmAudioPool.size = 0;
  1151. if (fShmControl.data != nullptr)
  1152. {
  1153. carla_shm_unmap(fShmControl.shm, fShmControl.data, sizeof(BridgeShmControl));
  1154. fShmControl.data = nullptr;
  1155. }
  1156. if (carla_is_shm_valid(fShmAudioPool.shm))
  1157. carla_shm_close(fShmAudioPool.shm);
  1158. if (carla_is_shm_valid(fShmControl.shm))
  1159. carla_shm_close(fShmControl.shm);
  1160. }
  1161. void resizeAudioPool(uint32_t bufferSize)
  1162. {
  1163. if (fShmAudioPool.data != nullptr)
  1164. carla_shm_unmap(fShmAudioPool.shm, fShmAudioPool.data, fShmAudioPool.size);
  1165. fShmAudioPool.size = (fInfo.aIns+fInfo.aOuts)*bufferSize*sizeof(float);
  1166. if (fShmAudioPool.size == 0)
  1167. fShmAudioPool.size = sizeof(float);
  1168. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, fShmAudioPool.size);
  1169. rdwr_writeOpcode(&fShmControl.data->ringBuffer, kPluginBridgeOpcodeReadyWait);
  1170. rdwr_writeInt(&fShmControl.data->ringBuffer, fShmAudioPool.size);
  1171. rdwr_commitWrite(&fShmControl.data->ringBuffer);
  1172. waitForServer();
  1173. }
  1174. void waitForServer()
  1175. {
  1176. sem_post(&fShmControl.data->runServer);
  1177. if (! jackbridge_sem_timedwait(&fShmControl.data->runClient, 5))
  1178. {
  1179. carla_stderr("waitForServer() timeout");
  1180. kData->active = false; // TODO
  1181. }
  1182. }
  1183. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1184. };
  1185. CARLA_BACKEND_END_NAMESPACE
  1186. #endif // ! BUILD_BRIDGE
  1187. CARLA_BACKEND_START_NAMESPACE
  1188. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1189. {
  1190. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1191. #ifndef BUILD_BRIDGE
  1192. if (bridgeBinary == nullptr)
  1193. {
  1194. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1195. return nullptr;
  1196. }
  1197. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1198. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1199. {
  1200. delete plugin;
  1201. return nullptr;
  1202. }
  1203. plugin->reload();
  1204. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1205. {
  1206. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1207. delete plugin;
  1208. return nullptr;
  1209. }
  1210. return plugin;
  1211. #else
  1212. init.engine->setLastError("Plugin bridge support not available");
  1213. return nullptr;
  1214. // unused
  1215. (void)bridgeBinary;
  1216. #endif
  1217. }
  1218. #ifndef BUILD_BRIDGE
  1219. // -------------------------------------------------------------------
  1220. // Bridge Helper
  1221. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeInfoType type,
  1222. const int argc, const lo_arg* const* const argv, const char* const types)
  1223. {
  1224. CARLA_ASSERT(plugin != nullptr && (plugin->hints() & PLUGIN_IS_BRIDGE) != 0);
  1225. return ((BridgePlugin*)plugin)->setOscPluginBridgeInfo(type, argc, argv, types);
  1226. }
  1227. BinaryType CarlaPluginGetBridgeBinaryType(CarlaPlugin* const plugin)
  1228. {
  1229. CARLA_ASSERT(plugin != nullptr && (plugin->hints() & PLUGIN_IS_BRIDGE) != 0);
  1230. return ((BridgePlugin*)plugin)->binaryType();
  1231. }
  1232. #endif
  1233. CARLA_BACKEND_END_NAMESPACE