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.

3182 lines
114KB

  1. // SPDX-FileCopyrightText: 2011-2025 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "CarlaPluginInternal.hpp"
  4. #include "CarlaEngine.hpp"
  5. #include "CarlaBackendUtils.hpp"
  6. #include "CarlaMathUtils.hpp"
  7. #include "CarlaNative.h"
  8. #include "water/misc/Time.h"
  9. #include "water/text/StringArray.h"
  10. // -----------------------------------------------------------------------
  11. // used in carla-base.cpp
  12. std::size_t carla_getNativePluginCount() noexcept;
  13. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  14. // -----------------------------------------------------------------------
  15. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  16. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  17. {
  18. gPluginDescriptors.append(desc);
  19. }
  20. // -----------------------------------------------------------------------
  21. static
  22. class NativePluginInitializer
  23. {
  24. public:
  25. NativePluginInitializer() noexcept
  26. : fNeedsInit(true) {}
  27. ~NativePluginInitializer() noexcept
  28. {
  29. gPluginDescriptors.clear();
  30. }
  31. void initIfNeeded() noexcept
  32. {
  33. if (! fNeedsInit)
  34. return;
  35. fNeedsInit = false;
  36. try {
  37. carla_register_all_native_plugins();
  38. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  39. }
  40. private:
  41. bool fNeedsInit;
  42. } sPluginInitializer;
  43. // -----------------------------------------------------------------------
  44. std::size_t carla_getNativePluginCount() noexcept
  45. {
  46. sPluginInitializer.initIfNeeded();
  47. return gPluginDescriptors.count();
  48. }
  49. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  50. {
  51. sPluginInitializer.initIfNeeded();
  52. return gPluginDescriptors.getAt(index, nullptr);
  53. }
  54. // -----------------------------------------------------------------------
  55. CARLA_BACKEND_START_NAMESPACE
  56. // -------------------------------------------------------------------
  57. // Fallback data
  58. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  59. static EngineEvent kNullEngineEvent = {
  60. kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, -1, 0.0f, true }}
  61. };
  62. // -----------------------------------------------------------------------
  63. struct NativePluginMidiOutData {
  64. uint32_t count;
  65. uint32_t* indexes;
  66. CarlaEngineEventPort** ports;
  67. NativePluginMidiOutData() noexcept
  68. : count(0),
  69. indexes(nullptr),
  70. ports(nullptr) {}
  71. ~NativePluginMidiOutData() noexcept
  72. {
  73. CARLA_SAFE_ASSERT_INT(count == 0, count);
  74. CARLA_SAFE_ASSERT(indexes == nullptr);
  75. CARLA_SAFE_ASSERT(ports == nullptr);
  76. }
  77. bool createNew(const uint32_t newCount)
  78. {
  79. CARLA_SAFE_ASSERT_INT(count == 0, count);
  80. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr, false);
  81. CARLA_SAFE_ASSERT_RETURN(ports == nullptr, false);
  82. CARLA_SAFE_ASSERT_RETURN(newCount > 0, false);
  83. indexes = new uint32_t[newCount];
  84. ports = new CarlaEngineEventPort*[newCount];
  85. count = newCount;
  86. carla_zeroStructs(indexes, newCount);
  87. carla_zeroStructs(ports, newCount);
  88. return true;
  89. }
  90. void clear() noexcept
  91. {
  92. if (ports != nullptr)
  93. {
  94. for (uint32_t i=0; i < count; ++i)
  95. {
  96. if (ports[i] != nullptr)
  97. {
  98. delete ports[i];
  99. ports[i] = nullptr;
  100. }
  101. }
  102. delete[] ports;
  103. ports = nullptr;
  104. }
  105. if (indexes != nullptr)
  106. {
  107. delete[] indexes;
  108. indexes = nullptr;
  109. }
  110. count = 0;
  111. }
  112. void initBuffers() const noexcept
  113. {
  114. for (uint32_t i=0; i < count; ++i)
  115. {
  116. if (ports[i] != nullptr)
  117. ports[i]->initBuffer();
  118. }
  119. }
  120. CARLA_DECLARE_NON_COPYABLE(NativePluginMidiOutData)
  121. };
  122. struct NativePluginMidiInData : NativePluginMidiOutData {
  123. struct MultiPortData {
  124. uint32_t cachedEventCount;
  125. uint32_t usedIndex;
  126. };
  127. MultiPortData* multiportData;
  128. NativePluginMidiInData() noexcept
  129. : NativePluginMidiOutData(),
  130. multiportData(nullptr) {}
  131. ~NativePluginMidiInData() noexcept
  132. {
  133. CARLA_SAFE_ASSERT(multiportData == nullptr);
  134. }
  135. bool createNew(const uint32_t newCount)
  136. {
  137. if (! NativePluginMidiOutData::createNew(newCount))
  138. return false;
  139. multiportData = new MultiPortData[newCount];
  140. carla_zeroStructs(multiportData, newCount);
  141. return true;
  142. }
  143. void clear() noexcept
  144. {
  145. if (multiportData != nullptr)
  146. {
  147. delete[] multiportData;
  148. multiportData = nullptr;
  149. }
  150. NativePluginMidiOutData::clear();
  151. }
  152. void initBuffers(CarlaEngineEventPort* const port) const noexcept
  153. {
  154. if (count == 1)
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  157. carla_zeroStruct(multiportData[0]);
  158. multiportData[0].cachedEventCount = port->getEventCount();
  159. return;
  160. }
  161. for (uint32_t i=0; i < count; ++i)
  162. {
  163. carla_zeroStruct(multiportData[i]);
  164. if (ports[i] != nullptr)
  165. {
  166. ports[i]->initBuffer();
  167. multiportData[i].cachedEventCount = ports[i]->getEventCount();
  168. }
  169. }
  170. }
  171. CARLA_DECLARE_NON_COPYABLE(NativePluginMidiInData)
  172. };
  173. // -----------------------------------------------------
  174. class CarlaPluginNative : public CarlaPlugin
  175. {
  176. public:
  177. CarlaPluginNative(CarlaEngine* const engine, const uint id)
  178. : CarlaPlugin(engine, id),
  179. fHandle(nullptr),
  180. fHandle2(nullptr),
  181. fHost(),
  182. fDescriptor(nullptr),
  183. fIsProcessing(false),
  184. fIsOffline(engine->isOffline()),
  185. fIsUiAvailable(false),
  186. fIsUiVisible(false),
  187. fNeedsIdle(false),
  188. fInlineDisplayNeedsRedraw(false),
  189. fInlineDisplayLastRedrawTime(0),
  190. fLastProjectFilename(),
  191. fLastProjectFolder(),
  192. fAudioAndCvInBuffers(nullptr),
  193. fAudioAndCvOutBuffers(nullptr),
  194. fMidiEventInCount(0),
  195. fMidiEventOutCount(0),
  196. fCurBufferSize(engine->getBufferSize()),
  197. fCurSampleRate(engine->getSampleRate()),
  198. fMidiIn(),
  199. fMidiOut(),
  200. fTimeInfo()
  201. {
  202. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  203. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  204. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  205. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  206. carla_zeroStruct(fTimeInfo);
  207. fHost.handle = this;
  208. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  209. fHost.uiName = nullptr;
  210. fHost.uiParentId = engine->getOptions().frontendWinId;
  211. fHost.get_buffer_size = carla_host_get_buffer_size;
  212. fHost.get_sample_rate = carla_host_get_sample_rate;
  213. fHost.is_offline = carla_host_is_offline;
  214. fHost.get_time_info = carla_host_get_time_info;
  215. fHost.write_midi_event = carla_host_write_midi_event;
  216. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  217. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  218. fHost.ui_closed = carla_host_ui_closed;
  219. fHost.ui_open_file = carla_host_ui_open_file;
  220. fHost.ui_save_file = carla_host_ui_save_file;
  221. fHost.dispatcher = carla_host_dispatcher;
  222. }
  223. ~CarlaPluginNative() override
  224. {
  225. carla_debug("CarlaPluginNative::~CarlaPluginNative()");
  226. fInlineDisplayNeedsRedraw = false;
  227. // close UI
  228. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  229. {
  230. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  231. fDescriptor->ui_show(fHandle, false);
  232. #ifndef BUILD_BRIDGE
  233. pData->transientTryCounter = 0;
  234. #endif
  235. }
  236. pData->singleMutex.lock();
  237. pData->masterMutex.lock();
  238. if (pData->client != nullptr && pData->client->isActive())
  239. pData->client->deactivate(true);
  240. CARLA_ASSERT(! fIsProcessing);
  241. if (pData->active)
  242. {
  243. deactivate();
  244. pData->active = false;
  245. }
  246. if (fDescriptor != nullptr)
  247. {
  248. if (fDescriptor->cleanup != nullptr)
  249. {
  250. if (fHandle != nullptr)
  251. fDescriptor->cleanup(fHandle);
  252. if (fHandle2 != nullptr)
  253. fDescriptor->cleanup(fHandle2);
  254. }
  255. fHandle = nullptr;
  256. fHandle2 = nullptr;
  257. fDescriptor = nullptr;
  258. }
  259. if (fHost.resourceDir != nullptr)
  260. {
  261. delete[] fHost.resourceDir;
  262. fHost.resourceDir = nullptr;
  263. }
  264. if (fHost.uiName != nullptr)
  265. {
  266. std::free(const_cast<char*>(fHost.uiName));
  267. fHost.uiName = nullptr;
  268. }
  269. clearBuffers();
  270. }
  271. // -------------------------------------------------------------------
  272. // Information (base)
  273. PluginType getType() const noexcept override
  274. {
  275. return PLUGIN_INTERNAL;
  276. }
  277. PluginCategory getCategory() const noexcept override
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  280. return static_cast<PluginCategory>(fDescriptor->category);
  281. }
  282. // -------------------------------------------------------------------
  283. // Information (count)
  284. uint32_t getMidiInCount() const noexcept override
  285. {
  286. return fMidiIn.count;
  287. }
  288. uint32_t getMidiOutCount() const noexcept override
  289. {
  290. return fMidiOut.count;
  291. }
  292. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  295. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  296. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  297. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  298. // FIXME - try
  299. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  300. return param->scalePointCount;
  301. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  302. return 0;
  303. }
  304. // -------------------------------------------------------------------
  305. // Information (current data)
  306. // nothing
  307. // -------------------------------------------------------------------
  308. // Information (per-plugin data)
  309. uint getOptionsAvailable() const noexcept override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  312. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  313. bool hasMidiProgs = false;
  314. if (fDescriptor->get_midi_program_count != nullptr)
  315. {
  316. try {
  317. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  318. } catch (...) {}
  319. }
  320. uint options = 0x0;
  321. // can't disable fixed buffers if required by the plugin
  322. if ((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0x0)
  323. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  324. // can't disable forced stereo if enabled in the engine, or using CV
  325. if (pData->engine->getOptions().forceStereo || pData->cvIn.count != 0 || pData->cvOut.count != 0)
  326. pass();
  327. // if inputs or outputs are just 1, then yes we can force stereo
  328. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  329. options |= PLUGIN_OPTION_FORCE_STEREO;
  330. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  331. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  332. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  333. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  334. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  335. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  336. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  337. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  338. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  339. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  340. if (fDescriptor->midiIns > 0)
  341. options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  342. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  343. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  344. else if (hasMidiProgs)
  345. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  346. return options;
  347. }
  348. float getParameterValue(const uint32_t parameterId) const noexcept override
  349. {
  350. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  351. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  352. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  353. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  354. // FIXME - try
  355. return fDescriptor->get_parameter_value(fHandle, parameterId);
  356. }
  357. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  358. {
  359. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  360. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  361. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  362. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  363. // FIXME - try
  364. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  365. {
  366. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  367. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  368. return scalePoint->value;
  369. }
  370. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  371. return 0.0f;
  372. }
  373. bool getLabel(char* const strBuf) const noexcept override
  374. {
  375. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  376. if (fDescriptor->label != nullptr)
  377. {
  378. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  379. return true;
  380. }
  381. return CarlaPlugin::getLabel(strBuf);
  382. }
  383. bool getMaker(char* const strBuf) const noexcept override
  384. {
  385. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  386. if (fDescriptor->maker != nullptr)
  387. {
  388. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  389. return true;
  390. }
  391. return CarlaPlugin::getMaker(strBuf);
  392. }
  393. bool getCopyright(char* const strBuf) const noexcept override
  394. {
  395. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  396. if (fDescriptor->copyright != nullptr)
  397. {
  398. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  399. return true;
  400. }
  401. return CarlaPlugin::getCopyright(strBuf);
  402. }
  403. bool getRealName(char* const strBuf) const noexcept override
  404. {
  405. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  406. if (fDescriptor->name != nullptr)
  407. {
  408. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  409. return true;
  410. }
  411. return CarlaPlugin::getRealName(strBuf);
  412. }
  413. bool getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  414. {
  415. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  416. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  417. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  418. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  419. // FIXME - try
  420. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  421. {
  422. if (param->name != nullptr)
  423. {
  424. std::strncpy(strBuf, param->name, STR_MAX);
  425. return true;
  426. }
  427. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  428. return CarlaPlugin::getParameterName(parameterId, strBuf);
  429. }
  430. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  431. return CarlaPlugin::getParameterName(parameterId, strBuf);
  432. }
  433. bool getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  434. {
  435. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  436. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  437. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  438. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  439. // FIXME - try
  440. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  441. {
  442. if (param->unit != nullptr)
  443. {
  444. std::strncpy(strBuf, param->unit, STR_MAX);
  445. return true;
  446. }
  447. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  448. }
  449. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  450. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  451. }
  452. bool getParameterComment(const uint32_t parameterId, char* const strBuf) const noexcept override
  453. {
  454. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  455. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  456. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  457. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  458. // FIXME - try
  459. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  460. {
  461. if (param->comment != nullptr)
  462. {
  463. std::strncpy(strBuf, param->comment, STR_MAX);
  464. return true;
  465. }
  466. return CarlaPlugin::getParameterComment(parameterId, strBuf);
  467. }
  468. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  469. return CarlaPlugin::getParameterComment(parameterId, strBuf);
  470. }
  471. bool getParameterGroupName(const uint32_t parameterId, char* const strBuf) const noexcept override
  472. {
  473. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  474. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  475. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  476. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  477. // FIXME - try
  478. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  479. {
  480. if (param->groupName != nullptr)
  481. {
  482. std::strncpy(strBuf, param->groupName, STR_MAX);
  483. return true;
  484. }
  485. return CarlaPlugin::getParameterGroupName(parameterId, strBuf);
  486. }
  487. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  488. return CarlaPlugin::getParameterGroupName(parameterId, strBuf);
  489. }
  490. bool getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  491. {
  492. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, false);
  493. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, false);
  494. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  495. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, false);
  496. // FIXME - try
  497. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  498. {
  499. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, false);
  500. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  501. if (scalePoint->label != nullptr)
  502. {
  503. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  504. return true;
  505. }
  506. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  507. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  508. }
  509. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  510. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  511. }
  512. // -------------------------------------------------------------------
  513. // Set data (state)
  514. void prepareForSave(bool) override
  515. {
  516. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  517. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  518. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  519. {
  520. char strBuf[STR_MAX+1];
  521. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  522. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  523. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  524. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  525. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  526. strBuf[STR_MAX] = '\0';
  527. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  528. }
  529. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  530. return;
  531. if (char* data = fDescriptor->get_state(fHandle))
  532. {
  533. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  534. std::free(data);
  535. }
  536. }
  537. // -------------------------------------------------------------------
  538. // Set data (internal stuff)
  539. void setName(const char* const newName) override
  540. {
  541. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  542. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  543. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  544. CarlaPlugin::setName(newName);
  545. if (pData->uiTitle.isEmpty())
  546. setWindowTitle(nullptr);
  547. }
  548. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  549. {
  550. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  551. pData->midiprog.current = fCurMidiProgs[channel];
  552. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  553. }
  554. void setWindowTitle(const char* const title) noexcept
  555. {
  556. String uiName;
  557. if (title != nullptr)
  558. {
  559. uiName = title;
  560. }
  561. else
  562. {
  563. uiName = pData->name;
  564. uiName += " (GUI)";
  565. }
  566. std::free(const_cast<char*>(fHost.uiName));
  567. fHost.uiName = uiName.getAndReleaseBuffer();
  568. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  569. {
  570. try {
  571. fDescriptor->dispatcher(fHandle,
  572. NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED,
  573. 0, 0,
  574. const_cast<char*>(fHost.uiName),
  575. 0.0f);
  576. } CARLA_SAFE_EXCEPTION("set custom ui title");
  577. }
  578. }
  579. // -------------------------------------------------------------------
  580. // Set data (plugin-specific stuff)
  581. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  582. {
  583. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  584. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  585. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  586. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  587. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  588. // FIXME - try
  589. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  590. if (fHandle2 != nullptr)
  591. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  592. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  593. }
  594. void setParameterValueRT(const uint32_t parameterId, const float value, const uint32_t frameOffset, const bool sendCallbackLater) noexcept override
  595. {
  596. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  597. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  598. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  599. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  600. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  601. // FIXME - try
  602. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  603. if (fHandle2 != nullptr)
  604. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  605. CarlaPlugin::setParameterValueRT(parameterId, fixedValue, frameOffset, sendCallbackLater);
  606. }
  607. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  608. {
  609. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  610. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  611. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  612. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  613. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  614. carla_debug("CarlaPluginNative::setCustomData(\"%s\", \"%s\", ..., %s)", type, key, bool2str(sendGui));
  615. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  616. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  617. if (std::strcmp(type, CUSTOM_DATA_TYPE_PATH) == 0)
  618. {
  619. CARLA_SAFE_ASSERT_RETURN(std::strcmp(key, "file") == 0,);
  620. CARLA_SAFE_ASSERT_RETURN(value[0] != '\0',);
  621. }
  622. else if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  623. {
  624. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid",
  625. type, key, value, bool2str(sendGui));
  626. }
  627. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  628. {
  629. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  630. {
  631. const ScopedSingleProcessLocker spl(this, true);
  632. fDescriptor->set_state(fHandle, value);
  633. if (fHandle2 != nullptr)
  634. fDescriptor->set_state(fHandle2, value);
  635. }
  636. }
  637. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  638. {
  639. water::StringArray midiProgramList(water::StringArray::fromTokens(value, ":", ""));
  640. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  641. {
  642. uint8_t channel = 0;
  643. for (water::String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  644. {
  645. const int index(it->getIntValue());
  646. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  647. {
  648. const uint32_t bank = pData->midiprog.data[index].bank;
  649. const uint32_t program = pData->midiprog.data[index].program;
  650. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  651. if (fHandle2 != nullptr)
  652. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  653. fCurMidiProgs[channel] = index;
  654. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  655. {
  656. pData->midiprog.current = index;
  657. pData->engine->callback(true, true,
  658. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  659. pData->id,
  660. index,
  661. 0, 0, 0.0f, nullptr);
  662. }
  663. }
  664. ++channel;
  665. }
  666. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  667. }
  668. }
  669. else
  670. {
  671. if (fDescriptor->set_custom_data != nullptr)
  672. {
  673. fDescriptor->set_custom_data(fHandle, key, value);
  674. if (fHandle2 != nullptr)
  675. fDescriptor->set_custom_data(fHandle2, key, value);
  676. }
  677. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  678. fDescriptor->ui_set_custom_data(fHandle, key, value);
  679. }
  680. CarlaPlugin::setCustomData(type, key, value, sendGui);
  681. }
  682. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  683. {
  684. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  685. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  686. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  687. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  688. // TODO, put into check below
  689. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  690. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  691. if (index >= 0)
  692. {
  693. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  694. const uint32_t bank = pData->midiprog.data[index].bank;
  695. const uint32_t program = pData->midiprog.data[index].program;
  696. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  697. try {
  698. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  699. } catch(...) {}
  700. if (fHandle2 != nullptr)
  701. {
  702. try {
  703. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  704. } catch(...) {}
  705. }
  706. fCurMidiProgs[channel] = index;
  707. }
  708. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  709. }
  710. // FIXME: this is never used
  711. void setMidiProgramRT(const uint32_t index, const bool sendCallbackLater) noexcept override
  712. {
  713. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  714. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  715. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  716. // TODO, put into check below
  717. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  718. return CarlaPlugin::setMidiProgramRT(index, sendCallbackLater);
  719. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  720. const uint32_t bank = pData->midiprog.data[index].bank;
  721. const uint32_t program = pData->midiprog.data[index].program;
  722. try {
  723. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  724. } catch(...) {}
  725. if (fHandle2 != nullptr)
  726. {
  727. try {
  728. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  729. } catch(...) {}
  730. }
  731. fCurMidiProgs[channel] = static_cast<int32_t>(index);
  732. CarlaPlugin::setMidiProgramRT(index, sendCallbackLater);
  733. }
  734. // -------------------------------------------------------------------
  735. // Set ui stuff
  736. void setCustomUITitle(const char* const title) noexcept override
  737. {
  738. setWindowTitle(title);
  739. CarlaPlugin::setCustomUITitle(title);
  740. }
  741. void showCustomUI(const bool yesNo) override
  742. {
  743. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  744. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  745. if (fDescriptor->ui_show == nullptr)
  746. return;
  747. fIsUiAvailable = true;
  748. fDescriptor->ui_show(fHandle, yesNo);
  749. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  750. if (yesNo && ! fIsUiAvailable)
  751. return;
  752. fIsUiVisible = yesNo;
  753. if (! yesNo)
  754. {
  755. #ifndef BUILD_BRIDGE
  756. pData->transientTryCounter = 0;
  757. #endif
  758. return;
  759. }
  760. #ifndef BUILD_BRIDGE
  761. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  762. pData->tryTransient();
  763. #endif
  764. if (fDescriptor->ui_set_custom_data != nullptr)
  765. {
  766. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  767. {
  768. const CustomData& cData(it.getValue(kCustomDataFallback));
  769. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  770. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  771. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  772. }
  773. }
  774. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  775. {
  776. const int32_t index = pData->midiprog.current;
  777. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  778. const uint32_t bank = pData->midiprog.data[index].bank;
  779. const uint32_t program = pData->midiprog.data[index].program;
  780. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  781. }
  782. if (fDescriptor->ui_set_parameter_value != nullptr)
  783. {
  784. for (uint32_t i=0; i < pData->param.count; ++i)
  785. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  786. }
  787. }
  788. void idle() override
  789. {
  790. if (fNeedsIdle)
  791. {
  792. fNeedsIdle = false;
  793. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_IDLE, 0, 0, nullptr, 0.0f);
  794. }
  795. if (fInlineDisplayNeedsRedraw)
  796. {
  797. // TESTING
  798. CARLA_SAFE_ASSERT(pData->enabled)
  799. CARLA_SAFE_ASSERT(!pData->engine->isAboutToClose());
  800. CARLA_SAFE_ASSERT(pData->client->isActive());
  801. if (pData->enabled && !pData->engine->isAboutToClose() && pData->client->isActive())
  802. {
  803. const int64_t timeNow = water::Time::currentTimeMillis();
  804. if (timeNow - fInlineDisplayLastRedrawTime > (1000 / 30))
  805. {
  806. fInlineDisplayNeedsRedraw = false;
  807. fInlineDisplayLastRedrawTime = timeNow;
  808. pData->engine->callback(true, true,
  809. ENGINE_CALLBACK_INLINE_DISPLAY_REDRAW,
  810. pData->id,
  811. 0, 0, 0, 0.0f, nullptr);
  812. }
  813. }
  814. else
  815. {
  816. fInlineDisplayNeedsRedraw = false;
  817. }
  818. }
  819. CarlaPlugin::idle();
  820. }
  821. void uiIdle() override
  822. {
  823. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  824. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  825. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  826. fDescriptor->ui_idle(fHandle);
  827. CarlaPlugin::uiIdle();
  828. }
  829. // -------------------------------------------------------------------
  830. // Plugin state
  831. void reload() override
  832. {
  833. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  834. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  835. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  836. carla_debug("CarlaPluginNative::reload() - start");
  837. const EngineProcessMode processMode = pData->engine->getProccessMode();
  838. // Safely disable plugin for reload
  839. const ScopedDisabler sd(this);
  840. if (pData->active)
  841. deactivate();
  842. clearBuffers();
  843. uint32_t aIns, aOuts, cvIns, cvOuts, mIns, mOuts, j;
  844. bool forcedStereoIn, forcedStereoOut;
  845. forcedStereoIn = forcedStereoOut = false;
  846. bool needsCtrlIn, needsCtrlOut;
  847. needsCtrlIn = needsCtrlOut = false;
  848. aIns = fDescriptor->audioIns;
  849. aOuts = fDescriptor->audioOuts;
  850. cvIns = fDescriptor->cvIns;
  851. cvOuts = fDescriptor->cvOuts;
  852. mIns = fDescriptor->midiIns;
  853. mOuts = fDescriptor->midiOuts;
  854. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  855. {
  856. if (fHandle2 == nullptr)
  857. fHandle2 = fDescriptor->instantiate(&fHost);
  858. if (fHandle2 != nullptr)
  859. {
  860. if (aIns == 1)
  861. {
  862. aIns = 2;
  863. forcedStereoIn = true;
  864. }
  865. if (aOuts == 1)
  866. {
  867. aOuts = 2;
  868. forcedStereoOut = true;
  869. }
  870. }
  871. }
  872. if (aIns > 0)
  873. {
  874. pData->audioIn.createNew(aIns);
  875. }
  876. if (aOuts > 0)
  877. {
  878. pData->audioOut.createNew(aOuts);
  879. needsCtrlIn = true;
  880. }
  881. if (cvIns > 0)
  882. {
  883. pData->cvIn.createNew(cvIns);
  884. }
  885. if (cvOuts > 0)
  886. {
  887. pData->cvOut.createNew(cvOuts);
  888. }
  889. if (const uint32_t acIns = aIns + cvIns)
  890. {
  891. fAudioAndCvInBuffers = new float*[acIns];
  892. carla_zeroPointers(fAudioAndCvInBuffers, acIns);
  893. }
  894. if (const uint32_t acOuts = aOuts + cvOuts)
  895. {
  896. fAudioAndCvOutBuffers = new float*[acOuts];
  897. carla_zeroPointers(fAudioAndCvOutBuffers, acOuts);
  898. }
  899. if (mIns > 0)
  900. {
  901. fMidiIn.createNew(mIns);
  902. needsCtrlIn = (mIns == 1);
  903. }
  904. if (mOuts > 0)
  905. {
  906. fMidiOut.createNew(mOuts);
  907. needsCtrlOut = (mOuts == 1);
  908. }
  909. const uint portNameSize(pData->engine->getMaxPortNameSize());
  910. String portName;
  911. // Audio Ins
  912. for (j=0; j < aIns; ++j)
  913. {
  914. portName.clear();
  915. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  916. {
  917. portName = pData->name;
  918. portName += ":";
  919. }
  920. if (fDescriptor->get_buffer_port_name != nullptr)
  921. {
  922. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoIn ? 0 : j, false);
  923. }
  924. else if (aIns > 1 && ! forcedStereoIn)
  925. {
  926. portName += "input_";
  927. portName += String(j+1);
  928. }
  929. else
  930. portName += "input";
  931. portName.truncate(portNameSize);
  932. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  933. pData->audioIn.ports[j].rindex = j;
  934. if (forcedStereoIn)
  935. {
  936. portName += "_2";
  937. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  938. pData->audioIn.ports[1].rindex = j;
  939. break;
  940. }
  941. }
  942. // Audio Outs
  943. for (j=0; j < aOuts; ++j)
  944. {
  945. portName.clear();
  946. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  947. {
  948. portName = pData->name;
  949. portName += ":";
  950. }
  951. if (fDescriptor->get_buffer_port_name != nullptr)
  952. {
  953. portName += fDescriptor->get_buffer_port_name(fHandle, forcedStereoOut ? 0 : j, true);
  954. }
  955. else if (aOuts > 1 && ! forcedStereoOut)
  956. {
  957. portName += "output_";
  958. portName += String(j+1);
  959. }
  960. else
  961. portName += "output";
  962. portName.truncate(portNameSize);
  963. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  964. pData->audioOut.ports[j].rindex = j;
  965. if (forcedStereoOut)
  966. {
  967. portName += "_2";
  968. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  969. pData->audioOut.ports[1].rindex = j;
  970. break;
  971. }
  972. }
  973. // CV Ins
  974. for (j=0; j < cvIns; ++j)
  975. {
  976. portName.clear();
  977. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  978. {
  979. portName = pData->name;
  980. portName += ":";
  981. }
  982. if (fDescriptor->get_buffer_port_name != nullptr)
  983. {
  984. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioIns + j, false);
  985. }
  986. else if (cvIns > 1)
  987. {
  988. portName += "cv_input_";
  989. portName += String(j+1);
  990. }
  991. else
  992. portName += "cv_input";
  993. portName.truncate(portNameSize);
  994. float min = -1.0f, max = 1.0f;
  995. if (fDescriptor->get_buffer_port_range != nullptr)
  996. {
  997. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  998. fDescriptor->audioIns + j,
  999. false))
  1000. {
  1001. min = range->minimum;
  1002. max = range->maximum;
  1003. }
  1004. }
  1005. pData->cvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true, j);
  1006. pData->cvIn.ports[j].rindex = j;
  1007. pData->cvIn.ports[j].port->setRange(min, max);
  1008. }
  1009. // CV Outs
  1010. for (j=0; j < cvOuts; ++j)
  1011. {
  1012. portName.clear();
  1013. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1014. {
  1015. portName = pData->name;
  1016. portName += ":";
  1017. }
  1018. if (fDescriptor->get_buffer_port_name != nullptr)
  1019. {
  1020. portName += fDescriptor->get_buffer_port_name(fHandle, fDescriptor->audioOuts + j, true);
  1021. }
  1022. else if (cvOuts > 1)
  1023. {
  1024. portName += "cv_output_";
  1025. portName += String(j+1);
  1026. }
  1027. else
  1028. portName += "cv_output";
  1029. portName.truncate(portNameSize);
  1030. float min = -1.0f, max = 1.0f;
  1031. if (fDescriptor->get_buffer_port_range != nullptr)
  1032. {
  1033. if (const NativePortRange* const range = fDescriptor->get_buffer_port_range(fHandle,
  1034. fDescriptor->audioOuts + j,
  1035. true))
  1036. {
  1037. min = range->minimum;
  1038. max = range->maximum;
  1039. }
  1040. }
  1041. pData->cvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false, j);
  1042. pData->cvOut.ports[j].rindex = j;
  1043. pData->cvOut.ports[j].port->setRange(min, max);
  1044. }
  1045. // MIDI Input (only if multiple)
  1046. if (mIns > 1)
  1047. {
  1048. for (j=0; j < mIns; ++j)
  1049. {
  1050. portName.clear();
  1051. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1052. {
  1053. portName = pData->name;
  1054. portName += ":";
  1055. }
  1056. portName += "midi-in_";
  1057. portName += String(j+1);
  1058. portName.truncate(portNameSize);
  1059. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  1060. fMidiIn.indexes[j] = j;
  1061. }
  1062. pData->event.portIn = fMidiIn.ports[0];
  1063. }
  1064. // MIDI Output (only if multiple)
  1065. if (mOuts > 1)
  1066. {
  1067. for (j=0; j < mOuts; ++j)
  1068. {
  1069. portName.clear();
  1070. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1071. {
  1072. portName = pData->name;
  1073. portName += ":";
  1074. }
  1075. portName += "midi-out_";
  1076. portName += String(j+1);
  1077. portName.truncate(portNameSize);
  1078. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  1079. fMidiOut.indexes[j] = j;
  1080. }
  1081. pData->event.portOut = fMidiOut.ports[0];
  1082. }
  1083. reloadParameters(&needsCtrlIn, &needsCtrlOut);
  1084. if (needsCtrlIn || mIns == 1)
  1085. {
  1086. portName.clear();
  1087. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1088. {
  1089. portName = pData->name;
  1090. portName += ":";
  1091. }
  1092. portName += "events-in";
  1093. portName.truncate(portNameSize);
  1094. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  1095. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1096. pData->event.cvSourcePorts = pData->client->createCVSourcePorts();
  1097. #endif
  1098. }
  1099. if (needsCtrlOut || mOuts == 1)
  1100. {
  1101. portName.clear();
  1102. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1103. {
  1104. portName = pData->name;
  1105. portName += ":";
  1106. }
  1107. portName += "events-out";
  1108. portName.truncate(portNameSize);
  1109. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1110. }
  1111. if (forcedStereoIn || forcedStereoOut)
  1112. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1113. else
  1114. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1115. // plugin hints
  1116. pData->hints = 0x0;
  1117. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1118. pData->hints |= PLUGIN_CAN_DRYWET;
  1119. if (aOuts > 0)
  1120. pData->hints |= PLUGIN_CAN_VOLUME;
  1121. if (aOuts >= 2 && aOuts % 2 == 0)
  1122. pData->hints |= PLUGIN_CAN_BALANCE;
  1123. // native plugin hints
  1124. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  1125. pData->hints |= PLUGIN_IS_RTSAFE;
  1126. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  1127. pData->hints |= PLUGIN_IS_SYNTH;
  1128. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  1129. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1130. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  1131. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  1132. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  1133. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1134. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE)
  1135. pData->hints |= PLUGIN_HAS_CUSTOM_UI_USING_FILE_OPEN;
  1136. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  1137. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  1138. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY)
  1139. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  1140. // extra plugin hints
  1141. pData->extraHints = 0x0;
  1142. bufferSizeChanged(pData->engine->getBufferSize());
  1143. reloadPrograms(true);
  1144. if (pData->active)
  1145. activate();
  1146. carla_debug("CarlaPluginNative::reload() - end");
  1147. }
  1148. void reloadParameters(bool* const needsCtrlIn, bool* const needsCtrlOut)
  1149. {
  1150. carla_debug("CarlaPluginNative::reloadParameters() - start");
  1151. const float sampleRate = static_cast<float>(pData->engine->getSampleRate());
  1152. const uint32_t params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr)
  1153. ? fDescriptor->get_parameter_count(fHandle)
  1154. : 0;
  1155. pData->param.clear();
  1156. if (params > 0)
  1157. {
  1158. pData->param.createNew(params, true);
  1159. }
  1160. for (uint32_t j=0; j < params; ++j)
  1161. {
  1162. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  1163. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  1164. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1165. pData->param.data[j].index = static_cast<int32_t>(j);
  1166. pData->param.data[j].rindex = static_cast<int32_t>(j);
  1167. float min, max, def, step, stepSmall, stepLarge;
  1168. // min value
  1169. min = paramInfo->ranges.min;
  1170. // max value
  1171. max = paramInfo->ranges.max;
  1172. if (min > max)
  1173. max = min;
  1174. if (carla_isEqual(min, max))
  1175. {
  1176. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  1177. max = min + 0.1f;
  1178. }
  1179. // default value
  1180. def = paramInfo->ranges.def;
  1181. if (def < min)
  1182. def = min;
  1183. else if (def > max)
  1184. def = max;
  1185. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  1186. {
  1187. min *= sampleRate;
  1188. max *= sampleRate;
  1189. def *= sampleRate;
  1190. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1191. }
  1192. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  1193. {
  1194. step = max - min;
  1195. stepSmall = step;
  1196. stepLarge = step;
  1197. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1198. }
  1199. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  1200. {
  1201. step = 1.0f;
  1202. stepSmall = 1.0f;
  1203. stepLarge = 10.0f;
  1204. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1205. }
  1206. else
  1207. {
  1208. float range = max - min;
  1209. step = range/100.0f;
  1210. stepSmall = range/1000.0f;
  1211. stepLarge = range/10.0f;
  1212. }
  1213. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  1214. {
  1215. pData->param.data[j].type = PARAMETER_OUTPUT;
  1216. if (needsCtrlOut != nullptr)
  1217. *needsCtrlOut = true;
  1218. }
  1219. else
  1220. {
  1221. pData->param.data[j].type = PARAMETER_INPUT;
  1222. if (needsCtrlIn != nullptr)
  1223. *needsCtrlIn = true;
  1224. }
  1225. // extra parameter hints
  1226. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  1227. {
  1228. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1229. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMATABLE)
  1230. {
  1231. pData->param.data[j].hints |= PARAMETER_IS_AUTOMATABLE;
  1232. pData->param.data[j].hints |= PARAMETER_CAN_BE_CV_CONTROLLED;
  1233. }
  1234. }
  1235. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  1236. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1237. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  1238. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1239. pData->param.ranges[j].min = min;
  1240. pData->param.ranges[j].max = max;
  1241. pData->param.ranges[j].def = def;
  1242. pData->param.ranges[j].step = step;
  1243. pData->param.ranges[j].stepSmall = stepSmall;
  1244. pData->param.ranges[j].stepLarge = stepLarge;
  1245. }
  1246. carla_debug("CarlaPluginNative::reloadParameters() - end");
  1247. }
  1248. void reloadPrograms(const bool doInit) override
  1249. {
  1250. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1251. uint32_t i, oldCount = pData->midiprog.count;
  1252. const int32_t current = pData->midiprog.current;
  1253. // Delete old programs
  1254. pData->midiprog.clear();
  1255. // Query new programs
  1256. uint32_t count = 0;
  1257. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1258. count = fDescriptor->get_midi_program_count(fHandle);
  1259. if (count > 0)
  1260. {
  1261. pData->midiprog.createNew(count);
  1262. // Update data
  1263. for (i=0; i < count; ++i)
  1264. {
  1265. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1266. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1267. pData->midiprog.data[i].bank = mpDesc->bank;
  1268. pData->midiprog.data[i].program = mpDesc->program;
  1269. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1270. }
  1271. }
  1272. if (doInit)
  1273. {
  1274. if (count > 0)
  1275. setMidiProgram(0, false, false, false, true);
  1276. }
  1277. else
  1278. {
  1279. // Check if current program is invalid
  1280. bool programChanged = false;
  1281. if (count == oldCount+1)
  1282. {
  1283. // one midi program added, probably created by user
  1284. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1285. programChanged = true;
  1286. }
  1287. else if (current < 0 && count > 0)
  1288. {
  1289. // programs exist now, but not before
  1290. pData->midiprog.current = 0;
  1291. programChanged = true;
  1292. }
  1293. else if (current >= 0 && count == 0)
  1294. {
  1295. // programs existed before, but not anymore
  1296. pData->midiprog.current = -1;
  1297. programChanged = true;
  1298. }
  1299. else if (current >= static_cast<int32_t>(count))
  1300. {
  1301. // current midi program > count
  1302. pData->midiprog.current = 0;
  1303. programChanged = true;
  1304. }
  1305. else
  1306. {
  1307. // no change
  1308. pData->midiprog.current = current;
  1309. }
  1310. if (programChanged)
  1311. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1312. pData->engine->callback(true, true,
  1313. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1314. pData->id,
  1315. 0, 0, 0, 0.0f, nullptr);
  1316. }
  1317. }
  1318. // -------------------------------------------------------------------
  1319. // Plugin processing
  1320. void activate() noexcept override
  1321. {
  1322. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1323. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1324. if (fDescriptor->activate != nullptr)
  1325. {
  1326. try {
  1327. fDescriptor->activate(fHandle);
  1328. } catch(...) {}
  1329. if (fHandle2 != nullptr)
  1330. {
  1331. try {
  1332. fDescriptor->activate(fHandle2);
  1333. } catch(...) {}
  1334. }
  1335. }
  1336. }
  1337. void deactivate() noexcept override
  1338. {
  1339. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1340. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1341. if (fDescriptor->deactivate != nullptr)
  1342. {
  1343. try {
  1344. fDescriptor->deactivate(fHandle);
  1345. } catch(...) {}
  1346. if (fHandle2 != nullptr)
  1347. {
  1348. try {
  1349. fDescriptor->deactivate(fHandle2);
  1350. } catch(...) {}
  1351. }
  1352. }
  1353. }
  1354. EngineEvent& findNextEvent()
  1355. {
  1356. if (fMidiIn.count == 1)
  1357. {
  1358. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1359. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1360. {
  1361. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1362. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1363. eventCount, multiportData.cachedEventCount);
  1364. return kNullEngineEvent;
  1365. }
  1366. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1367. }
  1368. uint32_t lowestSampleTime = 9999999;
  1369. uint32_t portMatching = 0;
  1370. bool found = false;
  1371. // process events in order for multiple ports
  1372. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1373. {
  1374. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1375. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1376. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1377. continue;
  1378. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1379. if (event.time < lowestSampleTime)
  1380. {
  1381. lowestSampleTime = event.time;
  1382. portMatching = m;
  1383. found = true;
  1384. }
  1385. }
  1386. if (found)
  1387. {
  1388. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1389. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1390. return eventPort->getEvent(multiportData.usedIndex++);
  1391. }
  1392. return kNullEngineEvent;
  1393. }
  1394. void process(const float* const* const audioIn, float** const audioOut,
  1395. const float* const* const cvIn, float** const cvOut, const uint32_t frames) override
  1396. {
  1397. // --------------------------------------------------------------------------------------------------------
  1398. // Check if active
  1399. if (! pData->active)
  1400. {
  1401. // disable any output sound
  1402. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1403. carla_zeroFloats(audioOut[i], frames);
  1404. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1405. carla_zeroFloats(cvOut[i], frames);
  1406. return;
  1407. }
  1408. fMidiEventInCount = fMidiEventOutCount = 0;
  1409. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1410. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1411. // --------------------------------------------------------------------------------------------------------
  1412. // Check if needs reset
  1413. if (pData->needsReset)
  1414. {
  1415. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1416. {
  1417. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1418. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1419. {
  1420. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1421. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1422. fMidiInEvents[k].data[2] = 0;
  1423. fMidiInEvents[k].size = 3;
  1424. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1425. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1426. fMidiInEvents[k+i].data[2] = 0;
  1427. fMidiInEvents[k+i].size = 3;
  1428. }
  1429. }
  1430. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1431. {
  1432. fMidiEventInCount = MAX_MIDI_NOTE;
  1433. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1434. {
  1435. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1436. fMidiInEvents[k].data[1] = k;
  1437. fMidiInEvents[k].data[2] = 0;
  1438. fMidiInEvents[k].size = 3;
  1439. }
  1440. }
  1441. pData->needsReset = false;
  1442. }
  1443. // --------------------------------------------------------------------------------------------------------
  1444. // Set TimeInfo
  1445. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1446. fTimeInfo.playing = timeInfo.playing;
  1447. fTimeInfo.frame = timeInfo.frame;
  1448. fTimeInfo.usecs = timeInfo.usecs;
  1449. if (timeInfo.bbt.valid)
  1450. {
  1451. fTimeInfo.bbt.valid = true;
  1452. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1453. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1454. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1455. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1456. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1457. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1458. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1459. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1460. }
  1461. else
  1462. {
  1463. fTimeInfo.bbt.valid = false;
  1464. }
  1465. #if 0
  1466. // This test code has proven to be quite useful
  1467. // So I am leaving it behind, I might need it again..
  1468. if (pData->id == 1)
  1469. {
  1470. static int64_t last_frame = timeInfo.frame;
  1471. static int64_t last_dev_frame = 0;
  1472. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1473. static double last_dev_val = 0.0;
  1474. int64_t cur_frame = timeInfo.frame;
  1475. int64_t cur_dev_frame = cur_frame - last_frame;
  1476. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1477. double cur_dev_val = cur_val - last_val;
  1478. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1479. {
  1480. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1481. frames,
  1482. timeInfo.frame,
  1483. cur_val,
  1484. cur_dev_frame,
  1485. cur_dev_val);
  1486. }
  1487. last_val = cur_val;
  1488. last_dev_val = cur_dev_val;
  1489. last_frame = cur_frame;
  1490. last_dev_frame = cur_dev_frame;
  1491. }
  1492. #endif
  1493. // --------------------------------------------------------------------------------------------------------
  1494. // Event Input and Processing
  1495. if (pData->event.portIn != nullptr)
  1496. {
  1497. // ----------------------------------------------------------------------------------------------------
  1498. // MIDI Input (External)
  1499. if (pData->extNotes.mutex.tryLock())
  1500. {
  1501. ExternalMidiNote note = { 0, 0, 0 };
  1502. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1503. {
  1504. note = pData->extNotes.data.getFirst(note, true);
  1505. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1506. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1507. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1508. nativeEvent.data[1] = note.note;
  1509. nativeEvent.data[2] = note.velo;
  1510. nativeEvent.size = 3;
  1511. }
  1512. pData->extNotes.mutex.unlock();
  1513. } // End of MIDI Input (External)
  1514. // ----------------------------------------------------------------------------------------------------
  1515. // Event Input (System)
  1516. #ifndef BUILD_BRIDGE
  1517. bool allNotesOffSent = false;
  1518. #endif
  1519. const bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1520. uint32_t startTime = 0;
  1521. uint32_t timeOffset = 0;
  1522. uint32_t nextBankId;
  1523. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1524. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1525. else
  1526. nextBankId = 0;
  1527. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1528. if (cvIn != nullptr && pData->event.cvSourcePorts != nullptr)
  1529. pData->event.cvSourcePorts->initPortBuffers(cvIn + pData->cvIn.count, frames, isSampleAccurate, pData->event.portIn);
  1530. #endif
  1531. for (;;)
  1532. {
  1533. EngineEvent& event(findNextEvent());
  1534. if (event.type == kEngineEventTypeNull)
  1535. break;
  1536. uint32_t eventTime = event.time;
  1537. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1538. if (eventTime < timeOffset)
  1539. {
  1540. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1541. eventTime, timeOffset, pData->name);
  1542. eventTime = timeOffset;
  1543. }
  1544. if (isSampleAccurate && eventTime > timeOffset)
  1545. {
  1546. if (processSingle(audioIn, audioOut, cvIn, cvOut, eventTime - timeOffset, timeOffset))
  1547. {
  1548. startTime = 0;
  1549. timeOffset = eventTime;
  1550. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1551. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1552. else
  1553. nextBankId = 0;
  1554. if (fMidiEventInCount > 0)
  1555. {
  1556. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1557. fMidiEventInCount = 0;
  1558. }
  1559. if (fMidiEventOutCount > 0)
  1560. {
  1561. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1562. fMidiEventOutCount = 0;
  1563. }
  1564. }
  1565. else
  1566. startTime += timeOffset;
  1567. }
  1568. // Control change
  1569. switch (event.type)
  1570. {
  1571. case kEngineEventTypeNull:
  1572. break;
  1573. case kEngineEventTypeControl: {
  1574. EngineControlEvent& ctrlEvent(event.ctrl);
  1575. switch (ctrlEvent.type)
  1576. {
  1577. case kEngineControlEventTypeNull:
  1578. break;
  1579. case kEngineControlEventTypeParameter: {
  1580. float value;
  1581. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1582. // non-midi
  1583. if (event.channel == kEngineEventNonMidiChannel)
  1584. {
  1585. const uint32_t k = ctrlEvent.param;
  1586. CARLA_SAFE_ASSERT_CONTINUE(k < pData->param.count);
  1587. ctrlEvent.handled = true;
  1588. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1589. setParameterValueRT(k, value, event.time, true);
  1590. continue;
  1591. }
  1592. // Control backend stuff
  1593. if (event.channel == pData->ctrlChannel)
  1594. {
  1595. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1596. {
  1597. ctrlEvent.handled = true;
  1598. value = ctrlEvent.normalizedValue;
  1599. setDryWetRT(value, true);
  1600. }
  1601. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1602. {
  1603. ctrlEvent.handled = true;
  1604. value = ctrlEvent.normalizedValue*127.0f/100.0f;
  1605. setVolumeRT(value, true);
  1606. }
  1607. else if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1608. {
  1609. float left, right;
  1610. value = ctrlEvent.normalizedValue/0.5f - 1.0f;
  1611. if (value < 0.0f)
  1612. {
  1613. left = -1.0f;
  1614. right = (value*2.0f)+1.0f;
  1615. }
  1616. else if (value > 0.0f)
  1617. {
  1618. left = (value*2.0f)-1.0f;
  1619. right = 1.0f;
  1620. }
  1621. else
  1622. {
  1623. left = -1.0f;
  1624. right = 1.0f;
  1625. }
  1626. ctrlEvent.handled = true;
  1627. setBalanceLeftRT(left, true);
  1628. setBalanceRightRT(right, true);
  1629. }
  1630. }
  1631. #endif
  1632. // Control plugin parameters
  1633. for (uint32_t k=0; k < pData->param.count; ++k)
  1634. {
  1635. if (pData->param.data[k].midiChannel != event.channel)
  1636. continue;
  1637. if (pData->param.data[k].mappedControlIndex != ctrlEvent.param)
  1638. continue;
  1639. if (pData->param.data[k].type != PARAMETER_INPUT)
  1640. continue;
  1641. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMATABLE) == 0)
  1642. continue;
  1643. ctrlEvent.handled = true;
  1644. value = pData->param.getFinalUnnormalizedValue(k, ctrlEvent.normalizedValue);
  1645. setParameterValueRT(k, value, event.time, true);
  1646. }
  1647. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_VALUE)
  1648. {
  1649. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1650. continue;
  1651. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1652. carla_zeroStruct(nativeEvent);
  1653. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1654. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1655. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1656. nativeEvent.data[2] = uint8_t(ctrlEvent.normalizedValue*127.0f + 0.5f);
  1657. nativeEvent.size = 3;
  1658. }
  1659. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1660. if (! ctrlEvent.handled)
  1661. checkForMidiLearn(event);
  1662. #endif
  1663. break;
  1664. } // case kEngineControlEventTypeParameter
  1665. case kEngineControlEventTypeMidiBank:
  1666. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1667. {
  1668. if (event.channel == pData->ctrlChannel)
  1669. nextBankId = ctrlEvent.param;
  1670. }
  1671. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1672. {
  1673. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1674. continue;
  1675. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1676. carla_zeroStruct(nativeEvent);
  1677. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1678. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1679. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1680. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1681. nativeEvent.size = 3;
  1682. }
  1683. break;
  1684. case kEngineControlEventTypeMidiProgram:
  1685. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1686. {
  1687. if (event.channel < MAX_MIDI_CHANNELS)
  1688. {
  1689. const uint32_t nextProgramId(ctrlEvent.param);
  1690. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1691. {
  1692. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1693. {
  1694. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1695. if (fHandle2 != nullptr)
  1696. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1697. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1698. if (event.channel == pData->ctrlChannel)
  1699. {
  1700. pData->postponeMidiProgramChangeRtEvent(true, k);
  1701. }
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. }
  1707. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1708. {
  1709. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1710. continue;
  1711. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1712. carla_zeroStruct(nativeEvent);
  1713. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1714. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1715. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1716. nativeEvent.size = 2;
  1717. }
  1718. break;
  1719. case kEngineControlEventTypeAllSoundOff:
  1720. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1721. {
  1722. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1723. continue;
  1724. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1725. carla_zeroStruct(nativeEvent);
  1726. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1727. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1728. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1729. nativeEvent.data[2] = 0;
  1730. nativeEvent.size = 3;
  1731. }
  1732. break;
  1733. case kEngineControlEventTypeAllNotesOff:
  1734. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1735. {
  1736. #ifndef BUILD_BRIDGE
  1737. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1738. {
  1739. allNotesOffSent = true;
  1740. postponeRtAllNotesOff();
  1741. }
  1742. #endif
  1743. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1744. continue;
  1745. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1746. carla_zeroStruct(nativeEvent);
  1747. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1748. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1749. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1750. nativeEvent.data[2] = 0;
  1751. nativeEvent.size = 3;
  1752. }
  1753. break;
  1754. }
  1755. break;
  1756. }
  1757. case kEngineEventTypeMidi: {
  1758. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1759. continue;
  1760. const EngineMidiEvent& midiEvent(event.midi);
  1761. if (midiEvent.size > 4)
  1762. continue;
  1763. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1764. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1765. #endif
  1766. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1767. if ((status == MIDI_STATUS_NOTE_OFF || status == MIDI_STATUS_NOTE_ON) && (pData->options & PLUGIN_OPTION_SKIP_SENDING_NOTES))
  1768. continue;
  1769. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1770. continue;
  1771. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1772. continue;
  1773. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1774. continue;
  1775. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1776. continue;
  1777. // Fix bad note-off
  1778. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1779. status = MIDI_STATUS_NOTE_OFF;
  1780. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1781. carla_zeroStruct(nativeEvent);
  1782. nativeEvent.port = midiEvent.port;
  1783. nativeEvent.time = isSampleAccurate ? startTime : eventTime;
  1784. nativeEvent.size = midiEvent.size;
  1785. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1786. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1787. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1788. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1789. if (status == MIDI_STATUS_NOTE_ON)
  1790. {
  1791. pData->postponeNoteOnRtEvent(true, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1792. }
  1793. else if (status == MIDI_STATUS_NOTE_OFF)
  1794. {
  1795. pData->postponeNoteOffRtEvent(true, event.channel, midiEvent.data[1]);
  1796. }
  1797. } break;
  1798. } // switch (event.type)
  1799. }
  1800. pData->postRtEvents.trySplice();
  1801. if (frames > timeOffset)
  1802. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1803. } // End of Event Input and Processing
  1804. // --------------------------------------------------------------------------------------------------------
  1805. // Plugin processing (no events)
  1806. else
  1807. {
  1808. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1809. } // End of Plugin processing (no events)
  1810. #ifndef BUILD_BRIDGE
  1811. // --------------------------------------------------------------------------------------------------------
  1812. // Control Output
  1813. if (pData->event.portOut != nullptr)
  1814. {
  1815. float value, curValue;
  1816. for (uint32_t k=0; k < pData->param.count; ++k)
  1817. {
  1818. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1819. continue;
  1820. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1821. pData->param.ranges[k].fixValue(curValue);
  1822. if (pData->param.data[k].mappedControlIndex > 0)
  1823. {
  1824. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1825. pData->event.portOut->writeControlEvent(0,
  1826. pData->param.data[k].midiChannel,
  1827. kEngineControlEventTypeParameter,
  1828. static_cast<uint16_t>(pData->param.data[k].mappedControlIndex),
  1829. -1,
  1830. value);
  1831. }
  1832. }
  1833. } // End of Control Output
  1834. #endif
  1835. }
  1836. bool processSingle(const float* const* const audioIn, float** const audioOut,
  1837. const float* const* const cvIn, float** const cvOut,
  1838. const uint32_t frames, const uint32_t timeOffset)
  1839. {
  1840. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1841. if (pData->audioIn.count > 0) {
  1842. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1843. }
  1844. if (pData->audioOut.count > 0) {
  1845. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1846. }
  1847. if (pData->cvIn.count > 0) {
  1848. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1849. }
  1850. if (pData->cvOut.count > 0) {
  1851. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1852. }
  1853. // --------------------------------------------------------------------------------------------------------
  1854. // Try lock, silence otherwise
  1855. if (fIsOffline)
  1856. {
  1857. pData->singleMutex.lock();
  1858. }
  1859. else if (! pData->singleMutex.tryLock())
  1860. {
  1861. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1862. {
  1863. for (uint32_t k=0; k < frames; ++k)
  1864. audioOut[i][k+timeOffset] = 0.0f;
  1865. }
  1866. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1867. {
  1868. for (uint32_t k=0; k < frames; ++k)
  1869. cvOut[i][k+timeOffset] = 0.0f;
  1870. }
  1871. return false;
  1872. }
  1873. // --------------------------------------------------------------------------------------------------------
  1874. // Set audio buffers
  1875. {
  1876. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1877. carla_copyFloats(fAudioAndCvInBuffers[i], audioIn[i]+timeOffset, frames);
  1878. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1879. carla_copyFloats(fAudioAndCvInBuffers[pData->audioIn.count+i], cvIn[i]+timeOffset, frames);
  1880. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1881. carla_zeroFloats(fAudioAndCvOutBuffers[i], frames);
  1882. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1883. carla_zeroFloats(fAudioAndCvOutBuffers[pData->audioOut.count+i], frames);
  1884. }
  1885. // --------------------------------------------------------------------------------------------------------
  1886. // Run plugin
  1887. fIsProcessing = true;
  1888. if (fHandle2 == nullptr)
  1889. {
  1890. fDescriptor->process(fHandle,
  1891. fAudioAndCvInBuffers, fAudioAndCvOutBuffers, frames,
  1892. fMidiInEvents, fMidiEventInCount);
  1893. }
  1894. else
  1895. {
  1896. fDescriptor->process(fHandle,
  1897. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[0] : nullptr,
  1898. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[0] : nullptr,
  1899. frames, fMidiInEvents, fMidiEventInCount);
  1900. fDescriptor->process(fHandle2,
  1901. (fAudioAndCvInBuffers != nullptr) ? &fAudioAndCvInBuffers[1] : nullptr,
  1902. (fAudioAndCvOutBuffers != nullptr) ? &fAudioAndCvOutBuffers[1] : nullptr,
  1903. frames, fMidiInEvents, fMidiEventInCount);
  1904. }
  1905. fIsProcessing = false;
  1906. if (fTimeInfo.playing)
  1907. fTimeInfo.frame += frames;
  1908. uint32_t i=0;
  1909. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1910. // --------------------------------------------------------------------------------------------------------
  1911. // Post-processing (dry/wet, volume and balance)
  1912. {
  1913. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1914. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1915. bool isPair;
  1916. float bufValue;
  1917. float* const oldBufLeft = pData->postProc.extraBuffer;
  1918. for (; i < pData->audioOut.count; ++i)
  1919. {
  1920. // Dry/Wet
  1921. if (doDryWet)
  1922. {
  1923. for (uint32_t k=0; k < frames; ++k)
  1924. {
  1925. bufValue = fAudioAndCvInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1926. fAudioAndCvOutBuffers[i][k] = (fAudioAndCvOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1927. }
  1928. }
  1929. // Balance
  1930. if (doBalance)
  1931. {
  1932. isPair = (i % 2 == 0);
  1933. if (isPair)
  1934. {
  1935. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1936. carla_copyFloats(oldBufLeft, fAudioAndCvOutBuffers[i], frames);
  1937. }
  1938. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1939. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1940. for (uint32_t k=0; k < frames; ++k)
  1941. {
  1942. if (isPair)
  1943. {
  1944. // left
  1945. fAudioAndCvOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1946. fAudioAndCvOutBuffers[i][k] += fAudioAndCvOutBuffers[i+1][k] * (1.0f - balRangeR);
  1947. }
  1948. else
  1949. {
  1950. // right
  1951. fAudioAndCvOutBuffers[i][k] = fAudioAndCvOutBuffers[i][k] * balRangeR;
  1952. fAudioAndCvOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1953. }
  1954. }
  1955. }
  1956. // Volume (and buffer copy)
  1957. {
  1958. for (uint32_t k=0; k < frames; ++k)
  1959. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k] * pData->postProc.volume;
  1960. }
  1961. }
  1962. } // End of Post-processing
  1963. #else
  1964. for (; i < pData->audioOut.count; ++i)
  1965. {
  1966. for (uint32_t k=0; k < frames; ++k)
  1967. audioOut[i][k+timeOffset] = fAudioAndCvOutBuffers[i][k];
  1968. }
  1969. #endif
  1970. // CV stuff too
  1971. for (; i < pData->cvOut.count; ++i)
  1972. {
  1973. for (uint32_t k=0; k < frames; ++k)
  1974. cvOut[i][k+timeOffset] = fAudioAndCvOutBuffers[pData->audioOut.count+i][k];
  1975. }
  1976. // --------------------------------------------------------------------------------------------------------
  1977. // MIDI Output
  1978. if (pData->event.portOut != nullptr)
  1979. {
  1980. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1981. {
  1982. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1983. const uint8_t port = fMidiOutEvents[k].port;
  1984. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1985. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1986. else
  1987. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1988. }
  1989. }
  1990. // --------------------------------------------------------------------------------------------------------
  1991. pData->singleMutex.unlock();
  1992. return true;
  1993. }
  1994. void bufferSizeChanged(const uint32_t newBufferSize) override
  1995. {
  1996. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1997. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1998. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  1999. {
  2000. if (fAudioAndCvInBuffers[i] != nullptr)
  2001. delete[] fAudioAndCvInBuffers[i];
  2002. fAudioAndCvInBuffers[i] = new float[newBufferSize];
  2003. }
  2004. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2005. {
  2006. if (fAudioAndCvOutBuffers[i] != nullptr)
  2007. delete[] fAudioAndCvOutBuffers[i];
  2008. fAudioAndCvOutBuffers[i] = new float[newBufferSize];
  2009. }
  2010. if (fCurBufferSize != newBufferSize)
  2011. {
  2012. fCurBufferSize = newBufferSize;
  2013. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2014. {
  2015. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  2016. if (fHandle2 != nullptr)
  2017. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  2018. }
  2019. }
  2020. CarlaPlugin::bufferSizeChanged(newBufferSize);
  2021. }
  2022. void sampleRateChanged(const double newSampleRate) override
  2023. {
  2024. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2025. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  2026. if (carla_isEqual(fCurSampleRate, newSampleRate))
  2027. return;
  2028. fCurSampleRate = newSampleRate;
  2029. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2030. {
  2031. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2032. if (fHandle2 != nullptr)
  2033. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  2034. }
  2035. }
  2036. void offlineModeChanged(const bool isOffline) override
  2037. {
  2038. if (fIsOffline == isOffline)
  2039. return;
  2040. fIsOffline = isOffline;
  2041. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  2042. {
  2043. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2044. if (fHandle2 != nullptr)
  2045. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  2046. }
  2047. }
  2048. // -------------------------------------------------------------------
  2049. // Plugin buffers
  2050. void initBuffers() const noexcept override
  2051. {
  2052. CarlaPlugin::initBuffers();
  2053. fMidiIn.initBuffers(pData->event.portIn);
  2054. fMidiOut.initBuffers();
  2055. }
  2056. void clearBuffers() noexcept override
  2057. {
  2058. carla_debug("CarlaPluginNative::clearBuffers() - start");
  2059. if (fAudioAndCvInBuffers != nullptr)
  2060. {
  2061. for (uint32_t i=0; i < (pData->audioIn.count+pData->cvIn.count); ++i)
  2062. {
  2063. if (fAudioAndCvInBuffers[i] != nullptr)
  2064. {
  2065. delete[] fAudioAndCvInBuffers[i];
  2066. fAudioAndCvInBuffers[i] = nullptr;
  2067. }
  2068. }
  2069. delete[] fAudioAndCvInBuffers;
  2070. fAudioAndCvInBuffers = nullptr;
  2071. }
  2072. if (fAudioAndCvOutBuffers != nullptr)
  2073. {
  2074. for (uint32_t i=0; i < (pData->audioOut.count+pData->cvOut.count); ++i)
  2075. {
  2076. if (fAudioAndCvOutBuffers[i] != nullptr)
  2077. {
  2078. delete[] fAudioAndCvOutBuffers[i];
  2079. fAudioAndCvOutBuffers[i] = nullptr;
  2080. }
  2081. }
  2082. delete[] fAudioAndCvOutBuffers;
  2083. fAudioAndCvOutBuffers = nullptr;
  2084. }
  2085. if (fMidiIn.count > 1)
  2086. pData->event.portIn = nullptr;
  2087. if (fMidiOut.count > 1)
  2088. pData->event.portOut = nullptr;
  2089. fMidiIn.clear();
  2090. fMidiOut.clear();
  2091. CarlaPlugin::clearBuffers();
  2092. carla_debug("CarlaPluginNative::clearBuffers() - end");
  2093. }
  2094. // -------------------------------------------------------------------
  2095. // Post-poned UI Stuff
  2096. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2097. {
  2098. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2099. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2100. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2101. if (! fIsUiVisible)
  2102. return;
  2103. if (fDescriptor->ui_set_parameter_value != nullptr)
  2104. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  2105. }
  2106. void uiMidiProgramChange(const uint32_t index) noexcept override
  2107. {
  2108. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2109. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2110. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2111. if (! fIsUiVisible)
  2112. return;
  2113. if (index >= pData->midiprog.count)
  2114. return;
  2115. if (fDescriptor->ui_set_midi_program != nullptr)
  2116. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2117. }
  2118. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2119. {
  2120. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2121. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2122. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2123. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2124. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2125. if (! fIsUiVisible)
  2126. return;
  2127. if (fDescriptor->dispatcher != nullptr)
  2128. {
  2129. uint8_t data[3] = {
  2130. uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT)),
  2131. note,
  2132. velo
  2133. };
  2134. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_MIDI_EVENT,
  2135. 3, 0,
  2136. data,
  2137. 0.0f);
  2138. }
  2139. }
  2140. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  2141. {
  2142. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  2143. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  2144. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2145. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2146. if (! fIsUiVisible)
  2147. return;
  2148. if (fDescriptor == nullptr || fHandle == nullptr)
  2149. return;
  2150. if (fDescriptor->dispatcher != nullptr)
  2151. {
  2152. uint8_t data[3] = {
  2153. uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT)),
  2154. note,
  2155. 0
  2156. };
  2157. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_MIDI_EVENT,
  2158. 3, 0,
  2159. data,
  2160. 0.0f);
  2161. }
  2162. }
  2163. // -------------------------------------------------------------------
  2164. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height) const
  2165. {
  2166. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  2167. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  2168. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  2169. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  2170. return fDescriptor->render_inline_display(fHandle, width, height);
  2171. }
  2172. // -------------------------------------------------------------------
  2173. protected:
  2174. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  2175. {
  2176. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  2177. return &fTimeInfo;
  2178. }
  2179. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  2180. {
  2181. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  2182. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  2183. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  2184. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  2185. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  2186. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  2187. {
  2188. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  2189. return false;
  2190. }
  2191. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  2192. return true;
  2193. }
  2194. void handleUiParameterChanged(const uint32_t index, const float value)
  2195. {
  2196. setParameterValue(index, value, false, true, true);
  2197. }
  2198. void handleUiCustomDataChanged(const char* const key, const char* const value)
  2199. {
  2200. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  2201. }
  2202. void handleUiClosed()
  2203. {
  2204. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  2205. fIsUiVisible = false;
  2206. }
  2207. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  2208. {
  2209. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  2210. }
  2211. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  2212. {
  2213. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  2214. }
  2215. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode,
  2216. const int32_t index, const intptr_t value, void* const ptr, const float opt)
  2217. {
  2218. #ifdef DEBUG
  2219. if (opcode != NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY && opcode != NATIVE_HOST_OPCODE_REQUEST_IDLE) {
  2220. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)",
  2221. opcode, index, value, ptr, static_cast<double>(opt));
  2222. }
  2223. #endif
  2224. switch (opcode)
  2225. {
  2226. case NATIVE_HOST_OPCODE_NULL:
  2227. break;
  2228. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  2229. // TODO
  2230. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2231. break;
  2232. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  2233. // TODO
  2234. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  2235. break;
  2236. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  2237. reloadParameters(nullptr, nullptr);
  2238. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2239. break;
  2240. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  2241. reloadPrograms(false);
  2242. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  2243. break;
  2244. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  2245. reload();
  2246. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  2247. break;
  2248. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  2249. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  2250. fIsUiAvailable = false;
  2251. break;
  2252. case NATIVE_HOST_OPCODE_HOST_IDLE:
  2253. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2254. break;
  2255. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  2256. return 1;
  2257. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  2258. switch (pData->engine->getProccessMode())
  2259. {
  2260. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  2261. case ENGINE_PROCESS_MODE_PATCHBAY:
  2262. fInlineDisplayNeedsRedraw = true;
  2263. break;
  2264. default:
  2265. break;
  2266. }
  2267. break;
  2268. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  2269. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  2270. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), value != 0);
  2271. break;
  2272. case NATIVE_HOST_OPCODE_REQUEST_IDLE:
  2273. fNeedsIdle = true;
  2274. break;
  2275. case NATIVE_HOST_OPCODE_GET_FILE_PATH:
  2276. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  2277. {
  2278. const EngineOptions& opts(pData->engine->getOptions());
  2279. const char* const filetype = (const char*)ptr;
  2280. const char* ret = nullptr;
  2281. if (std::strcmp(filetype, "carla") == 0)
  2282. {
  2283. ret = pData->engine->getCurrentProjectFilename();
  2284. if (fLastProjectFilename != ret)
  2285. {
  2286. fLastProjectFilename = ret;
  2287. bool found;
  2288. const size_t r = fLastProjectFilename.rfind(CARLA_OS_SEP, &found);
  2289. if (found)
  2290. {
  2291. fLastProjectFolder = ret;
  2292. fLastProjectFolder[r] = '\0';
  2293. }
  2294. else
  2295. {
  2296. fLastProjectFolder.clear();
  2297. }
  2298. }
  2299. ret = fLastProjectFolder.buffer();
  2300. }
  2301. else if (std::strcmp(filetype, "audio") == 0)
  2302. ret = opts.pathAudio;
  2303. else if (std::strcmp(filetype, "midi") == 0)
  2304. ret = opts.pathMIDI;
  2305. return static_cast<intptr_t>((uintptr_t)ret);
  2306. }
  2307. break;
  2308. case NATIVE_HOST_OPCODE_UI_RESIZE:
  2309. case NATIVE_HOST_OPCODE_PREVIEW_BUFFER_DATA:
  2310. // unused here
  2311. break;
  2312. }
  2313. return 0;
  2314. // unused for now
  2315. (void)opt;
  2316. }
  2317. // -------------------------------------------------------------------
  2318. public:
  2319. void* getNativeHandle() const noexcept override
  2320. {
  2321. return fHandle;
  2322. }
  2323. const void* getNativeDescriptor() const noexcept override
  2324. {
  2325. return fDescriptor;
  2326. }
  2327. // -------------------------------------------------------------------
  2328. bool init(const CarlaPluginPtr plugin,
  2329. const char* const name, const char* const label, const uint options)
  2330. {
  2331. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2332. // ---------------------------------------------------------------
  2333. // first checks
  2334. if (pData->client != nullptr)
  2335. {
  2336. pData->engine->setLastError("Plugin client is already registered");
  2337. return false;
  2338. }
  2339. if (label == nullptr || label[0] == '\0')
  2340. {
  2341. pData->engine->setLastError("null label");
  2342. return false;
  2343. }
  2344. // ---------------------------------------------------------------
  2345. // get descriptor that matches label
  2346. sPluginInitializer.initIfNeeded();
  2347. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2348. {
  2349. fDescriptor = it.getValue(nullptr);
  2350. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2351. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2352. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2353. break;
  2354. fDescriptor = nullptr;
  2355. }
  2356. if (fDescriptor == nullptr)
  2357. {
  2358. pData->engine->setLastError("Invalid internal plugin");
  2359. return false;
  2360. }
  2361. // ---------------------------------------------------------------
  2362. // set icon
  2363. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2364. pData->iconName = carla_strdup_safe("file");
  2365. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2366. pData->iconName = carla_strdup_safe("file");
  2367. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2368. pData->iconName = carla_strdup_safe("distrho");
  2369. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2370. pData->iconName = carla_strdup_safe("distrho");
  2371. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2372. pData->iconName = carla_strdup_safe("distrho");
  2373. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2374. pData->iconName = carla_strdup_safe("distrho");
  2375. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2376. pData->iconName = carla_strdup_safe("distrho");
  2377. // ---------------------------------------------------------------
  2378. // set info
  2379. if (name != nullptr && name[0] != '\0')
  2380. pData->name = pData->engine->getUniquePluginName(name);
  2381. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2382. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2383. else
  2384. pData->name = pData->engine->getUniquePluginName(label);
  2385. {
  2386. CARLA_ASSERT(fHost.uiName == nullptr);
  2387. String uiName;
  2388. if (pData->uiTitle.isNotEmpty())
  2389. {
  2390. uiName = pData->uiTitle;
  2391. }
  2392. else
  2393. {
  2394. uiName = pData->name;
  2395. uiName += " (GUI)";
  2396. }
  2397. fHost.uiName = uiName.getAndReleaseBuffer();
  2398. }
  2399. // ---------------------------------------------------------------
  2400. // register client
  2401. pData->client = pData->engine->addClient(plugin);
  2402. if (pData->client == nullptr || ! pData->client->isOk())
  2403. {
  2404. pData->engine->setLastError("Failed to register plugin client");
  2405. return false;
  2406. }
  2407. // ---------------------------------------------------------------
  2408. // initialize plugin
  2409. fHandle = fDescriptor->instantiate(&fHost);
  2410. if (fHandle == nullptr)
  2411. {
  2412. pData->engine->setLastError("Plugin failed to initialize");
  2413. return false;
  2414. }
  2415. // ---------------------------------------------------------------
  2416. // set options
  2417. bool hasMidiProgs = false;
  2418. if (fDescriptor->get_midi_program_count != nullptr)
  2419. {
  2420. try {
  2421. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2422. } catch (...) {}
  2423. }
  2424. pData->options = 0x0;
  2425. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2426. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2427. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2428. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2429. if (pData->engine->getOptions().forceStereo)
  2430. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2431. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2432. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2433. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2434. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CONTROL_CHANGES))
  2435. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2436. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2437. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_CHANNEL_PRESSURE))
  2438. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2439. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2440. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH))
  2441. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2442. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2443. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PITCHBEND))
  2444. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2445. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2446. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_ALL_SOUND_OFF))
  2447. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2448. if (fDescriptor->midiIns > 0)
  2449. if (isPluginOptionInverseEnabled(options, PLUGIN_OPTION_SKIP_SENDING_NOTES))
  2450. pData->options |= PLUGIN_OPTION_SKIP_SENDING_NOTES;
  2451. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2452. {
  2453. if (isPluginOptionEnabled(options, PLUGIN_OPTION_SEND_PROGRAM_CHANGES))
  2454. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2455. // makes no sense for a plugin to set program changes supported, but it has no midi programs
  2456. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2457. }
  2458. else if (hasMidiProgs)
  2459. {
  2460. if (isPluginOptionEnabled(options, PLUGIN_OPTION_MAP_PROGRAM_CHANGES))
  2461. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2462. }
  2463. return true;
  2464. }
  2465. private:
  2466. NativePluginHandle fHandle;
  2467. NativePluginHandle fHandle2;
  2468. NativeHostDescriptor fHost;
  2469. const NativePluginDescriptor* fDescriptor;
  2470. bool fIsProcessing;
  2471. bool fIsOffline;
  2472. bool fIsUiAvailable;
  2473. bool fIsUiVisible;
  2474. volatile bool fNeedsIdle;
  2475. bool fInlineDisplayNeedsRedraw;
  2476. int64_t fInlineDisplayLastRedrawTime;
  2477. String fLastProjectFilename;
  2478. String fLastProjectFolder;
  2479. float** fAudioAndCvInBuffers;
  2480. float** fAudioAndCvOutBuffers;
  2481. uint32_t fMidiEventInCount;
  2482. uint32_t fMidiEventOutCount;
  2483. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2484. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2485. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2486. uint32_t fCurBufferSize;
  2487. double fCurSampleRate;
  2488. NativePluginMidiInData fMidiIn;
  2489. NativePluginMidiOutData fMidiOut;
  2490. NativeTimeInfo fTimeInfo;
  2491. // -------------------------------------------------------------------
  2492. #define handlePtr ((CarlaPluginNative*)handle)
  2493. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2494. {
  2495. return handlePtr->fCurBufferSize;
  2496. }
  2497. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2498. {
  2499. return handlePtr->fCurSampleRate;
  2500. }
  2501. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2502. {
  2503. return handlePtr->fIsOffline;
  2504. }
  2505. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2506. {
  2507. return handlePtr->handleGetTimeInfo();
  2508. }
  2509. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2510. {
  2511. return handlePtr->handleWriteMidiEvent(event);
  2512. }
  2513. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2514. {
  2515. handlePtr->handleUiParameterChanged(index, value);
  2516. }
  2517. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2518. {
  2519. handlePtr->handleUiCustomDataChanged(key, value);
  2520. }
  2521. static void carla_host_ui_closed(NativeHostHandle handle)
  2522. {
  2523. handlePtr->handleUiClosed();
  2524. }
  2525. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2526. {
  2527. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2528. }
  2529. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2530. {
  2531. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2532. }
  2533. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2534. {
  2535. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2536. }
  2537. #undef handlePtr
  2538. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2539. };
  2540. // -----------------------------------------------------------------------
  2541. CarlaPluginPtr CarlaPlugin::newNative(const Initializer& init)
  2542. {
  2543. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})",
  2544. init.engine, init.filename, init.name, init.label, init.uniqueId);
  2545. std::shared_ptr<CarlaPluginNative> plugin(new CarlaPluginNative(init.engine, init.id));
  2546. if (! plugin->init(plugin, init.name, init.label, init.options))
  2547. return nullptr;
  2548. return plugin;
  2549. }
  2550. // used in CarlaStandalone.cpp
  2551. const void* carla_render_inline_display_internal(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height);
  2552. const void* carla_render_inline_display_internal(const CarlaPluginPtr& plugin, uint32_t width, uint32_t height)
  2553. {
  2554. const std::shared_ptr<CarlaPluginNative>& nativePlugin((const std::shared_ptr<CarlaPluginNative>&)plugin);
  2555. return nativePlugin->renderInlineDisplay(width, height);
  2556. }
  2557. // -----------------------------------------------------------------------
  2558. CARLA_BACKEND_END_NAMESPACE