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.

1580 lines
55KB

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