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.

1457 lines
50KB

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