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.

1443 lines
49KB

  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. // -------------------------------------------------------------------
  282. // Set ui stuff
  283. void showCustomUI(const bool yesNo) override
  284. {
  285. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  286. if (yesNo)
  287. {
  288. if (fWindow == nullptr)
  289. {
  290. juce::String uiName(pData->name);
  291. uiName += " (GUI)";
  292. fWindow = new JucePluginWindow(pData->engine->getOptions().frontendWinId);
  293. fWindow->setName(uiName);
  294. }
  295. if (juce::AudioProcessorEditor* const editor = fInstance->createEditorIfNeeded())
  296. fWindow->show(editor);
  297. }
  298. else
  299. {
  300. if (fWindow != nullptr)
  301. fWindow->hide();
  302. if (juce::AudioProcessorEditor* const editor = fInstance->getActiveEditor())
  303. delete editor;
  304. fWindow = nullptr;
  305. }
  306. }
  307. void uiIdle() override
  308. {
  309. if (fWindow != nullptr)
  310. {
  311. if (fWindow->wasClosedByUser())
  312. {
  313. showCustomUI(false);
  314. pData->engine->callback(true, true,
  315. ENGINE_CALLBACK_UI_STATE_CHANGED,
  316. pData->id,
  317. 0,
  318. 0, 0, 0.0f, nullptr);
  319. }
  320. }
  321. CarlaPlugin::uiIdle();
  322. }
  323. // -------------------------------------------------------------------
  324. // Plugin state
  325. void reload() override
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  328. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  329. carla_debug("CarlaPluginJuce::reload() - start");
  330. const EngineProcessMode processMode(pData->engine->getProccessMode());
  331. // Safely disable plugin for reload
  332. const ScopedDisabler sd(this);
  333. if (pData->active)
  334. deactivate();
  335. clearBuffers();
  336. fInstance->refreshParameterList();
  337. uint32_t aIns, aOuts, mIns, mOuts, params;
  338. mIns = mOuts = 0;
  339. bool needsCtrlIn, needsCtrlOut;
  340. needsCtrlIn = needsCtrlOut = false;
  341. aIns = (fInstance->getTotalNumInputChannels() > 0) ? static_cast<uint32_t>(fInstance->getTotalNumInputChannels()) : 0;
  342. aOuts = (fInstance->getTotalNumOutputChannels() > 0) ? static_cast<uint32_t>(fInstance->getTotalNumOutputChannels()) : 0;
  343. params = (fInstance->getNumParameters() > 0) ? static_cast<uint32_t>(fInstance->getNumParameters()) : 0;
  344. if (fInstance->acceptsMidi())
  345. {
  346. mIns = 1;
  347. needsCtrlIn = true;
  348. }
  349. if (fInstance->producesMidi())
  350. {
  351. mOuts = 1;
  352. needsCtrlOut = true;
  353. }
  354. if (aIns > 0)
  355. {
  356. pData->audioIn.createNew(aIns);
  357. }
  358. if (aOuts > 0)
  359. {
  360. pData->audioOut.createNew(aOuts);
  361. needsCtrlIn = true;
  362. }
  363. if (params > 0)
  364. {
  365. pData->param.createNew(params, false);
  366. needsCtrlIn = true;
  367. }
  368. const uint portNameSize(pData->engine->getMaxPortNameSize());
  369. CarlaString portName;
  370. // Audio Ins
  371. for (uint32_t j=0; j < aIns; ++j)
  372. {
  373. portName.clear();
  374. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  375. {
  376. portName = pData->name;
  377. portName += ":";
  378. }
  379. if (aIns > 1)
  380. {
  381. portName += "input_";
  382. portName += CarlaString(j+1);
  383. }
  384. else
  385. portName += "input";
  386. portName.truncate(portNameSize);
  387. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  388. pData->audioIn.ports[j].rindex = j;
  389. }
  390. // Audio Outs
  391. for (uint32_t j=0; j < aOuts; ++j)
  392. {
  393. portName.clear();
  394. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  395. {
  396. portName = pData->name;
  397. portName += ":";
  398. }
  399. if (aOuts > 1)
  400. {
  401. portName += "output_";
  402. portName += CarlaString(j+1);
  403. }
  404. else
  405. portName += "output";
  406. portName.truncate(portNameSize);
  407. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  408. pData->audioOut.ports[j].rindex = j;
  409. }
  410. for (uint32_t j=0; j < params; ++j)
  411. {
  412. pData->param.data[j].type = PARAMETER_INPUT;
  413. pData->param.data[j].index = static_cast<int32_t>(j);
  414. pData->param.data[j].rindex = static_cast<int32_t>(j);
  415. float min, max, def, step, stepSmall, stepLarge;
  416. // TODO
  417. //const int numSteps(fInstance->getParameterNumSteps(static_cast<int>(j)));
  418. {
  419. min = 0.0f;
  420. max = 1.0f;
  421. step = 0.001f;
  422. stepSmall = 0.0001f;
  423. stepLarge = 0.1f;
  424. }
  425. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  426. #ifndef BUILD_BRIDGE
  427. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  428. #endif
  429. if (fInstance->isParameterAutomatable(static_cast<int>(j)))
  430. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  431. // FIXME?
  432. def = fInstance->getParameterDefaultValue(static_cast<int>(j));
  433. if (def < min)
  434. def = min;
  435. else if (def > max)
  436. def = max;
  437. pData->param.ranges[j].min = min;
  438. pData->param.ranges[j].max = max;
  439. pData->param.ranges[j].def = def;
  440. pData->param.ranges[j].step = step;
  441. pData->param.ranges[j].stepSmall = stepSmall;
  442. pData->param.ranges[j].stepLarge = stepLarge;
  443. }
  444. if (needsCtrlIn)
  445. {
  446. portName.clear();
  447. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  448. {
  449. portName = pData->name;
  450. portName += ":";
  451. }
  452. portName += "events-in";
  453. portName.truncate(portNameSize);
  454. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  455. }
  456. if (needsCtrlOut)
  457. {
  458. portName.clear();
  459. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  460. {
  461. portName = pData->name;
  462. portName += ":";
  463. }
  464. portName += "events-out";
  465. portName.truncate(portNameSize);
  466. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  467. }
  468. // plugin hints
  469. pData->hints = 0x0;
  470. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  471. if (fDesc.isInstrument)
  472. pData->hints |= PLUGIN_IS_SYNTH;
  473. if (fInstance->hasEditor())
  474. {
  475. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  476. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  477. }
  478. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  479. pData->hints |= PLUGIN_CAN_DRYWET;
  480. if (aOuts > 0)
  481. pData->hints |= PLUGIN_CAN_VOLUME;
  482. if (aOuts >= 2 && aOuts % 2 == 0)
  483. pData->hints |= PLUGIN_CAN_BALANCE;
  484. // extra plugin hints
  485. pData->extraHints = 0x0;
  486. if (mIns > 0)
  487. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  488. if (mOuts > 0)
  489. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  490. fInstance->setPlayConfigDetails(static_cast<int>(aIns), static_cast<int>(aOuts), pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  491. bufferSizeChanged(pData->engine->getBufferSize());
  492. reloadPrograms(true);
  493. if (pData->active)
  494. activate();
  495. carla_debug("CarlaPluginJuce::reload() - end");
  496. }
  497. void reloadPrograms(const bool doInit) override
  498. {
  499. carla_debug("CarlaPluginJuce::reloadPrograms(%s)", bool2str(doInit));
  500. const uint32_t oldCount = pData->prog.count;
  501. const int32_t current = pData->prog.current;
  502. // Delete old programs
  503. pData->prog.clear();
  504. // Query new programs
  505. uint32_t newCount = (fInstance->getNumPrograms() > 0) ? static_cast<uint32_t>(fInstance->getNumPrograms()) : 0;
  506. if (newCount > 0)
  507. {
  508. pData->prog.createNew(newCount);
  509. // Update names
  510. for (int i=0, count=fInstance->getNumPrograms(); i<count; ++i)
  511. pData->prog.names[i] = carla_strdup(fInstance->getProgramName(i).toRawUTF8());
  512. }
  513. if (doInit)
  514. {
  515. if (newCount > 0)
  516. setProgram(0, false, false, false, true);
  517. }
  518. else
  519. {
  520. // Check if current program is invalid
  521. bool programChanged = false;
  522. if (newCount == oldCount+1)
  523. {
  524. // one program added, probably created by user
  525. pData->prog.current = static_cast<int32_t>(oldCount);
  526. programChanged = true;
  527. }
  528. else if (current < 0 && newCount > 0)
  529. {
  530. // programs exist now, but not before
  531. pData->prog.current = 0;
  532. programChanged = true;
  533. }
  534. else if (current >= 0 && newCount == 0)
  535. {
  536. // programs existed before, but not anymore
  537. pData->prog.current = -1;
  538. programChanged = true;
  539. }
  540. else if (current >= static_cast<int32_t>(newCount))
  541. {
  542. // current program > count
  543. pData->prog.current = 0;
  544. programChanged = true;
  545. }
  546. else
  547. {
  548. // no change
  549. pData->prog.current = current;
  550. }
  551. if (programChanged)
  552. {
  553. setProgram(pData->prog.current, true, true, true, false);
  554. }
  555. else
  556. {
  557. // Program was changed during update, re-set it
  558. if (pData->prog.current >= 0)
  559. fInstance->setCurrentProgram(pData->prog.current);
  560. }
  561. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0, 0.0f, nullptr);
  562. }
  563. }
  564. // -------------------------------------------------------------------
  565. // Plugin processing
  566. void activate() noexcept override
  567. {
  568. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  569. try {
  570. fInstance->prepareToPlay(pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  571. } catch(...) {}
  572. }
  573. void deactivate() noexcept override
  574. {
  575. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  576. try {
  577. fInstance->releaseResources();
  578. } catch(...) {}
  579. }
  580. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  581. {
  582. // --------------------------------------------------------------------------------------------------------
  583. // Check if active
  584. if (! pData->active)
  585. {
  586. // disable any output sound
  587. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  588. carla_zeroFloats(audioOut[i], frames);
  589. return;
  590. }
  591. // --------------------------------------------------------------------------------------------------------
  592. // Check if needs reset
  593. if (pData->needsReset)
  594. {
  595. fInstance->reset();
  596. pData->needsReset = false;
  597. }
  598. // --------------------------------------------------------------------------------------------------------
  599. // Event Input
  600. fMidiBuffer.clear();
  601. if (pData->event.portIn != nullptr)
  602. {
  603. // ----------------------------------------------------------------------------------------------------
  604. // MIDI Input (External)
  605. if (pData->extNotes.mutex.tryLock())
  606. {
  607. for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin2(); it.valid(); it.next())
  608. {
  609. const ExternalMidiNote& note(it.getValue(kExternalMidiNoteFallback));
  610. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  611. uint8_t midiEvent[3];
  612. midiEvent[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  613. midiEvent[1] = note.note;
  614. midiEvent[2] = note.velo;
  615. fMidiBuffer.addEvent(midiEvent, 3, 0);
  616. }
  617. pData->extNotes.data.clear();
  618. pData->extNotes.mutex.unlock();
  619. } // End of MIDI Input (External)
  620. // ----------------------------------------------------------------------------------------------------
  621. // Event Input (System)
  622. #ifndef BUILD_BRIDGE
  623. bool allNotesOffSent = false;
  624. #endif
  625. for (uint32_t i=0, numEvents=pData->event.portIn->getEventCount(); i < numEvents; ++i)
  626. {
  627. const EngineEvent& event(pData->event.portIn->getEvent(i));
  628. if (event.time >= frames)
  629. continue;
  630. switch (event.type)
  631. {
  632. case kEngineEventTypeNull:
  633. break;
  634. case kEngineEventTypeControl: {
  635. const EngineControlEvent& ctrlEvent(event.ctrl);
  636. switch (ctrlEvent.type)
  637. {
  638. case kEngineControlEventTypeNull:
  639. break;
  640. case kEngineControlEventTypeParameter: {
  641. #ifndef BUILD_BRIDGE
  642. // Control backend stuff
  643. if (event.channel == pData->ctrlChannel)
  644. {
  645. float value;
  646. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  647. {
  648. value = ctrlEvent.value;
  649. setDryWetRT(value);
  650. }
  651. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  652. {
  653. value = ctrlEvent.value*127.0f/100.0f;
  654. setVolumeRT(value);
  655. }
  656. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  657. {
  658. float left, right;
  659. value = ctrlEvent.value/0.5f - 1.0f;
  660. if (value < 0.0f)
  661. {
  662. left = -1.0f;
  663. right = (value*2.0f)+1.0f;
  664. }
  665. else if (value > 0.0f)
  666. {
  667. left = (value*2.0f)-1.0f;
  668. right = 1.0f;
  669. }
  670. else
  671. {
  672. left = -1.0f;
  673. right = 1.0f;
  674. }
  675. setBalanceLeftRT(left);
  676. setBalanceRightRT(right);
  677. }
  678. }
  679. #endif
  680. // Control plugin parameters
  681. uint32_t k;
  682. for (k=0; k < pData->param.count; ++k)
  683. {
  684. if (pData->param.data[k].midiChannel != event.channel)
  685. continue;
  686. if (pData->param.data[k].midiCC != ctrlEvent.param)
  687. continue;
  688. if (pData->param.data[k].type != PARAMETER_INPUT)
  689. continue;
  690. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  691. continue;
  692. float value;
  693. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  694. {
  695. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  696. }
  697. else
  698. {
  699. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  700. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  701. else
  702. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  703. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  704. value = std::rint(value);
  705. }
  706. setParameterValueRT(k, value);
  707. }
  708. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  709. {
  710. uint8_t midiData[3];
  711. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  712. midiData[1] = uint8_t(ctrlEvent.param);
  713. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  714. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  715. }
  716. break;
  717. } // case kEngineControlEventTypeParameter
  718. case kEngineControlEventTypeMidiBank:
  719. if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  720. {
  721. uint8_t midiData[3];
  722. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  723. midiData[1] = MIDI_CONTROL_BANK_SELECT;
  724. midiData[2] = 0;
  725. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  726. midiData[1] = MIDI_CONTROL_BANK_SELECT__LSB;
  727. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  728. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  729. }
  730. break;
  731. case kEngineControlEventTypeMidiProgram:
  732. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  733. {
  734. if (ctrlEvent.param < pData->prog.count)
  735. {
  736. setProgramRT(ctrlEvent.param);
  737. }
  738. }
  739. else if ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) != 0)
  740. {
  741. uint8_t midiData[3];
  742. midiData[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  743. midiData[1] = uint8_t(ctrlEvent.value*127.0f);
  744. fMidiBuffer.addEvent(midiData, 2, static_cast<int>(event.time));
  745. }
  746. break;
  747. case kEngineControlEventTypeAllSoundOff:
  748. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  749. {
  750. uint8_t midiData[3];
  751. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  752. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  753. midiData[2] = 0;
  754. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  755. }
  756. break;
  757. case kEngineControlEventTypeAllNotesOff:
  758. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  759. {
  760. #ifndef BUILD_BRIDGE
  761. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  762. {
  763. allNotesOffSent = true;
  764. postponeRtAllNotesOff();
  765. }
  766. #endif
  767. uint8_t midiData[3];
  768. midiData[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  769. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  770. midiData[2] = 0;
  771. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  772. }
  773. break;
  774. } // switch (ctrlEvent.type)
  775. break;
  776. } // case kEngineEventTypeControl
  777. case kEngineEventTypeMidi: {
  778. const EngineMidiEvent& midiEvent(event.midi);
  779. const uint8_t* const midiData(midiEvent.size > EngineMidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data);
  780. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiData));
  781. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  782. continue;
  783. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  784. continue;
  785. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  786. continue;
  787. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  788. continue;
  789. // Fix bad note-off
  790. if (status == MIDI_STATUS_NOTE_ON && midiData[2] == 0)
  791. status = MIDI_STATUS_NOTE_OFF;
  792. // put back channel in data
  793. uint8_t midiData2[midiEvent.size];
  794. midiData2[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  795. std::memcpy(midiData2+1, midiData+1, static_cast<std::size_t>(midiEvent.size-1));
  796. fMidiBuffer.addEvent(midiData2, midiEvent.size, static_cast<int>(event.time));
  797. if (status == MIDI_STATUS_NOTE_ON)
  798. {
  799. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  800. event.channel,
  801. midiData[1],
  802. midiData[2],
  803. 0.0f);
  804. }
  805. else if (status == MIDI_STATUS_NOTE_OFF)
  806. {
  807. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  808. event.channel,
  809. midiData[1],
  810. 0, 0.0f);
  811. }
  812. } break;
  813. } // switch (event.type)
  814. }
  815. pData->postRtEvents.trySplice();
  816. } // End of Event Input
  817. // --------------------------------------------------------------------------------------------------------
  818. // Set TimeInfo
  819. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  820. fPosInfo.isPlaying = timeInfo.playing;
  821. if (timeInfo.bbt.valid)
  822. {
  823. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.bar > 0, timeInfo.bbt.bar);
  824. CARLA_SAFE_ASSERT_INT(timeInfo.bbt.beat > 0, timeInfo.bbt.beat);
  825. const double ppqBar = static_cast<double>(timeInfo.bbt.beatsPerBar) * (timeInfo.bbt.bar - 1);
  826. const double ppqBeat = static_cast<double>(timeInfo.bbt.beat - 1);
  827. const double ppqTick = timeInfo.bbt.tick / timeInfo.bbt.ticksPerBeat;
  828. fPosInfo.bpm = timeInfo.bbt.beatsPerMinute;
  829. fPosInfo.timeSigNumerator = static_cast<int>(timeInfo.bbt.beatsPerBar);
  830. fPosInfo.timeSigDenominator = static_cast<int>(timeInfo.bbt.beatType);
  831. fPosInfo.timeInSamples = static_cast<int64_t>(timeInfo.frame);
  832. fPosInfo.timeInSeconds = static_cast<double>(fPosInfo.timeInSamples)/pData->engine->getSampleRate();
  833. fPosInfo.ppqPosition = ppqBar + ppqBeat + ppqTick;
  834. fPosInfo.ppqPositionOfLastBarStart = ppqBar;
  835. }
  836. // --------------------------------------------------------------------------------------------------------
  837. // Process
  838. processSingle(audioIn, audioOut, frames);
  839. }
  840. bool processSingle(const float** const inBuffer, float** const outBuffer, const uint32_t frames)
  841. {
  842. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  843. if (pData->audioIn.count > 0)
  844. {
  845. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  846. }
  847. if (pData->audioOut.count > 0)
  848. {
  849. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  850. }
  851. // --------------------------------------------------------------------------------------------------------
  852. // Try lock, silence otherwise
  853. if (pData->engine->isOffline())
  854. {
  855. pData->singleMutex.lock();
  856. }
  857. else if (! pData->singleMutex.tryLock())
  858. {
  859. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  860. carla_zeroFloats(outBuffer[i], frames);
  861. return false;
  862. }
  863. // --------------------------------------------------------------------------------------------------------
  864. // Set audio in buffers
  865. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  866. fAudioBuffer.copyFrom(static_cast<int>(i), 0, inBuffer[i], static_cast<int>(frames));
  867. // --------------------------------------------------------------------------------------------------------
  868. // Run plugin
  869. fInstance->processBlock(fAudioBuffer, fMidiBuffer);
  870. // --------------------------------------------------------------------------------------------------------
  871. // Set audio out buffers
  872. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  873. carla_copyFloats(outBuffer[i], fAudioBuffer.getReadPointer(static_cast<int>(i)), frames);
  874. // --------------------------------------------------------------------------------------------------------
  875. // Midi out
  876. if (! fMidiBuffer.isEmpty())
  877. {
  878. if (pData->event.portOut != nullptr)
  879. {
  880. const uint8_t* midiEventData;
  881. int midiEventSize, midiEventPosition;
  882. for (juce::MidiBuffer::Iterator i(fMidiBuffer); i.getNextEvent(midiEventData, midiEventSize, midiEventPosition);)
  883. {
  884. CARLA_SAFE_ASSERT_BREAK(midiEventPosition >= 0 && midiEventPosition < static_cast<int>(frames));
  885. CARLA_SAFE_ASSERT_BREAK(midiEventSize > 0);
  886. if (! pData->event.portOut->writeMidiEvent(static_cast<uint32_t>(midiEventPosition), static_cast<uint8_t>(midiEventSize), midiEventData))
  887. break;
  888. }
  889. }
  890. fMidiBuffer.clear();
  891. }
  892. // --------------------------------------------------------------------------------------------------------
  893. pData->singleMutex.unlock();
  894. return true;
  895. }
  896. void bufferSizeChanged(const uint32_t newBufferSize) override
  897. {
  898. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  899. carla_debug("CarlaPluginJuce::bufferSizeChanged(%i)", newBufferSize);
  900. fAudioBuffer.setSize(static_cast<int>(std::max<uint32_t>(pData->audioIn.count, pData->audioOut.count)), static_cast<int>(newBufferSize));
  901. if (pData->active)
  902. {
  903. deactivate();
  904. activate();
  905. }
  906. }
  907. void sampleRateChanged(const double newSampleRate) override
  908. {
  909. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  910. carla_debug("CarlaPluginJuce::sampleRateChanged(%g)", newSampleRate);
  911. if (pData->active)
  912. {
  913. deactivate();
  914. activate();
  915. }
  916. }
  917. // -------------------------------------------------------------------
  918. // Plugin buffers
  919. // nothing
  920. // -------------------------------------------------------------------
  921. // Post-poned UI Stuff
  922. // nothing
  923. // -------------------------------------------------------------------
  924. void* getNativeHandle() const noexcept override
  925. {
  926. return (fInstance != nullptr) ? fInstance->getPlatformSpecificData() : nullptr;
  927. }
  928. // -------------------------------------------------------------------
  929. protected:
  930. void audioProcessorParameterChanged(juce::AudioProcessor*, int index, float value) override
  931. {
  932. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  933. const uint32_t uindex(static_cast<uint32_t>(index));
  934. const float fixedValue(pData->param.getFixedValue(uindex, value));
  935. CarlaPlugin::setParameterValue(static_cast<uint32_t>(index), fixedValue, false, true, true);
  936. }
  937. void audioProcessorChanged(juce::AudioProcessor*) override
  938. {
  939. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0, 0.0f, nullptr);
  940. }
  941. void audioProcessorParameterChangeGestureBegin(juce::AudioProcessor*, int index) override
  942. {
  943. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  944. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), true);
  945. }
  946. void audioProcessorParameterChangeGestureEnd(juce::AudioProcessor*, int index) override
  947. {
  948. CARLA_SAFE_ASSERT_RETURN(index >= 0,);
  949. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), false);
  950. }
  951. bool getCurrentPosition(CurrentPositionInfo& result) override
  952. {
  953. carla_copyStruct(result, fPosInfo);
  954. return true;
  955. }
  956. // -------------------------------------------------------------------
  957. public:
  958. 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)
  959. {
  960. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  961. // ---------------------------------------------------------------
  962. // first checks
  963. if (pData->client != nullptr)
  964. {
  965. pData->engine->setLastError("Plugin client is already registered");
  966. return false;
  967. }
  968. if (format == nullptr || format[0] == '\0')
  969. {
  970. pData->engine->setLastError("null format");
  971. return false;
  972. }
  973. // AU and VST3 require label
  974. if (std::strcmp(format, "AU") == 0 || std::strcmp(format, "VST3") == 0)
  975. {
  976. if (label == nullptr || label[0] == '\0')
  977. {
  978. pData->engine->setLastError("null label");
  979. return false;
  980. }
  981. }
  982. juce::String fileOrIdentifier;
  983. if (std::strcmp(format, "AU") == 0)
  984. {
  985. fileOrIdentifier = label;
  986. }
  987. else
  988. {
  989. // VST2 and VST3 require filename
  990. if (filename == nullptr || filename[0] == '\0')
  991. {
  992. pData->engine->setLastError("null filename");
  993. return false;
  994. }
  995. juce::String jfilename(filename);
  996. #ifdef CARLA_OS_WIN
  997. // Fix for wine usage
  998. if (juce::juce_isRunningInWine() && filename[0] == '/')
  999. {
  1000. jfilename.replace("/", "\\");
  1001. jfilename = "Z:" + jfilename;
  1002. }
  1003. #endif
  1004. fileOrIdentifier = jfilename;
  1005. if (label != nullptr && label[0] != '\0')
  1006. fDesc.name = label;
  1007. }
  1008. fFormatManager.addDefaultFormats();
  1009. {
  1010. juce::OwnedArray<juce::PluginDescription> pluginDescriptions;
  1011. juce::KnownPluginList plist;
  1012. for (int i = 0; i < fFormatManager.getNumFormats(); ++i)
  1013. plist.scanAndAddFile(fileOrIdentifier, true, pluginDescriptions, *fFormatManager.getFormat(i));
  1014. if (pluginDescriptions.size() == 0)
  1015. {
  1016. pData->engine->setLastError("Failed to get plugin description");
  1017. return false;
  1018. }
  1019. fDesc = *pluginDescriptions[0];
  1020. }
  1021. if (uniqueId != 0)
  1022. fDesc.uid = static_cast<int>(uniqueId);
  1023. juce::String error;
  1024. fInstance = fFormatManager.createPluginInstance(fDesc,
  1025. pData->engine->getSampleRate(),
  1026. static_cast<int>(pData->engine->getBufferSize()),
  1027. error);
  1028. if (fInstance == nullptr)
  1029. {
  1030. pData->engine->setLastError(error.toRawUTF8());
  1031. return false;
  1032. }
  1033. fInstance->fillInPluginDescription(fDesc);
  1034. fInstance->setPlayHead(this);
  1035. fInstance->addListener(this);
  1036. fFormatName = format;
  1037. // ---------------------------------------------------------------
  1038. // get info
  1039. if (name != nullptr && name[0] != '\0')
  1040. pData->name = pData->engine->getUniquePluginName(name);
  1041. else
  1042. pData->name = pData->engine->getUniquePluginName(fInstance->getName().toRawUTF8());
  1043. if (filename != nullptr && filename[0] != '\0')
  1044. pData->filename = carla_strdup(filename);
  1045. // ---------------------------------------------------------------
  1046. // register client
  1047. pData->client = pData->engine->addClient(this);
  1048. if (pData->client == nullptr || ! pData->client->isOk())
  1049. {
  1050. pData->engine->setLastError("Failed to register plugin client");
  1051. return false;
  1052. }
  1053. // ---------------------------------------------------------------
  1054. // set default options
  1055. pData->options = 0x0;
  1056. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1057. pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  1058. if (fInstance->acceptsMidi())
  1059. {
  1060. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1061. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1062. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1063. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1064. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1065. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1066. if (options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1067. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1068. }
  1069. if (fInstance->getNumPrograms() > 1 && ((pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES) == 0))
  1070. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1071. return true;
  1072. }
  1073. private:
  1074. juce::PluginDescription fDesc;
  1075. juce::AudioPluginInstance* fInstance;
  1076. juce::AudioPluginFormatManager fFormatManager;
  1077. juce::AudioSampleBuffer fAudioBuffer;
  1078. juce::MidiBuffer fMidiBuffer;
  1079. CurrentPositionInfo fPosInfo;
  1080. juce::MemoryBlock fChunk;
  1081. juce::String fFormatName;
  1082. ScopedPointer<JucePluginWindow> fWindow;
  1083. bool isJuceSaveFormat(const void* const data, const std::size_t dataSize)
  1084. {
  1085. if (fFormatName != "VST2")
  1086. return true;
  1087. if (dataSize < 160)
  1088. return false;
  1089. const int32_t* const set = (const int32_t*)data;
  1090. if (! compareMagic(set[0], "CcnK"))
  1091. return false;
  1092. if (! compareMagic(set[2], "FBCh"))
  1093. return false;
  1094. if (fxbSwap(set[3]) > 1)
  1095. return false;
  1096. const int32_t chunkSize = fxbSwap(set[39]);
  1097. return static_cast<std::size_t>(chunkSize + 160) == dataSize;
  1098. }
  1099. static bool compareMagic(int32_t magic, const char* name) noexcept
  1100. {
  1101. return magic == (int32_t)juce::ByteOrder::littleEndianInt (name)
  1102. || magic == (int32_t)juce::ByteOrder::bigEndianInt (name);
  1103. }
  1104. static int32_t fxbSwap(const int32_t x) noexcept
  1105. {
  1106. return (int32_t)juce::ByteOrder::swapIfLittleEndian ((uint32_t) x);
  1107. }
  1108. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginJuce)
  1109. };
  1110. CARLA_BACKEND_END_NAMESPACE
  1111. #endif // USING_JUCE
  1112. // -------------------------------------------------------------------------------------------------------------------
  1113. CARLA_BACKEND_START_NAMESPACE
  1114. CarlaPlugin* CarlaPlugin::newJuce(const Initializer& init, const char* const format)
  1115. {
  1116. carla_debug("CarlaPlugin::newJuce({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, format);
  1117. #ifdef USING_JUCE
  1118. CarlaPluginJuce* const plugin(new CarlaPluginJuce(init.engine, init.id));
  1119. if (! plugin->init(init.filename, init.name, init.label, init.uniqueId, init.options, format))
  1120. {
  1121. delete plugin;
  1122. return nullptr;
  1123. }
  1124. return plugin;
  1125. #else
  1126. init.engine->setLastError("Juce-based plugin not available");
  1127. return nullptr;
  1128. // unused
  1129. (void)format;
  1130. #endif
  1131. }
  1132. CARLA_BACKEND_END_NAMESPACE
  1133. // -------------------------------------------------------------------------------------------------------------------