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 71KB

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