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.

2537 lines
91KB

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