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.

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