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.

1097 lines
36KB

  1. /*
  2. * Carla Juce Plugin
  3. * Copyright (C) 2013-2014 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. #ifdef HAVE_JUCE
  20. #include "CarlaBackendUtils.hpp"
  21. #include "JucePluginWindow.hpp"
  22. #include "juce_audio_processors.h"
  23. using namespace juce;
  24. CARLA_BACKEND_START_NAMESPACE
  25. class JucePlugin : public CarlaPlugin
  26. {
  27. public:
  28. JucePlugin(CarlaEngine* const engine, const uint id)
  29. : CarlaPlugin(engine, id),
  30. fInstance(nullptr),
  31. fAudioBuffer(1, 0)
  32. {
  33. carla_debug("JucePlugin::JucePlugin(%p, %i)", engine, id);
  34. fMidiBuffer.ensureSize(2048);
  35. fMidiBuffer.clear();
  36. }
  37. ~JucePlugin() override
  38. {
  39. carla_debug("JucePlugin::~JucePlugin()");
  40. // close UI
  41. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  42. showCustomUI(false);
  43. pData->singleMutex.lock();
  44. pData->masterMutex.lock();
  45. if (pData->client != nullptr && pData->client->isActive())
  46. pData->client->deactivate();
  47. if (pData->active)
  48. {
  49. deactivate();
  50. pData->active = false;
  51. }
  52. if (fInstance != nullptr)
  53. {
  54. delete fInstance;
  55. fInstance = nullptr;
  56. }
  57. clearBuffers();
  58. }
  59. // -------------------------------------------------------------------
  60. // Information (base)
  61. PluginType getType() const noexcept override
  62. {
  63. PluginType type = PLUGIN_NONE;
  64. try {
  65. type = getPluginTypeFromString(fDesc.pluginFormatName.toRawUTF8());
  66. } catch(...) {}
  67. return type;
  68. }
  69. PluginCategory getCategory() const noexcept override
  70. {
  71. PluginCategory category = PLUGIN_CATEGORY_NONE;
  72. try {
  73. category = getPluginCategoryFromName(fDesc.category.toRawUTF8());
  74. } catch(...) {}
  75. return category;
  76. }
  77. int64_t getUniqueId() const noexcept override
  78. {
  79. return fDesc.uid;
  80. }
  81. // -------------------------------------------------------------------
  82. // Information (count)
  83. // nothing
  84. // -------------------------------------------------------------------
  85. // Information (current data)
  86. // nothing
  87. // -------------------------------------------------------------------
  88. // Information (per-plugin data)
  89. unsigned int getOptionsAvailable() const noexcept override
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0x0);
  92. unsigned int options = 0x0;
  93. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  94. //options |= PLUGIN_OPTION_USE_CHUNKS;
  95. if (fInstance->acceptsMidi())
  96. {
  97. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  98. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  99. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  100. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  101. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  102. }
  103. return options;
  104. }
  105. float getParameterValue(const uint32_t parameterId) const noexcept override
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  108. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr, 0.0f);
  109. return fInstance->getParameter(static_cast<int>(parameterId));
  110. }
  111. void getLabel(char* const strBuf) const noexcept override
  112. {
  113. std::strncpy(strBuf, fDesc.name.toRawUTF8(), STR_MAX);
  114. }
  115. void getMaker(char* const strBuf) const noexcept override
  116. {
  117. std::strncpy(strBuf, fDesc.manufacturerName.toRawUTF8(), STR_MAX);
  118. }
  119. void getCopyright(char* const strBuf) const noexcept override
  120. {
  121. getMaker(strBuf);
  122. }
  123. void getRealName(char* const strBuf) const noexcept override
  124. {
  125. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  126. std::strncpy(strBuf, fInstance->getName().toRawUTF8(), STR_MAX);
  127. }
  128. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  131. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  132. std::strncpy(strBuf, fInstance->getParameterName(static_cast<int>(parameterId)).toRawUTF8(), STR_MAX);
  133. }
  134. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  137. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  138. std::strncpy(strBuf, fInstance->getParameterLabel(static_cast<int>(parameterId)).toRawUTF8(), STR_MAX);
  139. }
  140. // -------------------------------------------------------------------
  141. // Set data (state)
  142. // nothing
  143. // -------------------------------------------------------------------
  144. // Set data (internal stuff)
  145. void setName(const char* const newName) override
  146. {
  147. CarlaPlugin::setName(newName);
  148. if (fWindow != nullptr)
  149. {
  150. String uiName(pData->name);
  151. uiName += " (GUI)";
  152. fWindow->setName(uiName);
  153. }
  154. }
  155. // -------------------------------------------------------------------
  156. // Set data (plugin-specific stuff)
  157. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  160. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  161. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  162. fInstance->setParameter(static_cast<int>(parameterId), value);
  163. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  164. }
  165. // -------------------------------------------------------------------
  166. // Set ui stuff
  167. void showCustomUI(const bool yesNo) override
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  170. #ifdef CARLA_OS_LINUX
  171. const MessageManagerLock mmLock;
  172. #endif
  173. if (yesNo)
  174. {
  175. if (fWindow == nullptr)
  176. {
  177. String uiName(pData->name);
  178. uiName += " (GUI)";
  179. fWindow = new JucePluginWindow();
  180. fWindow->setName(uiName);
  181. }
  182. if (AudioProcessorEditor* const editor = fInstance->createEditorIfNeeded())
  183. fWindow->show(editor);
  184. }
  185. else
  186. {
  187. if (fWindow != nullptr)
  188. fWindow->hide();
  189. if (AudioProcessorEditor* const editor = fInstance->getActiveEditor())
  190. delete editor;
  191. fWindow = nullptr;
  192. }
  193. }
  194. void idle() override
  195. {
  196. if (fWindow != nullptr)
  197. {
  198. if (fWindow->wasClosedByUser())
  199. {
  200. showCustomUI(false);
  201. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  202. }
  203. }
  204. CarlaPlugin::idle();
  205. }
  206. // -------------------------------------------------------------------
  207. // Plugin state
  208. void reload() override
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  211. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  212. carla_debug("VstPlugin::reload() - start");
  213. const EngineProcessMode processMode(pData->engine->getProccessMode());
  214. // Safely disable plugin for reload
  215. const ScopedDisabler sd(this);
  216. if (pData->active)
  217. deactivate();
  218. clearBuffers();
  219. fInstance->refreshParameterList();
  220. uint32_t aIns, aOuts, mIns, mOuts, params;
  221. aIns = aOuts = mIns = mOuts = params = 0;
  222. bool needsCtrlIn, needsCtrlOut;
  223. needsCtrlIn = needsCtrlOut = false;
  224. aIns = (fInstance->getNumInputChannels() > 0) ? static_cast<uint32_t>(fInstance->getNumInputChannels()) : 0;
  225. aOuts = (fInstance->getNumOutputChannels() > 0) ? static_cast<uint32_t>(fInstance->getNumOutputChannels()) : 0;
  226. params = (fInstance->getNumParameters() > 0) ? static_cast<uint32_t>(fInstance->getNumParameters()) : 0;
  227. if (fInstance->acceptsMidi())
  228. {
  229. mIns = 1;
  230. needsCtrlIn = true;
  231. }
  232. if (fInstance->producesMidi())
  233. {
  234. mOuts = 1;
  235. needsCtrlOut = true;
  236. }
  237. if (aIns > 0)
  238. {
  239. pData->audioIn.createNew(aIns);
  240. }
  241. if (aOuts > 0)
  242. {
  243. pData->audioOut.createNew(aOuts);
  244. needsCtrlIn = true;
  245. }
  246. if (params > 0)
  247. {
  248. pData->param.createNew(params, false);
  249. needsCtrlIn = true;
  250. }
  251. const uint portNameSize(pData->engine->getMaxPortNameSize());
  252. CarlaString portName;
  253. // Audio Ins
  254. for (uint32_t j=0; j < aIns; ++j)
  255. {
  256. portName.clear();
  257. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  258. {
  259. portName = pData->name;
  260. portName += ":";
  261. }
  262. if (aIns > 1)
  263. {
  264. portName += "input_";
  265. portName += CarlaString(j+1);
  266. }
  267. else
  268. portName += "input";
  269. portName.truncate(portNameSize);
  270. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  271. pData->audioIn.ports[j].rindex = j;
  272. }
  273. // Audio Outs
  274. for (uint32_t j=0; j < aOuts; ++j)
  275. {
  276. portName.clear();
  277. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  278. {
  279. portName = pData->name;
  280. portName += ":";
  281. }
  282. if (aOuts > 1)
  283. {
  284. portName += "output_";
  285. portName += CarlaString(j+1);
  286. }
  287. else
  288. portName += "output";
  289. portName.truncate(portNameSize);
  290. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  291. pData->audioOut.ports[j].rindex = j;
  292. }
  293. for (uint32_t j=0; j < params; ++j)
  294. {
  295. pData->param.data[j].type = PARAMETER_INPUT;
  296. pData->param.data[j].hints = 0x0;
  297. pData->param.data[j].index = static_cast<int32_t>(j);
  298. pData->param.data[j].rindex = static_cast<int32_t>(j);
  299. pData->param.data[j].midiCC = -1;
  300. pData->param.data[j].midiChannel = 0;
  301. float min, max, def, step, stepSmall, stepLarge;
  302. // TODO
  303. //const int numSteps(fInstance->getParameterNumSteps(static_cast<int>(j)));
  304. {
  305. min = 0.0f;
  306. max = 1.0f;
  307. step = 0.001f;
  308. stepSmall = 0.0001f;
  309. stepLarge = 0.1f;
  310. }
  311. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  312. #ifndef BUILD_BRIDGE
  313. //pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  314. #endif
  315. if (fInstance->isParameterAutomatable(static_cast<int>(j)))
  316. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  317. // FIXME?
  318. def = fInstance->getParameterDefaultValue(static_cast<int>(j));
  319. if (def < min)
  320. def = min;
  321. else if (def > max)
  322. def = max;
  323. pData->param.ranges[j].min = min;
  324. pData->param.ranges[j].max = max;
  325. pData->param.ranges[j].def = def;
  326. pData->param.ranges[j].step = step;
  327. pData->param.ranges[j].stepSmall = stepSmall;
  328. pData->param.ranges[j].stepLarge = stepLarge;
  329. }
  330. if (needsCtrlIn)
  331. {
  332. portName.clear();
  333. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  334. {
  335. portName = pData->name;
  336. portName += ":";
  337. }
  338. portName += "events-in";
  339. portName.truncate(portNameSize);
  340. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  341. }
  342. if (needsCtrlOut)
  343. {
  344. portName.clear();
  345. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  346. {
  347. portName = pData->name;
  348. portName += ":";
  349. }
  350. portName += "events-out";
  351. portName.truncate(portNameSize);
  352. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  353. }
  354. // plugin hints
  355. pData->hints = 0x0;
  356. if (fDesc.isInstrument)
  357. pData->hints |= PLUGIN_IS_SYNTH;
  358. if (fInstance->hasEditor())
  359. {
  360. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  361. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  362. }
  363. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  364. pData->hints |= PLUGIN_CAN_DRYWET;
  365. if (aOuts > 0)
  366. pData->hints |= PLUGIN_CAN_VOLUME;
  367. if (aOuts >= 2 && aOuts % 2 == 0)
  368. pData->hints |= PLUGIN_CAN_BALANCE;
  369. // extra plugin hints
  370. pData->extraHints = 0x0;
  371. if (mIns > 0)
  372. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_IN;
  373. if (mOuts > 0)
  374. pData->extraHints |= PLUGIN_EXTRA_HINT_HAS_MIDI_OUT;
  375. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  376. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  377. fInstance->setPlayConfigDetails(static_cast<int>(aIns), static_cast<int>(aOuts), pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  378. bufferSizeChanged(pData->engine->getBufferSize());
  379. //reloadPrograms(true);
  380. if (pData->active)
  381. activate();
  382. carla_debug("VstPlugin::reload() - end");
  383. }
  384. // -------------------------------------------------------------------
  385. // Plugin processing
  386. void activate() noexcept override
  387. {
  388. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  389. try {
  390. fInstance->prepareToPlay(pData->engine->getSampleRate(), static_cast<int>(pData->engine->getBufferSize()));
  391. } catch(...) {}
  392. }
  393. void deactivate() noexcept override
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(fInstance != nullptr,);
  396. try {
  397. fInstance->releaseResources();
  398. } catch(...) {}
  399. }
  400. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  401. {
  402. // --------------------------------------------------------------------------------------------------------
  403. // Check if active
  404. if (! pData->active)
  405. {
  406. // disable any output sound
  407. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  408. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  409. return;
  410. }
  411. // --------------------------------------------------------------------------------------------------------
  412. // Check if needs reset
  413. if (pData->needsReset)
  414. {
  415. fInstance->reset();
  416. pData->needsReset = false;
  417. }
  418. // --------------------------------------------------------------------------------------------------------
  419. // TimeInfo
  420. // TODO
  421. // --------------------------------------------------------------------------------------------------------
  422. // Event Input
  423. fMidiBuffer.clear();
  424. if (pData->event.portIn != nullptr)
  425. {
  426. // ----------------------------------------------------------------------------------------------------
  427. // MIDI Input (External)
  428. if (pData->extNotes.mutex.tryLock())
  429. {
  430. for (; ! pData->extNotes.data.isEmpty();)
  431. {
  432. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  433. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  434. uint8_t midiEvent[3];
  435. midiEvent[0] = static_cast<uint8_t>(note.channel + (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF);
  436. midiEvent[1] = note.note;
  437. midiEvent[2] = note.velo;
  438. fMidiBuffer.addEvent(midiEvent, 3, 0);
  439. }
  440. pData->extNotes.mutex.unlock();
  441. } // End of MIDI Input (External)
  442. // ----------------------------------------------------------------------------------------------------
  443. // Event Input (System)
  444. bool allNotesOffSent = false;
  445. uint32_t numEvents = pData->event.portIn->getEventCount();
  446. uint32_t nextBankId;
  447. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  448. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  449. else
  450. nextBankId = 0;
  451. for (uint32_t i=0; i < numEvents; ++i)
  452. {
  453. const EngineEvent& event(pData->event.portIn->getEvent(i));
  454. if (event.time >= frames)
  455. continue;
  456. switch (event.type)
  457. {
  458. case kEngineEventTypeNull:
  459. break;
  460. case kEngineEventTypeControl: {
  461. const EngineControlEvent& ctrlEvent(event.ctrl);
  462. switch (ctrlEvent.type)
  463. {
  464. case kEngineControlEventTypeNull:
  465. break;
  466. case kEngineControlEventTypeParameter: {
  467. #ifndef BUILD_BRIDGE
  468. // Control backend stuff
  469. if (event.channel == pData->ctrlChannel)
  470. {
  471. float value;
  472. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  473. {
  474. value = ctrlEvent.value;
  475. setDryWet(value, false, false);
  476. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  477. break;
  478. }
  479. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  480. {
  481. value = ctrlEvent.value*127.0f/100.0f;
  482. setVolume(value, false, false);
  483. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  484. break;
  485. }
  486. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  487. {
  488. float left, right;
  489. value = ctrlEvent.value/0.5f - 1.0f;
  490. if (value < 0.0f)
  491. {
  492. left = -1.0f;
  493. right = (value*2.0f)+1.0f;
  494. }
  495. else if (value > 0.0f)
  496. {
  497. left = (value*2.0f)-1.0f;
  498. right = 1.0f;
  499. }
  500. else
  501. {
  502. left = -1.0f;
  503. right = 1.0f;
  504. }
  505. setBalanceLeft(left, false, false);
  506. setBalanceRight(right, false, false);
  507. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  508. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  509. break;
  510. }
  511. }
  512. #endif
  513. // Control plugin parameters
  514. uint32_t k;
  515. for (k=0; k < pData->param.count; ++k)
  516. {
  517. if (pData->param.data[k].midiChannel != event.channel)
  518. continue;
  519. if (pData->param.data[k].midiCC != ctrlEvent.param)
  520. continue;
  521. if (pData->param.data[k].type != PARAMETER_INPUT)
  522. continue;
  523. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  524. continue;
  525. float value;
  526. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  527. {
  528. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  529. }
  530. else
  531. {
  532. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  533. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  534. value = std::rint(value);
  535. }
  536. setParameterValue(k, value, false, false, false);
  537. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  538. break;
  539. }
  540. // check if event is already handled
  541. if (k != pData->param.count)
  542. break;
  543. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  544. {
  545. uint8_t midiData[3];
  546. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  547. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  548. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  549. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  550. }
  551. break;
  552. } // case kEngineControlEventTypeParameter
  553. case kEngineControlEventTypeMidiBank:
  554. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  555. nextBankId = ctrlEvent.param;
  556. break;
  557. case kEngineControlEventTypeMidiProgram:
  558. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  559. {
  560. const uint32_t nextProgramId = ctrlEvent.param;
  561. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  562. {
  563. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  564. {
  565. const int32_t index(static_cast<int32_t>(k));
  566. setMidiProgram(index, false, false, false);
  567. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  568. break;
  569. }
  570. }
  571. }
  572. break;
  573. case kEngineControlEventTypeAllSoundOff:
  574. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  575. {
  576. uint8_t midiData[3];
  577. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  578. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  579. midiData[2] = 0;
  580. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  581. }
  582. break;
  583. case kEngineControlEventTypeAllNotesOff:
  584. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  585. {
  586. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  587. {
  588. allNotesOffSent = true;
  589. sendMidiAllNotesOffToCallback();
  590. }
  591. uint8_t midiData[3];
  592. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  593. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  594. midiData[2] = 0;
  595. fMidiBuffer.addEvent(midiData, 3, static_cast<int>(event.time));
  596. }
  597. break;
  598. } // switch (ctrlEvent.type)
  599. break;
  600. } // case kEngineEventTypeControl
  601. case kEngineEventTypeMidi:
  602. {
  603. const EngineMidiEvent& midiEvent(event.midi);
  604. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  605. uint8_t channel = event.channel;
  606. // Fix bad note-off
  607. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  608. status = MIDI_STATUS_NOTE_OFF;
  609. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  610. continue;
  611. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  612. continue;
  613. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  614. continue;
  615. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  616. continue;
  617. fMidiBuffer.addEvent(midiEvent.data, midiEvent.size, static_cast<int>(event.time));
  618. if (status == MIDI_STATUS_NOTE_ON)
  619. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  620. else if (status == MIDI_STATUS_NOTE_OFF)
  621. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  622. break;
  623. } // case kEngineEventTypeMidi
  624. } // switch (event.type)
  625. }
  626. pData->postRtEvents.trySplice();
  627. } // End of Event Input
  628. // --------------------------------------------------------------------------------------------------------
  629. // Process
  630. processSingle(inBuffer, outBuffer, frames);
  631. // --------------------------------------------------------------------------------------------------------
  632. // MIDI Output
  633. if (pData->event.portOut != nullptr)
  634. {
  635. // TODO
  636. }
  637. }
  638. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  639. {
  640. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  641. if (pData->audioIn.count > 0)
  642. {
  643. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  644. }
  645. if (pData->audioOut.count > 0)
  646. {
  647. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  648. }
  649. // --------------------------------------------------------------------------------------------------------
  650. // Try lock, silence otherwise
  651. if (pData->engine->isOffline())
  652. {
  653. pData->singleMutex.lock();
  654. }
  655. else if (! pData->singleMutex.tryLock())
  656. {
  657. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  658. FloatVectorOperations::clear(outBuffer[i], static_cast<int>(frames));
  659. return false;
  660. }
  661. // --------------------------------------------------------------------------------------------------------
  662. // Set audio in buffers
  663. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  664. fAudioBuffer.copyFrom(static_cast<int>(i), 0, inBuffer[i], static_cast<int>(frames));
  665. // --------------------------------------------------------------------------------------------------------
  666. // Run plugin
  667. fInstance->processBlock(fAudioBuffer, fMidiBuffer);
  668. // --------------------------------------------------------------------------------------------------------
  669. // Set audio out buffers
  670. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  671. FloatVectorOperations::copy(outBuffer[i], fAudioBuffer.getSampleData(static_cast<int>(i)), static_cast<int>(frames));
  672. // --------------------------------------------------------------------------------------------------------
  673. pData->singleMutex.unlock();
  674. return true;
  675. }
  676. void bufferSizeChanged(const uint32_t newBufferSize) override
  677. {
  678. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  679. carla_debug("VstPlugin::bufferSizeChanged(%i)", newBufferSize);
  680. fAudioBuffer.setSize(static_cast<int>(std::max<uint32_t>(pData->audioIn.count, pData->audioOut.count)), static_cast<int>(newBufferSize));
  681. if (pData->active)
  682. {
  683. deactivate();
  684. activate();
  685. }
  686. }
  687. void sampleRateChanged(const double newSampleRate) override
  688. {
  689. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  690. carla_debug("VstPlugin::sampleRateChanged(%g)", newSampleRate);
  691. if (pData->active)
  692. {
  693. deactivate();
  694. activate();
  695. }
  696. }
  697. // -------------------------------------------------------------------
  698. // Plugin buffers
  699. // nothing
  700. // -------------------------------------------------------------------
  701. // Post-poned UI Stuff
  702. // nothing
  703. // -------------------------------------------------------------------
  704. protected:
  705. // TODO
  706. // -------------------------------------------------------------------
  707. public:
  708. bool init(const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const char* const format)
  709. {
  710. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  711. // ---------------------------------------------------------------
  712. // first checks
  713. if (pData->client != nullptr)
  714. {
  715. pData->engine->setLastError("Plugin client is already registered");
  716. return false;
  717. }
  718. if (filename == nullptr || filename[0] == '\0')
  719. {
  720. pData->engine->setLastError("null filename");
  721. return false;
  722. }
  723. if (label == nullptr || label[0] == '\0')
  724. {
  725. pData->engine->setLastError("null label");
  726. return false;
  727. }
  728. if (format == nullptr || format[0] == '\0')
  729. {
  730. pData->engine->setLastError("null format");
  731. return false;
  732. }
  733. #ifdef CARLA_OS_LINUX
  734. const MessageManagerLock mmLock;
  735. #endif
  736. // ---------------------------------------------------------------
  737. // fix path for wine usage
  738. String jfilename(filename);
  739. #ifdef CARLA_OS_WIN
  740. if (jfilename.startsWith("/"))
  741. {
  742. jfilename.replace("/", "\\");
  743. jfilename = "Z:" + jfilename;
  744. }
  745. #endif
  746. //fDesc.name = fDesc.descriptiveName = label;
  747. fDesc.uid = static_cast<int>(uniqueId);
  748. fDesc.fileOrIdentifier = jfilename;
  749. fDesc.pluginFormatName = format;
  750. fFormatManager.addDefaultFormats();
  751. String error;
  752. fInstance = fFormatManager.createPluginInstance(fDesc, 44100, 512, error);
  753. if (fInstance == nullptr)
  754. {
  755. pData->engine->setLastError(error.toRawUTF8());
  756. return false;
  757. }
  758. fInstance->fillInPluginDescription(fDesc);
  759. // ---------------------------------------------------------------
  760. // get info
  761. if (name != nullptr && name[0] != '\0')
  762. pData->name = pData->engine->getUniquePluginName(name);
  763. else
  764. pData->name = pData->engine->getUniquePluginName(fInstance->getName().toRawUTF8());
  765. pData->filename = carla_strdup(filename);
  766. // ---------------------------------------------------------------
  767. // register client
  768. pData->client = pData->engine->addClient(this);
  769. if (pData->client == nullptr || ! pData->client->isOk())
  770. {
  771. pData->engine->setLastError("Failed to register plugin client");
  772. return false;
  773. }
  774. // ---------------------------------------------------------------
  775. // load plugin settings
  776. {
  777. // set default options
  778. pData->options = 0x0;
  779. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  780. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  781. //pData->options |= PLUGIN_OPTION_USE_CHUNKS;
  782. if (fInstance->acceptsMidi())
  783. {
  784. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  785. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  786. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  787. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  788. }
  789. // set identifier string
  790. String juceId(fDesc.createIdentifierString());
  791. CarlaString identifier("Juce/");
  792. identifier += juceId.toRawUTF8();
  793. pData->identifier = identifier.dup();
  794. // load settings
  795. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  796. // ignore settings, we need this anyway
  797. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  798. }
  799. return true;
  800. }
  801. private:
  802. PluginDescription fDesc;
  803. AudioPluginInstance* fInstance;
  804. AudioPluginFormatManager fFormatManager;
  805. AudioSampleBuffer fAudioBuffer;
  806. MidiBuffer fMidiBuffer;
  807. const char* fUniqueId;
  808. ScopedPointer<JucePluginWindow> fWindow;
  809. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePlugin)
  810. };
  811. CARLA_BACKEND_END_NAMESPACE
  812. #endif // HAVE_JUCE
  813. // -------------------------------------------------------------------------------------------------------------------
  814. CARLA_BACKEND_START_NAMESPACE
  815. CarlaPlugin* CarlaPlugin::newJuce(const Initializer& init, const char* const format)
  816. {
  817. carla_debug("CarlaPlugin::newJuce({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "}, %s)", init.engine, init.filename, init.name, init.label, init.uniqueId, format);
  818. #ifdef HAVE_JUCE
  819. JucePlugin* const plugin(new JucePlugin(init.engine, init.id));
  820. if (! plugin->init(init.filename, init.name, init.label, init.uniqueId, format))
  821. {
  822. delete plugin;
  823. return nullptr;
  824. }
  825. plugin->reload();
  826. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  827. {
  828. init.engine->setLastError("Carla's rack mode can only work with Stereo VST3 plugins, sorry!");
  829. delete plugin;
  830. return nullptr;
  831. }
  832. return plugin;
  833. #else
  834. init.engine->setLastError("Juce support not available");
  835. return nullptr;
  836. // unused
  837. (void)format;
  838. #endif
  839. }
  840. CARLA_BACKEND_END_NAMESPACE
  841. // -------------------------------------------------------------------------------------------------------------------