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.

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