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.

2500 lines
88KB

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