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.

1416 lines
48KB

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