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.

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