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.

BridgePlugin.cpp 75KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2011-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. #ifndef BUILD_BRIDGE
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaBase64Utils.hpp"
  22. #include "CarlaBridgeUtils.hpp"
  23. #include "CarlaMathUtils.hpp"
  24. #include "CarlaShmUtils.hpp"
  25. #include "jackbridge/JackBridge.hpp"
  26. #include <cerrno>
  27. #include <cmath>
  28. #include <ctime>
  29. #define CARLA_BRIDGE_CHECK_OSC_TYPES(/* argc, types, */ argcToCompare, typesToCompare) \
  30. /* check argument count */ \
  31. if (argc != argcToCompare) \
  32. { \
  33. carla_stderr("BridgePlugin::%s() - argument count mismatch: %i != %i", __FUNCTION__, argc, argcToCompare); \
  34. return 1; \
  35. } \
  36. if (argc > 0) \
  37. { \
  38. /* check for nullness */ \
  39. if (! (types && typesToCompare)) \
  40. { \
  41. carla_stderr("BridgePlugin::%s() - argument types are null", __FUNCTION__); \
  42. return 1; \
  43. } \
  44. /* check argument types */ \
  45. if (std::strcmp(types, typesToCompare) != 0) \
  46. { \
  47. carla_stderr("BridgePlugin::%s() - argument types mismatch: '%s' != '%s'", __FUNCTION__, types, typesToCompare); \
  48. return 1; \
  49. } \
  50. }
  51. // -------------------------------------------------------------------------------------------------------------------
  52. using juce::File;
  53. using juce::String;
  54. using juce::StringArray;
  55. CARLA_BACKEND_START_NAMESPACE
  56. // -------------------------------------------------------------------------------------------------------------------
  57. // call carla_shm_create with for a XXXXXX temp filename
  58. static shm_t shm_mkstemp(char* const fileBase)
  59. {
  60. CARLA_SAFE_ASSERT_RETURN(fileBase != nullptr, gNullCarlaShm);
  61. const size_t fileBaseLen(std::strlen(fileBase));
  62. CARLA_SAFE_ASSERT_RETURN(fileBaseLen > 6, gNullCarlaShm);
  63. CARLA_SAFE_ASSERT_RETURN(std::strcmp(fileBase + fileBaseLen - 6, "XXXXXX") == 0, gNullCarlaShm);
  64. static const char charSet[] = "abcdefghijklmnopqrstuvwxyz"
  65. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  66. "0123456789";
  67. static const int charSetLen = static_cast<int>(std::strlen(charSet) - 1); // -1 to avoid trailing '\0'
  68. // try until getting a valid shm or an error occurs
  69. for (;;)
  70. {
  71. for (size_t c = fileBaseLen - 6; c < fileBaseLen; ++c)
  72. fileBase[c] = charSet[std::rand() % charSetLen];
  73. const shm_t shm = carla_shm_create(fileBase);
  74. if (carla_is_shm_valid(shm))
  75. return shm;
  76. if (errno != EEXIST)
  77. return gNullCarlaShm;
  78. }
  79. }
  80. // -------------------------------------------------------------------------------------------------------------------
  81. struct BridgeAudioPool {
  82. CarlaString filename;
  83. float* data;
  84. size_t size;
  85. shm_t shm;
  86. BridgeAudioPool() noexcept
  87. : filename(),
  88. data(nullptr),
  89. size(0),
  90. shm(shm_t_INIT) {}
  91. ~BridgeAudioPool() noexcept
  92. {
  93. // should be cleared by now
  94. CARLA_SAFE_ASSERT(data == nullptr);
  95. clear();
  96. }
  97. void clear() noexcept
  98. {
  99. filename.clear();
  100. if (! carla_is_shm_valid(shm))
  101. return;
  102. if (data != nullptr)
  103. {
  104. carla_shm_unmap(shm, data, size);
  105. data = nullptr;
  106. }
  107. size = 0;
  108. carla_shm_close(shm);
  109. }
  110. void resize(const uint32_t bufferSize, const uint32_t portCount) noexcept
  111. {
  112. if (data != nullptr)
  113. carla_shm_unmap(shm, data, size);
  114. size = portCount*bufferSize*sizeof(float);
  115. if (size == 0)
  116. size = sizeof(float);
  117. data = (float*)carla_shm_map(shm, size);
  118. }
  119. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  120. };
  121. // -------------------------------------------------------------------------------------------------------------------
  122. struct BridgeRtControl : public CarlaRingBuffer<StackBuffer> {
  123. CarlaString filename;
  124. BridgeRtData* data;
  125. shm_t shm;
  126. BridgeRtControl()
  127. : filename(),
  128. data(nullptr),
  129. shm(shm_t_INIT) {}
  130. ~BridgeRtControl()
  131. {
  132. // should be cleared by now
  133. CARLA_SAFE_ASSERT(data == nullptr);
  134. clear();
  135. }
  136. void clear() noexcept
  137. {
  138. filename.clear();
  139. if (! carla_is_shm_valid(shm))
  140. return;
  141. if (data != nullptr)
  142. {
  143. carla_shm_unmap(shm, data, sizeof(BridgeRtData));
  144. data = nullptr;
  145. }
  146. carla_shm_close(shm);
  147. }
  148. bool mapData() noexcept
  149. {
  150. CARLA_SAFE_ASSERT(data == nullptr);
  151. if (carla_shm_map<BridgeRtData>(shm, data))
  152. {
  153. setRingBuffer(&data->ringBuffer, true);
  154. return true;
  155. }
  156. return false;
  157. }
  158. void unmapData() noexcept
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  161. carla_shm_unmap(shm, data, sizeof(BridgeRtData));
  162. data = nullptr;
  163. setRingBuffer(nullptr, false);
  164. }
  165. bool waitForServer(const int secs) noexcept
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  168. jackbridge_sem_post(&data->sem.server);
  169. return jackbridge_sem_timedwait(&data->sem.client, secs);
  170. }
  171. void writeOpcode(const PluginBridgeRtOpcode opcode) noexcept
  172. {
  173. writeInt(static_cast<int32_t>(opcode));
  174. }
  175. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtControl)
  176. };
  177. // -------------------------------------------------------------------------------------------------------------------
  178. struct BridgeNonRtControl : public CarlaRingBuffer<BigStackBuffer> {
  179. CarlaMutex mutex;
  180. CarlaString filename;
  181. BridgeNonRtData* data;
  182. shm_t shm;
  183. BridgeNonRtControl() noexcept
  184. : mutex(),
  185. filename(),
  186. data(nullptr),
  187. shm(shm_t_INIT) {}
  188. ~BridgeNonRtControl() noexcept
  189. {
  190. // should be cleared by now
  191. CARLA_SAFE_ASSERT(data == nullptr);
  192. clear();
  193. }
  194. void clear() noexcept
  195. {
  196. filename.clear();
  197. if (! carla_is_shm_valid(shm))
  198. return;
  199. if (data != nullptr)
  200. {
  201. carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
  202. data = nullptr;
  203. }
  204. carla_shm_close(shm);
  205. }
  206. bool mapData() noexcept
  207. {
  208. CARLA_SAFE_ASSERT(data == nullptr);
  209. if (carla_shm_map<BridgeNonRtData>(shm, data))
  210. {
  211. setRingBuffer(&data->ringBuffer, true);
  212. return true;
  213. }
  214. return false;
  215. }
  216. void unmapData() noexcept
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  219. carla_shm_unmap(shm, data, sizeof(BridgeNonRtData));
  220. data = nullptr;
  221. setRingBuffer(nullptr, false);
  222. }
  223. void writeOpcode(const PluginBridgeNonRtOpcode opcode) noexcept
  224. {
  225. writeInt(static_cast<int32_t>(opcode));
  226. }
  227. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtControl)
  228. };
  229. // -------------------------------------------------------------------------------------------------------------------
  230. struct BridgeParamInfo {
  231. float value;
  232. CarlaString name;
  233. CarlaString unit;
  234. BridgeParamInfo() noexcept
  235. : value(0.0f),
  236. name(),
  237. unit() {}
  238. CARLA_DECLARE_NON_COPY_STRUCT(BridgeParamInfo)
  239. };
  240. // -------------------------------------------------------------------------------------------------------------------
  241. class BridgePlugin : public CarlaPlugin
  242. {
  243. public:
  244. BridgePlugin(CarlaEngine* const engine, const uint id, const BinaryType btype, const PluginType ptype)
  245. : CarlaPlugin(engine, id),
  246. fBinaryType(btype),
  247. fPluginType(ptype),
  248. fInitiated(false),
  249. fInitError(false),
  250. fSaved(false),
  251. fNeedsSemDestroy(false),
  252. fTimedOut(false),
  253. fLastPongCounter(-1),
  254. fBridgeBinary(),
  255. fShmAudioPool(),
  256. fShmRtControl(),
  257. fShmNonRtControl(),
  258. fInfo(),
  259. fParams(nullptr),
  260. leakDetector_BridgePlugin()
  261. {
  262. carla_debug("BridgePlugin::BridgePlugin(%p, %i, %s, %s)", engine, id, BinaryType2Str(btype), PluginType2Str(ptype));
  263. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_BRIDGE);
  264. pData->hints |= PLUGIN_IS_BRIDGE;
  265. }
  266. ~BridgePlugin() override
  267. {
  268. carla_debug("BridgePlugin::~BridgePlugin()");
  269. // close UI
  270. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  271. pData->transientTryCounter = 0;
  272. pData->singleMutex.lock();
  273. pData->masterMutex.lock();
  274. if (pData->client != nullptr && pData->client->isActive())
  275. pData->client->deactivate();
  276. if (pData->active)
  277. {
  278. deactivate();
  279. pData->active = false;
  280. }
  281. if (pData->osc.thread.isThreadRunning())
  282. {
  283. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtQuit);
  284. fShmNonRtControl.commitWrite();
  285. if (! fTimedOut)
  286. fShmRtControl.waitForServer(3);
  287. }
  288. pData->osc.data.clear();
  289. pData->osc.thread.stopThread(3000);
  290. if (fNeedsSemDestroy)
  291. {
  292. jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
  293. jackbridge_sem_destroy(&fShmRtControl.data->sem.client);
  294. }
  295. fShmAudioPool.clear();
  296. fShmRtControl.clear();
  297. fShmNonRtControl.clear();
  298. clearBuffers();
  299. fInfo.chunk.clear();
  300. }
  301. // -------------------------------------------------------------------
  302. // Information (base)
  303. BinaryType getBinaryType() const noexcept
  304. {
  305. return fBinaryType;
  306. }
  307. PluginType getType() const noexcept override
  308. {
  309. return fPluginType;
  310. }
  311. PluginCategory getCategory() const noexcept override
  312. {
  313. return fInfo.category;
  314. }
  315. int64_t getUniqueId() const noexcept override
  316. {
  317. return fInfo.uniqueId;
  318. }
  319. // -------------------------------------------------------------------
  320. // Information (count)
  321. uint32_t getMidiInCount() const noexcept override
  322. {
  323. return fInfo.mIns;
  324. }
  325. uint32_t getMidiOutCount() const noexcept override
  326. {
  327. return fInfo.mOuts;
  328. }
  329. // -------------------------------------------------------------------
  330. // Information (current data)
  331. std::size_t getChunkData(void** const dataPtr) noexcept override
  332. {
  333. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  334. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  335. CARLA_SAFE_ASSERT_RETURN(fInfo.chunk.size() > 0, 0);
  336. *dataPtr = fInfo.chunk.data();
  337. return fInfo.chunk.size();
  338. }
  339. // -------------------------------------------------------------------
  340. // Information (per-plugin data)
  341. uint getOptionsAvailable() const noexcept override
  342. {
  343. return fInfo.optionsAvailable;
  344. }
  345. float getParameterValue(const uint32_t parameterId) const noexcept override
  346. {
  347. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  348. return fParams[parameterId].value;
  349. }
  350. void getLabel(char* const strBuf) const noexcept override
  351. {
  352. std::strncpy(strBuf, fInfo.label, STR_MAX);
  353. }
  354. void getMaker(char* const strBuf) const noexcept override
  355. {
  356. std::strncpy(strBuf, fInfo.maker, STR_MAX);
  357. }
  358. void getCopyright(char* const strBuf) const noexcept override
  359. {
  360. std::strncpy(strBuf, fInfo.copyright, STR_MAX);
  361. }
  362. void getRealName(char* const strBuf) const noexcept override
  363. {
  364. std::strncpy(strBuf, fInfo.name, STR_MAX);
  365. }
  366. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  367. {
  368. CARLA_ASSERT(parameterId < pData->param.count);
  369. std::strncpy(strBuf, fParams[parameterId].name.buffer(), STR_MAX);
  370. }
  371. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  372. {
  373. CARLA_ASSERT(parameterId < pData->param.count);
  374. std::strncpy(strBuf, fParams[parameterId].unit.buffer(), STR_MAX);
  375. }
  376. // -------------------------------------------------------------------
  377. // Set data (state)
  378. void prepareForSave() override
  379. {
  380. fSaved = false;
  381. {
  382. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  383. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPrepareForSave);
  384. fShmNonRtControl.commitWrite();
  385. }
  386. carla_stdout("BridgePlugin::prepareForSave() - sent, now waiting...");
  387. for (int i=0; i < 200; ++i)
  388. {
  389. if (fSaved)
  390. break;
  391. carla_msleep(30);
  392. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  393. pData->engine->idle();
  394. }
  395. if (! fSaved)
  396. carla_stderr("BridgePlugin::prepareForSave() - Timeout while requesting save state");
  397. else
  398. carla_stdout("BridgePlugin::prepareForSave() - success!");
  399. }
  400. // -------------------------------------------------------------------
  401. // Set data (internal stuff)
  402. void setOption(const uint option, const bool yesNo, const bool sendCallback) override
  403. {
  404. {
  405. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  406. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOption);
  407. fShmNonRtControl.writeUInt(option);
  408. fShmNonRtControl.writeBool(yesNo);
  409. fShmNonRtControl.commitWrite();
  410. }
  411. CarlaPlugin::setOption(option, yesNo, sendCallback);
  412. }
  413. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  414. {
  415. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  416. {
  417. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  418. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetCtrlChannel);
  419. fShmNonRtControl.writeShort(channel);
  420. fShmNonRtControl.commitWrite();
  421. }
  422. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  423. }
  424. // -------------------------------------------------------------------
  425. // Set data (plugin-specific stuff)
  426. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  427. {
  428. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  429. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  430. fParams[parameterId].value = fixedValue;
  431. {
  432. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  433. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  434. fShmNonRtControl.writeUInt(parameterId);
  435. fShmNonRtControl.writeFloat(value);
  436. fShmNonRtControl.commitWrite();
  437. }
  438. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  439. }
  440. void setParameterMidiChannel(const uint32_t parameterId, const uint8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  441. {
  442. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  443. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  444. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  445. {
  446. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  447. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterValue);
  448. fShmNonRtControl.writeUInt(parameterId);
  449. fShmNonRtControl.writeByte(channel);
  450. fShmNonRtControl.commitWrite();
  451. }
  452. CarlaPlugin::setParameterMidiChannel(parameterId, channel, sendOsc, sendCallback);
  453. }
  454. void setParameterMidiCC(const uint32_t parameterId, const int16_t cc, const bool sendOsc, const bool sendCallback) noexcept override
  455. {
  456. CARLA_SAFE_ASSERT_RETURN(sendOsc || sendCallback,); // never call this from RT
  457. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  458. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc <= 0x5F,);
  459. {
  460. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  461. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetParameterMidiCC);
  462. fShmNonRtControl.writeUInt(parameterId);
  463. fShmNonRtControl.writeShort(cc);
  464. fShmNonRtControl.commitWrite();
  465. }
  466. CarlaPlugin::setParameterMidiCC(parameterId, cc, sendOsc, sendCallback);
  467. }
  468. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  469. {
  470. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  471. {
  472. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  473. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetProgram);
  474. fShmNonRtControl.writeInt(index);
  475. fShmNonRtControl.commitWrite();
  476. }
  477. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  478. }
  479. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  480. {
  481. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  482. {
  483. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  484. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetMidiProgram);
  485. fShmNonRtControl.writeInt(index);
  486. fShmNonRtControl.commitWrite();
  487. }
  488. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  489. }
  490. #if 0
  491. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  492. {
  493. CARLA_ASSERT(type);
  494. CARLA_ASSERT(key);
  495. CARLA_ASSERT(value);
  496. if (sendGui)
  497. {
  498. // TODO - if type is chunk|binary, store it in a file and send path instead
  499. QString cData;
  500. cData = type;
  501. cData += "·";
  502. cData += key;
  503. cData += "·";
  504. cData += value;
  505. osc_send_configure(&osc.data, CARLA_BRIDGE_MSG_SET_CUSTOM, cData.toUtf8().constData());
  506. }
  507. CarlaPlugin::setCustomData(type, key, value, sendGui);
  508. }
  509. #endif
  510. void setChunkData(const void* const data, const std::size_t dataSize) override
  511. {
  512. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  513. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  514. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  515. CarlaString dataBase64 = CarlaString::asBase64(data, dataSize);
  516. CARLA_SAFE_ASSERT_RETURN(dataBase64.length() > 0,);
  517. String filePath(File::getSpecialLocation(File::tempDirectory).getFullPathName());
  518. filePath += CARLA_OS_SEP_STR;
  519. filePath += ".CarlaChunk_";
  520. filePath += fShmAudioPool.filename.buffer() + 18;
  521. if (File(filePath).replaceWithText(dataBase64.buffer()))
  522. {
  523. const uint32_t ulength(static_cast<uint32_t>(filePath.length()));
  524. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  525. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetChunkDataFile);
  526. fShmNonRtControl.writeUInt(ulength);
  527. fShmNonRtControl.writeCustomData(filePath.toRawUTF8(), ulength);
  528. fShmNonRtControl.commitWrite();
  529. }
  530. }
  531. // -------------------------------------------------------------------
  532. // Set ui stuff
  533. void showCustomUI(const bool yesNo) override
  534. {
  535. {
  536. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  537. fShmNonRtControl.writeOpcode(yesNo ? kPluginBridgeNonRtShowUI : kPluginBridgeNonRtHideUI);
  538. fShmNonRtControl.commitWrite();
  539. }
  540. if (yesNo)
  541. {
  542. pData->tryTransient();
  543. }
  544. else
  545. {
  546. pData->transientTryCounter = 0;
  547. }
  548. }
  549. void idle() override
  550. {
  551. if (pData->osc.thread.isThreadRunning())
  552. {
  553. if (fTimedOut && pData->active)
  554. setActive(false, true, true);
  555. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  556. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtPing);
  557. fShmNonRtControl.commitWrite();
  558. }
  559. else
  560. carla_stderr2("TESTING: Bridge has closed!");
  561. CarlaPlugin::idle();
  562. }
  563. // -------------------------------------------------------------------
  564. // Plugin state
  565. void reload() override
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  568. carla_debug("BridgePlugin::reload() - start");
  569. const EngineProcessMode processMode(pData->engine->getProccessMode());
  570. // Safely disable plugin for reload
  571. const ScopedDisabler sd(this);
  572. bool needsCtrlIn, needsCtrlOut;
  573. needsCtrlIn = needsCtrlOut = false;
  574. if (fInfo.aIns > 0)
  575. {
  576. pData->audioIn.createNew(fInfo.aIns);
  577. }
  578. if (fInfo.aOuts > 0)
  579. {
  580. pData->audioOut.createNew(fInfo.aOuts);
  581. needsCtrlIn = true;
  582. }
  583. if (fInfo.mIns > 0)
  584. needsCtrlIn = true;
  585. if (fInfo.mOuts > 0)
  586. needsCtrlOut = true;
  587. const uint portNameSize(pData->engine->getMaxPortNameSize());
  588. CarlaString portName;
  589. // Audio Ins
  590. for (uint32_t j=0; j < fInfo.aIns; ++j)
  591. {
  592. portName.clear();
  593. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  594. {
  595. portName = pData->name;
  596. portName += ":";
  597. }
  598. if (fInfo.aIns > 1)
  599. {
  600. portName += "input_";
  601. portName += CarlaString(j+1);
  602. }
  603. else
  604. portName += "input";
  605. portName.truncate(portNameSize);
  606. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  607. pData->audioIn.ports[j].rindex = j;
  608. }
  609. // Audio Outs
  610. for (uint32_t j=0; j < fInfo.aOuts; ++j)
  611. {
  612. portName.clear();
  613. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  614. {
  615. portName = pData->name;
  616. portName += ":";
  617. }
  618. if (fInfo.aOuts > 1)
  619. {
  620. portName += "output_";
  621. portName += CarlaString(j+1);
  622. }
  623. else
  624. portName += "output";
  625. portName.truncate(portNameSize);
  626. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  627. pData->audioOut.ports[j].rindex = j;
  628. }
  629. if (needsCtrlIn)
  630. {
  631. portName.clear();
  632. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  633. {
  634. portName = pData->name;
  635. portName += ":";
  636. }
  637. portName += "event-in";
  638. portName.truncate(portNameSize);
  639. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  640. }
  641. if (needsCtrlOut)
  642. {
  643. portName.clear();
  644. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  645. {
  646. portName = pData->name;
  647. portName += ":";
  648. }
  649. portName += "event-out";
  650. portName.truncate(portNameSize);
  651. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  652. }
  653. // extra plugin hints
  654. pData->extraHints = 0x0;
  655. if (fInfo.mIns > 0)
  656. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  657. if (fInfo.mOuts > 0)
  658. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  659. if (fInfo.aIns <= 2 && fInfo.aOuts <= 2 && (fInfo.aIns == fInfo.aOuts || fInfo.aIns == 0 || fInfo.aOuts == 0))
  660. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  661. bufferSizeChanged(pData->engine->getBufferSize());
  662. reloadPrograms(true);
  663. carla_debug("BridgePlugin::reload() - end");
  664. }
  665. // -------------------------------------------------------------------
  666. // Plugin processing
  667. void activate() noexcept override
  668. {
  669. {
  670. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  671. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtActivate);
  672. fShmNonRtControl.commitWrite();
  673. }
  674. bool timedOut = true;
  675. try {
  676. timedOut = waitForServer(1);
  677. } catch(...) {}
  678. if (! timedOut)
  679. fTimedOut = false;
  680. }
  681. void deactivate() noexcept override
  682. {
  683. {
  684. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  685. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtDeactivate);
  686. fShmNonRtControl.commitWrite();
  687. }
  688. bool timedOut = true;
  689. try {
  690. timedOut = waitForServer(1);
  691. } catch(...) {}
  692. if (! timedOut)
  693. fTimedOut = false;
  694. }
  695. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  696. {
  697. // --------------------------------------------------------------------------------------------------------
  698. // Check if active
  699. if (fTimedOut || ! pData->active)
  700. {
  701. // disable any output sound
  702. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  703. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  704. return;
  705. }
  706. // --------------------------------------------------------------------------------------------------------
  707. // Check if needs reset
  708. if (pData->needsReset)
  709. {
  710. // TODO
  711. pData->needsReset = false;
  712. }
  713. // --------------------------------------------------------------------------------------------------------
  714. // Event Input
  715. if (pData->event.portIn != nullptr)
  716. {
  717. // ----------------------------------------------------------------------------------------------------
  718. // MIDI Input (External)
  719. if (pData->extNotes.mutex.tryLock())
  720. {
  721. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  722. {
  723. const ExternalMidiNote& note(it.getValue());
  724. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  725. uint8_t data1, data2, data3;
  726. data1 = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  727. data2 = note.note;
  728. data3 = note.velo;
  729. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  730. fShmRtControl.writeUInt(0); // time
  731. fShmRtControl.writeByte(0); // port
  732. fShmRtControl.writeByte(3); // size
  733. fShmRtControl.writeByte(data1);
  734. fShmRtControl.writeByte(data2);
  735. fShmRtControl.writeByte(data3);
  736. fShmRtControl.commitWrite();
  737. }
  738. pData->extNotes.data.clear();
  739. pData->extNotes.mutex.unlock();
  740. } // End of MIDI Input (External)
  741. // ----------------------------------------------------------------------------------------------------
  742. // Event Input (System)
  743. bool allNotesOffSent = false;
  744. for (uint32_t i=0, numEvents = pData->event.portIn->getEventCount(); i < numEvents; ++i)
  745. {
  746. const EngineEvent& event(pData->event.portIn->getEvent(i));
  747. // Control change
  748. switch (event.type)
  749. {
  750. case kEngineEventTypeNull:
  751. break;
  752. case kEngineEventTypeControl: {
  753. const EngineControlEvent& ctrlEvent = event.ctrl;
  754. switch (ctrlEvent.type)
  755. {
  756. case kEngineControlEventTypeNull:
  757. break;
  758. case kEngineControlEventTypeParameter:
  759. // Control backend stuff
  760. if (event.channel == pData->ctrlChannel)
  761. {
  762. float value;
  763. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  764. {
  765. value = ctrlEvent.value;
  766. setDryWet(value, false, false);
  767. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  768. break;
  769. }
  770. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  771. {
  772. value = ctrlEvent.value*127.0f/100.0f;
  773. setVolume(value, false, false);
  774. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  775. break;
  776. }
  777. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  778. {
  779. float left, right;
  780. value = ctrlEvent.value/0.5f - 1.0f;
  781. if (value < 0.0f)
  782. {
  783. left = -1.0f;
  784. right = (value*2.0f)+1.0f;
  785. }
  786. else if (value > 0.0f)
  787. {
  788. left = (value*2.0f)-1.0f;
  789. right = 1.0f;
  790. }
  791. else
  792. {
  793. left = -1.0f;
  794. right = 1.0f;
  795. }
  796. setBalanceLeft(left, false, false);
  797. setBalanceRight(right, false, false);
  798. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  799. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  800. break;
  801. }
  802. }
  803. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventParameter);
  804. fShmRtControl.writeUInt(event.time);
  805. fShmRtControl.writeByte(event.channel);
  806. fShmRtControl.writeUShort(event.ctrl.param);
  807. fShmRtControl.writeFloat(event.ctrl.value);
  808. fShmRtControl.commitWrite();
  809. break;
  810. case kEngineControlEventTypeMidiBank:
  811. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  812. {
  813. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiBank);
  814. fShmRtControl.writeUInt(event.time);
  815. fShmRtControl.writeByte(event.channel);
  816. fShmRtControl.writeUShort(event.ctrl.param);
  817. fShmRtControl.commitWrite();
  818. }
  819. break;
  820. case kEngineControlEventTypeMidiProgram:
  821. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  822. {
  823. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventMidiProgram);
  824. fShmRtControl.writeUInt(event.time);
  825. fShmRtControl.writeByte(event.channel);
  826. fShmRtControl.writeUShort(event.ctrl.param);
  827. fShmRtControl.commitWrite();
  828. }
  829. break;
  830. case kEngineControlEventTypeAllSoundOff:
  831. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  832. {
  833. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllSoundOff);
  834. fShmRtControl.writeUInt(event.time);
  835. fShmRtControl.writeByte(event.channel);
  836. fShmRtControl.commitWrite();
  837. }
  838. break;
  839. case kEngineControlEventTypeAllNotesOff:
  840. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  841. {
  842. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  843. {
  844. allNotesOffSent = true;
  845. sendMidiAllNotesOffToCallback();
  846. }
  847. fShmRtControl.writeOpcode(kPluginBridgeRtControlEventAllNotesOff);
  848. fShmRtControl.writeUInt(event.time);
  849. fShmRtControl.writeByte(event.channel);
  850. fShmRtControl.commitWrite();
  851. }
  852. break;
  853. } // switch (ctrlEvent.type)
  854. break;
  855. } // case kEngineEventTypeControl
  856. case kEngineEventTypeMidi: {
  857. const EngineMidiEvent& midiEvent(event.midi);
  858. if (midiEvent.size == 0 || midiEvent.size >= MAX_MIDI_VALUE)
  859. continue;
  860. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  861. uint8_t channel = event.channel;
  862. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  863. status = MIDI_STATUS_NOTE_OFF;
  864. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  865. continue;
  866. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  867. continue;
  868. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  869. continue;
  870. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  871. continue;
  872. fShmRtControl.writeOpcode(kPluginBridgeRtMidiEvent);
  873. fShmRtControl.writeUInt(event.time);
  874. fShmRtControl.writeByte(midiEvent.port);
  875. fShmRtControl.writeByte(midiEvent.size);
  876. for (uint8_t j=0; j < midiEvent.size; ++j)
  877. fShmRtControl.writeByte(midiEvent.data[j]);
  878. fShmRtControl.commitWrite();
  879. if (status == MIDI_STATUS_NOTE_ON)
  880. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  881. else if (status == MIDI_STATUS_NOTE_OFF)
  882. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  883. } break;
  884. }
  885. }
  886. pData->postRtEvents.trySplice();
  887. } // End of Event Input
  888. processSingle(inBuffer, outBuffer, frames);
  889. }
  890. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  891. {
  892. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  893. if (pData->audioIn.count > 0)
  894. {
  895. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  896. }
  897. if (pData->audioOut.count > 0)
  898. {
  899. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  900. }
  901. // --------------------------------------------------------------------------------------------------------
  902. // Try lock, silence otherwise
  903. if (pData->engine->isOffline())
  904. {
  905. pData->singleMutex.lock();
  906. }
  907. else if (! pData->singleMutex.tryLock())
  908. {
  909. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  910. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  911. return false;
  912. }
  913. // --------------------------------------------------------------------------------------------------------
  914. // Reset audio buffers
  915. //std::memset(fShmAudioPool.data, 0, fShmAudioPool.size);
  916. for (uint32_t i=0; i < fInfo.aIns; ++i)
  917. FloatVectorOperations::copy(fShmAudioPool.data + (i * frames), inBuffer[i], static_cast<int>(frames));
  918. // --------------------------------------------------------------------------------------------------------
  919. // TimeInfo
  920. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  921. BridgeTimeInfo& bridgeTimeInfo(fShmRtControl.data->timeInfo);
  922. bridgeTimeInfo.playing = timeInfo.playing;
  923. bridgeTimeInfo.frame = timeInfo.frame;
  924. bridgeTimeInfo.usecs = timeInfo.usecs;
  925. bridgeTimeInfo.valid = timeInfo.valid;
  926. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  927. {
  928. bridgeTimeInfo.bar = timeInfo.bbt.bar;
  929. bridgeTimeInfo.beat = timeInfo.bbt.beat;
  930. bridgeTimeInfo.tick = timeInfo.bbt.tick;
  931. bridgeTimeInfo.beatsPerBar = timeInfo.bbt.beatsPerBar;
  932. bridgeTimeInfo.beatType = timeInfo.bbt.beatType;
  933. bridgeTimeInfo.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  934. bridgeTimeInfo.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  935. bridgeTimeInfo.barStartTick = timeInfo.bbt.barStartTick;
  936. }
  937. // --------------------------------------------------------------------------------------------------------
  938. // Run plugin
  939. {
  940. fShmRtControl.writeOpcode(kPluginBridgeRtProcess);
  941. fShmRtControl.commitWrite();
  942. }
  943. if (! waitForServer(2))
  944. {
  945. pData->singleMutex.unlock();
  946. return true;
  947. }
  948. for (uint32_t i=0; i < fInfo.aOuts; ++i)
  949. FloatVectorOperations::copy(outBuffer[i], fShmAudioPool.data + ((i + fInfo.aIns) * frames), static_cast<int>(frames));
  950. // --------------------------------------------------------------------------------------------------------
  951. // Post-processing (dry/wet, volume and balance)
  952. {
  953. const bool doVolume = (pData->hints & PLUGIN_CAN_VOLUME) != 0 && pData->postProc.volume != 1.0f;
  954. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  955. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  956. bool isPair;
  957. float bufValue, oldBufLeft[doBalance ? frames : 1];
  958. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  959. {
  960. // Dry/Wet
  961. if (doDryWet)
  962. {
  963. for (uint32_t k=0; k < frames; ++k)
  964. {
  965. bufValue = inBuffer[(pData->audioIn.count == 1) ? 0 : i][k];
  966. outBuffer[i][k] = (outBuffer[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  967. }
  968. }
  969. // Balance
  970. if (doBalance)
  971. {
  972. isPair = (i % 2 == 0);
  973. if (isPair)
  974. {
  975. CARLA_ASSERT(i+1 < pData->audioOut.count);
  976. FloatVectorOperations::copy(oldBufLeft, outBuffer[i], static_cast<int>(frames));
  977. }
  978. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  979. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  980. for (uint32_t k=0; k < frames; ++k)
  981. {
  982. if (isPair)
  983. {
  984. // left
  985. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  986. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  987. }
  988. else
  989. {
  990. // right
  991. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  992. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  993. }
  994. }
  995. }
  996. // Volume (and buffer copy)
  997. if (doVolume)
  998. {
  999. for (uint32_t k=0; k < frames; ++k)
  1000. outBuffer[i][k] *= pData->postProc.volume;
  1001. }
  1002. }
  1003. } // End of Post-processing
  1004. // --------------------------------------------------------------------------------------------------------
  1005. pData->singleMutex.unlock();
  1006. return true;
  1007. }
  1008. void bufferSizeChanged(const uint32_t newBufferSize) override
  1009. {
  1010. resizeAudioPool(newBufferSize);
  1011. {
  1012. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1013. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1014. fShmNonRtControl.writeUInt(newBufferSize);
  1015. fShmNonRtControl.commitWrite();
  1016. }
  1017. fShmRtControl.waitForServer(1);
  1018. }
  1019. void sampleRateChanged(const double newSampleRate) override
  1020. {
  1021. {
  1022. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1023. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1024. fShmNonRtControl.writeDouble(newSampleRate);
  1025. fShmNonRtControl.commitWrite();
  1026. }
  1027. fShmRtControl.waitForServer(1);
  1028. }
  1029. void offlineModeChanged(const bool isOffline) override
  1030. {
  1031. {
  1032. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1033. fShmNonRtControl.writeOpcode(isOffline ? kPluginBridgeNonRtSetOffline : kPluginBridgeNonRtSetOnline);
  1034. fShmNonRtControl.commitWrite();
  1035. }
  1036. fShmRtControl.waitForServer(1);
  1037. }
  1038. // -------------------------------------------------------------------
  1039. // Plugin buffers
  1040. void clearBuffers() noexcept override
  1041. {
  1042. if (fParams != nullptr)
  1043. {
  1044. delete[] fParams;
  1045. fParams = nullptr;
  1046. }
  1047. CarlaPlugin::clearBuffers();
  1048. }
  1049. // -------------------------------------------------------------------
  1050. // OSC stuff
  1051. void updateOscURL() override
  1052. {
  1053. const String newURL(String(pData->engine->getOscServerPathUDP()) + String("/") + String(pData->id));
  1054. const uint32_t ulength(static_cast<uint32_t>(newURL.length()));
  1055. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1056. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetOscURL);
  1057. fShmNonRtControl.writeUInt(ulength);
  1058. fShmNonRtControl.writeCustomData(newURL.toRawUTF8(), ulength);
  1059. fShmNonRtControl.commitWrite();
  1060. }
  1061. // -------------------------------------------------------------------
  1062. // Post-poned UI Stuff
  1063. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1064. {
  1065. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1066. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1067. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiParameterChange);
  1068. fShmNonRtControl.writeUInt(index);
  1069. fShmNonRtControl.writeFloat(value);
  1070. fShmNonRtControl.commitWrite();
  1071. }
  1072. void uiProgramChange(const uint32_t index) noexcept override
  1073. {
  1074. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1075. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1076. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiProgramChange);
  1077. fShmNonRtControl.writeUInt(index);
  1078. fShmNonRtControl.commitWrite();
  1079. }
  1080. void uiMidiProgramChange(const uint32_t index) noexcept override
  1081. {
  1082. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1083. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1084. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiMidiProgramChange);
  1085. fShmNonRtControl.writeUInt(index);
  1086. fShmNonRtControl.commitWrite();
  1087. }
  1088. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1089. {
  1090. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1091. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1092. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1093. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1094. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiNoteOn);
  1095. fShmNonRtControl.writeByte(channel);
  1096. fShmNonRtControl.writeByte(note);
  1097. fShmNonRtControl.writeByte(velo);
  1098. fShmNonRtControl.commitWrite();
  1099. }
  1100. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1101. {
  1102. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1103. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1104. const CarlaMutexLocker _cml(fShmNonRtControl.mutex);
  1105. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtUiNoteOff);
  1106. fShmNonRtControl.writeByte(channel);
  1107. fShmNonRtControl.writeByte(note);
  1108. fShmNonRtControl.commitWrite();
  1109. }
  1110. // -------------------------------------------------------------------
  1111. int setOscPluginBridgeInfo(const PluginBridgeOscInfoType infoType, const int argc, const lo_arg* const* const argv, const char* const types)
  1112. {
  1113. #ifdef DEBUG
  1114. if (infoType != kPluginBridgeOscPong) {
  1115. carla_debug("BridgePlugin::setOscPluginBridgeInfo(%s, %i, %p, \"%s\")", PluginBridgeOscInfoType2str(infoType), argc, argv, types);
  1116. }
  1117. #endif
  1118. switch (infoType)
  1119. {
  1120. case kPluginBridgeOscNull:
  1121. break;
  1122. case kPluginBridgeOscPong:
  1123. if (fLastPongCounter > 0)
  1124. fLastPongCounter = 0;
  1125. break;
  1126. case kPluginBridgeOscPluginInfo1: {
  1127. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiih");
  1128. const int32_t category = argv[0]->i;
  1129. const int32_t hints = argv[1]->i;
  1130. const int32_t optionAv = argv[2]->i;
  1131. const int32_t optionEn = argv[3]->i;
  1132. const int64_t uniqueId = argv[4]->h;
  1133. CARLA_SAFE_ASSERT_BREAK(category >= 0);
  1134. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1135. CARLA_SAFE_ASSERT_BREAK(optionAv >= 0);
  1136. CARLA_SAFE_ASSERT_BREAK(optionEn >= 0);
  1137. pData->hints = static_cast<uint>(hints);
  1138. pData->hints |= PLUGIN_IS_BRIDGE;
  1139. pData->options = static_cast<uint>(optionEn);
  1140. fInfo.category = static_cast<PluginCategory>(category);
  1141. fInfo.uniqueId = uniqueId;
  1142. fInfo.optionsAvailable = static_cast<uint>(optionAv);
  1143. break;
  1144. }
  1145. case kPluginBridgeOscPluginInfo2: {
  1146. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ssss");
  1147. const char* const realName = (const char*)&argv[0]->s;
  1148. const char* const label = (const char*)&argv[1]->s;
  1149. const char* const maker = (const char*)&argv[2]->s;
  1150. const char* const copyright = (const char*)&argv[3]->s;
  1151. CARLA_SAFE_ASSERT_BREAK(realName != nullptr);
  1152. CARLA_SAFE_ASSERT_BREAK(label != nullptr);
  1153. CARLA_SAFE_ASSERT_BREAK(maker != nullptr);
  1154. CARLA_SAFE_ASSERT_BREAK(copyright != nullptr);
  1155. fInfo.name = realName;
  1156. fInfo.label = label;
  1157. fInfo.maker = maker;
  1158. fInfo.copyright = copyright;
  1159. if (pData->name == nullptr)
  1160. pData->name = pData->engine->getUniquePluginName(realName);
  1161. break;
  1162. }
  1163. case kPluginBridgeOscAudioCount: {
  1164. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1165. const int32_t ins = argv[0]->i;
  1166. const int32_t outs = argv[1]->i;
  1167. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1168. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1169. fInfo.aIns = static_cast<uint32_t>(ins);
  1170. fInfo.aOuts = static_cast<uint32_t>(outs);
  1171. break;
  1172. }
  1173. case kPluginBridgeOscMidiCount: {
  1174. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1175. const int32_t ins = argv[0]->i;
  1176. const int32_t outs = argv[1]->i;
  1177. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1178. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1179. fInfo.mIns = static_cast<uint32_t>(ins);
  1180. fInfo.mOuts = static_cast<uint32_t>(outs);
  1181. break;
  1182. }
  1183. case kPluginBridgeOscParameterCount: {
  1184. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ii");
  1185. const int32_t ins = argv[0]->i;
  1186. const int32_t outs = argv[1]->i;
  1187. CARLA_SAFE_ASSERT_BREAK(ins >= 0);
  1188. CARLA_SAFE_ASSERT_BREAK(outs >= 0);
  1189. // delete old data
  1190. pData->param.clear();
  1191. if (fParams != nullptr)
  1192. {
  1193. delete[] fParams;
  1194. fParams = nullptr;
  1195. }
  1196. if (int32_t count = ins+outs)
  1197. {
  1198. const int32_t maxParams(static_cast<int32_t>(pData->engine->getOptions().maxParameters));
  1199. if (count > maxParams)
  1200. {
  1201. count = maxParams;
  1202. carla_safe_assert_int2("count <= pData->engine->getOptions().maxParameters", __FILE__, __LINE__, count, maxParams);
  1203. }
  1204. const uint32_t ucount(static_cast<uint32_t>(count));
  1205. pData->param.createNew(ucount, false);
  1206. fParams = new BridgeParamInfo[ucount];
  1207. }
  1208. break;
  1209. }
  1210. case kPluginBridgeOscProgramCount: {
  1211. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1212. const int32_t count = argv[0]->i;
  1213. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1214. pData->prog.clear();
  1215. if (count > 0)
  1216. pData->prog.createNew(static_cast<uint32_t>(count));
  1217. break;
  1218. }
  1219. case kPluginBridgeOscMidiProgramCount: {
  1220. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1221. const int32_t count = argv[0]->i;
  1222. CARLA_SAFE_ASSERT_BREAK(count >= 0);
  1223. pData->midiprog.clear();
  1224. if (count > 0)
  1225. pData->midiprog.createNew(static_cast<uint32_t>(count));
  1226. break;
  1227. }
  1228. case kPluginBridgeOscParameterData1: {
  1229. CARLA_BRIDGE_CHECK_OSC_TYPES(5, "iiiii");
  1230. const int32_t index = argv[0]->i;
  1231. const int32_t rindex = argv[1]->i;
  1232. const int32_t type = argv[2]->i;
  1233. const int32_t hints = argv[3]->i;
  1234. const int32_t midiCC = argv[4]->i;
  1235. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1236. CARLA_SAFE_ASSERT_BREAK(rindex >= 0);
  1237. CARLA_SAFE_ASSERT_BREAK(type >= 0);
  1238. CARLA_SAFE_ASSERT_BREAK(hints >= 0);
  1239. CARLA_SAFE_ASSERT_BREAK(midiCC >= -1 && midiCC < 0x5F);
  1240. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1241. if (index < static_cast<int32_t>(pData->param.count))
  1242. {
  1243. pData->param.data[index].type = static_cast<ParameterType>(type);
  1244. pData->param.data[index].index = index;
  1245. pData->param.data[index].rindex = rindex;
  1246. pData->param.data[index].hints = static_cast<uint>(hints);
  1247. pData->param.data[index].midiCC = static_cast<int16_t>(midiCC);
  1248. }
  1249. break;
  1250. }
  1251. case kPluginBridgeOscParameterData2: {
  1252. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "iss");
  1253. const int32_t index = argv[0]->i;
  1254. const char* const name = (const char*)&argv[1]->s;
  1255. const char* const unit = (const char*)&argv[2]->s;
  1256. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1257. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1258. CARLA_SAFE_ASSERT_BREAK(unit != nullptr);
  1259. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1260. if (index < static_cast<int32_t>(pData->param.count))
  1261. {
  1262. fParams[index].name = name;
  1263. fParams[index].unit = unit;
  1264. }
  1265. break;
  1266. }
  1267. case kPluginBridgeOscParameterRanges1: {
  1268. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1269. const int32_t index = argv[0]->i;
  1270. const float def = argv[1]->f;
  1271. const float min = argv[2]->f;
  1272. const float max = argv[3]->f;
  1273. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1274. CARLA_SAFE_ASSERT_BREAK(min < max);
  1275. CARLA_SAFE_ASSERT_BREAK(def >= min);
  1276. CARLA_SAFE_ASSERT_BREAK(def <= max);
  1277. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1278. if (index < static_cast<int32_t>(pData->param.count))
  1279. {
  1280. pData->param.ranges[index].def = def;
  1281. pData->param.ranges[index].min = min;
  1282. pData->param.ranges[index].max = max;
  1283. }
  1284. break;
  1285. }
  1286. case kPluginBridgeOscParameterRanges2: {
  1287. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "ifff");
  1288. const int32_t index = argv[0]->i;
  1289. const float step = argv[1]->f;
  1290. const float stepSmall = argv[2]->f;
  1291. const float stepLarge = argv[3]->f;
  1292. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1293. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1294. if (index < static_cast<int32_t>(pData->param.count))
  1295. {
  1296. pData->param.ranges[index].step = step;
  1297. pData->param.ranges[index].stepSmall = stepSmall;
  1298. pData->param.ranges[index].stepLarge = stepLarge;
  1299. }
  1300. break;
  1301. }
  1302. case kPluginBridgeOscParameterValue: {
  1303. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1304. const int32_t index = argv[0]->i;
  1305. const float value = argv[1]->f;
  1306. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1307. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1308. if (index < static_cast<int32_t>(pData->param.count))
  1309. {
  1310. const uint32_t uindex(static_cast<uint32_t>(index));
  1311. const float fixedValue(pData->param.getFixedValue(uindex, value));
  1312. fParams[uindex].value = fixedValue;
  1313. CarlaPlugin::setParameterValue(uindex, fixedValue, false, true, true);
  1314. }
  1315. break;
  1316. }
  1317. case kPluginBridgeOscDefaultValue: {
  1318. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "if");
  1319. const int32_t index = argv[0]->i;
  1320. const float value = argv[1]->f;
  1321. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1322. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->param.count), index, pData->param.count);
  1323. if (index < static_cast<int32_t>(pData->param.count))
  1324. pData->param.ranges[index].def = value;
  1325. break;
  1326. }
  1327. case kPluginBridgeOscCurrentProgram: {
  1328. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1329. const int32_t index = argv[0]->i;
  1330. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1331. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1332. CarlaPlugin::setProgram(index, false, true, true);
  1333. break;
  1334. }
  1335. case kPluginBridgeOscCurrentMidiProgram: {
  1336. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "i");
  1337. const int32_t index = argv[0]->i;
  1338. CARLA_SAFE_ASSERT_BREAK(index >= -1);
  1339. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1340. CarlaPlugin::setMidiProgram(index, false, true, true);
  1341. break;
  1342. }
  1343. case kPluginBridgeOscProgramName: {
  1344. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "is");
  1345. const int32_t index = argv[0]->i;
  1346. const char* const name = (const char*)&argv[1]->s;
  1347. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1348. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1349. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->prog.count), index, pData->prog.count);
  1350. if (index < static_cast<int32_t>(pData->prog.count))
  1351. {
  1352. if (pData->prog.names[index] != nullptr)
  1353. delete[] pData->prog.names[index];
  1354. pData->prog.names[index] = carla_strdup(name);
  1355. }
  1356. break;
  1357. }
  1358. case kPluginBridgeOscMidiProgramData: {
  1359. CARLA_BRIDGE_CHECK_OSC_TYPES(4, "iiis");
  1360. const int32_t index = argv[0]->i;
  1361. const int32_t bank = argv[1]->i;
  1362. const int32_t program = argv[2]->i;
  1363. const char* const name = (const char*)&argv[3]->s;
  1364. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  1365. CARLA_SAFE_ASSERT_BREAK(bank >= 0);
  1366. CARLA_SAFE_ASSERT_BREAK(program >= 0);
  1367. CARLA_SAFE_ASSERT_BREAK(name != nullptr);
  1368. CARLA_SAFE_ASSERT_INT2(index < static_cast<int32_t>(pData->midiprog.count), index, pData->midiprog.count);
  1369. if (index < static_cast<int32_t>(pData->midiprog.count))
  1370. {
  1371. if (pData->midiprog.data[index].name != nullptr)
  1372. delete[] pData->midiprog.data[index].name;
  1373. pData->midiprog.data[index].bank = static_cast<uint32_t>(bank);
  1374. pData->midiprog.data[index].program = static_cast<uint32_t>(program);
  1375. pData->midiprog.data[index].name = carla_strdup(name);
  1376. }
  1377. break;
  1378. }
  1379. case kPluginBridgeOscConfigure: {
  1380. CARLA_BRIDGE_CHECK_OSC_TYPES(2, "ss");
  1381. const char* const key = (const char*)&argv[0]->s;
  1382. const char* const value = (const char*)&argv[1]->s;
  1383. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1384. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1385. if (std::strcmp(key, CARLA_BRIDGE_MSG_HIDE_GUI) == 0)
  1386. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1387. else if (std::strcmp(key, CARLA_BRIDGE_MSG_SAVED) == 0)
  1388. fSaved = true;
  1389. break;
  1390. }
  1391. case kPluginBridgeOscSetCustomData: {
  1392. CARLA_BRIDGE_CHECK_OSC_TYPES(3, "sss");
  1393. const char* const type = (const char*)&argv[0]->s;
  1394. const char* const key = (const char*)&argv[1]->s;
  1395. const char* const value = (const char*)&argv[2]->s;
  1396. CARLA_SAFE_ASSERT_BREAK(type != nullptr);
  1397. CARLA_SAFE_ASSERT_BREAK(key != nullptr);
  1398. CARLA_SAFE_ASSERT_BREAK(value != nullptr);
  1399. CarlaPlugin::setCustomData(type, key, value, false);
  1400. break;
  1401. }
  1402. case kPluginBridgeOscSetChunkDataFile: {
  1403. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1404. const char* const chunkFilePath = (const char*)&argv[0]->s;
  1405. CARLA_SAFE_ASSERT_BREAK(chunkFilePath != nullptr);
  1406. String realChunkFilePath(chunkFilePath);
  1407. carla_stdout("chunk save path BEFORE => %s", realChunkFilePath.toRawUTF8());
  1408. #ifndef CARLA_OS_WIN
  1409. // Using Wine, fix temp dir
  1410. if (fBinaryType == BINARY_WIN32 || fBinaryType == BINARY_WIN64)
  1411. {
  1412. // Get WINEPREFIX
  1413. String wineDir;
  1414. if (const char* const WINEPREFIX = getenv("WINEPREFIX"))
  1415. wineDir = String(WINEPREFIX);
  1416. else
  1417. wineDir = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  1418. const StringArray driveLetterSplit(StringArray::fromTokens(realChunkFilePath, ":/", ""));
  1419. realChunkFilePath = wineDir;
  1420. realChunkFilePath += "/drive_";
  1421. realChunkFilePath += driveLetterSplit[0].toLowerCase();
  1422. realChunkFilePath += "/";
  1423. realChunkFilePath += driveLetterSplit[1];
  1424. realChunkFilePath = realChunkFilePath.replace("\\", "/");
  1425. carla_stdout("chunk save path AFTER => %s", realChunkFilePath.toRawUTF8());
  1426. }
  1427. #endif
  1428. File chunkFile(realChunkFilePath);
  1429. if (chunkFile.existsAsFile())
  1430. {
  1431. fInfo.chunk = carla_getChunkFromBase64String(chunkFile.loadFileAsString().toRawUTF8());
  1432. chunkFile.deleteFile();
  1433. carla_stderr("chunk data final");
  1434. }
  1435. break;
  1436. }
  1437. case kPluginBridgeOscLatency:
  1438. // TODO
  1439. break;
  1440. case kPluginBridgeOscReady:
  1441. fInitiated = true;
  1442. break;
  1443. case kPluginBridgeOscError: {
  1444. CARLA_BRIDGE_CHECK_OSC_TYPES(1, "s");
  1445. const char* const error = (const char*)&argv[0]->s;
  1446. CARLA_ASSERT(error != nullptr);
  1447. pData->engine->setLastError(error);
  1448. fInitError = true;
  1449. fInitiated = true;
  1450. break;
  1451. }
  1452. }
  1453. return 0;
  1454. }
  1455. // -------------------------------------------------------------------
  1456. const void* getExtraStuff() const noexcept override
  1457. {
  1458. return fBridgeBinary.isNotEmpty() ? fBridgeBinary.buffer() : nullptr;
  1459. }
  1460. bool init(const char* const filename, const char* const name, const char* const label, const char* const bridgeBinary)
  1461. {
  1462. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1463. // ---------------------------------------------------------------
  1464. // first checks
  1465. if (pData->client != nullptr)
  1466. {
  1467. pData->engine->setLastError("Plugin client is already registered");
  1468. return false;
  1469. }
  1470. // ---------------------------------------------------------------
  1471. // set info
  1472. if (name != nullptr && name[0] != '\0')
  1473. pData->name = pData->engine->getUniquePluginName(name);
  1474. pData->filename = carla_strdup(filename);
  1475. if (bridgeBinary != nullptr)
  1476. fBridgeBinary = bridgeBinary;
  1477. std::srand(static_cast<uint>(std::time(nullptr)));
  1478. // ---------------------------------------------------------------
  1479. // SHM Audio Pool
  1480. {
  1481. char tmpFileBase[64];
  1482. std::sprintf(tmpFileBase, "/carla-bridge_shm_ap_XXXXXX");
  1483. fShmAudioPool.shm = shm_mkstemp(tmpFileBase);
  1484. if (! carla_is_shm_valid(fShmAudioPool.shm))
  1485. {
  1486. carla_stdout("Failed to open or create shared memory file #1");
  1487. return false;
  1488. }
  1489. fShmAudioPool.filename = tmpFileBase;
  1490. }
  1491. // ---------------------------------------------------------------
  1492. // SHM RT Control
  1493. {
  1494. char tmpFileBase[64];
  1495. std::sprintf(tmpFileBase, "/carla-bridge_shm_rt_XXXXXX");
  1496. fShmRtControl.shm = shm_mkstemp(tmpFileBase);
  1497. if (! carla_is_shm_valid(fShmRtControl.shm))
  1498. {
  1499. carla_stdout("Failed to open or create shared memory file #2");
  1500. // clear
  1501. carla_shm_close(fShmAudioPool.shm);
  1502. return false;
  1503. }
  1504. fShmRtControl.filename = tmpFileBase;
  1505. if (! fShmRtControl.mapData())
  1506. {
  1507. carla_stdout("Failed to map shared memory file #2");
  1508. // clear
  1509. carla_shm_close(fShmRtControl.shm);
  1510. carla_shm_close(fShmAudioPool.shm);
  1511. return false;
  1512. }
  1513. CARLA_SAFE_ASSERT(fShmRtControl.data != nullptr);
  1514. if (! jackbridge_sem_init(&fShmRtControl.data->sem.server))
  1515. {
  1516. carla_stdout("Failed to initialize shared memory semaphore #1");
  1517. // clear
  1518. fShmRtControl.unmapData();
  1519. carla_shm_close(fShmRtControl.shm);
  1520. carla_shm_close(fShmAudioPool.shm);
  1521. return false;
  1522. }
  1523. if (! jackbridge_sem_init(&fShmRtControl.data->sem.client))
  1524. {
  1525. carla_stdout("Failed to initialize shared memory semaphore #2");
  1526. // clear
  1527. jackbridge_sem_destroy(&fShmRtControl.data->sem.server);
  1528. fShmRtControl.unmapData();
  1529. carla_shm_close(fShmRtControl.shm);
  1530. carla_shm_close(fShmAudioPool.shm);
  1531. return false;
  1532. }
  1533. fNeedsSemDestroy = true;
  1534. }
  1535. // ---------------------------------------------------------------
  1536. // SHM Non-RT Control
  1537. {
  1538. char tmpFileBase[64];
  1539. std::sprintf(tmpFileBase, "/carla-bridge_shm_nonrt_XXXXXX");
  1540. fShmNonRtControl.shm = shm_mkstemp(tmpFileBase);
  1541. if (! carla_is_shm_valid(fShmNonRtControl.shm))
  1542. {
  1543. carla_stdout("Failed to open or create shared memory file #3");
  1544. return false;
  1545. }
  1546. fShmNonRtControl.filename = tmpFileBase;
  1547. if (! fShmNonRtControl.mapData())
  1548. {
  1549. carla_stdout("Failed to map shared memory file #3");
  1550. // clear
  1551. fShmNonRtControl.unmapData();
  1552. carla_shm_close(fShmNonRtControl.shm);
  1553. carla_shm_close(fShmRtControl.shm);
  1554. carla_shm_close(fShmAudioPool.shm);
  1555. return false;
  1556. }
  1557. }
  1558. carla_stdout("Carla Server Info:");
  1559. carla_stdout(" sizeof(BridgeRtData): " P_SIZE, sizeof(BridgeRtData));
  1560. carla_stdout(" sizeof(BridgeNonRtData): " P_SIZE, sizeof(BridgeNonRtData));
  1561. // initial values
  1562. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtNull);
  1563. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeRtData)));
  1564. fShmNonRtControl.writeInt(static_cast<int32_t>(sizeof(BridgeNonRtData)));
  1565. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetBufferSize);
  1566. fShmNonRtControl.writeInt(static_cast<int32_t>(pData->engine->getBufferSize()));
  1567. fShmNonRtControl.writeOpcode(kPluginBridgeNonRtSetSampleRate);
  1568. fShmNonRtControl.writeDouble(pData->engine->getSampleRate());
  1569. fShmNonRtControl.commitWrite();
  1570. // register plugin now so we can receive OSC (and wait for it)
  1571. pData->hints |= PLUGIN_IS_BRIDGE;
  1572. pData->engine->registerEnginePlugin(pData->id, this);
  1573. // init OSC
  1574. {
  1575. char shmIdStr[18+1] = { 0 };
  1576. std::strncpy(shmIdStr, &fShmAudioPool.filename[fShmAudioPool.filename.length()-6], 6);
  1577. std::strncat(shmIdStr, &fShmRtControl.filename[fShmRtControl.filename.length()-6], 6);
  1578. std::strncat(shmIdStr, &fShmNonRtControl.filename[fShmNonRtControl.filename.length()-6], 6);
  1579. pData->osc.thread.setOscData(bridgeBinary, label, getPluginTypeAsString(fPluginType), shmIdStr);
  1580. pData->osc.thread.startThread();
  1581. }
  1582. fInitiated = false;
  1583. fLastPongCounter = 0;
  1584. for (; fLastPongCounter++ < 500;)
  1585. {
  1586. if (fInitiated || ! pData->osc.thread.isThreadRunning())
  1587. break;
  1588. carla_msleep(25);
  1589. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1590. pData->engine->idle();
  1591. }
  1592. fLastPongCounter = -1;
  1593. if (fInitError || ! fInitiated)
  1594. {
  1595. pData->osc.thread.stopThread(6000);
  1596. if (! fInitError)
  1597. pData->engine->setLastError("Timeout while waiting for a response from plugin-bridge\n(or the plugin crashed on initialization?)");
  1598. return false;
  1599. }
  1600. // ---------------------------------------------------------------
  1601. // register client
  1602. if (pData->name == nullptr)
  1603. {
  1604. if (name != nullptr && name[0] != '\0')
  1605. pData->name = pData->engine->getUniquePluginName(name);
  1606. else if (label != nullptr && label[0] != '\0')
  1607. pData->name = pData->engine->getUniquePluginName(label);
  1608. else
  1609. pData->name = pData->engine->getUniquePluginName("unknown");
  1610. }
  1611. pData->client = pData->engine->addClient(this);
  1612. if (pData->client == nullptr || ! pData->client->isOk())
  1613. {
  1614. pData->engine->setLastError("Failed to register plugin client");
  1615. return false;
  1616. }
  1617. return true;
  1618. }
  1619. private:
  1620. const BinaryType fBinaryType;
  1621. const PluginType fPluginType;
  1622. bool fInitiated;
  1623. bool fInitError;
  1624. bool fSaved;
  1625. bool fNeedsSemDestroy;
  1626. bool fTimedOut;
  1627. volatile int32_t fLastPongCounter;
  1628. CarlaString fBridgeBinary;
  1629. BridgeAudioPool fShmAudioPool;
  1630. BridgeRtControl fShmRtControl;
  1631. BridgeNonRtControl fShmNonRtControl;
  1632. struct Info {
  1633. uint32_t aIns, aOuts;
  1634. uint32_t mIns, mOuts;
  1635. PluginCategory category;
  1636. uint optionsAvailable;
  1637. int64_t uniqueId;
  1638. CarlaString name;
  1639. CarlaString label;
  1640. CarlaString maker;
  1641. CarlaString copyright;
  1642. std::vector<uint8_t> chunk;
  1643. Info()
  1644. : aIns(0),
  1645. aOuts(0),
  1646. mIns(0),
  1647. mOuts(0),
  1648. category(PLUGIN_CATEGORY_NONE),
  1649. optionsAvailable(0),
  1650. uniqueId(0),
  1651. name(),
  1652. label(),
  1653. maker(),
  1654. copyright(),
  1655. chunk() {}
  1656. } fInfo;
  1657. BridgeParamInfo* fParams;
  1658. void resizeAudioPool(const uint32_t bufferSize)
  1659. {
  1660. fShmAudioPool.resize(bufferSize, fInfo.aIns+fInfo.aOuts);
  1661. fShmRtControl.writeOpcode(kPluginBridgeRtSetAudioPool);
  1662. fShmRtControl.writeLong(static_cast<int64_t>(fShmAudioPool.size));
  1663. fShmRtControl.commitWrite();
  1664. waitForServer();
  1665. }
  1666. bool waitForServer(const int secs = 5)
  1667. {
  1668. CARLA_SAFE_ASSERT_RETURN(! fTimedOut, false);
  1669. if (! fShmRtControl.waitForServer(secs))
  1670. {
  1671. carla_stderr("waitForServer() timeout here");
  1672. fTimedOut = true;
  1673. return false;
  1674. }
  1675. return true;
  1676. }
  1677. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BridgePlugin)
  1678. };
  1679. CARLA_BACKEND_END_NAMESPACE
  1680. #endif // ! BUILD_BRIDGE
  1681. // -------------------------------------------------------------------------------------------------------------------
  1682. CARLA_BACKEND_START_NAMESPACE
  1683. CarlaPlugin* CarlaPlugin::newBridge(const Initializer& init, BinaryType btype, PluginType ptype, const char* const bridgeBinary)
  1684. {
  1685. carla_debug("CarlaPlugin::newBridge({%p, \"%s\", \"%s\", \"%s\"}, %s, %s, \"%s\")", init.engine, init.filename, init.name, init.label, BinaryType2Str(btype), PluginType2Str(ptype), bridgeBinary);
  1686. #ifndef BUILD_BRIDGE
  1687. if (bridgeBinary == nullptr || bridgeBinary[0] == '\0')
  1688. {
  1689. init.engine->setLastError("Bridge not possible, bridge-binary not found");
  1690. return nullptr;
  1691. }
  1692. BridgePlugin* const plugin(new BridgePlugin(init.engine, init.id, btype, ptype));
  1693. if (! plugin->init(init.filename, init.name, init.label, bridgeBinary))
  1694. {
  1695. init.engine->registerEnginePlugin(init.id, nullptr);
  1696. delete plugin;
  1697. return nullptr;
  1698. }
  1699. plugin->reload();
  1700. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1701. {
  1702. init.engine->setLastError("Carla's rack mode can only work with Stereo Bridged plugins, sorry!");
  1703. delete plugin;
  1704. return nullptr;
  1705. }
  1706. return plugin;
  1707. #else
  1708. init.engine->setLastError("Plugin bridge support not available");
  1709. return nullptr;
  1710. // unused
  1711. (void)bridgeBinary;
  1712. #endif
  1713. }
  1714. #ifndef BUILD_BRIDGE
  1715. // -------------------------------------------------------------------------------------------------------------------
  1716. // Bridge Helper
  1717. #define bridgePlugin ((BridgePlugin*)plugin)
  1718. extern int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1719. const int argc, const lo_arg* const* const argv, const char* const types);
  1720. int CarlaPluginSetOscBridgeInfo(CarlaPlugin* const plugin, const PluginBridgeOscInfoType type,
  1721. const int argc, const lo_arg* const* const argv, const char* const types)
  1722. {
  1723. CARLA_SAFE_ASSERT(plugin != nullptr && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0);
  1724. return bridgePlugin->setOscPluginBridgeInfo(type, argc, argv, types);
  1725. }
  1726. #undef bridgePlugin
  1727. #endif
  1728. CARLA_BACKEND_END_NAMESPACE
  1729. // -------------------------------------------------------------------------------------------------------------------