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.

2520 lines
90KB

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