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.

2538 lines
84KB

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