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.

2564 lines
86KB

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