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.

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