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.

2670 lines
94KB

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