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.

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