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.

2506 lines
89KB

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