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.

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