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.

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