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.

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