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.

2516 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, 0, MAX_MIDI_CHANNELS);
  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_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  924. pData->midiprog.data[i].bank = mpDesc->bank;
  925. pData->midiprog.data[i].program = mpDesc->program;
  926. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  927. }
  928. }
  929. #ifndef BUILD_BRIDGE
  930. // Update OSC Names
  931. if (pData->engine->isOscControlRegistered())
  932. {
  933. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  934. for (i=0; i < count; ++i)
  935. 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);
  936. }
  937. #endif
  938. if (doInit)
  939. {
  940. if (count > 0)
  941. setMidiProgram(0, false, false, false);
  942. }
  943. else
  944. {
  945. // Check if current program is invalid
  946. bool programChanged = false;
  947. if (count == oldCount+1)
  948. {
  949. // one midi program added, probably created by user
  950. pData->midiprog.current = static_cast<int32_t>(oldCount);
  951. programChanged = true;
  952. }
  953. else if (current < 0 && count > 0)
  954. {
  955. // programs exist now, but not before
  956. pData->midiprog.current = 0;
  957. programChanged = true;
  958. }
  959. else if (current >= 0 && count == 0)
  960. {
  961. // programs existed before, but not anymore
  962. pData->midiprog.current = -1;
  963. programChanged = true;
  964. }
  965. else if (current >= static_cast<int32_t>(count))
  966. {
  967. // current midi program > count
  968. pData->midiprog.current = 0;
  969. programChanged = true;
  970. }
  971. else
  972. {
  973. // no change
  974. pData->midiprog.current = current;
  975. }
  976. if (programChanged)
  977. setMidiProgram(pData->midiprog.current, true, true, true);
  978. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  979. }
  980. }
  981. // -------------------------------------------------------------------
  982. // Plugin processing
  983. void activate() noexcept override
  984. {
  985. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  986. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  987. if (fDescriptor->activate != nullptr)
  988. {
  989. try {
  990. fDescriptor->activate(fHandle);
  991. } catch(...) {}
  992. if (fHandle2 != nullptr)
  993. {
  994. try {
  995. fDescriptor->activate(fHandle2);
  996. } catch(...) {}
  997. }
  998. }
  999. }
  1000. void deactivate() noexcept override
  1001. {
  1002. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1003. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1004. if (fDescriptor->deactivate != nullptr)
  1005. {
  1006. try {
  1007. fDescriptor->deactivate(fHandle);
  1008. } catch(...) {}
  1009. if (fHandle2 != nullptr)
  1010. {
  1011. try {
  1012. fDescriptor->deactivate(fHandle2);
  1013. } catch(...) {}
  1014. }
  1015. }
  1016. }
  1017. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1018. {
  1019. // --------------------------------------------------------------------------------------------------------
  1020. // Check if active
  1021. if (! pData->active)
  1022. {
  1023. // disable any output sound
  1024. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1025. FLOAT_CLEAR(outBuffer[i], frames);
  1026. return;
  1027. }
  1028. fMidiEventCount = 0;
  1029. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  1030. // --------------------------------------------------------------------------------------------------------
  1031. // Check if needs reset
  1032. if (pData->needsReset)
  1033. {
  1034. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1035. {
  1036. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1037. {
  1038. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1039. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1040. fMidiEvents[k].data[2] = 0;
  1041. fMidiEvents[k].size = 3;
  1042. fMidiEvents[k+i].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1043. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1044. fMidiEvents[k+i].data[2] = 0;
  1045. fMidiEvents[k+i].size = 3;
  1046. }
  1047. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1048. }
  1049. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1050. {
  1051. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1052. {
  1053. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1054. fMidiEvents[k].data[1] = k;
  1055. fMidiEvents[k].data[2] = 0;
  1056. fMidiEvents[k].size = 3;
  1057. }
  1058. fMidiEventCount = MAX_MIDI_NOTE;
  1059. }
  1060. pData->needsReset = false;
  1061. }
  1062. CARLA_PROCESS_CONTINUE_CHECK;
  1063. // --------------------------------------------------------------------------------------------------------
  1064. // Set TimeInfo
  1065. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1066. fTimeInfo.playing = timeInfo.playing;
  1067. fTimeInfo.frame = timeInfo.frame;
  1068. fTimeInfo.usecs = timeInfo.usecs;
  1069. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1070. {
  1071. fTimeInfo.bbt.valid = true;
  1072. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1073. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1074. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1075. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1076. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1077. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1078. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1079. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1080. }
  1081. else
  1082. fTimeInfo.bbt.valid = false;
  1083. CARLA_PROCESS_CONTINUE_CHECK;
  1084. // --------------------------------------------------------------------------------------------------------
  1085. // Event Input and Processing
  1086. if (pData->event.portIn != nullptr)
  1087. {
  1088. // ----------------------------------------------------------------------------------------------------
  1089. // MIDI Input (External)
  1090. if (pData->extNotes.mutex.tryLock())
  1091. {
  1092. ExternalMidiNote note = { 0, 0, 0 };
  1093. //for (RtLinkedList<ExternalMidiNote>::Itenerator it = pData->extNotes.data.begin(); it.valid(); it.next())
  1094. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1095. {
  1096. note = pData->extNotes.data.getFirst(note, true);
  1097. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1098. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1099. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1100. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1101. fMidiEvents[fMidiEventCount].size = 3;
  1102. fMidiEventCount += 1;
  1103. }
  1104. pData->extNotes.mutex.unlock();
  1105. } // End of MIDI Input (External)
  1106. // ----------------------------------------------------------------------------------------------------
  1107. // Event Input (System)
  1108. bool allNotesOffSent = false;
  1109. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1110. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1111. uint32_t startTime = 0;
  1112. uint32_t timeOffset = 0;
  1113. uint32_t nextBankId = 0;
  1114. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1115. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1116. for (uint32_t i=0; i < nEvents; ++i)
  1117. {
  1118. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1119. time = event.time;
  1120. if (time >= frames)
  1121. continue;
  1122. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1123. if (time > timeOffset && sampleAccurate)
  1124. {
  1125. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1126. {
  1127. startTime = 0;
  1128. timeOffset = time;
  1129. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1130. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1131. else
  1132. nextBankId = 0;
  1133. if (fMidiEventCount > 0)
  1134. {
  1135. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1136. fMidiEventCount = 0;
  1137. }
  1138. }
  1139. else
  1140. startTime += timeOffset;
  1141. }
  1142. // Control change
  1143. switch (event.type)
  1144. {
  1145. case kEngineEventTypeNull:
  1146. break;
  1147. case kEngineEventTypeControl:
  1148. {
  1149. const EngineControlEvent& ctrlEvent = event.ctrl;
  1150. switch (ctrlEvent.type)
  1151. {
  1152. case kEngineControlEventTypeNull:
  1153. break;
  1154. case kEngineControlEventTypeParameter:
  1155. {
  1156. #ifndef BUILD_BRIDGE
  1157. // Control backend stuff
  1158. if (event.channel == pData->ctrlChannel)
  1159. {
  1160. float value;
  1161. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1162. {
  1163. value = ctrlEvent.value;
  1164. setDryWet(value, false, false);
  1165. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1166. }
  1167. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1168. {
  1169. value = ctrlEvent.value*127.0f/100.0f;
  1170. setVolume(value, false, false);
  1171. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1172. }
  1173. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1174. {
  1175. float left, right;
  1176. value = ctrlEvent.value/0.5f - 1.0f;
  1177. if (value < 0.0f)
  1178. {
  1179. left = -1.0f;
  1180. right = (value*2.0f)+1.0f;
  1181. }
  1182. else if (value > 0.0f)
  1183. {
  1184. left = (value*2.0f)-1.0f;
  1185. right = 1.0f;
  1186. }
  1187. else
  1188. {
  1189. left = -1.0f;
  1190. right = 1.0f;
  1191. }
  1192. setBalanceLeft(left, false, false);
  1193. setBalanceRight(right, false, false);
  1194. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1195. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1196. }
  1197. }
  1198. #endif
  1199. // Control plugin parameters
  1200. for (uint32_t k=0; k < pData->param.count; ++k)
  1201. {
  1202. if (pData->param.data[k].midiChannel != event.channel)
  1203. continue;
  1204. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1205. continue;
  1206. if (pData->param.data[k].type != PARAMETER_INPUT)
  1207. continue;
  1208. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1209. continue;
  1210. float value;
  1211. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1212. {
  1213. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1214. }
  1215. else
  1216. {
  1217. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1218. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1219. value = std::rint(value);
  1220. }
  1221. setParameterValue(k, value, false, false, false);
  1222. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1223. }
  1224. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1225. {
  1226. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1227. continue;
  1228. fMidiEvents[fMidiEventCount].port = 0;
  1229. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1230. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1231. fMidiEvents[fMidiEventCount].data[1] = static_cast<uint8_t>(ctrlEvent.param);
  1232. fMidiEvents[fMidiEventCount].data[2] = uint8_t(ctrlEvent.value*127.0f);
  1233. fMidiEvents[fMidiEventCount].size = 3;
  1234. fMidiEventCount += 1;
  1235. }
  1236. break;
  1237. }
  1238. case kEngineControlEventTypeMidiBank:
  1239. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1240. nextBankId = ctrlEvent.param;
  1241. break;
  1242. case kEngineControlEventTypeMidiProgram:
  1243. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1244. {
  1245. const uint32_t nextProgramId(ctrlEvent.param);
  1246. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1247. {
  1248. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1249. {
  1250. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1251. if (fHandle2 != nullptr)
  1252. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1253. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1254. if (event.channel == pData->ctrlChannel)
  1255. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. break;
  1261. case kEngineControlEventTypeAllSoundOff:
  1262. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1263. {
  1264. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1265. continue;
  1266. fMidiEvents[fMidiEventCount].port = 0;
  1267. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1268. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1269. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1270. fMidiEvents[fMidiEventCount].data[2] = 0;
  1271. fMidiEvents[fMidiEventCount].size = 3;
  1272. fMidiEventCount += 1;
  1273. }
  1274. break;
  1275. case kEngineControlEventTypeAllNotesOff:
  1276. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1277. {
  1278. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1279. {
  1280. allNotesOffSent = true;
  1281. sendMidiAllNotesOffToCallback();
  1282. }
  1283. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1284. continue;
  1285. fMidiEvents[fMidiEventCount].port = 0;
  1286. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1287. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1288. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1289. fMidiEvents[fMidiEventCount].data[2] = 0;
  1290. fMidiEvents[fMidiEventCount].size = 3;
  1291. fMidiEventCount += 1;
  1292. }
  1293. break;
  1294. }
  1295. break;
  1296. }
  1297. case kEngineEventTypeMidi:
  1298. {
  1299. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1300. continue;
  1301. const EngineMidiEvent& midiEvent(event.midi);
  1302. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1303. uint8_t channel = event.channel;
  1304. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1305. continue;
  1306. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1307. continue;
  1308. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1309. continue;
  1310. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1311. continue;
  1312. // Fix bad note-off
  1313. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1314. status = MIDI_STATUS_NOTE_OFF;
  1315. fMidiEvents[fMidiEventCount].port = 0;
  1316. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1317. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1318. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(status + channel);
  1319. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1320. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1321. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1322. fMidiEventCount += 1;
  1323. if (status == MIDI_STATUS_NOTE_ON)
  1324. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1325. else if (status == MIDI_STATUS_NOTE_OFF)
  1326. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1327. break;
  1328. }
  1329. }
  1330. }
  1331. pData->postRtEvents.trySplice();
  1332. if (frames > timeOffset)
  1333. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1334. } // End of Event Input and Processing
  1335. // --------------------------------------------------------------------------------------------------------
  1336. // Plugin processing (no events)
  1337. else
  1338. {
  1339. processSingle(inBuffer, outBuffer, frames, 0);
  1340. } // End of Plugin processing (no events)
  1341. CARLA_PROCESS_CONTINUE_CHECK;
  1342. // --------------------------------------------------------------------------------------------------------
  1343. // Control and MIDI Output
  1344. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1345. {
  1346. float value, curValue;
  1347. for (uint32_t k=0; k < pData->param.count; ++k)
  1348. {
  1349. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1350. continue;
  1351. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1352. pData->param.ranges[k].fixValue(curValue);
  1353. if (pData->param.data[k].midiCC > 0)
  1354. {
  1355. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1356. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1357. }
  1358. }
  1359. // reverse lookup MIDI events
  1360. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1361. {
  1362. if (fMidiEvents[k].data[0] == 0)
  1363. break;
  1364. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1365. const uint8_t port = fMidiEvents[k].port;
  1366. if (pData->event.portOut != nullptr)
  1367. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1368. else if (port < fMidiOut.count)
  1369. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1370. }
  1371. } // End of Control and MIDI Output
  1372. }
  1373. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1374. {
  1375. CARLA_ASSERT(frames > 0);
  1376. if (frames == 0)
  1377. return false;
  1378. if (pData->audioIn.count > 0)
  1379. {
  1380. CARLA_ASSERT(inBuffer != nullptr);
  1381. if (inBuffer == nullptr)
  1382. return false;
  1383. }
  1384. if (pData->audioOut.count > 0)
  1385. {
  1386. CARLA_ASSERT(outBuffer != nullptr);
  1387. if (outBuffer == nullptr)
  1388. return false;
  1389. }
  1390. uint32_t i, k;
  1391. // --------------------------------------------------------------------------------------------------------
  1392. // Try lock, silence otherwise
  1393. if (pData->engine->isOffline())
  1394. {
  1395. pData->singleMutex.lock();
  1396. }
  1397. else if (! pData->singleMutex.tryLock())
  1398. {
  1399. for (i=0; i < pData->audioOut.count; ++i)
  1400. {
  1401. for (k=0; k < frames; ++k)
  1402. outBuffer[i][k+timeOffset] = 0.0f;
  1403. }
  1404. return false;
  1405. }
  1406. // --------------------------------------------------------------------------------------------------------
  1407. // Reset audio buffers
  1408. for (i=0; i < pData->audioIn.count; ++i)
  1409. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1410. for (i=0; i < pData->audioOut.count; ++i)
  1411. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1412. // --------------------------------------------------------------------------------------------------------
  1413. // Run plugin
  1414. fIsProcessing = true;
  1415. if (fHandle2 == nullptr)
  1416. {
  1417. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1418. }
  1419. else
  1420. {
  1421. fDescriptor->process(fHandle,
  1422. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1423. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1424. frames, fMidiEvents, fMidiEventCount);
  1425. fDescriptor->process(fHandle2,
  1426. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1427. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1428. frames, fMidiEvents, fMidiEventCount);
  1429. }
  1430. fIsProcessing = false;
  1431. fTimeInfo.frame += frames;
  1432. #ifndef BUILD_BRIDGE
  1433. // --------------------------------------------------------------------------------------------------------
  1434. // Post-processing (dry/wet, volume and balance)
  1435. {
  1436. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1437. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1438. bool isPair;
  1439. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1440. for (i=0; i < pData->audioOut.count; ++i)
  1441. {
  1442. // Dry/Wet
  1443. if (doDryWet)
  1444. {
  1445. for (k=0; k < frames; ++k)
  1446. {
  1447. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1448. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1449. }
  1450. }
  1451. // Balance
  1452. if (doBalance)
  1453. {
  1454. isPair = (i % 2 == 0);
  1455. if (isPair)
  1456. {
  1457. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1458. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1459. }
  1460. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1461. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1462. for (k=0; k < frames; ++k)
  1463. {
  1464. if (isPair)
  1465. {
  1466. // left
  1467. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1468. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1469. }
  1470. else
  1471. {
  1472. // right
  1473. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1474. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1475. }
  1476. }
  1477. }
  1478. // Volume (and buffer copy)
  1479. {
  1480. for (k=0; k < frames; ++k)
  1481. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1482. }
  1483. }
  1484. } // End of Post-processing
  1485. #else
  1486. for (i=0; i < pData->audioOut.count; ++i)
  1487. {
  1488. for (k=0; k < frames; ++k)
  1489. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1490. }
  1491. #endif
  1492. // --------------------------------------------------------------------------------------------------------
  1493. pData->singleMutex.unlock();
  1494. return true;
  1495. }
  1496. void bufferSizeChanged(const uint32_t newBufferSize) override
  1497. {
  1498. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1499. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1500. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1501. {
  1502. if (fAudioInBuffers[i] != nullptr)
  1503. delete[] fAudioInBuffers[i];
  1504. fAudioInBuffers[i] = new float[newBufferSize];
  1505. }
  1506. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1507. {
  1508. if (fAudioOutBuffers[i] != nullptr)
  1509. delete[] fAudioOutBuffers[i];
  1510. fAudioOutBuffers[i] = new float[newBufferSize];
  1511. }
  1512. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1513. {
  1514. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1515. if (fHandle2 != nullptr)
  1516. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1517. }
  1518. }
  1519. void sampleRateChanged(const double newSampleRate) override
  1520. {
  1521. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1522. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1523. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1524. {
  1525. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1526. if (fHandle2 != nullptr)
  1527. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1528. }
  1529. }
  1530. void offlineModeChanged(const bool isOffline) override
  1531. {
  1532. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1533. {
  1534. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1535. if (fHandle2 != nullptr)
  1536. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1537. }
  1538. }
  1539. // -------------------------------------------------------------------
  1540. // Plugin buffers
  1541. void initBuffers() const noexcept override
  1542. {
  1543. fMidiIn.initBuffers();
  1544. fMidiOut.initBuffers();
  1545. CarlaPlugin::initBuffers();
  1546. }
  1547. void clearBuffers() noexcept override
  1548. {
  1549. carla_debug("NativePlugin::clearBuffers() - start");
  1550. if (fAudioInBuffers != nullptr)
  1551. {
  1552. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1553. {
  1554. if (fAudioInBuffers[i] != nullptr)
  1555. {
  1556. delete[] fAudioInBuffers[i];
  1557. fAudioInBuffers[i] = nullptr;
  1558. }
  1559. }
  1560. delete[] fAudioInBuffers;
  1561. fAudioInBuffers = nullptr;
  1562. }
  1563. if (fAudioOutBuffers != nullptr)
  1564. {
  1565. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1566. {
  1567. if (fAudioOutBuffers[i] != nullptr)
  1568. {
  1569. delete[] fAudioOutBuffers[i];
  1570. fAudioOutBuffers[i] = nullptr;
  1571. }
  1572. }
  1573. delete[] fAudioOutBuffers;
  1574. fAudioOutBuffers = nullptr;
  1575. }
  1576. fMidiIn.clear();
  1577. fMidiOut.clear();
  1578. CarlaPlugin::clearBuffers();
  1579. carla_debug("NativePlugin::clearBuffers() - end");
  1580. }
  1581. // -------------------------------------------------------------------
  1582. // Post-poned UI Stuff
  1583. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1584. {
  1585. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1586. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1587. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1588. if (! fIsUiVisible)
  1589. return;
  1590. if (fDescriptor->ui_set_parameter_value != nullptr)
  1591. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1592. }
  1593. void uiMidiProgramChange(const uint32_t index) noexcept override
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1596. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1597. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1598. if (! fIsUiVisible)
  1599. return;
  1600. if (index >= pData->midiprog.count)
  1601. return;
  1602. if (fDescriptor->ui_set_midi_program != nullptr)
  1603. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1604. }
  1605. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) 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. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1612. if (! fIsUiVisible)
  1613. return;
  1614. // TODO
  1615. }
  1616. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1617. {
  1618. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1619. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1620. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1621. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1622. if (! fIsUiVisible)
  1623. return;
  1624. if (fDescriptor == nullptr || fHandle == nullptr)
  1625. return;
  1626. // TODO
  1627. }
  1628. // -------------------------------------------------------------------
  1629. protected:
  1630. uint32_t handleGetBufferSize() const noexcept
  1631. {
  1632. return pData->engine->getBufferSize();
  1633. }
  1634. double handleGetSampleRate() const noexcept
  1635. {
  1636. return pData->engine->getSampleRate();
  1637. }
  1638. bool handleIsOffline() const noexcept
  1639. {
  1640. return pData->engine->isOffline();
  1641. }
  1642. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1643. {
  1644. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1645. return &fTimeInfo;
  1646. }
  1647. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1648. {
  1649. CARLA_ASSERT(pData->enabled);
  1650. CARLA_ASSERT(fIsProcessing);
  1651. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1652. CARLA_ASSERT(event != nullptr);
  1653. CARLA_ASSERT(event->data[0] != 0);
  1654. if (! pData->enabled)
  1655. return false;
  1656. if (fMidiOut.count == 0)
  1657. return false;
  1658. if (event == nullptr)
  1659. return false;
  1660. if (event->data[0] == 0)
  1661. return false;
  1662. if (! fIsProcessing)
  1663. {
  1664. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1665. return false;
  1666. }
  1667. // reverse-find first free event, and put it there
  1668. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1669. {
  1670. if (fMidiEvents[i].data[0] == 0)
  1671. {
  1672. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1673. return true;
  1674. }
  1675. }
  1676. return false;
  1677. }
  1678. void handleUiParameterChanged(const uint32_t index, const float value)
  1679. {
  1680. setParameterValue(index, value, false, true, true);
  1681. }
  1682. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1683. {
  1684. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1685. }
  1686. void handleUiClosed()
  1687. {
  1688. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1689. fIsUiVisible = false;
  1690. }
  1691. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1692. {
  1693. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1694. }
  1695. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1696. {
  1697. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1698. }
  1699. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1700. {
  1701. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1702. intptr_t ret = 0;
  1703. switch (opcode)
  1704. {
  1705. case ::HOST_OPCODE_NULL:
  1706. break;
  1707. #ifdef BUILD_BRIDGE
  1708. case ::HOST_OPCODE_SET_VOLUME:
  1709. case ::HOST_OPCODE_SET_DRYWET:
  1710. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1711. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1712. case ::HOST_OPCODE_SET_PANNING:
  1713. break;
  1714. #else
  1715. case ::HOST_OPCODE_SET_VOLUME:
  1716. setVolume(opt, true, true);
  1717. break;
  1718. case ::HOST_OPCODE_SET_DRYWET:
  1719. setDryWet(opt, true, true);
  1720. break;
  1721. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1722. setBalanceLeft(opt, true, true);
  1723. break;
  1724. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1725. setBalanceRight(opt, true, true);
  1726. break;
  1727. case ::HOST_OPCODE_SET_PANNING:
  1728. setPanning(opt, true, true);
  1729. break;
  1730. #endif
  1731. case ::HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1732. case ::HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1733. // TODO
  1734. break;
  1735. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1736. // TODO
  1737. break;
  1738. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1739. // TODO
  1740. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1741. break;
  1742. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1743. // TODO
  1744. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1745. break;
  1746. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1747. reload(); // FIXME
  1748. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1749. break;
  1750. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1751. reloadPrograms(false);
  1752. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1753. break;
  1754. case ::HOST_OPCODE_RELOAD_ALL:
  1755. reload();
  1756. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1757. break;
  1758. case ::HOST_OPCODE_UI_UNAVAILABLE:
  1759. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1760. break;
  1761. }
  1762. return ret;
  1763. // unused for now
  1764. (void)index;
  1765. (void)value;
  1766. (void)ptr;
  1767. (void)opt;
  1768. }
  1769. // -------------------------------------------------------------------
  1770. public:
  1771. static size_t getPluginCount() noexcept
  1772. {
  1773. return gPluginDescriptors.count();
  1774. }
  1775. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1776. {
  1777. CARLA_SAFE_ASSERT_RETURN(index < gPluginDescriptors.count(), nullptr);
  1778. return gPluginDescriptors.getAt(index, nullptr);
  1779. }
  1780. // -------------------------------------------------------------------
  1781. bool init(const char* const name, const char* const label)
  1782. {
  1783. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1784. // ---------------------------------------------------------------
  1785. // first checks
  1786. if (pData->client != nullptr)
  1787. {
  1788. pData->engine->setLastError("Plugin client is already registered");
  1789. return false;
  1790. }
  1791. if (label == nullptr && label[0] != '\0')
  1792. {
  1793. pData->engine->setLastError("null label");
  1794. return false;
  1795. }
  1796. // ---------------------------------------------------------------
  1797. // get descriptor that matches label
  1798. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin(); it.valid(); it.next())
  1799. {
  1800. fDescriptor = it.getValue();
  1801. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1802. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1803. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1804. break;
  1805. fDescriptor = nullptr;
  1806. }
  1807. if (fDescriptor == nullptr)
  1808. {
  1809. pData->engine->setLastError("Invalid internal plugin");
  1810. return false;
  1811. }
  1812. // ---------------------------------------------------------------
  1813. // set icon
  1814. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1815. pData->iconName = carla_strdup("file");
  1816. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1817. pData->iconName = carla_strdup("file");
  1818. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1819. pData->iconName = carla_strdup("file");
  1820. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1821. pData->iconName = carla_strdup("distrho");
  1822. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1823. pData->iconName = carla_strdup("distrho");
  1824. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1825. pData->iconName = carla_strdup("distrho");
  1826. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1827. pData->iconName = carla_strdup("distrho");
  1828. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1829. pData->iconName = carla_strdup("distrho");
  1830. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1831. pData->iconName = carla_strdup("distrho");
  1832. // ---------------------------------------------------------------
  1833. // get info
  1834. if (name != nullptr && name[0] != '\0')
  1835. pData->name = pData->engine->getUniquePluginName(name);
  1836. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1837. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1838. else
  1839. pData->name = pData->engine->getUniquePluginName(label);
  1840. {
  1841. CARLA_ASSERT(fHost.uiName == nullptr);
  1842. char uiName[std::strlen(pData->name)+6+1];
  1843. std::strcpy(uiName, pData->name);
  1844. std::strcat(uiName, " (GUI)");
  1845. fHost.uiName = carla_strdup(uiName);
  1846. }
  1847. // ---------------------------------------------------------------
  1848. // register client
  1849. pData->client = pData->engine->addClient(this);
  1850. if (pData->client == nullptr || ! pData->client->isOk())
  1851. {
  1852. pData->engine->setLastError("Failed to register plugin client");
  1853. return false;
  1854. }
  1855. // ---------------------------------------------------------------
  1856. // initialize plugin
  1857. fHandle = fDescriptor->instantiate(&fHost);
  1858. if (fHandle == nullptr)
  1859. {
  1860. pData->engine->setLastError("Plugin failed to initialize");
  1861. return false;
  1862. }
  1863. // ---------------------------------------------------------------
  1864. // load plugin settings
  1865. {
  1866. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1867. // set default options
  1868. pData->options = 0x0;
  1869. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1870. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1871. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1872. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1873. if (pData->engine->getOptions().forceStereo)
  1874. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1875. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1876. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1877. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1878. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1879. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1880. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1881. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1882. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1883. #ifndef BUILD_BRIDGE
  1884. // set identifier string
  1885. CarlaString identifier("Native/");
  1886. identifier += label;
  1887. pData->identifier = identifier.dup();
  1888. // load settings
  1889. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1890. // ignore settings, we need this anyway
  1891. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1892. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1893. #endif
  1894. }
  1895. return true;
  1896. }
  1897. private:
  1898. NativePluginHandle fHandle;
  1899. NativePluginHandle fHandle2;
  1900. NativeHostDescriptor fHost;
  1901. const NativePluginDescriptor* fDescriptor;
  1902. bool fIsProcessing;
  1903. bool fIsUiVisible;
  1904. float** fAudioInBuffers;
  1905. float** fAudioOutBuffers;
  1906. uint32_t fMidiEventCount;
  1907. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1908. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1909. NativePluginMidiData fMidiIn;
  1910. NativePluginMidiData fMidiOut;
  1911. NativeTimeInfo fTimeInfo;
  1912. // -------------------------------------------------------------------
  1913. #define handlePtr ((NativePlugin*)handle)
  1914. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1915. {
  1916. return handlePtr->handleGetBufferSize();
  1917. }
  1918. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1919. {
  1920. return handlePtr->handleGetSampleRate();
  1921. }
  1922. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1923. {
  1924. return handlePtr->handleIsOffline();
  1925. }
  1926. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1927. {
  1928. return handlePtr->handleGetTimeInfo();
  1929. }
  1930. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1931. {
  1932. return handlePtr->handleWriteMidiEvent(event);
  1933. }
  1934. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1935. {
  1936. handlePtr->handleUiParameterChanged(index, value);
  1937. }
  1938. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1939. {
  1940. handlePtr->handleUiCustomDataChanged(key, value);
  1941. }
  1942. static void carla_host_ui_closed(NativeHostHandle handle)
  1943. {
  1944. handlePtr->handleUiClosed();
  1945. }
  1946. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1947. {
  1948. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1949. }
  1950. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1951. {
  1952. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1953. }
  1954. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1955. {
  1956. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1957. }
  1958. #undef handlePtr
  1959. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1960. };
  1961. CARLA_BACKEND_END_NAMESPACE
  1962. #endif // WANT_NATIVE
  1963. // -----------------------------------------------------------------------
  1964. CARLA_BACKEND_START_NAMESPACE
  1965. #ifdef WANT_NATIVE
  1966. size_t CarlaPlugin::getNativePluginCount() noexcept
  1967. {
  1968. return NativePlugin::getPluginCount();
  1969. }
  1970. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index) noexcept
  1971. {
  1972. return NativePlugin::getPluginDescriptor(index);
  1973. }
  1974. #endif
  1975. // -----------------------------------------------------------------------
  1976. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1977. {
  1978. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1979. #ifdef WANT_NATIVE
  1980. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1981. if (! plugin->init(init.name, init.label))
  1982. {
  1983. delete plugin;
  1984. return nullptr;
  1985. }
  1986. plugin->reload();
  1987. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1988. {
  1989. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1990. delete plugin;
  1991. return nullptr;
  1992. }
  1993. return plugin;
  1994. #else
  1995. init.engine->setLastError("Internal plugins support not available");
  1996. return nullptr;
  1997. #endif
  1998. }
  1999. CARLA_BACKEND_END_NAMESPACE
  2000. // -----------------------------------------------------------------------