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.

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