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.

2486 lines
86KB

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