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.

2558 lines
91KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaNative.h"
  21. #include "water/text/StringArray.h"
  22. using water::jmax;
  23. using water::String;
  24. using water::StringArray;
  25. CARLA_EXTERN_C
  26. std::size_t carla_getNativePluginCount() noexcept;
  27. CARLA_EXTERN_C
  28. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  29. // -----------------------------------------------------------------------
  30. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  31. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  32. {
  33. gPluginDescriptors.append(desc);
  34. }
  35. // -----------------------------------------------------------------------
  36. static
  37. class NativePluginInitializer
  38. {
  39. public:
  40. NativePluginInitializer() noexcept
  41. : fNeedsInit(true) {}
  42. ~NativePluginInitializer() noexcept
  43. {
  44. gPluginDescriptors.clear();
  45. }
  46. void initIfNeeded() noexcept
  47. {
  48. if (! fNeedsInit)
  49. return;
  50. fNeedsInit = false;
  51. try {
  52. carla_register_all_native_plugins();
  53. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  54. }
  55. private:
  56. bool fNeedsInit;
  57. } sPluginInitializer;
  58. // -----------------------------------------------------------------------
  59. std::size_t carla_getNativePluginCount() noexcept
  60. {
  61. sPluginInitializer.initIfNeeded();
  62. return gPluginDescriptors.count();
  63. }
  64. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  65. {
  66. sPluginInitializer.initIfNeeded();
  67. return gPluginDescriptors.getAt(index, nullptr);
  68. }
  69. // -----------------------------------------------------------------------
  70. CARLA_BACKEND_START_NAMESPACE
  71. // -------------------------------------------------------------------
  72. // Fallback data
  73. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  74. // -----------------------------------------------------------------------
  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. #ifndef BUILD_BRIDGE
  187. pData->transientTryCounter = 0;
  188. #endif
  189. }
  190. pData->singleMutex.lock();
  191. pData->masterMutex.lock();
  192. if (pData->client != nullptr && pData->client->isActive())
  193. pData->client->deactivate();
  194. CARLA_ASSERT(! fIsProcessing);
  195. if (pData->active)
  196. {
  197. deactivate();
  198. pData->active = false;
  199. }
  200. if (fDescriptor != nullptr)
  201. {
  202. if (fDescriptor->cleanup != nullptr)
  203. {
  204. if (fHandle != nullptr)
  205. fDescriptor->cleanup(fHandle);
  206. if (fHandle2 != nullptr)
  207. fDescriptor->cleanup(fHandle2);
  208. }
  209. fHandle = nullptr;
  210. fHandle2 = nullptr;
  211. fDescriptor = nullptr;
  212. }
  213. if (fHost.resourceDir != nullptr)
  214. {
  215. delete[] fHost.resourceDir;
  216. fHost.resourceDir = nullptr;
  217. }
  218. if (fHost.uiName != nullptr)
  219. {
  220. delete[] fHost.uiName;
  221. fHost.uiName = nullptr;
  222. }
  223. clearBuffers();
  224. }
  225. // -------------------------------------------------------------------
  226. // Information (base)
  227. PluginType getType() const noexcept override
  228. {
  229. return PLUGIN_INTERNAL;
  230. }
  231. PluginCategory getCategory() const noexcept override
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  234. return static_cast<PluginCategory>(fDescriptor->category);
  235. }
  236. // -------------------------------------------------------------------
  237. // Information (count)
  238. uint32_t getMidiInCount() const noexcept override
  239. {
  240. return fMidiIn.count;
  241. }
  242. uint32_t getMidiOutCount() const noexcept override
  243. {
  244. return fMidiOut.count;
  245. }
  246. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  249. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  250. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  251. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  252. // FIXME - try
  253. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  254. return param->scalePointCount;
  255. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  256. return 0;
  257. }
  258. // -------------------------------------------------------------------
  259. // Information (current data)
  260. // nothing
  261. // -------------------------------------------------------------------
  262. // Information (per-plugin data)
  263. uint getOptionsAvailable() const noexcept override
  264. {
  265. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  266. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  267. bool hasMidiProgs = false;
  268. if (fDescriptor->get_midi_program_count != nullptr)
  269. {
  270. try {
  271. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  272. } catch (...) {}
  273. }
  274. uint options = 0x0;
  275. // can't disable fixed buffers if using MIDI output
  276. if (fMidiOut.count == 0 && (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  277. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  278. // can't disable forced stereo if enabled in the engine
  279. if (pData->engine->getOptions().forceStereo)
  280. pass();
  281. // if inputs or outputs are just 1, then yes we can force stereo
  282. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  283. options |= PLUGIN_OPTION_FORCE_STEREO;
  284. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  285. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  286. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  287. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  288. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  289. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  290. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  291. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  292. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  293. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  294. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  295. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  296. else if (hasMidiProgs)
  297. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  298. return options;
  299. }
  300. float getParameterValue(const uint32_t parameterId) const noexcept override
  301. {
  302. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  303. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  304. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  306. // FIXME - try
  307. return fDescriptor->get_parameter_value(fHandle, parameterId);
  308. }
  309. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  310. {
  311. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  312. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  313. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  314. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  315. // FIXME - try
  316. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  319. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  320. return scalePoint->value;
  321. }
  322. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  323. return 0.0f;
  324. }
  325. void getLabel(char* const strBuf) const noexcept override
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  328. if (fDescriptor->label != nullptr)
  329. {
  330. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  331. return;
  332. }
  333. CarlaPlugin::getLabel(strBuf);
  334. }
  335. void getMaker(char* const strBuf) const noexcept override
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  338. if (fDescriptor->maker != nullptr)
  339. {
  340. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  341. return;
  342. }
  343. CarlaPlugin::getMaker(strBuf);
  344. }
  345. void getCopyright(char* const strBuf) const noexcept override
  346. {
  347. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  348. if (fDescriptor->copyright != nullptr)
  349. {
  350. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  351. return;
  352. }
  353. CarlaPlugin::getCopyright(strBuf);
  354. }
  355. void getRealName(char* const strBuf) const noexcept override
  356. {
  357. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  358. if (fDescriptor->name != nullptr)
  359. {
  360. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  361. return;
  362. }
  363. CarlaPlugin::getRealName(strBuf);
  364. }
  365. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  366. {
  367. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  368. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  369. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  370. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  371. // FIXME - try
  372. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  373. {
  374. if (param->name != nullptr)
  375. {
  376. std::strncpy(strBuf, param->name, STR_MAX);
  377. return;
  378. }
  379. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  380. return CarlaPlugin::getParameterName(parameterId, strBuf);
  381. }
  382. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  383. CarlaPlugin::getParameterName(parameterId, strBuf);
  384. }
  385. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  390. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  391. // FIXME - try
  392. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  393. {
  394. if (param->unit != nullptr)
  395. {
  396. std::strncpy(strBuf, param->unit, STR_MAX);
  397. return;
  398. }
  399. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  400. }
  401. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  402. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  403. }
  404. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  405. {
  406. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  407. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  408. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  409. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  410. // FIXME - try
  411. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  412. {
  413. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount,);
  414. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  415. if (scalePoint->label != nullptr)
  416. {
  417. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  418. return;
  419. }
  420. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  421. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  422. }
  423. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  424. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  425. }
  426. // -------------------------------------------------------------------
  427. // Set data (state)
  428. void prepareForSave() override
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  431. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  432. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  433. {
  434. char strBuf[STR_MAX+1];
  435. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  436. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  437. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  438. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  439. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  440. strBuf[STR_MAX] = '\0';
  441. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  442. }
  443. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  444. return;
  445. if (char* data = fDescriptor->get_state(fHandle))
  446. {
  447. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  448. std::free(data);
  449. }
  450. }
  451. // -------------------------------------------------------------------
  452. // Set data (internal stuff)
  453. void setName(const char* const newName) override
  454. {
  455. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  456. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  457. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  458. char uiName[std::strlen(newName)+6+1];
  459. std::strcpy(uiName, newName);
  460. std::strcat(uiName, " (GUI)");
  461. if (fHost.uiName != nullptr)
  462. delete[] fHost.uiName;
  463. fHost.uiName = carla_strdup(uiName);
  464. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  465. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  466. CarlaPlugin::setName(newName);
  467. }
  468. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  469. {
  470. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  471. pData->midiprog.current = fCurMidiProgs[channel];
  472. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  473. }
  474. // -------------------------------------------------------------------
  475. // Set data (plugin-specific stuff)
  476. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  477. {
  478. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  479. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  480. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  481. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  482. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  483. // FIXME - try
  484. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  485. if (fHandle2 != nullptr)
  486. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  487. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  488. }
  489. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  492. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  493. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  494. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  495. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  496. carla_debug("CarlaPluginNative::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  497. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  498. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  499. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  500. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  501. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  502. {
  503. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  504. {
  505. const ScopedSingleProcessLocker spl(this, true);
  506. fDescriptor->set_state(fHandle, value);
  507. if (fHandle2 != nullptr)
  508. fDescriptor->set_state(fHandle2, value);
  509. }
  510. }
  511. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  512. {
  513. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  514. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  515. {
  516. uint8_t channel = 0;
  517. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  518. {
  519. const int index(it->getIntValue());
  520. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  521. {
  522. const uint32_t bank = pData->midiprog.data[index].bank;
  523. const uint32_t program = pData->midiprog.data[index].program;
  524. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  525. if (fHandle2 != nullptr)
  526. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  527. fCurMidiProgs[channel] = index;
  528. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  529. {
  530. pData->midiprog.current = index;
  531. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  532. }
  533. }
  534. ++channel;
  535. }
  536. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  537. }
  538. }
  539. else
  540. {
  541. if (fDescriptor->set_custom_data != nullptr)
  542. {
  543. fDescriptor->set_custom_data(fHandle, key, value);
  544. if (fHandle2 != nullptr)
  545. fDescriptor->set_custom_data(fHandle2, key, value);
  546. }
  547. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  548. fDescriptor->ui_set_custom_data(fHandle, key, value);
  549. }
  550. CarlaPlugin::setCustomData(type, key, value, sendGui);
  551. }
  552. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  553. {
  554. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  555. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  556. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  557. // TODO, put into check below
  558. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  559. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  560. if (index >= 0)
  561. {
  562. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  563. const uint32_t bank = pData->midiprog.data[index].bank;
  564. const uint32_t program = pData->midiprog.data[index].program;
  565. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  566. try {
  567. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  568. } catch(...) {}
  569. if (fHandle2 != nullptr)
  570. {
  571. try {
  572. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  573. } catch(...) {}
  574. }
  575. fCurMidiProgs[channel] = index;
  576. }
  577. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  578. }
  579. // -------------------------------------------------------------------
  580. // Set ui stuff
  581. void showCustomUI(const bool yesNo) override
  582. {
  583. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  584. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  585. if (fDescriptor->ui_show == nullptr)
  586. return;
  587. fIsUiAvailable = true;
  588. fDescriptor->ui_show(fHandle, yesNo);
  589. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  590. if (yesNo && ! fIsUiAvailable)
  591. return;
  592. fIsUiVisible = yesNo;
  593. if (! yesNo)
  594. {
  595. #ifndef BUILD_BRIDGE
  596. pData->transientTryCounter = 0;
  597. #endif
  598. return;
  599. }
  600. #ifndef BUILD_BRIDGE
  601. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  602. pData->tryTransient();
  603. #endif
  604. if (fDescriptor->ui_set_custom_data != nullptr)
  605. {
  606. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  607. {
  608. const CustomData& cData(it.getValue(kCustomDataFallback));
  609. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  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_isEqual(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. pData->param.ranges[j].min = min;
  877. pData->param.ranges[j].max = max;
  878. pData->param.ranges[j].def = def;
  879. pData->param.ranges[j].step = step;
  880. pData->param.ranges[j].stepSmall = stepSmall;
  881. pData->param.ranges[j].stepLarge = stepLarge;
  882. }
  883. if (needsCtrlIn && mIns <= 1)
  884. {
  885. portName.clear();
  886. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  887. {
  888. portName = pData->name;
  889. portName += ":";
  890. }
  891. portName += "events-in";
  892. portName.truncate(portNameSize);
  893. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  894. }
  895. if (needsCtrlOut && mOuts <= 1)
  896. {
  897. portName.clear();
  898. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  899. {
  900. portName = pData->name;
  901. portName += ":";
  902. }
  903. portName += "events-out";
  904. portName.truncate(portNameSize);
  905. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  906. }
  907. if (forcedStereoIn || forcedStereoOut)
  908. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  909. else
  910. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  911. // plugin hints
  912. pData->hints = 0x0;
  913. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  914. pData->hints |= PLUGIN_CAN_DRYWET;
  915. if (aOuts > 0)
  916. pData->hints |= PLUGIN_CAN_VOLUME;
  917. if (aOuts >= 2 && aOuts % 2 == 0)
  918. pData->hints |= PLUGIN_CAN_BALANCE;
  919. // native plugin hints
  920. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  921. pData->hints |= PLUGIN_IS_RTSAFE;
  922. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  923. pData->hints |= PLUGIN_IS_SYNTH;
  924. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  925. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  926. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  927. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  928. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  929. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  930. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  931. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  932. // extra plugin hints
  933. pData->extraHints = 0x0;
  934. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  935. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  936. bufferSizeChanged(pData->engine->getBufferSize());
  937. reloadPrograms(true);
  938. if (pData->active)
  939. activate();
  940. carla_debug("CarlaPluginNative::reload() - end");
  941. }
  942. void reloadPrograms(const bool doInit) override
  943. {
  944. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  945. uint32_t i, oldCount = pData->midiprog.count;
  946. const int32_t current = pData->midiprog.current;
  947. // Delete old programs
  948. pData->midiprog.clear();
  949. // Query new programs
  950. uint32_t count = 0;
  951. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  952. count = fDescriptor->get_midi_program_count(fHandle);
  953. if (count > 0)
  954. {
  955. pData->midiprog.createNew(count);
  956. // Update data
  957. for (i=0; i < count; ++i)
  958. {
  959. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  960. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  961. pData->midiprog.data[i].bank = mpDesc->bank;
  962. pData->midiprog.data[i].program = mpDesc->program;
  963. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  964. }
  965. }
  966. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  967. // Update OSC Names
  968. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  969. {
  970. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  971. for (i=0; i < count; ++i)
  972. 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);
  973. }
  974. #endif
  975. if (doInit)
  976. {
  977. if (count > 0)
  978. setMidiProgram(0, false, false, false);
  979. }
  980. else
  981. {
  982. // Check if current program is invalid
  983. bool programChanged = false;
  984. if (count == oldCount+1)
  985. {
  986. // one midi program added, probably created by user
  987. pData->midiprog.current = static_cast<int32_t>(oldCount);
  988. programChanged = true;
  989. }
  990. else if (current < 0 && count > 0)
  991. {
  992. // programs exist now, but not before
  993. pData->midiprog.current = 0;
  994. programChanged = true;
  995. }
  996. else if (current >= 0 && count == 0)
  997. {
  998. // programs existed before, but not anymore
  999. pData->midiprog.current = -1;
  1000. programChanged = true;
  1001. }
  1002. else if (current >= static_cast<int32_t>(count))
  1003. {
  1004. // current midi program > count
  1005. pData->midiprog.current = 0;
  1006. programChanged = true;
  1007. }
  1008. else
  1009. {
  1010. // no change
  1011. pData->midiprog.current = current;
  1012. }
  1013. if (programChanged)
  1014. setMidiProgram(pData->midiprog.current, true, true, true);
  1015. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1016. }
  1017. }
  1018. // -------------------------------------------------------------------
  1019. // Plugin processing
  1020. void activate() noexcept override
  1021. {
  1022. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1023. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1024. if (fDescriptor->activate != nullptr)
  1025. {
  1026. try {
  1027. fDescriptor->activate(fHandle);
  1028. } catch(...) {}
  1029. if (fHandle2 != nullptr)
  1030. {
  1031. try {
  1032. fDescriptor->activate(fHandle2);
  1033. } catch(...) {}
  1034. }
  1035. }
  1036. }
  1037. void deactivate() noexcept override
  1038. {
  1039. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1040. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1041. if (fDescriptor->deactivate != nullptr)
  1042. {
  1043. try {
  1044. fDescriptor->deactivate(fHandle);
  1045. } catch(...) {}
  1046. if (fHandle2 != nullptr)
  1047. {
  1048. try {
  1049. fDescriptor->deactivate(fHandle2);
  1050. } catch(...) {}
  1051. }
  1052. }
  1053. }
  1054. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1055. {
  1056. // --------------------------------------------------------------------------------------------------------
  1057. // Check if active
  1058. if (! pData->active)
  1059. {
  1060. // disable any output sound
  1061. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1062. carla_zeroFloats(audioOut[i], frames);
  1063. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1064. carla_zeroFloats(cvOut[i], frames);
  1065. return;
  1066. }
  1067. fMidiEventCount = 0;
  1068. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  1069. // --------------------------------------------------------------------------------------------------------
  1070. // Check if needs reset
  1071. if (pData->needsReset)
  1072. {
  1073. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1074. {
  1075. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1076. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1077. {
  1078. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1079. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1080. fMidiEvents[k].data[2] = 0;
  1081. fMidiEvents[k].size = 3;
  1082. fMidiEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1083. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1084. fMidiEvents[k+i].data[2] = 0;
  1085. fMidiEvents[k+i].size = 3;
  1086. }
  1087. }
  1088. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1089. {
  1090. fMidiEventCount = MAX_MIDI_NOTE;
  1091. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1092. {
  1093. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1094. fMidiEvents[k].data[1] = k;
  1095. fMidiEvents[k].data[2] = 0;
  1096. fMidiEvents[k].size = 3;
  1097. }
  1098. }
  1099. pData->needsReset = false;
  1100. }
  1101. // --------------------------------------------------------------------------------------------------------
  1102. // Set TimeInfo
  1103. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1104. fTimeInfo.playing = timeInfo.playing;
  1105. fTimeInfo.frame = timeInfo.frame;
  1106. fTimeInfo.usecs = timeInfo.usecs;
  1107. if (timeInfo.bbt.valid)
  1108. {
  1109. fTimeInfo.bbt.valid = true;
  1110. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1111. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1112. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1113. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1114. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1115. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1116. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1117. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1118. }
  1119. else
  1120. {
  1121. fTimeInfo.bbt.valid = false;
  1122. }
  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_zeroStructs(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. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1259. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1260. else
  1261. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1262. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1263. value = std::rint(value);
  1264. }
  1265. setParameterValue(k, value, false, false, false);
  1266. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1267. }
  1268. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1269. {
  1270. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1271. continue;
  1272. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1273. carla_zeroStruct(nativeEvent);
  1274. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1275. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1276. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1277. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1278. nativeEvent.size = 3;
  1279. }
  1280. break;
  1281. } // case kEngineControlEventTypeParameter
  1282. case kEngineControlEventTypeMidiBank:
  1283. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1284. {
  1285. if (event.channel == pData->ctrlChannel)
  1286. nextBankId = ctrlEvent.param;
  1287. }
  1288. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1289. {
  1290. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1291. continue;
  1292. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1293. carla_zeroStruct(nativeEvent);
  1294. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1295. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1296. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1297. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1298. nativeEvent.size = 3;
  1299. }
  1300. break;
  1301. case kEngineControlEventTypeMidiProgram:
  1302. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1303. {
  1304. if (event.channel < MAX_MIDI_CHANNELS)
  1305. {
  1306. const uint32_t nextProgramId(ctrlEvent.param);
  1307. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1308. {
  1309. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1310. {
  1311. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1312. if (fHandle2 != nullptr)
  1313. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1314. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1315. if (event.channel == pData->ctrlChannel)
  1316. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1317. break;
  1318. }
  1319. }
  1320. }
  1321. }
  1322. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1323. {
  1324. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1325. continue;
  1326. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1327. carla_zeroStruct(nativeEvent);
  1328. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1329. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1330. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1331. nativeEvent.size = 2;
  1332. }
  1333. break;
  1334. case kEngineControlEventTypeAllSoundOff:
  1335. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1336. {
  1337. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1338. continue;
  1339. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1340. carla_zeroStruct(nativeEvent);
  1341. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1342. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1343. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1344. nativeEvent.data[2] = 0;
  1345. nativeEvent.size = 3;
  1346. }
  1347. break;
  1348. case kEngineControlEventTypeAllNotesOff:
  1349. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1350. {
  1351. #ifndef BUILD_BRIDGE
  1352. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1353. {
  1354. allNotesOffSent = true;
  1355. sendMidiAllNotesOffToCallback();
  1356. }
  1357. #endif
  1358. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1359. continue;
  1360. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1361. carla_zeroStruct(nativeEvent);
  1362. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1363. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1364. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1365. nativeEvent.data[2] = 0;
  1366. nativeEvent.size = 3;
  1367. }
  1368. break;
  1369. }
  1370. break;
  1371. }
  1372. case kEngineEventTypeMidi: {
  1373. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1374. continue;
  1375. const EngineMidiEvent& midiEvent(event.midi);
  1376. if (midiEvent.size > 4)
  1377. continue;
  1378. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1379. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1380. #endif
  1381. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1382. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1383. continue;
  1384. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1385. continue;
  1386. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1387. continue;
  1388. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1389. continue;
  1390. // Fix bad note-off
  1391. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1392. status = MIDI_STATUS_NOTE_OFF;
  1393. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1394. carla_zeroStruct(nativeEvent);
  1395. nativeEvent.port = midiEvent.port;
  1396. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1397. nativeEvent.size = midiEvent.size;
  1398. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1399. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1400. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1401. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1402. if (status == MIDI_STATUS_NOTE_ON)
  1403. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1404. else if (status == MIDI_STATUS_NOTE_OFF)
  1405. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1406. } break;
  1407. } // switch (event.type)
  1408. }
  1409. pData->postRtEvents.trySplice();
  1410. if (frames > timeOffset)
  1411. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1412. } // eventPort
  1413. } // End of Event Input and Processing
  1414. // --------------------------------------------------------------------------------------------------------
  1415. // Plugin processing (no events)
  1416. else
  1417. {
  1418. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1419. } // End of Plugin processing (no events)
  1420. // --------------------------------------------------------------------------------------------------------
  1421. // Control and MIDI Output
  1422. if (pData->event.portOut != nullptr)
  1423. {
  1424. #ifndef BUILD_BRIDGE
  1425. float value, curValue;
  1426. for (uint32_t k=0; k < pData->param.count; ++k)
  1427. {
  1428. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1429. continue;
  1430. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1431. pData->param.ranges[k].fixValue(curValue);
  1432. if (pData->param.data[k].midiCC > 0)
  1433. {
  1434. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1435. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1436. }
  1437. }
  1438. #endif
  1439. // reverse lookup MIDI events
  1440. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1441. {
  1442. if (fMidiEvents[k].data[0] == 0)
  1443. break;
  1444. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1445. const uint8_t port = fMidiEvents[k].port;
  1446. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1447. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1448. else
  1449. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1450. }
  1451. } // End of Control and MIDI Output
  1452. }
  1453. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  1454. {
  1455. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1456. if (pData->audioIn.count > 0)
  1457. {
  1458. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1459. }
  1460. if (pData->audioOut.count > 0)
  1461. {
  1462. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1463. }
  1464. if (pData->cvIn.count > 0)
  1465. {
  1466. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1467. }
  1468. if (pData->cvOut.count > 0)
  1469. {
  1470. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1471. }
  1472. // --------------------------------------------------------------------------------------------------------
  1473. // Try lock, silence otherwise
  1474. if (fIsOffline)
  1475. {
  1476. pData->singleMutex.lock();
  1477. }
  1478. else if (! pData->singleMutex.tryLock())
  1479. {
  1480. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1481. {
  1482. for (uint32_t k=0; k < frames; ++k)
  1483. audioOut[i][k+timeOffset] = 0.0f;
  1484. }
  1485. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1486. {
  1487. for (uint32_t k=0; k < frames; ++k)
  1488. cvOut[i][k+timeOffset] = 0.0f;
  1489. }
  1490. return false;
  1491. }
  1492. // --------------------------------------------------------------------------------------------------------
  1493. // Set audio buffers
  1494. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1495. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1496. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1497. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1498. #if 0
  1499. // --------------------------------------------------------------------------------------------------------
  1500. // Set CV buffers
  1501. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1502. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  1503. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1504. carla_zeroFloats(fCvOutBuffers[i], frames);
  1505. #endif
  1506. // --------------------------------------------------------------------------------------------------------
  1507. // Run plugin
  1508. fIsProcessing = true;
  1509. if (fHandle2 == nullptr)
  1510. {
  1511. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1512. }
  1513. else
  1514. {
  1515. fDescriptor->process(fHandle,
  1516. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1517. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1518. frames, fMidiEvents, fMidiEventCount);
  1519. fDescriptor->process(fHandle2,
  1520. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1521. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1522. frames, fMidiEvents, fMidiEventCount);
  1523. }
  1524. fIsProcessing = false;
  1525. fTimeInfo.frame += frames;
  1526. #ifndef BUILD_BRIDGE
  1527. // --------------------------------------------------------------------------------------------------------
  1528. // Post-processing (dry/wet, volume and balance)
  1529. {
  1530. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1531. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1532. bool isPair;
  1533. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1534. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1535. {
  1536. // Dry/Wet
  1537. if (doDryWet)
  1538. {
  1539. for (uint32_t k=0; k < frames; ++k)
  1540. {
  1541. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1542. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1543. }
  1544. }
  1545. // Balance
  1546. if (doBalance)
  1547. {
  1548. isPair = (i % 2 == 0);
  1549. if (isPair)
  1550. {
  1551. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1552. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1553. }
  1554. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1555. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1556. for (uint32_t k=0; k < frames; ++k)
  1557. {
  1558. if (isPair)
  1559. {
  1560. // left
  1561. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1562. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1563. }
  1564. else
  1565. {
  1566. // right
  1567. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1568. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1569. }
  1570. }
  1571. }
  1572. // Volume (and buffer copy)
  1573. {
  1574. for (uint32_t k=0; k < frames; ++k)
  1575. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1576. }
  1577. }
  1578. } // End of Post-processing
  1579. #else
  1580. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1581. {
  1582. for (uint32_t k=0; k < frames; ++k)
  1583. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1584. }
  1585. #endif
  1586. #if 0
  1587. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1588. {
  1589. for (uint32_t k=0; k < frames; ++k)
  1590. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1591. }
  1592. #endif
  1593. // --------------------------------------------------------------------------------------------------------
  1594. pData->singleMutex.unlock();
  1595. return true;
  1596. }
  1597. void bufferSizeChanged(const uint32_t newBufferSize) override
  1598. {
  1599. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1600. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1601. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1602. {
  1603. if (fAudioInBuffers[i] != nullptr)
  1604. delete[] fAudioInBuffers[i];
  1605. fAudioInBuffers[i] = new float[newBufferSize];
  1606. }
  1607. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1608. {
  1609. if (fAudioOutBuffers[i] != nullptr)
  1610. delete[] fAudioOutBuffers[i];
  1611. fAudioOutBuffers[i] = new float[newBufferSize];
  1612. }
  1613. if (fCurBufferSize == newBufferSize)
  1614. return;
  1615. fCurBufferSize = newBufferSize;
  1616. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1617. {
  1618. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1619. if (fHandle2 != nullptr)
  1620. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1621. }
  1622. }
  1623. void sampleRateChanged(const double newSampleRate) override
  1624. {
  1625. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1626. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1627. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1628. return;
  1629. fCurSampleRate = newSampleRate;
  1630. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1631. {
  1632. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1633. if (fHandle2 != nullptr)
  1634. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1635. }
  1636. }
  1637. void offlineModeChanged(const bool isOffline) override
  1638. {
  1639. if (fIsOffline == isOffline)
  1640. return;
  1641. fIsOffline = isOffline;
  1642. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1643. {
  1644. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1645. if (fHandle2 != nullptr)
  1646. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1647. }
  1648. }
  1649. // -------------------------------------------------------------------
  1650. // Plugin buffers
  1651. void initBuffers() const noexcept override
  1652. {
  1653. fMidiIn.initBuffers();
  1654. fMidiOut.initBuffers();
  1655. CarlaPlugin::initBuffers();
  1656. }
  1657. void clearBuffers() noexcept override
  1658. {
  1659. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1660. if (fAudioInBuffers != nullptr)
  1661. {
  1662. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1663. {
  1664. if (fAudioInBuffers[i] != nullptr)
  1665. {
  1666. delete[] fAudioInBuffers[i];
  1667. fAudioInBuffers[i] = nullptr;
  1668. }
  1669. }
  1670. delete[] fAudioInBuffers;
  1671. fAudioInBuffers = nullptr;
  1672. }
  1673. if (fAudioOutBuffers != nullptr)
  1674. {
  1675. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1676. {
  1677. if (fAudioOutBuffers[i] != nullptr)
  1678. {
  1679. delete[] fAudioOutBuffers[i];
  1680. fAudioOutBuffers[i] = nullptr;
  1681. }
  1682. }
  1683. delete[] fAudioOutBuffers;
  1684. fAudioOutBuffers = nullptr;
  1685. }
  1686. if (fMidiIn.count > 1)
  1687. pData->event.portIn = nullptr;
  1688. if (fMidiOut.count > 1)
  1689. pData->event.portOut = nullptr;
  1690. fMidiIn.clear();
  1691. fMidiOut.clear();
  1692. CarlaPlugin::clearBuffers();
  1693. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1694. }
  1695. // -------------------------------------------------------------------
  1696. // Post-poned UI Stuff
  1697. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1698. {
  1699. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1700. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1701. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1702. if (! fIsUiVisible)
  1703. return;
  1704. if (fDescriptor->ui_set_parameter_value != nullptr)
  1705. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1706. }
  1707. void uiMidiProgramChange(const uint32_t index) noexcept override
  1708. {
  1709. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1710. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1711. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1712. if (! fIsUiVisible)
  1713. return;
  1714. if (index >= pData->midiprog.count)
  1715. return;
  1716. if (fDescriptor->ui_set_midi_program != nullptr)
  1717. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1718. }
  1719. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1720. {
  1721. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1722. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1723. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1724. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1725. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1726. if (! fIsUiVisible)
  1727. return;
  1728. // TODO
  1729. }
  1730. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1731. {
  1732. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1733. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1734. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1735. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1736. if (! fIsUiVisible)
  1737. return;
  1738. if (fDescriptor == nullptr || fHandle == nullptr)
  1739. return;
  1740. // TODO
  1741. }
  1742. // -------------------------------------------------------------------
  1743. protected:
  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. fIsUiAvailable = 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. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1824. ret = 1;
  1825. break;
  1826. }
  1827. return ret;
  1828. // unused for now
  1829. (void)index;
  1830. (void)value;
  1831. (void)ptr;
  1832. (void)opt;
  1833. }
  1834. // -------------------------------------------------------------------
  1835. public:
  1836. void* getNativeHandle() const noexcept override
  1837. {
  1838. return fHandle;
  1839. }
  1840. const void* getNativeDescriptor() const noexcept override
  1841. {
  1842. return fDescriptor;
  1843. }
  1844. // -------------------------------------------------------------------
  1845. bool init(const char* const name, const char* const label, const uint options)
  1846. {
  1847. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1848. // ---------------------------------------------------------------
  1849. // first checks
  1850. if (pData->client != nullptr)
  1851. {
  1852. pData->engine->setLastError("Plugin client is already registered");
  1853. return false;
  1854. }
  1855. if (label == nullptr && label[0] != '\0')
  1856. {
  1857. pData->engine->setLastError("null label");
  1858. return false;
  1859. }
  1860. // ---------------------------------------------------------------
  1861. // get descriptor that matches label
  1862. sPluginInitializer.initIfNeeded();
  1863. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  1864. {
  1865. fDescriptor = it.getValue(nullptr);
  1866. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1867. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1868. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1869. break;
  1870. fDescriptor = nullptr;
  1871. }
  1872. if (fDescriptor == nullptr)
  1873. {
  1874. pData->engine->setLastError("Invalid internal plugin");
  1875. return false;
  1876. }
  1877. // ---------------------------------------------------------------
  1878. // set icon
  1879. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1880. pData->iconName = carla_strdup_safe("file");
  1881. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1882. pData->iconName = carla_strdup_safe("file");
  1883. // ---------------------------------------------------------------
  1884. // get info
  1885. if (name != nullptr && name[0] != '\0')
  1886. pData->name = pData->engine->getUniquePluginName(name);
  1887. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1888. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1889. else
  1890. pData->name = pData->engine->getUniquePluginName(label);
  1891. {
  1892. CARLA_ASSERT(fHost.uiName == nullptr);
  1893. char uiName[std::strlen(pData->name)+6+1];
  1894. std::strcpy(uiName, pData->name);
  1895. std::strcat(uiName, " (GUI)");
  1896. fHost.uiName = carla_strdup(uiName);
  1897. }
  1898. // ---------------------------------------------------------------
  1899. // register client
  1900. pData->client = pData->engine->addClient(this);
  1901. if (pData->client == nullptr || ! pData->client->isOk())
  1902. {
  1903. pData->engine->setLastError("Failed to register plugin client");
  1904. return false;
  1905. }
  1906. // ---------------------------------------------------------------
  1907. // initialize plugin
  1908. fHandle = fDescriptor->instantiate(&fHost);
  1909. if (fHandle == nullptr)
  1910. {
  1911. pData->engine->setLastError("Plugin failed to initialize");
  1912. return false;
  1913. }
  1914. // ---------------------------------------------------------------
  1915. // set default options
  1916. bool hasMidiProgs = false;
  1917. if (fDescriptor->get_midi_program_count != nullptr)
  1918. {
  1919. try {
  1920. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  1921. } catch (...) {}
  1922. }
  1923. pData->options = 0x0;
  1924. if (fMidiOut.count != 0 || (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1925. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1926. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1927. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1928. if (pData->engine->getOptions().forceStereo)
  1929. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1930. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1931. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1932. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1933. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1934. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1935. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1936. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  1937. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1938. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1939. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1940. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  1941. {
  1942. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1943. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1944. }
  1945. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  1946. {
  1947. CARLA_SAFE_ASSERT(! hasMidiProgs);
  1948. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1949. }
  1950. else if (hasMidiProgs)
  1951. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1952. return true;
  1953. }
  1954. private:
  1955. NativePluginHandle fHandle;
  1956. NativePluginHandle fHandle2;
  1957. NativeHostDescriptor fHost;
  1958. const NativePluginDescriptor* fDescriptor;
  1959. bool fIsProcessing;
  1960. bool fIsOffline;
  1961. bool fIsUiAvailable;
  1962. bool fIsUiVisible;
  1963. float** fAudioInBuffers;
  1964. float** fAudioOutBuffers;
  1965. uint32_t fMidiEventCount;
  1966. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1967. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1968. uint32_t fCurBufferSize;
  1969. double fCurSampleRate;
  1970. NativePluginMidiData fMidiIn;
  1971. NativePluginMidiData fMidiOut;
  1972. NativeTimeInfo fTimeInfo;
  1973. // -------------------------------------------------------------------
  1974. #define handlePtr ((CarlaPluginNative*)handle)
  1975. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1976. {
  1977. return handlePtr->fCurBufferSize;
  1978. }
  1979. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1980. {
  1981. return handlePtr->fCurSampleRate;
  1982. }
  1983. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1984. {
  1985. return handlePtr->fIsOffline;
  1986. }
  1987. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1988. {
  1989. return handlePtr->handleGetTimeInfo();
  1990. }
  1991. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1992. {
  1993. return handlePtr->handleWriteMidiEvent(event);
  1994. }
  1995. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1996. {
  1997. handlePtr->handleUiParameterChanged(index, value);
  1998. }
  1999. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2000. {
  2001. handlePtr->handleUiCustomDataChanged(key, value);
  2002. }
  2003. static void carla_host_ui_closed(NativeHostHandle handle)
  2004. {
  2005. handlePtr->handleUiClosed();
  2006. }
  2007. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2008. {
  2009. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2010. }
  2011. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2012. {
  2013. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2014. }
  2015. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2016. {
  2017. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2018. }
  2019. #undef handlePtr
  2020. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2021. };
  2022. // -----------------------------------------------------------------------
  2023. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2024. {
  2025. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2026. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2027. if (! plugin->init(init.name, init.label, init.options))
  2028. {
  2029. delete plugin;
  2030. return nullptr;
  2031. }
  2032. return plugin;
  2033. }
  2034. // -----------------------------------------------------------------------
  2035. CARLA_BACKEND_END_NAMESPACE