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.

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