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.

CarlaPluginJuce.cpp 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * Carla Juce Plugin
  3. * Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #if defined(USING_JUCE)
  20. #include "CarlaBackendUtils.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  23. # pragma GCC diagnostic push
  24. # pragma GCC diagnostic ignored "-Wcast-qual"
  25. # pragma GCC diagnostic ignored "-Wclass-memaccess"
  26. # pragma GCC diagnostic ignored "-Wconversion"
  27. # pragma GCC diagnostic ignored "-Wdouble-promotion"
  28. # pragma GCC diagnostic ignored "-Weffc++"
  29. # pragma GCC diagnostic ignored "-Wfloat-equal"
  30. # pragma GCC diagnostic ignored "-Woverloaded-virtual"
  31. # pragma GCC diagnostic ignored "-Wsign-conversion"
  32. # pragma GCC diagnostic ignored "-Wundef"
  33. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  34. #endif
  35. #include "AppConfig.h"
  36. #include "juce_audio_processors/juce_audio_processors.h"
  37. #include "juce_gui_basics/juce_gui_basics.h"
  38. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  39. # pragma GCC diagnostic pop
  40. #endif
  41. #include "JucePluginWindow.hpp"
  42. namespace juce {
  43. extern bool juce_isRunningInWine();
  44. }
  45. CARLA_BACKEND_START_NAMESPACE
  46. // -------------------------------------------------------------------------------------------------------------------
  47. // Fallback data
  48. static const ExternalMidiNote kExternalMidiNoteFallback = { -1, 0, 0 };
  49. // -------------------------------------------------------------------------------------------------------------------
  50. class CarlaPluginJuce : public CarlaPlugin,
  51. private juce::AudioPlayHead,
  52. private juce::AudioProcessorListener
  53. {
  54. public:
  55. CarlaPluginJuce(CarlaEngine* const engine, const uint id)
  56. : CarlaPlugin(engine, id),
  57. fDesc(),
  58. fInstance(nullptr),
  59. fFormatManager(),
  60. fAudioBuffer(),
  61. fMidiBuffer(),
  62. fPosInfo(),
  63. fChunk(),
  64. fFormatName(),
  65. fWindow()
  66. {
  67. carla_debug("CarlaPluginJuce::CarlaPluginJuce(%p, %i)", engine, id);
  68. fMidiBuffer.ensureSize(2048);
  69. fMidiBuffer.clear();
  70. fPosInfo.resetToDefault();
  71. }
  72. ~CarlaPluginJuce() override
  73. {
  74. carla_debug("CarlaPluginJuce::~CarlaPluginJuce()");
  75. // close UI
  76. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  77. showCustomUI(false);
  78. pData->singleMutex.lock();
  79. pData->masterMutex.lock();
  80. if (pData->client != nullptr && pData->client->isActive())
  81. pData->client->deactivate();
  82. if (pData->active)
  83. {
  84. deactivate();
  85. pData->active = false;
  86. }
  87. if (fInstance != nullptr)
  88. {
  89. delete fInstance;
  90. fInstance = nullptr;
  91. }
  92. clearBuffers();
  93. }
  94. // -------------------------------------------------------------------
  95. // Information (base)
  96. PluginType getType() const noexcept override
  97. {
  98. return getPluginTypeFromString(fDesc.pluginFormatName.toRawUTF8());
  99. }
  100. PluginCategory getCategory() const noexcept override
  101. {
  102. if (fDesc.isInstrument)
  103. return PLUGIN_CATEGORY_SYNTH;
  104. return getPluginCategoryFromName(fDesc.category.toRawUTF8());
  105. }
  106. int64_t getUniqueId() const noexcept override
  107. {
  108. return fDesc.uid;
  109. }
  110. // -------------------------------------------------------------------
  111. // Information (count)
  112. // nothing
  113. // -------------------------------------------------------------------
  114. // Information (current data)
  115. std::size_t getChunkData(void** const dataPtr) noexcept override
  116. {
  117. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS, 0);
  118. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0);
  119. CARLA_SAFE_ASSERT_RETURN(dataPtr != nullptr, 0);
  120. *dataPtr = nullptr;
  121. try {
  122. fChunk.reset();
  123. fInstance->getStateInformation(fChunk);
  124. } CARLA_SAFE_EXCEPTION_RETURN("CarlaPluginJuce::getChunkData", 0);
  125. if (const std::size_t size = fChunk.getSize())
  126. {
  127. *dataPtr = fChunk.getData();
  128. return size;
  129. }
  130. return 0;
  131. }
  132. // -------------------------------------------------------------------
  133. // Information (per-plugin data)
  134. uint getOptionsAvailable() const noexcept override
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0x0);
  137. uint options = 0x0;
  138. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  139. options |= PLUGIN_OPTION_USE_CHUNKS;
  140. if (fInstance->getNumPrograms() > 1)
  141. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  142. if (fInstance->acceptsMidi())
  143. {
  144. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  145. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  146. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  147. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  148. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  149. }
  150. return options;
  151. }
  152. float getParameterValue(const uint32_t parameterId) const noexcept override
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  155. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0.0f);
  156. return fInstance->getParameter(static_cast<int>(parameterId));
  157. }
  158. void getLabel(char* const strBuf) const noexcept override
  159. {
  160. if (fDesc.pluginFormatName == "AU" || fDesc.pluginFormatName == "AudioUnit")
  161. std::strncpy(strBuf, fDesc.fileOrIdentifier.toRawUTF8(), STR_MAX);
  162. else
  163. std::strncpy(strBuf, fDesc.name.toRawUTF8(), STR_MAX);
  164. }
  165. void getMaker(char* const strBuf) const noexcept override
  166. {
  167. std::strncpy(strBuf, fDesc.manufacturerName.toRawUTF8(), STR_MAX);
  168. }
  169. void getCopyright(char* const strBuf) const noexcept override
  170. {
  171. getMaker(strBuf);
  172. }
  173. void getRealName(char* const strBuf) const noexcept override
  174. {
  175. std::strncpy(strBuf, fDesc.descriptiveName.toRawUTF8(), STR_MAX);
  176. }
  177. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  180. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  181. std::strncpy(strBuf, fInstance->getParameterName(static_cast<int>(parameterId), STR_MAX).toRawUTF8(), STR_MAX);
  182. }
  183. void getParameterText(const uint32_t parameterId, char* const strBuf) noexcept override
  184. {
  185. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  186. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  187. std::strncpy(strBuf, fInstance->getParameterText(static_cast<int>(parameterId), STR_MAX).toRawUTF8(), STR_MAX);
  188. }
  189. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  192. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  193. std::strncpy(strBuf, fInstance->getParameterLabel(static_cast<int>(parameterId)).toRawUTF8(), STR_MAX);
  194. }
  195. // -------------------------------------------------------------------
  196. // Set data (state)
  197. // nothing
  198. // -------------------------------------------------------------------
  199. // Set data (internal stuff)
  200. void setName(const char* const newName) override
  201. {
  202. CarlaPlugin::setName(newName);
  203. if (fWindow != nullptr)
  204. {
  205. juce::String uiName(pData->name);
  206. uiName += " (GUI)";
  207. fWindow->setName(uiName);
  208. }
  209. }
  210. // -------------------------------------------------------------------
  211. // Set data (plugin-specific stuff)
  212. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  213. {
  214. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  215. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  216. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  217. fInstance->setParameter(static_cast<int>(parameterId), value);
  218. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  219. }
  220. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  223. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  224. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  225. fInstance->setParameter(static_cast<int>(parameterId), value);
  226. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  227. }
  228. void setChunkData(const void* const data, const std::size_t dataSize) override
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(pData->options & PLUGIN_OPTION_USE_CHUNKS,);
  231. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  232. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  233. CARLA_SAFE_ASSERT_RETURN(dataSize > 0,);
  234. if (isJuceSaveFormat(data, dataSize))
  235. {
  236. const ScopedSingleProcessLocker spl(this, true);
  237. fInstance->setStateInformation(data, static_cast<int>(dataSize));
  238. }
  239. else
  240. {
  241. uint8_t* const dataCompat = (uint8_t*)std::malloc(dataSize + 160);
  242. CARLA_SAFE_ASSERT_RETURN(dataCompat != nullptr,);
  243. carla_stdout("NOTE: Loading plugin state in Carla compatibiity mode");
  244. std::memset(dataCompat, 0, 160);
  245. std::memcpy(dataCompat+160, data, dataSize);
  246. int32_t* const set = (int32_t*)dataCompat;
  247. set[0] = (int32_t)juce::ByteOrder::littleEndianInt("CcnK");
  248. set[2] = (int32_t)juce::ByteOrder::littleEndianInt("FBCh");
  249. set[3] = fxbSwap(1);
  250. set[39] = fxbSwap(static_cast<int32_t>(dataSize));
  251. {
  252. const ScopedSingleProcessLocker spl(this, true);
  253. fInstance->setStateInformation(dataCompat, static_cast<int>(dataSize+160));
  254. }
  255. std::free(dataCompat);
  256. }
  257. pData->updateParameterValues(this, true, true, false);
  258. }
  259. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  262. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  263. if (index >= 0)
  264. {
  265. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  266. try {
  267. fInstance->setCurrentProgram(index);
  268. } CARLA_SAFE_EXCEPTION("setCurrentProgram");
  269. }
  270. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  271. }
  272. // -------------------------------------------------------------------
  273. // Set ui stuff
  274. void showCustomUI(const bool yesNo) override
  275. {
  276. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  277. if (yesNo)
  278. {
  279. if (fWindow == nullptr)
  280. {
  281. juce::String uiName(pData->name);
  282. uiName += " (GUI)";
  283. fWindow = new JucePluginWindow(pData->engine->getOptions().frontendWinId);
  284. fWindow->setName(uiName);
  285. }
  286. if (juce::AudioProcessorEditor* const editor = fInstance->createEditorIfNeeded())
  287. fWindow->show(editor);
  288. }
  289. else
  290. {
  291. if (fWindow != nullptr)
  292. fWindow->hide();
  293. if (juce::AudioProcessorEditor* const editor = fInstance->getActiveEditor())
  294. delete editor;
  295. fWindow = nullptr;
  296. }
  297. }
  298. void uiIdle() override
  299. {
  300. if (fWindow != nullptr)
  301. {
  302. if (fWindow->wasClosedByUser())
  303. {
  304. showCustomUI(false);
  305. pData->engine->callback(true, true,
  306. ENGINE_CALLBACK_UI_STATE_CHANGED,
  307. pData->id,
  308. 0,
  309. 0, 0, 0.0f, nullptr);
  310. }
  311. }
  312. CarlaPlugin::uiIdle();
  313. }
  314. // -------------------------------------------------------------------
  315. // Plugin state
  316. void reload() override
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  319. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  320. carla_debug("CarlaPluginJuce::reload() - start");
  321. const EngineProcessMode processMode(pData->engine->getProccessMode());
  322. // Safely disable plugin for reload
  323. const ScopedDisabler sd(this);
  324. if (pData->active)
  325. deactivate();
  326. clearBuffers();
  327. fInstance->refreshParameterList();
  328. uint32_t aIns, aOuts, mIns, mOuts, params;
  329. mIns = mOuts = 0;
  330. bool needsCtrlIn, needsCtrlOut;
  331. needsCtrlIn = needsCtrlOut = false;
  332. aIns = (fInstance->getTotalNumInputChannels() > 0) ? static_cast<uint32_t>(fInstance->getTotalNumInputChannels()) : 0;
  333. aOuts = (fInstance->getTotalNumOutputChannels() > 0) ? static_cast<uint32_t>(fInstance->getTotalNumOutputChannels()) : 0;
  334. params = (fInstance->getNumParameters() > 0) ? static_cast<uint32_t>(fInstance->getNumParameters()) : 0;
  335. if (fInstance->acceptsMidi())
  336. {
  337. mIns = 1;
  338. needsCtrlIn = true;
  339. }
  340. if (fInstance->producesMidi())
  341. {
  342. mOuts = 1;
  343. needsCtrlOut = true;
  344. }
  345. if (aIns > 0)
  346. {
  347. pData->audioIn.createNew(aIns);
  348. }
  349. if (aOuts > 0)
  350. {
  351. pData->audioOut.createNew(aOuts);
  352. needsCtrlIn = true;
  353. }
  354. if (params > 0)
  355. {
  356. pData->param.createNew(params, false);
  357. needsCtrlIn = true;
  358. }
  359. const uint portNameSize(pData->engine->getMaxPortNameSize());
  360. CarlaString portName;
  361. // Audio Ins
  362. for (uint32_t j=0; j < aIns; ++j)
  363. {
  364. portName.clear();
  365. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  366. {
  367. portName = pData->name;
  368. portName += ":";
  369. }
  370. if (aIns > 1)
  371. {
  372. portName += "input_";
  373. portName += CarlaString(j+1);
  374. }
  375. else
  376. portName += "input";
  377. portName.truncate(portNameSize);
  378. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  379. pData->audioIn.ports[j].rindex = j;
  380. }
  381. // Audio Outs
  382. for (uint32_t j=0; j < aOuts; ++j)
  383. {
  384. portName.clear();
  385. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  386. {
  387. portName = pData->name;
  388. portName += ":";
  389. }
  390. if (aOuts > 1)
  391. {
  392. portName += "output_";
  393. portName += CarlaString(j+1);
  394. }
  395. else
  396. portName += "output";
  397. portName.truncate(portNameSize);
  398. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  399. pData->audioOut.ports[j].rindex = j;
  400. }
  401. for (uint32_t j=0; j < params; ++j)
  402. {
  403. pData->param.data[j].type = PARAMETER_INPUT;
  404. pData->param.data[j].index = static_cast<int32_t>(j);
  405. pData->param.data[j].rindex = static_cast<int32_t>(j);
  406. float min, max, def, step, stepSmall, stepLarge;
  407. // TODO
  408. //const int numSteps(fInstance->getParameterNumSteps(static_cast<int>(j)));
  409. {
  410. min = 0.0f;
  411. max = 1.0f;
  412. step = 0.001f;
  413. stepSmall = 0.0001f;
  414. stepLarge = 0.1f;
  415. }
  416. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  417. #ifndef BUILD_BRIDGE
  418. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  419. #endif
  420. if (fInstance->isParameterAutomatable(static_cast<int>(j)))
  421. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  422. // FIXME?
  423. def = fInstance->getParameterDefaultValue(static_cast<int>(j));
  424. if (def < min)
  425. def = min;
  426. else if (def > max)
  427. def = max;
  428. pData->param.ranges[j].min = min;
  429. pData->param.ranges[j].max = max;
  430. pData->param.ranges[j].def = def;
  431. pData->param.ranges[j].step = step;
  432. pData->param.ranges[j].stepSmall = stepSmall;
  433. pData->param.ranges[j].stepLarge = stepLarge;
  434. }
  435. if (needsCtrlIn)
  436. {
  437. portName.clear();
  438. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  439. {
  440. portName = pData->name;
  441. portName += ":";
  442. }
  443. portName += "events-in";
  444. portName.truncate(portNameSize);
  445. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  446. }
  447. if (needsCtrlOut)
  448. {
  449. portName.clear();
  450. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  451. {
  452. portName = pData->name;
  453. portName += ":";
  454. }
  455. portName += "events-out";
  456. portName.truncate(portNameSize);
  457. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  458. }
  459. // plugin hints
  460. pData->hints = 0x0;
  461. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  462. if (fDesc.isInstrument)
  463. pData->hints |= PLUGIN_IS_SYNTH;
  464. if (fInstance->hasEditor())
  465. {
  466. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  467. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  468. }
  469. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  470. pData->hints |= PLUGIN_CAN_DRYWET;
  471. if (aOuts > 0)
  472. pData->hints |= PLUGIN_CAN_VOLUME;
  473. if (aOuts >= 2 && aOuts % 2 == 0)
  474. pData->hints |= PLUGIN_CAN_BALANCE;
  475. // extra plugin hints
  476. pData->extraHints = 0x0;
  477. if (mIns > 0)
  478. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  479. if (mOuts > 0)
  480. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  481. fInstance->setPlayConfigDetails(static_cast<int>(aIns), static_cast<int>(aOuts), pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  482. bufferSizeChanged(pData->engine->getBufferSize());
  483. reloadPrograms(true);
  484. if (pData->active)
  485. activate();
  486. carla_debug("CarlaPluginJuce::reload() - end");
  487. }
  488. void reloadPrograms(const bool doInit) override
  489. {
  490. carla_debug("CarlaPluginJuce::reloadPrograms(%s)", bool2str(doInit));
  491. const uint32_t oldCount = pData->prog.count;
  492. const int32_t current = pData->prog.current;
  493. // Delete old programs
  494. pData->prog.clear();
  495. // Query new programs
  496. uint32_t newCount = (fInstance->getNumPrograms() > 0) ? static_cast<uint32_t>(fInstance->getNumPrograms()) : 0;
  497. if (newCount > 0)
  498. {
  499. pData->prog.createNew(newCount);
  500. // Update names
  501. for (int i=0, count=fInstance->getNumPrograms(); i<count; ++i)
  502. pData->prog.names[i] = carla_strdup(fInstance->getProgramName(i).toRawUTF8());
  503. }
  504. if (doInit)
  505. {
  506. if (newCount > 0)
  507. setProgram(0, false, false, false, true);
  508. }
  509. else
  510. {
  511. // Check if current program is invalid
  512. bool programChanged = false;
  513. if (newCount == oldCount+1)
  514. {
  515. // one program added, probably created by user
  516. pData->prog.current = static_cast<int32_t>(oldCount);
  517. programChanged = true;
  518. }
  519. else if (current < 0 && newCount > 0)
  520. {
  521. // programs exist now, but not before
  522. pData->prog.current = 0;
  523. programChanged = true;
  524. }
  525. else if (current >= 0 && newCount == 0)
  526. {
  527. // programs existed before, but not anymore
  528. pData->prog.current = -1;
  529. programChanged = true;
  530. }
  531. else if (current >= static_cast<int32_t>(newCount))
  532. {
  533. // current program > count
  534. pData->prog.current = 0;
  535. programChanged = true;
  536. }
  537. else
  538. {
  539. // no change
  540. pData->prog.current = current;
  541. }
  542. if (programChanged)
  543. {
  544. setProgram(pData->prog.current, true, true, true, false);
  545. }
  546. else
  547. {
  548. // Program was changed during update, re-set it
  549. if (pData->prog.current >= 0)
  550. fInstance->setCurrentProgram(pData->prog.current);
  551. }
  552. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  553. }
  554. }
  555. // -------------------------------------------------------------------
  556. // Plugin processing
  557. void activate() noexcept override
  558. {
  559. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  560. try {
  561. fInstance->prepareToPlay(pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  562. } catch(...) {}
  563. }
  564. void deactivate() noexcept override
  565. {
  566. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  567. try {
  568. fInstance->releaseResources();
  569. } catch(...) {}
  570. }
  571. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  572. {
  573. // --------------------------------------------------------------------------------------------------------
  574. // Check if active
  575. if (! pData->active)
  576. {
  577. // disable any output sound
  578. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  579. carla_zeroFloats(audioOut[i], frames);
  580. return;
  581. }
  582. // --------------------------------------------------------------------------------------------------------
  583. // Check if needs reset
  584. if (pData->needsReset)
  585. {
  586. fInstance->reset();
  587. pData->needsReset = false;
  588. }
  589. // --------------------------------------------------------------------------------------------------------
  590. // Event Input
  591. fMidiBuffer.clear();
  592. if (pData->event.portIn != nullptr)
  593. {
  594. // ----------------------------------------------------------------------------------------------------
  595. // MIDI Input (External)
  596. if (pData->extNotes.mutex.tryLock())
  597. {
  598. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  599. {
  600. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  601. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  602. uint8_t midiEvent[3];
  603. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  604. midiEvent[1] = note.note;
  605. midiEvent[2] = note.velo;
  606. fMidiBuffer.addEvent(midiEvent, 3, 0);
  607. }
  608. pData->extNotes.data.clear();
  609. pData->extNotes.mutex.unlock();
  610. } // End of MIDI Input (External)
  611. // ----------------------------------------------------------------------------------------------------
  612. // Event Input (System)
  613. #ifndef BUILD_BRIDGE
  614. bool allNotesOffSent = false;
  615. #endif
  616. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  617. {
  618. const EngineEvent& event(pData->event.portIn->getEvent(i));
  619. if (event.time >= frames)
  620. continue;
  621. switch (event.type)
  622. {
  623. case kEngineEventTypeNull:
  624. break;
  625. case kEngineEventTypeControl: {
  626. const EngineControlEvent& ctrlEvent(event.ctrl);
  627. switch (ctrlEvent.type)
  628. {
  629. case kEngineControlEventTypeNull:
  630. break;
  631. case kEngineControlEventTypeParameter: {
  632. #ifndef BUILD_BRIDGE
  633. // Control backend stuff
  634. if (event.channel == pData->ctrlChannel)
  635. {
  636. float value;
  637. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  638. {
  639. value = ctrlEvent.value;
  640. setDryWetRT(value);
  641. }
  642. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  643. {
  644. value = ctrlEvent.value*127.0f/100.0f;
  645. setVolumeRT(value);
  646. }
  647. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  648. {
  649. float left, right;
  650. value = ctrlEvent.value/0.5f - 1.0f;
  651. if (value < 0.0f)
  652. {
  653. left = -1.0f;
  654. right = (value*2.0f)+1.0f;
  655. }
  656. else if (value > 0.0f)
  657. {
  658. left = (value*2.0f)-1.0f;
  659. right = 1.0f;
  660. }
  661. else
  662. {
  663. left = -1.0f;
  664. right = 1.0f;
  665. }
  666. setBalanceLeftRT(left);
  667. setBalanceRightRT(right);
  668. }
  669. }
  670. #endif
  671. // Control plugin parameters
  672. uint32_t k;
  673. for (k=0; k < pData->param.count; ++k)
  674. {
  675. if (pData->param.data[k].midiChannel != event.channel)
  676. continue;
  677. if (pData->param.data[k].midiCC != ctrlEvent.param)
  678. continue;
  679. if (pData->param.data[k].type != PARAMETER_INPUT)
  680. continue;
  681. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  682. continue;
  683. float value;
  684. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  685. {
  686. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  687. }
  688. else
  689. {
  690. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  691. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  692. else
  693. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  694. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  695. value = std::rint(value);
  696. }
  697. setParameterValueRT(k, value);
  698. }
  699. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  700. {
  701. uint8_t midiData[3];
  702. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  703. midiData[1] = uint8_t(ctrlEvent.param);
  704. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  705. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  706. }
  707. break;
  708. } // case kEngineControlEventTypeParameter
  709. case kEngineControlEventTypeMidiBank:
  710. break;
  711. case kEngineControlEventTypeMidiProgram:
  712. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  713. {
  714. if (ctrlEvent.param < pData->prog.count)
  715. {
  716. setProgramRT(ctrlEvent.param);
  717. break;
  718. }
  719. }
  720. break;
  721. case kEngineControlEventTypeAllSoundOff:
  722. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  723. {
  724. uint8_t midiData[3];
  725. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  726. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  727. midiData[2] = 0;
  728. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  729. }
  730. break;
  731. case kEngineControlEventTypeAllNotesOff:
  732. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  733. {
  734. #ifndef BUILD_BRIDGE
  735. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  736. {
  737. allNotesOffSent = true;
  738. postponeRtAllNotesOff();
  739. }
  740. #endif
  741. uint8_t midiData[3];
  742. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  743. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  744. midiData[2] = 0;
  745. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  746. }
  747. break;
  748. } // switch (ctrlEvent.type)
  749. break;
  750. } // case kEngineEventTypeControl
  751. case kEngineEventTypeMidi: {
  752. const EngineMidiEvent& midiEvent(event.midi);
  753. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  754. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  755. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  756. continue;
  757. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  758. continue;
  759. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  760. continue;
  761. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  762. continue;
  763. // Fix bad note-off
  764. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  765. status = MIDI_STATUS_NOTE_OFF;
  766. // put back channel in data
  767. uint8_t midiData2[midiEvent.size];
  768. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  769. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  770. fMidiBuffer.addEvent(midiData2, midiEvent.size, static_cast<int>(event.time));
  771. if (status == MIDI_STATUS_NOTE_ON)
  772. {
  773. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  774. event.channel,
  775. midiData[1],
  776. midiData[2],
  777. 0.0f);
  778. }
  779. else if (status == MIDI_STATUS_NOTE_OFF)
  780. {
  781. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  782. event.channel,
  783. midiData[1],
  784. 0, 0.0f);
  785. }
  786. } break;
  787. } // switch (event.type)
  788. }
  789. pData->postRtEvents.trySplice();
  790. } // End of Event Input
  791. // --------------------------------------------------------------------------------------------------------
  792. // Set TimeInfo
  793. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  794. fPosInfo.isPlaying = timeInfo.playing;
  795. if (timeInfo.bbt.valid)
  796. {
  797. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  798. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  799. const double ppqBar = static_cast<double>(timeInfo.bbt.beatsPerBar) * (timeInfo.bbt.bar - 1);
  800. const double ppqBeat = static_cast<double>(timeInfo.bbt.beat - 1);
  801. const double ppqTick = timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat;
  802. fPosInfo.bpm = timeInfo.bbt.beatsPerMinute;
  803. fPosInfo.timeSigNumerator = static_cast<int>(timeInfo.bbt.beatsPerBar);
  804. fPosInfo.timeSigDenominator = static_cast<int>(timeInfo.bbt.beatType);
  805. fPosInfo.timeInSamples = static_cast<int64_t>(timeInfo.frame);
  806. fPosInfo.timeInSeconds = static_cast<double>(fPosInfo.timeInSamples)/pData->engine->getSampleRate();
  807. fPosInfo.ppqPosition = ppqBar + ppqBeat + ppqTick;
  808. fPosInfo.ppqPositionOfLastBarStart = ppqBar;
  809. }
  810. // --------------------------------------------------------------------------------------------------------
  811. // Process
  812. processSingle(audioIn, audioOut, frames);
  813. }
  814. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames)
  815. {
  816. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  817. if (pData->audioIn.count > 0)
  818. {
  819. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  820. }
  821. if (pData->audioOut.count > 0)
  822. {
  823. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  824. }
  825. // --------------------------------------------------------------------------------------------------------
  826. // Try lock, silence otherwise
  827. if (pData->engine->isOffline())
  828. {
  829. pData->singleMutex.lock();
  830. }
  831. else if (! pData->singleMutex.tryLock())
  832. {
  833. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  834. carla_zeroFloats(outBuffer[i], frames);
  835. return false;
  836. }
  837. // --------------------------------------------------------------------------------------------------------
  838. // Set audio in buffers
  839. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  840. fAudioBuffer.copyFrom(static_cast<int>(i), 0, inBuffer[i], static_cast<int>(frames));
  841. // --------------------------------------------------------------------------------------------------------
  842. // Run plugin
  843. fInstance->processBlock(fAudioBuffer, fMidiBuffer);
  844. // --------------------------------------------------------------------------------------------------------
  845. // Set audio out buffers
  846. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  847. carla_copyFloats(outBuffer[i], fAudioBuffer.getReadPointer(static_cast<int>(i)), frames);
  848. // --------------------------------------------------------------------------------------------------------
  849. // Midi out
  850. if (! fMidiBuffer.isEmpty())
  851. {
  852. if (pData->event.portOut != nullptr)
  853. {
  854. const uint8_t* midiEventData;
  855. int midiEventSize, midiEventPosition;
  856. for (juce::MidiBuffer::Iterator i(fMidiBuffer); i.getNextEvent(midiEventData, midiEventSize, midiEventPosition);)
  857. {
  858. CARLA_SAFE_ASSERT_BREAK(midiEventPosition >= 0 && midiEventPosition < static_cast<int>(frames));
  859. CARLA_SAFE_ASSERT_BREAK(midiEventSize > 0);
  860. if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(midiEventPosition), static_cast<uint8_t>(midiEventSize), midiEventData))
  861. break;
  862. }
  863. }
  864. fMidiBuffer.clear();
  865. }
  866. // --------------------------------------------------------------------------------------------------------
  867. pData->singleMutex.unlock();
  868. return true;
  869. }
  870. void bufferSizeChanged(const uint32_t newBufferSize) override
  871. {
  872. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  873. carla_debug("CarlaPluginJuce::bufferSizeChanged(%i)", newBufferSize);
  874. fAudioBuffer.setSize(static_cast<int>(std::max<uint32_t>(pData->audioIn.count, pData->audioOut.count)), static_cast<int>(newBufferSize));
  875. if (pData->active)
  876. {
  877. deactivate();
  878. activate();
  879. }
  880. }
  881. void sampleRateChanged(const double newSampleRate) override
  882. {
  883. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  884. carla_debug("CarlaPluginJuce::sampleRateChanged(%g)", newSampleRate);
  885. if (pData->active)
  886. {
  887. deactivate();
  888. activate();
  889. }
  890. }
  891. // -------------------------------------------------------------------
  892. // Plugin buffers
  893. // nothing
  894. // -------------------------------------------------------------------
  895. // Post-poned UI Stuff
  896. // nothing
  897. // -------------------------------------------------------------------
  898. void* getNativeHandle() const noexcept override
  899. {
  900. return (fInstance != nullptr) ? fInstance->getPlatformSpecificData() : nullptr;
  901. }
  902. // -------------------------------------------------------------------
  903. protected:
  904. void audioProcessorParameterChanged(juce::AudioProcessor*, int index, float value) override
  905. {
  906. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  907. const uint32_t uindex(static_cast<uint32_t>(index));
  908. const float fixedValue(pData->param.getFixedValue(uindex, value));
  909. CarlaPlugin::setParameterValue(static_cast<uint32_t>(index), fixedValue, false, true, true);
  910. }
  911. void audioProcessorChanged(juce::AudioProcessor*) override
  912. {
  913. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  914. }
  915. void audioProcessorParameterChangeGestureBegin(juce::AudioProcessor*, int index) override
  916. {
  917. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  918. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), true);
  919. }
  920. void audioProcessorParameterChangeGestureEnd(juce::AudioProcessor*, int index) override
  921. {
  922. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  923. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), false);
  924. }
  925. bool getCurrentPosition(CurrentPositionInfo& result) override
  926. {
  927. carla_copyStruct(result, fPosInfo);
  928. return true;
  929. }
  930. // -------------------------------------------------------------------
  931. public:
  932. bool init(const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const uint options, const char* const format)
  933. {
  934. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  935. // ---------------------------------------------------------------
  936. // first checks
  937. if (pData->client != nullptr)
  938. {
  939. pData->engine->setLastError("Plugin client is already registered");
  940. return false;
  941. }
  942. if (format == nullptr || format[0] == '\0')
  943. {
  944. pData->engine->setLastError("null format");
  945. return false;
  946. }
  947. // AU and VST3 require label
  948. if (std::strcmp(format, "AU") == 0 || std::strcmp(format, "VST3") == 0)
  949. {
  950. if (label == nullptr || label[0] == '\0')
  951. {
  952. pData->engine->setLastError("null label");
  953. return false;
  954. }
  955. }
  956. juce::String fileOrIdentifier;
  957. if (std::strcmp(format, "AU") == 0)
  958. {
  959. fileOrIdentifier = label;
  960. }
  961. else
  962. {
  963. // VST2 and VST3 require filename
  964. if (filename == nullptr || filename[0] == '\0')
  965. {
  966. pData->engine->setLastError("null filename");
  967. return false;
  968. }
  969. juce::String jfilename(filename);
  970. #ifdef CARLA_OS_WIN
  971. // Fix for wine usage
  972. if (juce::juce_isRunningInWine() && filename[0] == '/')
  973. {
  974. jfilename.replace("/", "\\");
  975. jfilename = "Z:" + jfilename;
  976. }
  977. #endif
  978. fileOrIdentifier = jfilename;
  979. if (label != nullptr && label[0] != '\0')
  980. fDesc.name = label;
  981. }
  982. fFormatManager.addDefaultFormats();
  983. {
  984. juce::OwnedArray<juce::PluginDescription> pluginDescriptions;
  985. juce::KnownPluginList plist;
  986. for (int i = 0; i < fFormatManager.getNumFormats(); ++i)
  987. plist.scanAndAddFile(fileOrIdentifier, true, pluginDescriptions, *fFormatManager.getFormat(i));
  988. if (pluginDescriptions.size() == 0)
  989. {
  990. pData->engine->setLastError("Failed to get plugin description");
  991. return false;
  992. }
  993. fDesc = *pluginDescriptions[0];
  994. }
  995. if (uniqueId != 0)
  996. fDesc.uid = static_cast<int>(uniqueId);
  997. juce::String error;
  998. fInstance = fFormatManager.createPluginInstance(fDesc,
  999. pData->engine->getSampleRate(),
  1000. static_cast<int>(pData->engine->getBufferSize()),
  1001. error);
  1002. if (fInstance == nullptr)
  1003. {
  1004. pData->engine->setLastError(error.toRawUTF8());
  1005. return false;
  1006. }
  1007. fInstance->fillInPluginDescription(fDesc);
  1008. fInstance->setPlayHead(this);
  1009. fInstance->addListener(this);
  1010. fFormatName = format;
  1011. // ---------------------------------------------------------------
  1012. // get info
  1013. if (name != nullptr && name[0] != '\0')
  1014. pData->name = pData->engine->getUniquePluginName(name);
  1015. else
  1016. pData->name = pData->engine->getUniquePluginName(fInstance->getName().toRawUTF8());
  1017. if (filename != nullptr && filename[0] != '\0')
  1018. pData->filename = carla_strdup(filename);
  1019. // ---------------------------------------------------------------
  1020. // register client
  1021. pData->client = pData->engine->addClient(this);
  1022. if (pData->client == nullptr || ! pData->client->isOk())
  1023. {
  1024. pData->engine->setLastError("Failed to register plugin client");
  1025. return false;
  1026. }
  1027. // ---------------------------------------------------------------
  1028. // set default options
  1029. pData->options = 0x0;
  1030. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1031. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1032. if (fInstance->getNumPrograms() > 1)
  1033. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1034. if (fInstance->acceptsMidi())
  1035. {
  1036. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1037. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1038. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1039. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1040. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1041. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1042. }
  1043. return true;
  1044. }
  1045. private:
  1046. juce::PluginDescription fDesc;
  1047. juce::AudioPluginInstance* fInstance;
  1048. juce::AudioPluginFormatManager fFormatManager;
  1049. juce::AudioSampleBuffer fAudioBuffer;
  1050. juce::MidiBuffer fMidiBuffer;
  1051. CurrentPositionInfo fPosInfo;
  1052. juce::MemoryBlock fChunk;
  1053. juce::String fFormatName;
  1054. ScopedPointer<JucePluginWindow> fWindow;
  1055. bool isJuceSaveFormat(const void* const data, const std::size_t dataSize)
  1056. {
  1057. if (fFormatName != "VST2")
  1058. return true;
  1059. if (dataSize < 160)
  1060. return false;
  1061. const int32_t* const set = (const int32_t*)data;
  1062. if (! compareMagic(set[0], "CcnK"))
  1063. return false;
  1064. if (! compareMagic(set[2], "FBCh"))
  1065. return false;
  1066. if (fxbSwap(set[3]) > 1)
  1067. return false;
  1068. const int32_t chunkSize = fxbSwap(set[39]);
  1069. return static_cast<std::size_t>(chunkSize + 160) == dataSize;
  1070. }
  1071. static bool compareMagic(int32_t magic, const char* name) noexcept
  1072. {
  1073. return magic == (int32_t)juce::ByteOrder::littleEndianInt (name)
  1074. || magic == (int32_t)juce::ByteOrder::bigEndianInt (name);
  1075. }
  1076. static int32_t fxbSwap(const int32_t x) noexcept
  1077. {
  1078. return (int32_t)juce::ByteOrder::swapIfLittleEndian ((uint32_t) x);
  1079. }
  1080. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginJuce)
  1081. };
  1082. CARLA_BACKEND_END_NAMESPACE
  1083. #endif // USING_JUCE
  1084. // -------------------------------------------------------------------------------------------------------------------
  1085. CARLA_BACKEND_START_NAMESPACE
  1086. CarlaPlugin* CarlaPlugin::newJuce(const Initializer& init, const char* const format)
  1087. {
  1088. carla_debug("CarlaPlugin::newJuce({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, format);
  1089. #ifdef USING_JUCE
  1090. CarlaPluginJuce* const plugin(new CarlaPluginJuce(init.engine, init.id));
  1091. if (! plugin->init(init.filename, init.name, init.label, init.uniqueId, init.options, format))
  1092. {
  1093. delete plugin;
  1094. return nullptr;
  1095. }
  1096. return plugin;
  1097. #else
  1098. init.engine->setLastError("Juce-based plugin not available");
  1099. return nullptr;
  1100. // unused
  1101. (void)format;
  1102. #endif
  1103. }
  1104. CARLA_BACKEND_END_NAMESPACE
  1105. // -------------------------------------------------------------------------------------------------------------------