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.

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