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.

1469 lines
51KB

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