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.

2524 lines
90KB

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