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.

2905 lines
103KB

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