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.

2477 lines
85KB

  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< ::MidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  103. carla_zeroStruct< ::TimeInfo>(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_GUI)
  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 Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  191. return param->scalePointCount;
  192. carla_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 getAvailableOptions() 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() != 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 Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  244. {
  245. const ParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  246. return scalePoint->value;
  247. }
  248. carla_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 Parameter* 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_assert("param->name != nullptr", __FILE__, __LINE__);
  297. return CarlaPlugin::getParameterName(parameterId, strBuf);
  298. }
  299. carla_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_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 Parameter* 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. carla_assert("param->unit != nullptr", __FILE__, __LINE__);
  332. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  333. }
  334. carla_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  335. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  336. }
  337. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const override
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  341. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  342. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  343. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  344. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  345. {
  346. const ParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  347. if (scalePoint->label != nullptr)
  348. {
  349. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  350. return;
  351. }
  352. carla_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  353. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  354. }
  355. carla_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  356. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  357. }
  358. // -------------------------------------------------------------------
  359. // Set data (state)
  360. void prepareForSave() override
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  363. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  364. if (pData->midiprog.count > 0 && fDescriptor->category == ::PLUGIN_CATEGORY_SYNTH)
  365. {
  366. char strBuf[STR_MAX+1];
  367. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  368. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  369. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  370. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  371. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  372. strBuf[STR_MAX] = '\0';
  373. CarlaPlugin::setCustomData(CUSTOM_DATA_STRING, "midiPrograms", strBuf, false);
  374. }
  375. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  376. return;
  377. if (char* data = fDescriptor->get_state(fHandle))
  378. {
  379. CarlaPlugin::setCustomData(CUSTOM_DATA_CHUNK, "State", data, false);
  380. std::free(data);
  381. }
  382. }
  383. // -------------------------------------------------------------------
  384. // Set data (internal stuff)
  385. void setName(const char* const newName) override
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  390. char uiName[std::strlen(newName)+6+1];
  391. std::strcpy(uiName, newName);
  392. std::strcat(uiName, " (GUI)");
  393. if (fHost.uiName != nullptr)
  394. delete[] fHost.uiName;
  395. fHost.uiName = carla_strdup(uiName);
  396. if (fDescriptor->dispatcher != nullptr)
  397. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  398. CarlaPlugin::setName(newName);
  399. }
  400. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) override
  401. {
  402. if (channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  403. pData->midiprog.current = fCurMidiProgs[channel];
  404. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  405. }
  406. // -------------------------------------------------------------------
  407. // Set data (plugin-specific stuff)
  408. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  411. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  412. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  413. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  414. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  415. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  416. if (fHandle2 != nullptr)
  417. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  418. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  419. }
  420. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  421. {
  422. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  423. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  424. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  425. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  426. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  427. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  428. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_CHUNK) != 0)
  429. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  430. if (std::strcmp(type, CUSTOM_DATA_CHUNK) == 0)
  431. {
  432. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  433. {
  434. const ScopedSingleProcessLocker spl(this, true);
  435. fDescriptor->set_state(fHandle, value);
  436. if (fHandle2 != nullptr)
  437. fDescriptor->set_state(fHandle2, value);
  438. }
  439. }
  440. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  441. {
  442. #if 0 // TODO
  443. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  444. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  445. {
  446. uint i = 0;
  447. foreach (const QString& midiProg, midiProgramList)
  448. {
  449. bool ok;
  450. const uint index(midiProg.toUInt(&ok));
  451. if (ok && index < pData->midiprog.count)
  452. {
  453. const uint32_t bank = pData->midiprog.data[index].bank;
  454. const uint32_t program = pData->midiprog.data[index].program;
  455. fDescriptor->set_midi_program(fHandle, i, bank, program);
  456. if (fHandle2 != nullptr)
  457. fDescriptor->set_midi_program(fHandle2, i, bank, program);
  458. fCurMidiProgs[i] = index;
  459. if (pData->ctrlChannel == static_cast<int32_t>(i))
  460. {
  461. pData->midiprog.current = index;
  462. pData->engine->callback(CALLBACK_MIDI_PROGRAM_CHANGED, fId, index, 0, 0.0f, nullptr);
  463. }
  464. }
  465. ++i;
  466. }
  467. }
  468. #endif
  469. }
  470. else
  471. {
  472. if (fDescriptor->set_custom_data != nullptr)
  473. {
  474. fDescriptor->set_custom_data(fHandle, key, value);
  475. if (fHandle2 != nullptr)
  476. fDescriptor->set_custom_data(fHandle2, key, value);
  477. }
  478. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  479. fDescriptor->ui_set_custom_data(fHandle, key, value);
  480. }
  481. CarlaPlugin::setCustomData(type, key, value, sendGui);
  482. }
  483. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  484. {
  485. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  486. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  487. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  488. if ((fHints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  489. return;
  490. if (index >= 0)
  491. {
  492. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  493. const uint32_t bank = pData->midiprog.data[index].bank;
  494. const uint32_t program = pData->midiprog.data[index].program;
  495. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  496. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  497. if (fHandle2 != nullptr)
  498. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  499. fCurMidiProgs[channel] = index;
  500. }
  501. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  502. }
  503. // -------------------------------------------------------------------
  504. // Set gui stuff
  505. void showGui(const bool yesNo) override
  506. {
  507. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  508. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  509. if (fDescriptor->ui_show == nullptr)
  510. return;
  511. fDescriptor->ui_show(fHandle, yesNo);
  512. fIsUiVisible = yesNo;
  513. if (! yesNo)
  514. return;
  515. if (fDescriptor->ui_set_custom_data != nullptr)
  516. {
  517. for (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  518. {
  519. const CustomData& cData(*it);
  520. if (std::strcmp(cData.type, CUSTOM_DATA_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  521. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  522. }
  523. }
  524. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  525. {
  526. const uint32_t index = pData->midiprog.current;
  527. const uint8_t channel = (pData->ctrlChannel >= 0 || pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  528. const uint32_t bank = pData->midiprog.data[index].bank;
  529. const uint32_t program = pData->midiprog.data[index].program;
  530. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  531. }
  532. if (fDescriptor->ui_set_parameter_value != nullptr)
  533. {
  534. for (uint32_t i=0; i < pData->param.count; ++i)
  535. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  536. }
  537. }
  538. void idleGui() override
  539. {
  540. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  541. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  542. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  543. fDescriptor->ui_idle(fHandle);
  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 ProcessMode 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 == 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 == 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 == 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 == 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 ::Parameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  712. CARLA_ASSERT(paramInfo != nullptr);
  713. if (paramInfo == nullptr)
  714. continue;
  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. pData->param.data[j].type = PARAMETER_OUTPUT;
  771. needsCtrlOut = true;
  772. }
  773. else
  774. {
  775. pData->param.data[j].type = PARAMETER_INPUT;
  776. needsCtrlIn = true;
  777. }
  778. // extra parameter hints
  779. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  780. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  781. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  782. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  783. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  784. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  785. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  786. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  787. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  788. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  789. pData->param.ranges[j].min = min;
  790. pData->param.ranges[j].max = max;
  791. pData->param.ranges[j].def = def;
  792. pData->param.ranges[j].step = step;
  793. pData->param.ranges[j].stepSmall = stepSmall;
  794. pData->param.ranges[j].stepLarge = stepLarge;
  795. }
  796. if (needsCtrlIn)
  797. {
  798. portName.clear();
  799. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  800. {
  801. portName = fName;
  802. portName += ":";
  803. }
  804. portName += "events-in";
  805. portName.truncate(portNameSize);
  806. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  807. }
  808. if (needsCtrlOut)
  809. {
  810. portName.clear();
  811. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  812. {
  813. portName = fName;
  814. portName += ":";
  815. }
  816. portName += "events-out";
  817. portName.truncate(portNameSize);
  818. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  819. }
  820. if (forcedStereoIn || forcedStereoOut)
  821. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  822. else
  823. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  824. // plugin hints
  825. fHints = 0x0;
  826. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  827. fHints |= PLUGIN_CAN_DRYWET;
  828. if (aOuts > 0)
  829. fHints |= PLUGIN_CAN_VOLUME;
  830. if (aOuts >= 2 && aOuts % 2 == 0)
  831. fHints |= PLUGIN_CAN_BALANCE;
  832. // native plugin hints
  833. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  834. fHints |= PLUGIN_IS_RTSAFE;
  835. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  836. fHints |= PLUGIN_HAS_GUI;
  837. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  838. fHints |= PLUGIN_NEEDS_SINGLE_THREAD;
  839. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  840. fHints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  841. // extra plugin hints
  842. pData->extraHints = 0x0;
  843. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  844. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  845. bufferSizeChanged(pData->engine->getBufferSize());
  846. reloadPrograms(true);
  847. if (pData->active)
  848. activate();
  849. carla_debug("NativePlugin::reload() - end");
  850. }
  851. void reloadPrograms(const bool init) override
  852. {
  853. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  854. uint32_t i, oldCount = pData->midiprog.count;
  855. const int32_t current = pData->midiprog.current;
  856. // Delete old programs
  857. pData->midiprog.clear();
  858. // Query new programs
  859. uint32_t count = 0;
  860. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  861. count = fDescriptor->get_midi_program_count(fHandle);
  862. if (count > 0)
  863. {
  864. pData->midiprog.createNew(count);
  865. // Update data
  866. for (i=0; i < count; ++i)
  867. {
  868. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  869. CARLA_ASSERT(mpDesc != nullptr);
  870. CARLA_ASSERT(mpDesc->name != nullptr);
  871. pData->midiprog.data[i].bank = mpDesc->bank;
  872. pData->midiprog.data[i].program = mpDesc->program;
  873. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  874. }
  875. }
  876. #ifndef BUILD_BRIDGE
  877. // Update OSC Names
  878. if (pData->engine->isOscControlRegistered())
  879. {
  880. pData->engine->oscSend_control_set_midi_program_count(fId, count);
  881. for (i=0; i < count; ++i)
  882. 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);
  883. }
  884. #endif
  885. if (init)
  886. {
  887. if (count > 0)
  888. setMidiProgram(0, false, false, false);
  889. }
  890. else
  891. {
  892. // Check if current program is invalid
  893. bool programChanged = false;
  894. if (count == oldCount+1)
  895. {
  896. // one midi program added, probably created by user
  897. pData->midiprog.current = oldCount;
  898. programChanged = true;
  899. }
  900. else if (current < 0 && count > 0)
  901. {
  902. // programs exist now, but not before
  903. pData->midiprog.current = 0;
  904. programChanged = true;
  905. }
  906. else if (current >= 0 && count == 0)
  907. {
  908. // programs existed before, but not anymore
  909. pData->midiprog.current = -1;
  910. programChanged = true;
  911. }
  912. else if (current >= static_cast<int32_t>(count))
  913. {
  914. // current midi program > count
  915. pData->midiprog.current = 0;
  916. programChanged = true;
  917. }
  918. else
  919. {
  920. // no change
  921. pData->midiprog.current = current;
  922. }
  923. if (programChanged)
  924. setMidiProgram(pData->midiprog.current, true, true, true);
  925. pData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  926. }
  927. }
  928. // -------------------------------------------------------------------
  929. // Plugin processing
  930. void activate() override
  931. {
  932. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  933. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  934. if (fDescriptor->activate != nullptr)
  935. {
  936. fDescriptor->activate(fHandle);
  937. if (fHandle2 != nullptr)
  938. fDescriptor->activate(fHandle2);
  939. }
  940. }
  941. void deactivate() override
  942. {
  943. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  944. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  945. if (fDescriptor->deactivate != nullptr)
  946. {
  947. fDescriptor->deactivate(fHandle);
  948. if (fHandle2 != nullptr)
  949. fDescriptor->deactivate(fHandle2);
  950. }
  951. }
  952. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  953. {
  954. uint32_t i, k;
  955. // --------------------------------------------------------------------------------------------------------
  956. // Check if active
  957. if (! pData->active)
  958. {
  959. // disable any output sound
  960. for (i=0; i < pData->audioOut.count; ++i)
  961. FloatVectorOperations::clear(outBuffer[i], frames);
  962. return;
  963. }
  964. fMidiEventCount = 0;
  965. carla_zeroStruct< ::MidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  966. // --------------------------------------------------------------------------------------------------------
  967. // Check if needs reset
  968. if (pData->needsReset)
  969. {
  970. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  971. {
  972. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  973. {
  974. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  975. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  976. fMidiEvents[k].data[2] = 0;
  977. fMidiEvents[k].size = 3;
  978. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  979. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  980. fMidiEvents[k+i].data[2] = 0;
  981. fMidiEvents[k+i].size = 3;
  982. }
  983. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  984. }
  985. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  986. {
  987. for (k=0; k < MAX_MIDI_NOTE; ++k)
  988. {
  989. fMidiEvents[k].data[0] = MIDI_STATUS_NOTE_OFF + pData->ctrlChannel;
  990. fMidiEvents[k].data[1] = k;
  991. fMidiEvents[k].data[2] = 0;
  992. fMidiEvents[k].size = 3;
  993. }
  994. fMidiEventCount = MAX_MIDI_NOTE;
  995. }
  996. pData->needsReset = false;
  997. }
  998. CARLA_PROCESS_CONTINUE_CHECK;
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Set TimeInfo
  1001. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1002. fTimeInfo.playing = timeInfo.playing;
  1003. fTimeInfo.frame = timeInfo.frame;
  1004. fTimeInfo.usecs = timeInfo.usecs;
  1005. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1006. {
  1007. fTimeInfo.bbt.valid = true;
  1008. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1009. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1010. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1011. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1012. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1013. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1014. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1015. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1016. }
  1017. else
  1018. fTimeInfo.bbt.valid = false;
  1019. CARLA_PROCESS_CONTINUE_CHECK;
  1020. // --------------------------------------------------------------------------------------------------------
  1021. // Event Input and Processing
  1022. if (pData->event.portIn != nullptr)
  1023. {
  1024. // ----------------------------------------------------------------------------------------------------
  1025. // MIDI Input (External)
  1026. if (pData->extNotes.mutex.tryLock())
  1027. {
  1028. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1029. {
  1030. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  1031. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1032. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1033. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  1034. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1035. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1036. fMidiEvents[fMidiEventCount].size = 3;
  1037. fMidiEventCount += 1;
  1038. }
  1039. pData->extNotes.mutex.unlock();
  1040. } // End of MIDI Input (External)
  1041. // ----------------------------------------------------------------------------------------------------
  1042. // Event Input (System)
  1043. bool allNotesOffSent = false;
  1044. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1045. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1046. uint32_t startTime = 0;
  1047. uint32_t timeOffset = 0;
  1048. uint32_t nextBankId = 0;
  1049. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1050. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1051. for (i=0; i < nEvents; ++i)
  1052. {
  1053. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1054. time = event.time;
  1055. if (time >= frames)
  1056. continue;
  1057. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1058. if (time > timeOffset && sampleAccurate)
  1059. {
  1060. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1061. {
  1062. startTime = 0;
  1063. timeOffset = time;
  1064. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1065. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1066. else
  1067. nextBankId = 0;
  1068. if (fMidiEventCount > 0)
  1069. {
  1070. carla_zeroStruct< ::MidiEvent>(fMidiEvents, fMidiEventCount);
  1071. fMidiEventCount = 0;
  1072. }
  1073. }
  1074. else
  1075. startTime += timeOffset;
  1076. }
  1077. // Control change
  1078. switch (event.type)
  1079. {
  1080. case kEngineEventTypeNull:
  1081. break;
  1082. case kEngineEventTypeControl:
  1083. {
  1084. const EngineControlEvent& ctrlEvent = event.ctrl;
  1085. switch (ctrlEvent.type)
  1086. {
  1087. case kEngineControlEventTypeNull:
  1088. break;
  1089. case kEngineControlEventTypeParameter:
  1090. {
  1091. #ifndef BUILD_BRIDGE
  1092. // Control backend stuff
  1093. if (event.channel == pData->ctrlChannel)
  1094. {
  1095. float value;
  1096. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1097. {
  1098. value = ctrlEvent.value;
  1099. setDryWet(value, false, false);
  1100. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1101. }
  1102. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1103. {
  1104. value = ctrlEvent.value*127.0f/100.0f;
  1105. setVolume(value, false, false);
  1106. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1107. }
  1108. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1109. {
  1110. float left, right;
  1111. value = ctrlEvent.value/0.5f - 1.0f;
  1112. if (value < 0.0f)
  1113. {
  1114. left = -1.0f;
  1115. right = (value*2.0f)+1.0f;
  1116. }
  1117. else if (value > 0.0f)
  1118. {
  1119. left = (value*2.0f)-1.0f;
  1120. right = 1.0f;
  1121. }
  1122. else
  1123. {
  1124. left = -1.0f;
  1125. right = 1.0f;
  1126. }
  1127. setBalanceLeft(left, false, false);
  1128. setBalanceRight(right, false, false);
  1129. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1130. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1131. }
  1132. }
  1133. #endif
  1134. // Control plugin parameters
  1135. for (k=0; k < pData->param.count; ++k)
  1136. {
  1137. if (pData->param.data[k].midiChannel != event.channel)
  1138. continue;
  1139. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1140. continue;
  1141. if (pData->param.data[k].type != PARAMETER_INPUT)
  1142. continue;
  1143. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1144. continue;
  1145. float value;
  1146. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1147. {
  1148. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1149. }
  1150. else
  1151. {
  1152. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1153. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1154. value = std::rint(value);
  1155. }
  1156. setParameterValue(k, value, false, false, false);
  1157. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1158. }
  1159. if ((fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1160. {
  1161. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1162. continue;
  1163. fMidiEvents[fMidiEventCount].port = 0;
  1164. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1165. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1166. fMidiEvents[fMidiEventCount].data[1] = ctrlEvent.param;
  1167. fMidiEvents[fMidiEventCount].data[2] = ctrlEvent.value*127.0f;
  1168. fMidiEvents[fMidiEventCount].size = 3;
  1169. fMidiEventCount += 1;
  1170. }
  1171. break;
  1172. }
  1173. case kEngineControlEventTypeMidiBank:
  1174. if (event.channel == pData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1175. nextBankId = ctrlEvent.param;
  1176. break;
  1177. case kEngineControlEventTypeMidiProgram:
  1178. if (event.channel < MAX_MIDI_CHANNELS && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1179. {
  1180. const uint32_t nextProgramId(ctrlEvent.param);
  1181. for (k=0; k < pData->midiprog.count; ++k)
  1182. {
  1183. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1184. {
  1185. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1186. if (fHandle2 != nullptr)
  1187. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1188. fCurMidiProgs[event.channel] = k;
  1189. if (event.channel == pData->ctrlChannel)
  1190. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. break;
  1196. case kEngineControlEventTypeAllSoundOff:
  1197. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1198. {
  1199. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1200. continue;
  1201. fMidiEvents[fMidiEventCount].port = 0;
  1202. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1203. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1204. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1205. fMidiEvents[fMidiEventCount].data[2] = 0;
  1206. fMidiEvents[fMidiEventCount].size = 3;
  1207. fMidiEventCount += 1;
  1208. }
  1209. break;
  1210. case kEngineControlEventTypeAllNotesOff:
  1211. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1212. {
  1213. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1214. {
  1215. allNotesOffSent = true;
  1216. sendMidiAllNotesOffToCallback();
  1217. }
  1218. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1219. continue;
  1220. fMidiEvents[fMidiEventCount].port = 0;
  1221. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1222. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1223. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1224. fMidiEvents[fMidiEventCount].data[2] = 0;
  1225. fMidiEvents[fMidiEventCount].size = 3;
  1226. fMidiEventCount += 1;
  1227. }
  1228. break;
  1229. }
  1230. break;
  1231. }
  1232. case kEngineEventTypeMidi:
  1233. {
  1234. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1235. continue;
  1236. const EngineMidiEvent& midiEvent(event.midi);
  1237. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1238. uint8_t channel = event.channel;
  1239. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1240. continue;
  1241. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1242. continue;
  1243. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1244. continue;
  1245. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1246. continue;
  1247. // Fix bad note-off
  1248. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1249. status -= 0x10;
  1250. fMidiEvents[fMidiEventCount].port = 0;
  1251. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1252. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1253. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1254. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1255. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1256. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1257. fMidiEventCount += 1;
  1258. if (status == MIDI_STATUS_NOTE_ON)
  1259. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1260. else if (status == MIDI_STATUS_NOTE_OFF)
  1261. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. pData->postRtEvents.trySplice();
  1267. if (frames > timeOffset)
  1268. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1269. } // End of Event Input and Processing
  1270. // --------------------------------------------------------------------------------------------------------
  1271. // Plugin processing (no events)
  1272. else
  1273. {
  1274. processSingle(inBuffer, outBuffer, frames, 0);
  1275. } // End of Plugin processing (no events)
  1276. CARLA_PROCESS_CONTINUE_CHECK;
  1277. // --------------------------------------------------------------------------------------------------------
  1278. // Control and MIDI Output
  1279. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1280. {
  1281. float value, curValue;
  1282. for (k=0; k < pData->param.count; ++k)
  1283. {
  1284. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1285. continue;
  1286. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1287. pData->param.ranges[k].fixValue(curValue);
  1288. if (pData->param.data[k].midiCC > 0)
  1289. {
  1290. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1291. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1292. }
  1293. }
  1294. // reverse lookup MIDI events
  1295. for (k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1296. {
  1297. if (fMidiEvents[k].data[0] == 0)
  1298. break;
  1299. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1300. const uint8_t port = fMidiEvents[k].port;
  1301. if (pData->event.portOut != nullptr)
  1302. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1303. else if (port < fMidiOut.count)
  1304. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1305. }
  1306. } // End of Control and MIDI Output
  1307. }
  1308. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1309. {
  1310. CARLA_ASSERT(frames > 0);
  1311. if (frames == 0)
  1312. return false;
  1313. if (pData->audioIn.count > 0)
  1314. {
  1315. CARLA_ASSERT(inBuffer != nullptr);
  1316. if (inBuffer == nullptr)
  1317. return false;
  1318. }
  1319. if (pData->audioOut.count > 0)
  1320. {
  1321. CARLA_ASSERT(outBuffer != nullptr);
  1322. if (outBuffer == nullptr)
  1323. return false;
  1324. }
  1325. uint32_t i, k;
  1326. // --------------------------------------------------------------------------------------------------------
  1327. // Try lock, silence otherwise
  1328. if (pData->engine->isOffline())
  1329. {
  1330. pData->singleMutex.lock();
  1331. }
  1332. else if (! pData->singleMutex.tryLock())
  1333. {
  1334. for (i=0; i < pData->audioOut.count; ++i)
  1335. {
  1336. for (k=0; k < frames; ++k)
  1337. outBuffer[i][k+timeOffset] = 0.0f;
  1338. }
  1339. return false;
  1340. }
  1341. // --------------------------------------------------------------------------------------------------------
  1342. // Reset audio buffers
  1343. for (i=0; i < pData->audioIn.count; ++i)
  1344. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1345. for (i=0; i < pData->audioOut.count; ++i)
  1346. FloatVectorOperations::clear(fAudioOutBuffers[i], frames);
  1347. // --------------------------------------------------------------------------------------------------------
  1348. // Run plugin
  1349. fIsProcessing = true;
  1350. if (fHandle2 == nullptr)
  1351. {
  1352. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1353. }
  1354. else
  1355. {
  1356. fDescriptor->process(fHandle,
  1357. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1358. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1359. frames, fMidiEvents, fMidiEventCount);
  1360. fDescriptor->process(fHandle2,
  1361. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1362. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1363. frames, fMidiEvents, fMidiEventCount);
  1364. }
  1365. fIsProcessing = false;
  1366. fTimeInfo.frame += frames;
  1367. #ifndef BUILD_BRIDGE
  1368. // --------------------------------------------------------------------------------------------------------
  1369. // Post-processing (dry/wet, volume and balance)
  1370. {
  1371. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1372. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1373. bool isPair;
  1374. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1375. for (i=0; i < pData->audioOut.count; ++i)
  1376. {
  1377. // Dry/Wet
  1378. if (doDryWet)
  1379. {
  1380. for (k=0; k < frames; ++k)
  1381. {
  1382. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1383. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1384. }
  1385. }
  1386. // Balance
  1387. if (doBalance)
  1388. {
  1389. isPair = (i % 2 == 0);
  1390. if (isPair)
  1391. {
  1392. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1393. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], frames);
  1394. }
  1395. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1396. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1397. for (k=0; k < frames; ++k)
  1398. {
  1399. if (isPair)
  1400. {
  1401. // left
  1402. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1403. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1404. }
  1405. else
  1406. {
  1407. // right
  1408. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1409. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1410. }
  1411. }
  1412. }
  1413. // Volume (and buffer copy)
  1414. {
  1415. for (k=0; k < frames; ++k)
  1416. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1417. }
  1418. }
  1419. } // End of Post-processing
  1420. #else
  1421. for (i=0; i < pData->audioOut.count; ++i)
  1422. {
  1423. for (k=0; k < frames; ++k)
  1424. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1425. }
  1426. #endif
  1427. // --------------------------------------------------------------------------------------------------------
  1428. pData->singleMutex.unlock();
  1429. return true;
  1430. }
  1431. void bufferSizeChanged(const uint32_t newBufferSize) override
  1432. {
  1433. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1434. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1435. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1436. {
  1437. if (fAudioInBuffers[i] != nullptr)
  1438. delete[] fAudioInBuffers[i];
  1439. fAudioInBuffers[i] = new float[newBufferSize];
  1440. }
  1441. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1442. {
  1443. if (fAudioOutBuffers[i] != nullptr)
  1444. delete[] fAudioOutBuffers[i];
  1445. fAudioOutBuffers[i] = new float[newBufferSize];
  1446. }
  1447. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1448. {
  1449. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1450. if (fHandle2 != nullptr)
  1451. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1452. }
  1453. }
  1454. void sampleRateChanged(const double newSampleRate) override
  1455. {
  1456. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1457. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1458. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1459. {
  1460. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, newSampleRate);
  1461. if (fHandle2 != nullptr)
  1462. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, newSampleRate);
  1463. }
  1464. }
  1465. void offlineModeChanged(const bool isOffline) override
  1466. {
  1467. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1468. {
  1469. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1470. if (fHandle2 != nullptr)
  1471. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1472. }
  1473. }
  1474. // -------------------------------------------------------------------
  1475. // Plugin buffers
  1476. void initBuffers() override
  1477. {
  1478. fMidiIn.initBuffers();
  1479. fMidiOut.initBuffers();
  1480. CarlaPlugin::initBuffers();
  1481. }
  1482. void clearBuffers() override
  1483. {
  1484. carla_debug("NativePlugin::clearBuffers() - start");
  1485. if (fAudioInBuffers != nullptr)
  1486. {
  1487. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1488. {
  1489. if (fAudioInBuffers[i] != nullptr)
  1490. {
  1491. delete[] fAudioInBuffers[i];
  1492. fAudioInBuffers[i] = nullptr;
  1493. }
  1494. }
  1495. delete[] fAudioInBuffers;
  1496. fAudioInBuffers = nullptr;
  1497. }
  1498. if (fAudioOutBuffers != nullptr)
  1499. {
  1500. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1501. {
  1502. if (fAudioOutBuffers[i] != nullptr)
  1503. {
  1504. delete[] fAudioOutBuffers[i];
  1505. fAudioOutBuffers[i] = nullptr;
  1506. }
  1507. }
  1508. delete[] fAudioOutBuffers;
  1509. fAudioOutBuffers = nullptr;
  1510. }
  1511. fMidiIn.clear();
  1512. fMidiOut.clear();
  1513. CarlaPlugin::clearBuffers();
  1514. carla_debug("NativePlugin::clearBuffers() - end");
  1515. }
  1516. // -------------------------------------------------------------------
  1517. // Post-poned UI Stuff
  1518. void uiParameterChange(const uint32_t index, const float value) override
  1519. {
  1520. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1521. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1522. CARLA_ASSERT(index < pData->param.count);
  1523. if (! fIsUiVisible)
  1524. return;
  1525. if (fDescriptor == nullptr || fHandle == nullptr)
  1526. return;
  1527. if (index >= pData->param.count)
  1528. return;
  1529. if (fDescriptor->ui_set_parameter_value != nullptr)
  1530. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1531. }
  1532. void uiMidiProgramChange(const uint32_t index) override
  1533. {
  1534. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1535. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1536. CARLA_ASSERT(index < pData->midiprog.count);
  1537. if (! fIsUiVisible)
  1538. return;
  1539. if (fDescriptor == nullptr || fHandle == nullptr)
  1540. return;
  1541. if (index >= pData->midiprog.count)
  1542. return;
  1543. if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1544. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1545. }
  1546. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1547. {
  1548. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1549. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1550. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1551. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1552. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1553. if (! fIsUiVisible)
  1554. return;
  1555. if (fDescriptor == nullptr || fHandle == nullptr)
  1556. return;
  1557. if (channel >= MAX_MIDI_CHANNELS)
  1558. return;
  1559. if (note >= MAX_MIDI_NOTE)
  1560. return;
  1561. if (velo >= MAX_MIDI_VALUE)
  1562. return;
  1563. // TODO
  1564. }
  1565. void uiNoteOff(const uint8_t channel, const uint8_t note) 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. if (! fIsUiVisible)
  1572. return;
  1573. if (fDescriptor == nullptr || fHandle == nullptr)
  1574. return;
  1575. if (channel >= MAX_MIDI_CHANNELS)
  1576. return;
  1577. if (note >= MAX_MIDI_NOTE)
  1578. return;
  1579. // TODO
  1580. }
  1581. // -------------------------------------------------------------------
  1582. protected:
  1583. uint32_t handleGetBufferSize() const
  1584. {
  1585. return pData->engine->getBufferSize();
  1586. }
  1587. double handleGetSampleRate() const
  1588. {
  1589. return pData->engine->getSampleRate();
  1590. }
  1591. bool handleIsOffline() const
  1592. {
  1593. return pData->engine->isOffline();
  1594. }
  1595. const ::TimeInfo* handleGetTimeInfo() const
  1596. {
  1597. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1598. return &fTimeInfo;
  1599. }
  1600. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1601. {
  1602. CARLA_ASSERT(fEnabled);
  1603. CARLA_ASSERT(fIsProcessing);
  1604. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1605. CARLA_ASSERT(event != nullptr);
  1606. CARLA_ASSERT(event->data[0] != 0);
  1607. if (! fEnabled)
  1608. return false;
  1609. if (fMidiOut.count == 0)
  1610. return false;
  1611. if (event == nullptr)
  1612. return false;
  1613. if (event->data[0] == 0)
  1614. return false;
  1615. if (! fIsProcessing)
  1616. {
  1617. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1618. return false;
  1619. }
  1620. // reverse-find first free event, and put it there
  1621. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1622. {
  1623. if (fMidiEvents[i].data[0] == 0)
  1624. {
  1625. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1626. return true;
  1627. }
  1628. }
  1629. return false;
  1630. }
  1631. void handleUiParameterChanged(const uint32_t index, const float value)
  1632. {
  1633. setParameterValue(index, value, false, true, true);
  1634. }
  1635. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1636. {
  1637. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1638. }
  1639. void handleUiClosed()
  1640. {
  1641. pData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1642. fIsUiVisible = false;
  1643. }
  1644. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1645. {
  1646. static CarlaString retStr;
  1647. #if 0
  1648. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1649. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1650. #endif
  1651. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1652. }
  1653. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1654. {
  1655. static CarlaString retStr;
  1656. #if 0
  1657. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1658. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1659. #endif
  1660. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1661. }
  1662. intptr_t handleDispatcher(const ::HostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1663. {
  1664. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1665. intptr_t ret = 0;
  1666. switch (opcode)
  1667. {
  1668. case ::HOST_OPCODE_NULL:
  1669. break;
  1670. #ifdef BUILD_BRIDGE
  1671. case ::HOST_OPCODE_SET_VOLUME:
  1672. case ::HOST_OPCODE_SET_DRYWET:
  1673. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1674. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1675. case ::HOST_OPCODE_SET_PANNING:
  1676. break;
  1677. #else
  1678. case ::HOST_OPCODE_SET_VOLUME:
  1679. setVolume(opt, true, true);
  1680. break;
  1681. case ::HOST_OPCODE_SET_DRYWET:
  1682. setDryWet(opt, true, true);
  1683. break;
  1684. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1685. setBalanceLeft(opt, true, true);
  1686. break;
  1687. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1688. setBalanceRight(opt, true, true);
  1689. break;
  1690. case ::HOST_OPCODE_SET_PANNING:
  1691. setPanning(opt, true, true);
  1692. break;
  1693. #endif
  1694. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1695. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1696. // TODO
  1697. break;
  1698. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1699. // TODO
  1700. break;
  1701. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1702. break;
  1703. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1704. break;
  1705. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1706. break;
  1707. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1708. break;
  1709. case ::HOST_OPCODE_RELOAD_ALL:
  1710. break;
  1711. case HOST_OPCODE_UI_UNAVAILABLE:
  1712. pData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  1713. break;
  1714. }
  1715. return ret;
  1716. // unused for now
  1717. (void)index;
  1718. (void)value;
  1719. (void)ptr;
  1720. }
  1721. // -------------------------------------------------------------------
  1722. public:
  1723. static size_t getPluginCount()
  1724. {
  1725. return sPluginDescriptors.count();
  1726. }
  1727. static const ::PluginDescriptor* getPluginDescriptor(const size_t index)
  1728. {
  1729. CARLA_ASSERT(index < sPluginDescriptors.count());
  1730. if (index < sPluginDescriptors.count())
  1731. return sPluginDescriptors.getAt(index);
  1732. return nullptr;
  1733. }
  1734. static void registerPlugin(const ::PluginDescriptor* desc)
  1735. {
  1736. sPluginDescriptors.append(desc);
  1737. }
  1738. // -------------------------------------------------------------------
  1739. bool init(const char* const name, const char* const label)
  1740. {
  1741. // ---------------------------------------------------------------
  1742. // first checks
  1743. if (pData->engine == nullptr)
  1744. {
  1745. return false;
  1746. }
  1747. if (pData->client != nullptr)
  1748. {
  1749. pData->engine->setLastError("Plugin client is already registered");
  1750. return false;
  1751. }
  1752. if (label == nullptr && label[0] != '\0')
  1753. {
  1754. pData->engine->setLastError("null label");
  1755. return false;
  1756. }
  1757. // ---------------------------------------------------------------
  1758. // get descriptor that matches label
  1759. for (NonRtList<const ::PluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1760. {
  1761. fDescriptor = *it;
  1762. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1763. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1764. break;
  1765. fDescriptor = nullptr;
  1766. }
  1767. if (fDescriptor == nullptr)
  1768. {
  1769. pData->engine->setLastError("Invalid internal plugin");
  1770. return false;
  1771. }
  1772. // ---------------------------------------------------------------
  1773. // set icon
  1774. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1775. fIconName = "file";
  1776. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1777. fIconName = "file";
  1778. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1779. fIconName = "file";
  1780. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1781. fIconName = "distrho";
  1782. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1783. fIconName = "distrho";
  1784. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1785. fIconName = "distrho";
  1786. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1787. fIconName = "distrho";
  1788. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1789. fIconName = "distrho";
  1790. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1791. fIconName = "distrho";
  1792. // ---------------------------------------------------------------
  1793. // get info
  1794. if (name != nullptr)
  1795. fName = pData->engine->getUniquePluginName(name);
  1796. else if (fDescriptor->name != nullptr)
  1797. fName = pData->engine->getUniquePluginName(fDescriptor->name);
  1798. else
  1799. fName = pData->engine->getUniquePluginName(label);
  1800. {
  1801. CARLA_ASSERT(fHost.uiName == nullptr);
  1802. char uiName[fName.length()+6+1];
  1803. std::strcpy(uiName, (const char*)fName);
  1804. std::strcat(uiName, " (GUI)");
  1805. fHost.uiName = carla_strdup(uiName);
  1806. }
  1807. // ---------------------------------------------------------------
  1808. // register client
  1809. pData->client = pData->engine->addClient(this);
  1810. if (pData->client == nullptr || ! pData->client->isOk())
  1811. {
  1812. pData->engine->setLastError("Failed to register plugin client");
  1813. return false;
  1814. }
  1815. // ---------------------------------------------------------------
  1816. // initialize plugin
  1817. fHandle = fDescriptor->instantiate(&fHost);
  1818. if (fHandle == nullptr)
  1819. {
  1820. pData->engine->setLastError("Plugin failed to initialize");
  1821. return false;
  1822. }
  1823. // ---------------------------------------------------------------
  1824. // load plugin settings
  1825. {
  1826. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1827. // set default options
  1828. fOptions = 0x0;
  1829. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1830. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1831. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1832. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1833. if (pData->engine->getOptions().forceStereo)
  1834. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1835. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1836. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1837. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1838. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1839. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1840. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1841. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1842. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1843. // load settings
  1844. pData->idStr = "Native/";
  1845. pData->idStr += label;
  1846. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1847. // ignore settings, we need this anyway
  1848. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1849. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1850. }
  1851. return true;
  1852. }
  1853. class ScopedInitializer
  1854. {
  1855. public:
  1856. ScopedInitializer()
  1857. {
  1858. carla_register_all_plugins();
  1859. }
  1860. ~ScopedInitializer()
  1861. {
  1862. sPluginDescriptors.clear();
  1863. }
  1864. };
  1865. private:
  1866. ::PluginHandle fHandle;
  1867. ::PluginHandle fHandle2;
  1868. ::HostDescriptor fHost;
  1869. const ::PluginDescriptor* fDescriptor;
  1870. bool fIsProcessing;
  1871. bool fIsUiVisible;
  1872. float** fAudioInBuffers;
  1873. float** fAudioOutBuffers;
  1874. uint32_t fMidiEventCount;
  1875. ::MidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1876. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1877. NativePluginMidiData fMidiIn;
  1878. NativePluginMidiData fMidiOut;
  1879. ::TimeInfo fTimeInfo;
  1880. static NonRtList<const ::PluginDescriptor*> sPluginDescriptors;
  1881. // -------------------------------------------------------------------
  1882. #define handlePtr ((NativePlugin*)handle)
  1883. static uint32_t carla_host_get_buffer_size(::HostHandle handle)
  1884. {
  1885. return handlePtr->handleGetBufferSize();
  1886. }
  1887. static double carla_host_get_sample_rate(::HostHandle handle)
  1888. {
  1889. return handlePtr->handleGetSampleRate();
  1890. }
  1891. static bool carla_host_is_offline(::HostHandle handle)
  1892. {
  1893. return handlePtr->handleIsOffline();
  1894. }
  1895. static const ::TimeInfo* carla_host_get_time_info(::HostHandle handle)
  1896. {
  1897. return handlePtr->handleGetTimeInfo();
  1898. }
  1899. static bool carla_host_write_midi_event(::HostHandle handle, const ::MidiEvent* event)
  1900. {
  1901. return handlePtr->handleWriteMidiEvent(event);
  1902. }
  1903. static void carla_host_ui_parameter_changed(::HostHandle handle, uint32_t index, float value)
  1904. {
  1905. handlePtr->handleUiParameterChanged(index, value);
  1906. }
  1907. static void carla_host_ui_custom_data_changed(::HostHandle handle, const char* key, const char* value)
  1908. {
  1909. handlePtr->handleUiCustomDataChanged(key, value);
  1910. }
  1911. static void carla_host_ui_closed(::HostHandle handle)
  1912. {
  1913. handlePtr->handleUiClosed();
  1914. }
  1915. static const char* carla_host_ui_open_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  1916. {
  1917. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1918. }
  1919. static const char* carla_host_ui_save_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  1920. {
  1921. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1922. }
  1923. static intptr_t carla_host_dispatcher(::HostHandle handle, ::HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1924. {
  1925. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1926. }
  1927. #undef handlePtr
  1928. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1929. };
  1930. NonRtList<const ::PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1931. static const NativePlugin::ScopedInitializer _si;
  1932. CARLA_BACKEND_END_NAMESPACE
  1933. void carla_register_native_plugin(const PluginDescriptor* desc)
  1934. {
  1935. CARLA_BACKEND_USE_NAMESPACE
  1936. NativePlugin::registerPlugin(desc);
  1937. }
  1938. #else // WANT_NATIVE
  1939. # warning Building without Internal plugin support
  1940. #endif
  1941. CARLA_BACKEND_START_NAMESPACE
  1942. // -----------------------------------------------------------------------
  1943. #ifdef WANT_NATIVE
  1944. size_t CarlaPlugin::getNativePluginCount()
  1945. {
  1946. return NativePlugin::getPluginCount();
  1947. }
  1948. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1949. {
  1950. return NativePlugin::getPluginDescriptor(index);
  1951. }
  1952. #endif
  1953. // -----------------------------------------------------------------------
  1954. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1955. {
  1956. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1957. #ifdef WANT_NATIVE
  1958. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1959. if (! plugin->init(init.name, init.label))
  1960. {
  1961. delete plugin;
  1962. return nullptr;
  1963. }
  1964. plugin->reload();
  1965. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1966. {
  1967. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1968. delete plugin;
  1969. return nullptr;
  1970. }
  1971. return plugin;
  1972. #else
  1973. init.engine->setLastError("Internal plugins support not available");
  1974. return nullptr;
  1975. #endif
  1976. }
  1977. CARLA_BACKEND_END_NAMESPACE