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.

2948 lines
105KB

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