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.

2704 lines
95KB

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