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.

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