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.

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