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.

2476 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. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  332. }
  333. carla_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 Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  344. {
  345. const ParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  346. if (scalePoint->label != nullptr)
  347. {
  348. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  349. return;
  350. }
  351. carla_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  352. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  353. }
  354. carla_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_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_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_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_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_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(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 showGui(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 (NonRtList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  517. {
  518. const CustomData& cData(*it);
  519. if (std::strcmp(cData.type, CUSTOM_DATA_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 idleGui() 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. }
  544. // -------------------------------------------------------------------
  545. // Plugin state
  546. void reload() override
  547. {
  548. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  549. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  550. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  551. carla_debug("NativePlugin::reload() - start");
  552. const ProcessMode processMode(pData->engine->getProccessMode());
  553. // Safely disable plugin for reload
  554. const ScopedDisabler sd(this);
  555. if (pData->active)
  556. deactivate();
  557. clearBuffers();
  558. const float sampleRate((float)pData->engine->getSampleRate());
  559. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  560. bool forcedStereoIn, forcedStereoOut;
  561. forcedStereoIn = forcedStereoOut = false;
  562. bool needsCtrlIn, needsCtrlOut;
  563. needsCtrlIn = needsCtrlOut = false;
  564. aIns = fDescriptor->audioIns;
  565. aOuts = fDescriptor->audioOuts;
  566. mIns = fDescriptor->midiIns;
  567. mOuts = fDescriptor->midiOuts;
  568. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  569. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  570. {
  571. if (fHandle2 == nullptr)
  572. fHandle2 = fDescriptor->instantiate(&fHost);
  573. if (fHandle2 != nullptr)
  574. {
  575. if (aIns == 1)
  576. {
  577. aIns = 2;
  578. forcedStereoIn = true;
  579. }
  580. if (aOuts == 1)
  581. {
  582. aOuts = 2;
  583. forcedStereoOut = true;
  584. }
  585. }
  586. }
  587. if (aIns > 0)
  588. {
  589. pData->audioIn.createNew(aIns);
  590. fAudioInBuffers = new float*[aIns];
  591. for (uint32_t i=0; i < aIns; ++i)
  592. fAudioInBuffers[i] = nullptr;
  593. }
  594. if (aOuts > 0)
  595. {
  596. pData->audioOut.createNew(aOuts);
  597. fAudioOutBuffers = new float*[aOuts];
  598. needsCtrlIn = true;
  599. for (uint32_t i=0; i < aOuts; ++i)
  600. fAudioOutBuffers[i] = nullptr;
  601. }
  602. if (mIns > 0)
  603. {
  604. fMidiIn.createNew(mIns);
  605. needsCtrlIn = (mIns == 1);
  606. }
  607. if (mOuts > 0)
  608. {
  609. fMidiOut.createNew(mOuts);
  610. needsCtrlOut = (mOuts == 1);
  611. }
  612. if (params > 0)
  613. {
  614. pData->param.createNew(params);
  615. }
  616. const uint portNameSize(pData->engine->getMaxPortNameSize());
  617. CarlaString portName;
  618. // Audio Ins
  619. for (j=0; j < aIns; ++j)
  620. {
  621. portName.clear();
  622. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  623. {
  624. portName = fName;
  625. portName += ":";
  626. }
  627. if (aIns > 1 && ! forcedStereoIn)
  628. {
  629. portName += "input_";
  630. portName += CarlaString(j+1);
  631. }
  632. else
  633. portName += "input";
  634. portName.truncate(portNameSize);
  635. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  636. pData->audioIn.ports[j].rindex = j;
  637. if (forcedStereoIn)
  638. {
  639. portName += "_2";
  640. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  641. pData->audioIn.ports[1].rindex = j;
  642. break;
  643. }
  644. }
  645. // Audio Outs
  646. for (j=0; j < aOuts; ++j)
  647. {
  648. portName.clear();
  649. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  650. {
  651. portName = fName;
  652. portName += ":";
  653. }
  654. if (aOuts > 1 && ! forcedStereoOut)
  655. {
  656. portName += "output_";
  657. portName += CarlaString(j+1);
  658. }
  659. else
  660. portName += "output";
  661. portName.truncate(portNameSize);
  662. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  663. pData->audioOut.ports[j].rindex = j;
  664. if (forcedStereoOut)
  665. {
  666. portName += "_2";
  667. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  668. pData->audioOut.ports[1].rindex = j;
  669. break;
  670. }
  671. }
  672. // MIDI Input (only if multiple)
  673. if (mIns > 1)
  674. {
  675. for (j=0; j < mIns; ++j)
  676. {
  677. portName.clear();
  678. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  679. {
  680. portName = fName;
  681. portName += ":";
  682. }
  683. portName += "midi-in_";
  684. portName += CarlaString(j+1);
  685. portName.truncate(portNameSize);
  686. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  687. fMidiIn.indexes[j] = j;
  688. }
  689. }
  690. // MIDI Output (only if multiple)
  691. if (mOuts > 1)
  692. {
  693. for (j=0; j < mOuts; ++j)
  694. {
  695. portName.clear();
  696. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  697. {
  698. portName = fName;
  699. portName += ":";
  700. }
  701. portName += "midi-out_";
  702. portName += CarlaString(j+1);
  703. portName.truncate(portNameSize);
  704. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  705. fMidiOut.indexes[j] = j;
  706. }
  707. }
  708. for (j=0; j < params; ++j)
  709. {
  710. const ::Parameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  711. CARLA_ASSERT(paramInfo != nullptr);
  712. if (paramInfo == nullptr)
  713. continue;
  714. pData->param.data[j].index = j;
  715. pData->param.data[j].rindex = j;
  716. pData->param.data[j].hints = 0x0;
  717. pData->param.data[j].midiChannel = 0;
  718. pData->param.data[j].midiCC = -1;
  719. float min, max, def, step, stepSmall, stepLarge;
  720. // min value
  721. min = paramInfo->ranges.min;
  722. // max value
  723. max = paramInfo->ranges.max;
  724. if (min > max)
  725. max = min;
  726. else if (max < min)
  727. min = max;
  728. if (max - min == 0.0f)
  729. {
  730. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  731. max = min + 0.1f;
  732. }
  733. // default value
  734. def = paramInfo->ranges.def;
  735. if (def < min)
  736. def = min;
  737. else if (def > max)
  738. def = max;
  739. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  740. {
  741. min *= sampleRate;
  742. max *= sampleRate;
  743. def *= sampleRate;
  744. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  745. }
  746. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  747. {
  748. step = max - min;
  749. stepSmall = step;
  750. stepLarge = step;
  751. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  752. }
  753. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  754. {
  755. step = 1.0f;
  756. stepSmall = 1.0f;
  757. stepLarge = 10.0f;
  758. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  759. }
  760. else
  761. {
  762. float range = max - min;
  763. step = range/100.0f;
  764. stepSmall = range/1000.0f;
  765. stepLarge = range/10.0f;
  766. }
  767. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  768. {
  769. pData->param.data[j].type = PARAMETER_OUTPUT;
  770. needsCtrlOut = true;
  771. }
  772. else
  773. {
  774. pData->param.data[j].type = PARAMETER_INPUT;
  775. needsCtrlIn = true;
  776. }
  777. // extra parameter hints
  778. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  779. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  780. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  781. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  782. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  783. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  784. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  785. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  786. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  787. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  788. pData->param.ranges[j].min = min;
  789. pData->param.ranges[j].max = max;
  790. pData->param.ranges[j].def = def;
  791. pData->param.ranges[j].step = step;
  792. pData->param.ranges[j].stepSmall = stepSmall;
  793. pData->param.ranges[j].stepLarge = stepLarge;
  794. }
  795. if (needsCtrlIn)
  796. {
  797. portName.clear();
  798. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  799. {
  800. portName = fName;
  801. portName += ":";
  802. }
  803. portName += "events-in";
  804. portName.truncate(portNameSize);
  805. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  806. }
  807. if (needsCtrlOut)
  808. {
  809. portName.clear();
  810. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  811. {
  812. portName = fName;
  813. portName += ":";
  814. }
  815. portName += "events-out";
  816. portName.truncate(portNameSize);
  817. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  818. }
  819. if (forcedStereoIn || forcedStereoOut)
  820. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  821. else
  822. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  823. // plugin hints
  824. fHints = 0x0;
  825. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  826. fHints |= PLUGIN_CAN_DRYWET;
  827. if (aOuts > 0)
  828. fHints |= PLUGIN_CAN_VOLUME;
  829. if (aOuts >= 2 && aOuts % 2 == 0)
  830. fHints |= PLUGIN_CAN_BALANCE;
  831. // native plugin hints
  832. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  833. fHints |= PLUGIN_IS_RTSAFE;
  834. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  835. fHints |= PLUGIN_HAS_GUI;
  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 ::MidiProgram* 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(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. FloatVectorOperations::clear(outBuffer[i], frames);
  961. return;
  962. }
  963. fMidiEventCount = 0;
  964. carla_zeroStruct< ::MidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  965. // --------------------------------------------------------------------------------------------------------
  966. // Check if needs reset
  967. if (pData->needsReset)
  968. {
  969. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  970. {
  971. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  972. {
  973. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  974. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  975. fMidiEvents[k].data[2] = 0;
  976. fMidiEvents[k].size = 3;
  977. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  978. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  979. fMidiEvents[k+i].data[2] = 0;
  980. fMidiEvents[k+i].size = 3;
  981. }
  982. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  983. }
  984. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  985. {
  986. for (k=0; k < MAX_MIDI_NOTE; ++k)
  987. {
  988. fMidiEvents[k].data[0] = MIDI_STATUS_NOTE_OFF + pData->ctrlChannel;
  989. fMidiEvents[k].data[1] = k;
  990. fMidiEvents[k].data[2] = 0;
  991. fMidiEvents[k].size = 3;
  992. }
  993. fMidiEventCount = MAX_MIDI_NOTE;
  994. }
  995. pData->needsReset = false;
  996. }
  997. CARLA_PROCESS_CONTINUE_CHECK;
  998. // --------------------------------------------------------------------------------------------------------
  999. // Set TimeInfo
  1000. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1001. fTimeInfo.playing = timeInfo.playing;
  1002. fTimeInfo.frame = timeInfo.frame;
  1003. fTimeInfo.usecs = timeInfo.usecs;
  1004. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1005. {
  1006. fTimeInfo.bbt.valid = true;
  1007. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1008. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1009. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1010. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1011. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1012. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1013. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1014. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1015. }
  1016. else
  1017. fTimeInfo.bbt.valid = false;
  1018. CARLA_PROCESS_CONTINUE_CHECK;
  1019. // --------------------------------------------------------------------------------------------------------
  1020. // Event Input and Processing
  1021. if (pData->event.portIn != nullptr)
  1022. {
  1023. // ----------------------------------------------------------------------------------------------------
  1024. // MIDI Input (External)
  1025. if (pData->extNotes.mutex.tryLock())
  1026. {
  1027. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1028. {
  1029. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  1030. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1031. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1032. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  1033. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1034. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1035. fMidiEvents[fMidiEventCount].size = 3;
  1036. fMidiEventCount += 1;
  1037. }
  1038. pData->extNotes.mutex.unlock();
  1039. } // End of MIDI Input (External)
  1040. // ----------------------------------------------------------------------------------------------------
  1041. // Event Input (System)
  1042. bool allNotesOffSent = false;
  1043. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1044. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1045. uint32_t startTime = 0;
  1046. uint32_t timeOffset = 0;
  1047. uint32_t nextBankId = 0;
  1048. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1049. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1050. for (i=0; i < nEvents; ++i)
  1051. {
  1052. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1053. time = event.time;
  1054. if (time >= frames)
  1055. continue;
  1056. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1057. if (time > timeOffset && sampleAccurate)
  1058. {
  1059. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1060. {
  1061. startTime = 0;
  1062. timeOffset = time;
  1063. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1064. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1065. else
  1066. nextBankId = 0;
  1067. if (fMidiEventCount > 0)
  1068. {
  1069. carla_zeroStruct< ::MidiEvent>(fMidiEvents, fMidiEventCount);
  1070. fMidiEventCount = 0;
  1071. }
  1072. }
  1073. else
  1074. startTime += timeOffset;
  1075. }
  1076. // Control change
  1077. switch (event.type)
  1078. {
  1079. case kEngineEventTypeNull:
  1080. break;
  1081. case kEngineEventTypeControl:
  1082. {
  1083. const EngineControlEvent& ctrlEvent = event.ctrl;
  1084. switch (ctrlEvent.type)
  1085. {
  1086. case kEngineControlEventTypeNull:
  1087. break;
  1088. case kEngineControlEventTypeParameter:
  1089. {
  1090. #ifndef BUILD_BRIDGE
  1091. // Control backend stuff
  1092. if (event.channel == pData->ctrlChannel)
  1093. {
  1094. float value;
  1095. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1096. {
  1097. value = ctrlEvent.value;
  1098. setDryWet(value, false, false);
  1099. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1100. }
  1101. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1102. {
  1103. value = ctrlEvent.value*127.0f/100.0f;
  1104. setVolume(value, false, false);
  1105. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1106. }
  1107. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1108. {
  1109. float left, right;
  1110. value = ctrlEvent.value/0.5f - 1.0f;
  1111. if (value < 0.0f)
  1112. {
  1113. left = -1.0f;
  1114. right = (value*2.0f)+1.0f;
  1115. }
  1116. else if (value > 0.0f)
  1117. {
  1118. left = (value*2.0f)-1.0f;
  1119. right = 1.0f;
  1120. }
  1121. else
  1122. {
  1123. left = -1.0f;
  1124. right = 1.0f;
  1125. }
  1126. setBalanceLeft(left, false, false);
  1127. setBalanceRight(right, false, false);
  1128. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1129. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1130. }
  1131. }
  1132. #endif
  1133. // Control plugin parameters
  1134. for (k=0; k < pData->param.count; ++k)
  1135. {
  1136. if (pData->param.data[k].midiChannel != event.channel)
  1137. continue;
  1138. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1139. continue;
  1140. if (pData->param.data[k].type != PARAMETER_INPUT)
  1141. continue;
  1142. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1143. continue;
  1144. float value;
  1145. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1146. {
  1147. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1148. }
  1149. else
  1150. {
  1151. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1152. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1153. value = std::rint(value);
  1154. }
  1155. setParameterValue(k, value, false, false, false);
  1156. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1157. }
  1158. if ((fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1159. {
  1160. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1161. continue;
  1162. fMidiEvents[fMidiEventCount].port = 0;
  1163. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1164. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1165. fMidiEvents[fMidiEventCount].data[1] = ctrlEvent.param;
  1166. fMidiEvents[fMidiEventCount].data[2] = ctrlEvent.value*127.0f;
  1167. fMidiEvents[fMidiEventCount].size = 3;
  1168. fMidiEventCount += 1;
  1169. }
  1170. break;
  1171. }
  1172. case kEngineControlEventTypeMidiBank:
  1173. if (event.channel == pData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1174. nextBankId = ctrlEvent.param;
  1175. break;
  1176. case kEngineControlEventTypeMidiProgram:
  1177. if (event.channel < MAX_MIDI_CHANNELS && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1178. {
  1179. const uint32_t nextProgramId(ctrlEvent.param);
  1180. for (k=0; k < pData->midiprog.count; ++k)
  1181. {
  1182. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1183. {
  1184. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1185. if (fHandle2 != nullptr)
  1186. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1187. fCurMidiProgs[event.channel] = k;
  1188. if (event.channel == pData->ctrlChannel)
  1189. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. break;
  1195. case kEngineControlEventTypeAllSoundOff:
  1196. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1197. {
  1198. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1199. continue;
  1200. fMidiEvents[fMidiEventCount].port = 0;
  1201. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1202. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1203. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1204. fMidiEvents[fMidiEventCount].data[2] = 0;
  1205. fMidiEvents[fMidiEventCount].size = 3;
  1206. fMidiEventCount += 1;
  1207. }
  1208. break;
  1209. case kEngineControlEventTypeAllNotesOff:
  1210. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1211. {
  1212. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1213. {
  1214. allNotesOffSent = true;
  1215. sendMidiAllNotesOffToCallback();
  1216. }
  1217. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1218. continue;
  1219. fMidiEvents[fMidiEventCount].port = 0;
  1220. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1221. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1222. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1223. fMidiEvents[fMidiEventCount].data[2] = 0;
  1224. fMidiEvents[fMidiEventCount].size = 3;
  1225. fMidiEventCount += 1;
  1226. }
  1227. break;
  1228. }
  1229. break;
  1230. }
  1231. case kEngineEventTypeMidi:
  1232. {
  1233. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1234. continue;
  1235. const EngineMidiEvent& midiEvent(event.midi);
  1236. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1237. uint8_t channel = event.channel;
  1238. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1239. continue;
  1240. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1241. continue;
  1242. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1243. continue;
  1244. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1245. continue;
  1246. // Fix bad note-off
  1247. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1248. status -= 0x10;
  1249. fMidiEvents[fMidiEventCount].port = 0;
  1250. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1251. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1252. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1253. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1254. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1255. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1256. fMidiEventCount += 1;
  1257. if (status == MIDI_STATUS_NOTE_ON)
  1258. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1259. else if (status == MIDI_STATUS_NOTE_OFF)
  1260. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. pData->postRtEvents.trySplice();
  1266. if (frames > timeOffset)
  1267. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1268. } // End of Event Input and Processing
  1269. // --------------------------------------------------------------------------------------------------------
  1270. // Plugin processing (no events)
  1271. else
  1272. {
  1273. processSingle(inBuffer, outBuffer, frames, 0);
  1274. } // End of Plugin processing (no events)
  1275. CARLA_PROCESS_CONTINUE_CHECK;
  1276. // --------------------------------------------------------------------------------------------------------
  1277. // Control and MIDI Output
  1278. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1279. {
  1280. float value, curValue;
  1281. for (k=0; k < pData->param.count; ++k)
  1282. {
  1283. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1284. continue;
  1285. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1286. pData->param.ranges[k].fixValue(curValue);
  1287. if (pData->param.data[k].midiCC > 0)
  1288. {
  1289. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1290. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1291. }
  1292. }
  1293. // reverse lookup MIDI events
  1294. for (k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1295. {
  1296. if (fMidiEvents[k].data[0] == 0)
  1297. break;
  1298. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1299. const uint8_t port = fMidiEvents[k].port;
  1300. if (pData->event.portOut != nullptr)
  1301. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1302. else if (port < fMidiOut.count)
  1303. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1304. }
  1305. } // End of Control and MIDI Output
  1306. }
  1307. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1308. {
  1309. CARLA_ASSERT(frames > 0);
  1310. if (frames == 0)
  1311. return false;
  1312. if (pData->audioIn.count > 0)
  1313. {
  1314. CARLA_ASSERT(inBuffer != nullptr);
  1315. if (inBuffer == nullptr)
  1316. return false;
  1317. }
  1318. if (pData->audioOut.count > 0)
  1319. {
  1320. CARLA_ASSERT(outBuffer != nullptr);
  1321. if (outBuffer == nullptr)
  1322. return false;
  1323. }
  1324. uint32_t i, k;
  1325. // --------------------------------------------------------------------------------------------------------
  1326. // Try lock, silence otherwise
  1327. if (pData->engine->isOffline())
  1328. {
  1329. pData->singleMutex.lock();
  1330. }
  1331. else if (! pData->singleMutex.tryLock())
  1332. {
  1333. for (i=0; i < pData->audioOut.count; ++i)
  1334. {
  1335. for (k=0; k < frames; ++k)
  1336. outBuffer[i][k+timeOffset] = 0.0f;
  1337. }
  1338. return false;
  1339. }
  1340. // --------------------------------------------------------------------------------------------------------
  1341. // Reset audio buffers
  1342. for (i=0; i < pData->audioIn.count; ++i)
  1343. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1344. for (i=0; i < pData->audioOut.count; ++i)
  1345. FloatVectorOperations::clear(fAudioOutBuffers[i], frames);
  1346. // --------------------------------------------------------------------------------------------------------
  1347. // Run plugin
  1348. fIsProcessing = true;
  1349. if (fHandle2 == nullptr)
  1350. {
  1351. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1352. }
  1353. else
  1354. {
  1355. fDescriptor->process(fHandle,
  1356. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1357. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1358. frames, fMidiEvents, fMidiEventCount);
  1359. fDescriptor->process(fHandle2,
  1360. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1361. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1362. frames, fMidiEvents, fMidiEventCount);
  1363. }
  1364. fIsProcessing = false;
  1365. fTimeInfo.frame += frames;
  1366. #ifndef BUILD_BRIDGE
  1367. // --------------------------------------------------------------------------------------------------------
  1368. // Post-processing (dry/wet, volume and balance)
  1369. {
  1370. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1371. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1372. bool isPair;
  1373. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1374. for (i=0; i < pData->audioOut.count; ++i)
  1375. {
  1376. // Dry/Wet
  1377. if (doDryWet)
  1378. {
  1379. for (k=0; k < frames; ++k)
  1380. {
  1381. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1382. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1383. }
  1384. }
  1385. // Balance
  1386. if (doBalance)
  1387. {
  1388. isPair = (i % 2 == 0);
  1389. if (isPair)
  1390. {
  1391. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1392. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], frames);
  1393. }
  1394. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1395. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1396. for (k=0; k < frames; ++k)
  1397. {
  1398. if (isPair)
  1399. {
  1400. // left
  1401. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1402. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1403. }
  1404. else
  1405. {
  1406. // right
  1407. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1408. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1409. }
  1410. }
  1411. }
  1412. // Volume (and buffer copy)
  1413. {
  1414. for (k=0; k < frames; ++k)
  1415. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1416. }
  1417. }
  1418. } // End of Post-processing
  1419. #else
  1420. for (i=0; i < pData->audioOut.count; ++i)
  1421. {
  1422. for (k=0; k < frames; ++k)
  1423. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1424. }
  1425. #endif
  1426. // --------------------------------------------------------------------------------------------------------
  1427. pData->singleMutex.unlock();
  1428. return true;
  1429. }
  1430. void bufferSizeChanged(const uint32_t newBufferSize) override
  1431. {
  1432. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1433. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1434. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1435. {
  1436. if (fAudioInBuffers[i] != nullptr)
  1437. delete[] fAudioInBuffers[i];
  1438. fAudioInBuffers[i] = new float[newBufferSize];
  1439. }
  1440. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1441. {
  1442. if (fAudioOutBuffers[i] != nullptr)
  1443. delete[] fAudioOutBuffers[i];
  1444. fAudioOutBuffers[i] = new float[newBufferSize];
  1445. }
  1446. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1447. {
  1448. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1449. if (fHandle2 != nullptr)
  1450. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1451. }
  1452. }
  1453. void sampleRateChanged(const double newSampleRate) override
  1454. {
  1455. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1456. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1457. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1458. {
  1459. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, newSampleRate);
  1460. if (fHandle2 != nullptr)
  1461. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, newSampleRate);
  1462. }
  1463. }
  1464. void offlineModeChanged(const bool isOffline) override
  1465. {
  1466. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1467. {
  1468. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1469. if (fHandle2 != nullptr)
  1470. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1471. }
  1472. }
  1473. // -------------------------------------------------------------------
  1474. // Plugin buffers
  1475. void initBuffers() override
  1476. {
  1477. fMidiIn.initBuffers();
  1478. fMidiOut.initBuffers();
  1479. CarlaPlugin::initBuffers();
  1480. }
  1481. void clearBuffers() override
  1482. {
  1483. carla_debug("NativePlugin::clearBuffers() - start");
  1484. if (fAudioInBuffers != nullptr)
  1485. {
  1486. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1487. {
  1488. if (fAudioInBuffers[i] != nullptr)
  1489. {
  1490. delete[] fAudioInBuffers[i];
  1491. fAudioInBuffers[i] = nullptr;
  1492. }
  1493. }
  1494. delete[] fAudioInBuffers;
  1495. fAudioInBuffers = nullptr;
  1496. }
  1497. if (fAudioOutBuffers != nullptr)
  1498. {
  1499. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1500. {
  1501. if (fAudioOutBuffers[i] != nullptr)
  1502. {
  1503. delete[] fAudioOutBuffers[i];
  1504. fAudioOutBuffers[i] = nullptr;
  1505. }
  1506. }
  1507. delete[] fAudioOutBuffers;
  1508. fAudioOutBuffers = nullptr;
  1509. }
  1510. fMidiIn.clear();
  1511. fMidiOut.clear();
  1512. CarlaPlugin::clearBuffers();
  1513. carla_debug("NativePlugin::clearBuffers() - end");
  1514. }
  1515. // -------------------------------------------------------------------
  1516. // Post-poned UI Stuff
  1517. void uiParameterChange(const uint32_t index, const float value) override
  1518. {
  1519. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1520. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1521. CARLA_ASSERT(index < pData->param.count);
  1522. if (! fIsUiVisible)
  1523. return;
  1524. if (fDescriptor == nullptr || fHandle == nullptr)
  1525. return;
  1526. if (index >= pData->param.count)
  1527. return;
  1528. if (fDescriptor->ui_set_parameter_value != nullptr)
  1529. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1530. }
  1531. void uiMidiProgramChange(const uint32_t index) override
  1532. {
  1533. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1534. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1535. CARLA_ASSERT(index < pData->midiprog.count);
  1536. if (! fIsUiVisible)
  1537. return;
  1538. if (fDescriptor == nullptr || fHandle == nullptr)
  1539. return;
  1540. if (index >= pData->midiprog.count)
  1541. return;
  1542. if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1543. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1544. }
  1545. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1546. {
  1547. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1548. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1549. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1550. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1551. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1552. if (! fIsUiVisible)
  1553. return;
  1554. if (fDescriptor == nullptr || fHandle == nullptr)
  1555. return;
  1556. if (channel >= MAX_MIDI_CHANNELS)
  1557. return;
  1558. if (note >= MAX_MIDI_NOTE)
  1559. return;
  1560. if (velo >= MAX_MIDI_VALUE)
  1561. return;
  1562. // TODO
  1563. }
  1564. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1565. {
  1566. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1567. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1568. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1569. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  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. // TODO
  1579. }
  1580. // -------------------------------------------------------------------
  1581. protected:
  1582. uint32_t handleGetBufferSize() const
  1583. {
  1584. return pData->engine->getBufferSize();
  1585. }
  1586. double handleGetSampleRate() const
  1587. {
  1588. return pData->engine->getSampleRate();
  1589. }
  1590. bool handleIsOffline() const
  1591. {
  1592. return pData->engine->isOffline();
  1593. }
  1594. const ::TimeInfo* handleGetTimeInfo() const
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1597. return &fTimeInfo;
  1598. }
  1599. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1600. {
  1601. CARLA_ASSERT(fEnabled);
  1602. CARLA_ASSERT(fIsProcessing);
  1603. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1604. CARLA_ASSERT(event != nullptr);
  1605. CARLA_ASSERT(event->data[0] != 0);
  1606. if (! fEnabled)
  1607. return false;
  1608. if (fMidiOut.count == 0)
  1609. return false;
  1610. if (event == nullptr)
  1611. return false;
  1612. if (event->data[0] == 0)
  1613. return false;
  1614. if (! fIsProcessing)
  1615. {
  1616. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1617. return false;
  1618. }
  1619. // reverse-find first free event, and put it there
  1620. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1621. {
  1622. if (fMidiEvents[i].data[0] == 0)
  1623. {
  1624. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1625. return true;
  1626. }
  1627. }
  1628. return false;
  1629. }
  1630. void handleUiParameterChanged(const uint32_t index, const float value)
  1631. {
  1632. setParameterValue(index, value, false, true, true);
  1633. }
  1634. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1635. {
  1636. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1637. }
  1638. void handleUiClosed()
  1639. {
  1640. pData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1641. fIsUiVisible = false;
  1642. }
  1643. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1644. {
  1645. static CarlaString retStr;
  1646. #if 0
  1647. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1648. retStr = QFileDialog::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1649. #endif
  1650. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1651. }
  1652. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1653. {
  1654. static CarlaString retStr;
  1655. #if 0
  1656. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1657. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1658. #endif
  1659. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1660. }
  1661. intptr_t handleDispatcher(const ::HostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1662. {
  1663. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1664. intptr_t ret = 0;
  1665. switch (opcode)
  1666. {
  1667. case ::HOST_OPCODE_NULL:
  1668. break;
  1669. #ifdef BUILD_BRIDGE
  1670. case ::HOST_OPCODE_SET_VOLUME:
  1671. case ::HOST_OPCODE_SET_DRYWET:
  1672. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1673. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1674. case ::HOST_OPCODE_SET_PANNING:
  1675. break;
  1676. #else
  1677. case ::HOST_OPCODE_SET_VOLUME:
  1678. setVolume(opt, true, true);
  1679. break;
  1680. case ::HOST_OPCODE_SET_DRYWET:
  1681. setDryWet(opt, true, true);
  1682. break;
  1683. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1684. setBalanceLeft(opt, true, true);
  1685. break;
  1686. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1687. setBalanceRight(opt, true, true);
  1688. break;
  1689. case ::HOST_OPCODE_SET_PANNING:
  1690. setPanning(opt, true, true);
  1691. break;
  1692. #endif
  1693. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1694. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1695. // TODO
  1696. break;
  1697. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1698. // TODO
  1699. break;
  1700. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1701. break;
  1702. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1703. break;
  1704. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1705. break;
  1706. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1707. break;
  1708. case ::HOST_OPCODE_RELOAD_ALL:
  1709. break;
  1710. case HOST_OPCODE_UI_UNAVAILABLE:
  1711. pData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  1712. break;
  1713. }
  1714. return ret;
  1715. // unused for now
  1716. (void)index;
  1717. (void)value;
  1718. (void)ptr;
  1719. }
  1720. // -------------------------------------------------------------------
  1721. public:
  1722. static size_t getPluginCount()
  1723. {
  1724. return sPluginDescriptors.count();
  1725. }
  1726. static const ::PluginDescriptor* getPluginDescriptor(const size_t index)
  1727. {
  1728. CARLA_ASSERT(index < sPluginDescriptors.count());
  1729. if (index < sPluginDescriptors.count())
  1730. return sPluginDescriptors.getAt(index);
  1731. return nullptr;
  1732. }
  1733. static void registerPlugin(const ::PluginDescriptor* desc)
  1734. {
  1735. sPluginDescriptors.append(desc);
  1736. }
  1737. // -------------------------------------------------------------------
  1738. bool init(const char* const name, const char* const label)
  1739. {
  1740. // ---------------------------------------------------------------
  1741. // first checks
  1742. if (pData->engine == nullptr)
  1743. {
  1744. return false;
  1745. }
  1746. if (pData->client != nullptr)
  1747. {
  1748. pData->engine->setLastError("Plugin client is already registered");
  1749. return false;
  1750. }
  1751. if (label == nullptr && label[0] != '\0')
  1752. {
  1753. pData->engine->setLastError("null label");
  1754. return false;
  1755. }
  1756. // ---------------------------------------------------------------
  1757. // get descriptor that matches label
  1758. for (NonRtList<const ::PluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1759. {
  1760. fDescriptor = *it;
  1761. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1762. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1763. break;
  1764. fDescriptor = nullptr;
  1765. }
  1766. if (fDescriptor == nullptr)
  1767. {
  1768. pData->engine->setLastError("Invalid internal plugin");
  1769. return false;
  1770. }
  1771. // ---------------------------------------------------------------
  1772. // set icon
  1773. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1774. fIconName = "file";
  1775. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1776. fIconName = "file";
  1777. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1778. fIconName = "file";
  1779. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1780. fIconName = "distrho";
  1781. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1782. fIconName = "distrho";
  1783. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1784. fIconName = "distrho";
  1785. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1786. fIconName = "distrho";
  1787. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1788. fIconName = "distrho";
  1789. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1790. fIconName = "distrho";
  1791. // ---------------------------------------------------------------
  1792. // get info
  1793. if (name != nullptr)
  1794. fName = pData->engine->getUniquePluginName(name);
  1795. else if (fDescriptor->name != nullptr)
  1796. fName = pData->engine->getUniquePluginName(fDescriptor->name);
  1797. else
  1798. fName = pData->engine->getUniquePluginName(label);
  1799. {
  1800. CARLA_ASSERT(fHost.uiName == nullptr);
  1801. char uiName[fName.length()+6+1];
  1802. std::strcpy(uiName, (const char*)fName);
  1803. std::strcat(uiName, " (GUI)");
  1804. fHost.uiName = carla_strdup(uiName);
  1805. }
  1806. // ---------------------------------------------------------------
  1807. // register client
  1808. pData->client = pData->engine->addClient(this);
  1809. if (pData->client == nullptr || ! pData->client->isOk())
  1810. {
  1811. pData->engine->setLastError("Failed to register plugin client");
  1812. return false;
  1813. }
  1814. // ---------------------------------------------------------------
  1815. // initialize plugin
  1816. fHandle = fDescriptor->instantiate(&fHost);
  1817. if (fHandle == nullptr)
  1818. {
  1819. pData->engine->setLastError("Plugin failed to initialize");
  1820. return false;
  1821. }
  1822. // ---------------------------------------------------------------
  1823. // load plugin settings
  1824. {
  1825. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1826. // set default options
  1827. fOptions = 0x0;
  1828. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1829. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1830. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1831. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1832. if (pData->engine->getOptions().forceStereo)
  1833. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1834. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1835. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1836. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1837. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1838. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1839. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1840. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1841. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1842. // load settings
  1843. pData->idStr = "Native/";
  1844. pData->idStr += label;
  1845. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1846. // ignore settings, we need this anyway
  1847. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1848. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1849. }
  1850. return true;
  1851. }
  1852. class ScopedInitializer
  1853. {
  1854. public:
  1855. ScopedInitializer()
  1856. {
  1857. carla_register_all_plugins();
  1858. }
  1859. ~ScopedInitializer()
  1860. {
  1861. sPluginDescriptors.clear();
  1862. }
  1863. };
  1864. private:
  1865. ::PluginHandle fHandle;
  1866. ::PluginHandle fHandle2;
  1867. ::HostDescriptor fHost;
  1868. const ::PluginDescriptor* fDescriptor;
  1869. bool fIsProcessing;
  1870. bool fIsUiVisible;
  1871. float** fAudioInBuffers;
  1872. float** fAudioOutBuffers;
  1873. uint32_t fMidiEventCount;
  1874. ::MidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1875. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1876. NativePluginMidiData fMidiIn;
  1877. NativePluginMidiData fMidiOut;
  1878. ::TimeInfo fTimeInfo;
  1879. static NonRtList<const ::PluginDescriptor*> sPluginDescriptors;
  1880. // -------------------------------------------------------------------
  1881. #define handlePtr ((NativePlugin*)handle)
  1882. static uint32_t carla_host_get_buffer_size(::HostHandle handle)
  1883. {
  1884. return handlePtr->handleGetBufferSize();
  1885. }
  1886. static double carla_host_get_sample_rate(::HostHandle handle)
  1887. {
  1888. return handlePtr->handleGetSampleRate();
  1889. }
  1890. static bool carla_host_is_offline(::HostHandle handle)
  1891. {
  1892. return handlePtr->handleIsOffline();
  1893. }
  1894. static const ::TimeInfo* carla_host_get_time_info(::HostHandle handle)
  1895. {
  1896. return handlePtr->handleGetTimeInfo();
  1897. }
  1898. static bool carla_host_write_midi_event(::HostHandle handle, const ::MidiEvent* event)
  1899. {
  1900. return handlePtr->handleWriteMidiEvent(event);
  1901. }
  1902. static void carla_host_ui_parameter_changed(::HostHandle handle, uint32_t index, float value)
  1903. {
  1904. handlePtr->handleUiParameterChanged(index, value);
  1905. }
  1906. static void carla_host_ui_custom_data_changed(::HostHandle handle, const char* key, const char* value)
  1907. {
  1908. handlePtr->handleUiCustomDataChanged(key, value);
  1909. }
  1910. static void carla_host_ui_closed(::HostHandle handle)
  1911. {
  1912. handlePtr->handleUiClosed();
  1913. }
  1914. static const char* carla_host_ui_open_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  1915. {
  1916. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1917. }
  1918. static const char* carla_host_ui_save_file(::HostHandle handle, bool isDir, const char* title, const char* filter)
  1919. {
  1920. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1921. }
  1922. static intptr_t carla_host_dispatcher(::HostHandle handle, ::HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1923. {
  1924. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1925. }
  1926. #undef handlePtr
  1927. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1928. };
  1929. NonRtList<const ::PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1930. static const NativePlugin::ScopedInitializer _si;
  1931. CARLA_BACKEND_END_NAMESPACE
  1932. void carla_register_native_plugin(const PluginDescriptor* desc)
  1933. {
  1934. CARLA_BACKEND_USE_NAMESPACE
  1935. NativePlugin::registerPlugin(desc);
  1936. }
  1937. #else // WANT_NATIVE
  1938. # warning Building without Internal plugin support
  1939. #endif
  1940. CARLA_BACKEND_START_NAMESPACE
  1941. // -----------------------------------------------------------------------
  1942. #ifdef WANT_NATIVE
  1943. size_t CarlaPlugin::getNativePluginCount()
  1944. {
  1945. return NativePlugin::getPluginCount();
  1946. }
  1947. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1948. {
  1949. return NativePlugin::getPluginDescriptor(index);
  1950. }
  1951. #endif
  1952. // -----------------------------------------------------------------------
  1953. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1954. {
  1955. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1956. #ifdef WANT_NATIVE
  1957. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1958. if (! plugin->init(init.name, init.label))
  1959. {
  1960. delete plugin;
  1961. return nullptr;
  1962. }
  1963. plugin->reload();
  1964. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1965. {
  1966. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1967. delete plugin;
  1968. return nullptr;
  1969. }
  1970. return plugin;
  1971. #else
  1972. init.engine->setLastError("Internal plugins support not available");
  1973. return nullptr;
  1974. #endif
  1975. }
  1976. CARLA_BACKEND_END_NAMESPACE