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.

3009 lines
108KB

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