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.

2441 lines
80KB

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