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.

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