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.

1436 lines
49KB

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