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.

2552 lines
91KB

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