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.

1463 lines
50KB

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