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.

2064 lines
67KB

  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 <QtGui/QFileDialog>
  21. CARLA_BACKEND_START_NAMESPACE
  22. struct NativePluginMidiData {
  23. uint32_t count;
  24. uint32_t* indexes;
  25. CarlaEngineEventPort** ports;
  26. NativePluginMidiData()
  27. : count(0),
  28. indexes(nullptr),
  29. ports(nullptr) {}
  30. void createNew(const uint32_t count)
  31. {
  32. CARLA_ASSERT(ports == nullptr);
  33. CARLA_ASSERT(indexes == nullptr);
  34. if (ports == nullptr)
  35. {
  36. ports = new CarlaEngineEventPort*[count];
  37. for (uint32_t i=0; i < count; i++)
  38. ports[i] = nullptr;
  39. }
  40. if (indexes == nullptr)
  41. {
  42. indexes = new uint32_t[count];
  43. for (uint32_t i=0; i < count; i++)
  44. indexes[i] = 0;
  45. }
  46. this->count = count;
  47. }
  48. void clear()
  49. {
  50. if (ports != nullptr)
  51. {
  52. for (uint32_t i=0; i < count; i++)
  53. {
  54. if (ports[i] != nullptr)
  55. {
  56. delete ports[i];
  57. ports[i] = nullptr;
  58. }
  59. }
  60. delete[] ports;
  61. ports = nullptr;
  62. }
  63. if (indexes != nullptr)
  64. {
  65. delete[] indexes;
  66. indexes = nullptr;
  67. }
  68. count = 0;
  69. }
  70. void initBuffers(CarlaEngine* const engine)
  71. {
  72. for (uint32_t i=0; i < count; i++)
  73. {
  74. if (ports[i] != nullptr)
  75. ports[i]->initBuffer(engine);
  76. }
  77. }
  78. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(NativePluginMidiData)
  79. };
  80. class NativePlugin : public CarlaPlugin
  81. {
  82. public:
  83. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  84. : CarlaPlugin(engine, id),
  85. fHandle(nullptr),
  86. fHandle2(nullptr),
  87. fDescriptor(nullptr),
  88. fIsProcessing(false),
  89. fIsUiVisible(false),
  90. fAudioInBuffers(nullptr),
  91. fAudioOutBuffers(nullptr),
  92. fMidiEventCount(0)
  93. {
  94. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  95. fHost.handle = this;
  96. fHost.get_buffer_size = carla_host_get_buffer_size;
  97. fHost.get_sample_rate = carla_host_get_sample_rate;
  98. fHost.get_time_info = carla_host_get_time_info;
  99. fHost.write_midi_event = carla_host_write_midi_event;
  100. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  101. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  102. fHost.ui_closed = carla_host_ui_closed;
  103. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*MAX_MIDI_EVENTS*2);
  104. }
  105. ~NativePlugin()
  106. {
  107. carla_debug("NativePlugin::~NativePlugin()");
  108. if (fDescriptor != nullptr)
  109. {
  110. if (fDescriptor->ui_show != nullptr && fIsUiVisible)
  111. fDescriptor->ui_show(fHandle, false);
  112. if (fDescriptor->deactivate != nullptr && kData->activeBefore)
  113. {
  114. if (fHandle != nullptr)
  115. fDescriptor->deactivate(fHandle);
  116. if (fHandle2 != nullptr)
  117. fDescriptor->deactivate(fHandle2);
  118. }
  119. if (fDescriptor->cleanup != nullptr)
  120. {
  121. if (fHandle != nullptr)
  122. fDescriptor->cleanup(fHandle);
  123. if (fHandle2 != nullptr)
  124. fDescriptor->cleanup(fHandle2);
  125. }
  126. fHandle = nullptr;
  127. fHandle2 = nullptr;
  128. fDescriptor = nullptr;
  129. }
  130. deleteBuffers();
  131. }
  132. // -------------------------------------------------------------------
  133. // Information (base)
  134. PluginType type() const
  135. {
  136. return PLUGIN_INTERNAL;
  137. }
  138. PluginCategory category() const
  139. {
  140. CARLA_ASSERT(fDescriptor != nullptr);
  141. if (fDescriptor != nullptr)
  142. return static_cast<PluginCategory>(fDescriptor->category);
  143. return getPluginCategoryFromName(fName);
  144. }
  145. // -------------------------------------------------------------------
  146. // Information (count)
  147. uint32_t midiInCount()
  148. {
  149. return fMidiIn.count;
  150. }
  151. uint32_t midiOutCount()
  152. {
  153. return fMidiOut.count;
  154. }
  155. uint32_t parameterScalePointCount(const uint32_t parameterId) const
  156. {
  157. CARLA_ASSERT(fDescriptor != nullptr);
  158. CARLA_ASSERT(fHandle != nullptr);
  159. CARLA_ASSERT(parameterId < kData->param.count);
  160. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  161. {
  162. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  163. return param->scalePointCount;
  164. }
  165. return 0;
  166. }
  167. // -------------------------------------------------------------------
  168. // Information (per-plugin data)
  169. unsigned int availableOptions()
  170. {
  171. CARLA_ASSERT(fDescriptor != nullptr);
  172. unsigned int options = 0x0;
  173. if (fDescriptor->name != nullptr)
  174. {
  175. if (std::strcmp(fDescriptor->name, "ZynAddSubFX") == 0)
  176. {
  177. // nothing
  178. }
  179. else
  180. {
  181. options |= PLUGIN_OPTION_FIXED_BUFFER;
  182. }
  183. }
  184. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  185. //if ((kData->audioIns.count() == 1 || kData->audioOuts.count() == 0) || (kData->audioIns.count() == 0 || kData->audioOuts.count() == 1))
  186. // options |= PLUGIN_OPTION_FORCE_STEREO;
  187. if (fMidiIn.count > 0)
  188. {
  189. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  190. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  191. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  192. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  193. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  194. }
  195. return options;
  196. }
  197. float getParameterValue(const uint32_t parameterId)
  198. {
  199. CARLA_ASSERT(fDescriptor != nullptr);
  200. CARLA_ASSERT(fHandle != nullptr);
  201. CARLA_ASSERT(parameterId < kData->param.count);
  202. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_value != nullptr)
  203. return fDescriptor->get_parameter_value(fHandle, parameterId);
  204. return 0.0f;
  205. }
  206. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  207. {
  208. CARLA_ASSERT(fDescriptor != nullptr);
  209. CARLA_ASSERT(fHandle != nullptr);
  210. CARLA_ASSERT(parameterId < kData->param.count);
  211. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  212. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  213. {
  214. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  215. {
  216. const ParameterScalePoint& scalePoint = param->scalePoints[scalePointId];
  217. return scalePoint.value;
  218. }
  219. }
  220. return 0.0f;
  221. }
  222. void getLabel(char* const strBuf)
  223. {
  224. CARLA_ASSERT(fDescriptor != nullptr);
  225. if (fDescriptor != nullptr && fDescriptor->label != nullptr)
  226. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  227. else
  228. CarlaPlugin::getLabel(strBuf);
  229. }
  230. void getMaker(char* const strBuf)
  231. {
  232. CARLA_ASSERT(fDescriptor != nullptr);
  233. if (fDescriptor != nullptr && fDescriptor->maker != nullptr)
  234. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  235. else
  236. CarlaPlugin::getMaker(strBuf);
  237. }
  238. void getCopyright(char* const strBuf)
  239. {
  240. CARLA_ASSERT(fDescriptor != nullptr);
  241. if (fDescriptor != nullptr && fDescriptor->copyright != nullptr)
  242. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  243. else
  244. CarlaPlugin::getCopyright(strBuf);
  245. }
  246. void getRealName(char* const strBuf)
  247. {
  248. CARLA_ASSERT(fDescriptor != nullptr);
  249. if (fDescriptor != nullptr && fDescriptor->name != nullptr)
  250. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  251. else
  252. CarlaPlugin::getRealName(strBuf);
  253. }
  254. void getParameterName(const uint32_t parameterId, char* const strBuf)
  255. {
  256. CARLA_ASSERT(fDescriptor != nullptr);
  257. CARLA_ASSERT(fHandle != nullptr);
  258. CARLA_ASSERT(parameterId < kData->param.count);
  259. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  260. {
  261. const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId);
  262. if (param != nullptr && param->name != nullptr)
  263. {
  264. std::strncpy(strBuf, param->name, STR_MAX);
  265. return;
  266. }
  267. }
  268. CarlaPlugin::getParameterName(parameterId, strBuf);
  269. }
  270. void getParameterText(const uint32_t parameterId, char* const strBuf)
  271. {
  272. CARLA_ASSERT(fDescriptor != nullptr);
  273. CARLA_ASSERT(fHandle != nullptr);
  274. CARLA_ASSERT(parameterId < kData->param.count);
  275. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_text != nullptr)
  276. {
  277. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId))
  278. {
  279. std::strncpy(strBuf, text, STR_MAX);
  280. return;
  281. }
  282. }
  283. CarlaPlugin::getParameterText(parameterId, strBuf);
  284. }
  285. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  286. {
  287. CARLA_ASSERT(fDescriptor != nullptr);
  288. CARLA_ASSERT(fHandle != nullptr);
  289. CARLA_ASSERT(parameterId < kData->param.count);
  290. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  291. {
  292. const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId);
  293. if (param != nullptr && param->unit != nullptr)
  294. {
  295. std::strncpy(strBuf, param->unit, STR_MAX);
  296. return;
  297. }
  298. }
  299. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  300. }
  301. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  302. {
  303. CARLA_ASSERT(fDescriptor != nullptr);
  304. CARLA_ASSERT(fHandle != nullptr);
  305. CARLA_ASSERT(parameterId < kData->param.count);
  306. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  307. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  308. {
  309. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  310. {
  311. const ParameterScalePoint& scalePoint = param->scalePoints[scalePointId];
  312. if (scalePoint.label != nullptr)
  313. {
  314. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  315. return;
  316. }
  317. }
  318. }
  319. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  320. }
  321. // -------------------------------------------------------------------
  322. // Set data (plugin-specific stuff)
  323. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  324. {
  325. CARLA_ASSERT(fDescriptor != nullptr);
  326. CARLA_ASSERT(fHandle != nullptr);
  327. CARLA_ASSERT(parameterId < kData->param.count);
  328. const float fixedValue = kData->param.fixValue(parameterId, value);
  329. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->set_parameter_value != nullptr)
  330. {
  331. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  332. if (fHandle2 != nullptr)
  333. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  334. }
  335. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  336. }
  337. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  338. {
  339. CARLA_ASSERT(fDescriptor != nullptr);
  340. CARLA_ASSERT(fHandle != nullptr);
  341. CARLA_ASSERT(type != nullptr);
  342. CARLA_ASSERT(key != nullptr);
  343. CARLA_ASSERT(value != nullptr);
  344. if (type == nullptr)
  345. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  346. if (std::strcmp(type, CUSTOM_DATA_STRING) != 0)
  347. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  348. if (key == nullptr)
  349. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  350. if (value == nullptr)
  351. return carla_stderr2("Nativelugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  352. if (fDescriptor != nullptr && fHandle != nullptr)
  353. {
  354. if (fDescriptor->set_custom_data != nullptr)
  355. {
  356. fDescriptor->set_custom_data(fHandle, key, value);
  357. if (fHandle2)
  358. fDescriptor->set_custom_data(fHandle2, key, value);
  359. }
  360. if (sendGui && fDescriptor->ui_set_custom_data != nullptr)
  361. fDescriptor->ui_set_custom_data(fHandle, key, value);
  362. }
  363. CarlaPlugin::setCustomData(type, key, value, sendGui);
  364. }
  365. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback)
  366. {
  367. CARLA_ASSERT(fDescriptor != nullptr);
  368. CARLA_ASSERT(fHandle != nullptr);
  369. CARLA_ASSERT(index >= -1 && index < static_cast<int32_t>(kData->midiprog.count));
  370. if (index < -1)
  371. index = -1;
  372. else if (index > static_cast<int32_t>(kData->midiprog.count))
  373. return;
  374. if (fDescriptor != nullptr && fHandle != nullptr && index >= 0)
  375. {
  376. const uint32_t bank = kData->midiprog.data[index].bank;
  377. const uint32_t program = kData->midiprog.data[index].program;
  378. const ScopedProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  379. fDescriptor->set_midi_program(fHandle, bank, program);
  380. if (fHandle2 != nullptr)
  381. fDescriptor->set_midi_program(fHandle2, bank, program);
  382. }
  383. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  384. }
  385. // -------------------------------------------------------------------
  386. // Set gui stuff
  387. void showGui(const bool yesNo)
  388. {
  389. CARLA_ASSERT(fDescriptor != nullptr);
  390. CARLA_ASSERT(fHandle != nullptr);
  391. if (fDescriptor != nullptr && fHandle != nullptr)
  392. {
  393. // FIXME - this be as a host-side call to request files
  394. if (fDescriptor->name != nullptr && std::strcmp(fDescriptor->label, "audiofile") == 0)
  395. {
  396. QString filenameTry = QFileDialog::getOpenFileName(nullptr, "Open Audio File");
  397. if (! filenameTry.isEmpty())
  398. {
  399. const char* const filename = filenameTry.toUtf8().constData();
  400. setCustomData(CUSTOM_DATA_STRING, "file", filename, false);
  401. }
  402. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  403. }
  404. else if (fDescriptor->ui_show != nullptr)
  405. {
  406. fDescriptor->ui_show(fHandle, yesNo);
  407. if (yesNo)
  408. {
  409. // Update UI values
  410. if (kData->midiprog.current >= 0)
  411. {
  412. const MidiProgramData& mpData = kData->midiprog.getCurrent();
  413. fDescriptor->ui_set_midi_program(fHandle, mpData.bank, mpData.program);
  414. }
  415. for (uint32_t i=0; i < kData->param.count; i++)
  416. {
  417. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  418. }
  419. }
  420. }
  421. }
  422. fIsUiVisible = yesNo;
  423. }
  424. void idleGui()
  425. {
  426. CARLA_ASSERT(fDescriptor != nullptr);
  427. CARLA_ASSERT(fHandle != nullptr);
  428. if (fIsUiVisible && fDescriptor != nullptr && fHandle != nullptr && fDescriptor->ui_idle != nullptr)
  429. fDescriptor->ui_idle(fHandle);
  430. }
  431. // -------------------------------------------------------------------
  432. // Plugin state
  433. void reload()
  434. {
  435. carla_debug("NativePlugin::reload() - start");
  436. CARLA_ASSERT(kData->engine != nullptr);
  437. CARLA_ASSERT(fDescriptor != nullptr);
  438. CARLA_ASSERT(fHandle != nullptr);
  439. const ProcessMode processMode(kData->engine->getProccessMode());
  440. // Safely disable plugin for reload
  441. const ScopedDisabler sd(this);
  442. deleteBuffers();
  443. const float sampleRate = (float)kData->engine->getSampleRate();
  444. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  445. bool forcedStereoIn, forcedStereoOut;
  446. forcedStereoIn = forcedStereoOut = false;
  447. bool needsCtrlIn, needsCtrlOut;
  448. needsCtrlIn = needsCtrlOut = false;
  449. aIns = fDescriptor->audioIns;
  450. aOuts = fDescriptor->audioOuts;
  451. mIns = fDescriptor->midiIns;
  452. mOuts = fDescriptor->midiOuts;
  453. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  454. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  455. {
  456. if (fHandle2 == nullptr)
  457. fHandle2 = fDescriptor->instantiate(fDescriptor, &fHost);
  458. if (aIns == 1)
  459. {
  460. aIns = 2;
  461. forcedStereoIn = true;
  462. }
  463. if (aOuts == 1)
  464. {
  465. aOuts = 2;
  466. forcedStereoOut = true;
  467. }
  468. }
  469. if (aIns > 0)
  470. {
  471. kData->audioIn.createNew(aIns);
  472. fAudioInBuffers = new float*[aIns];
  473. for (uint32_t i=0; i < aIns; i++)
  474. fAudioInBuffers[i] = nullptr;
  475. }
  476. if (aOuts > 0)
  477. {
  478. kData->audioOut.createNew(aOuts);
  479. fAudioOutBuffers = new float*[aOuts];
  480. needsCtrlIn = true;
  481. for (uint32_t i=0; i < aOuts; i++)
  482. fAudioOutBuffers[i] = nullptr;
  483. }
  484. if (mIns > 0)
  485. {
  486. fMidiIn.createNew(mIns);
  487. needsCtrlIn = (mIns == 1);
  488. }
  489. if (mOuts > 0)
  490. {
  491. fMidiOut.createNew(mOuts);
  492. needsCtrlOut = (mOuts == 1);
  493. }
  494. if (params > 0)
  495. {
  496. kData->param.createNew(params);
  497. }
  498. const uint portNameSize = kData->engine->maxPortNameSize();
  499. CarlaString portName;
  500. // Audio Ins
  501. for (j=0; j < aIns; j++)
  502. {
  503. portName.clear();
  504. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  505. {
  506. portName = fName;
  507. portName += ":";
  508. }
  509. if (aIns > 1)
  510. {
  511. portName += "input_";
  512. portName += CarlaString(j+1);
  513. }
  514. else
  515. portName += "input";
  516. portName.truncate(portNameSize);
  517. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  518. kData->audioIn.ports[j].rindex = j;
  519. if (forcedStereoIn)
  520. {
  521. portName += "_2";
  522. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  523. kData->audioIn.ports[1].rindex = j;
  524. break;
  525. }
  526. }
  527. // Audio Outs
  528. for (j=0; j < aOuts; j++)
  529. {
  530. portName.clear();
  531. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  532. {
  533. portName = fName;
  534. portName += ":";
  535. }
  536. if (aOuts > 1)
  537. {
  538. portName += "output_";
  539. portName += CarlaString(j+1);
  540. }
  541. else
  542. portName += "output";
  543. portName.truncate(portNameSize);
  544. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  545. kData->audioOut.ports[j].rindex = j;
  546. if (forcedStereoOut)
  547. {
  548. portName += "_2";
  549. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  550. kData->audioOut.ports[1].rindex = j;
  551. break;
  552. }
  553. }
  554. // MIDI Input (only if multiple)
  555. if (mIns > 1)
  556. {
  557. for (j=0; j < mIns; j++)
  558. {
  559. portName.clear();
  560. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  561. {
  562. portName = fName;
  563. portName += ":";
  564. }
  565. portName += "midi-in_";
  566. portName += CarlaString(j+1);
  567. portName.truncate(portNameSize);
  568. fMidiIn.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  569. fMidiIn.indexes[j] = j;
  570. }
  571. }
  572. // MIDI Output (only if multiple)
  573. if (mOuts > 1)
  574. {
  575. for (j=0; j < mOuts; j++)
  576. {
  577. portName.clear();
  578. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  579. {
  580. portName = fName;
  581. portName += ":";
  582. }
  583. portName += "midi-out_";
  584. portName += CarlaString(j+1);
  585. portName.truncate(portNameSize);
  586. fMidiOut.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  587. fMidiOut.indexes[j] = j;
  588. }
  589. }
  590. for (j=0; j < params; j++)
  591. {
  592. const ::Parameter* const paramInfo = fDescriptor->get_parameter_info(fHandle, j);
  593. kData->param.data[j].index = j;
  594. kData->param.data[j].rindex = j;
  595. kData->param.data[j].hints = 0x0;
  596. kData->param.data[j].midiChannel = 0;
  597. kData->param.data[j].midiCC = -1;
  598. float min, max, def, step, stepSmall, stepLarge;
  599. // min value
  600. min = paramInfo->ranges.min;
  601. // max value
  602. max = paramInfo->ranges.max;
  603. if (min > max)
  604. max = min;
  605. else if (max < min)
  606. min = max;
  607. if (max - min == 0.0f)
  608. {
  609. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  610. max = min + 0.1f;
  611. }
  612. // default value
  613. def = paramInfo->ranges.def;
  614. if (def < min)
  615. def = min;
  616. else if (def > max)
  617. def = max;
  618. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  619. {
  620. min *= sampleRate;
  621. max *= sampleRate;
  622. def *= sampleRate;
  623. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  624. }
  625. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  626. {
  627. step = max - min;
  628. stepSmall = step;
  629. stepLarge = step;
  630. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  631. }
  632. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  633. {
  634. step = 1.0f;
  635. stepSmall = 1.0f;
  636. stepLarge = 10.0f;
  637. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  638. }
  639. else
  640. {
  641. float range = max - min;
  642. step = range/100.0f;
  643. stepSmall = range/1000.0f;
  644. stepLarge = range/10.0f;
  645. }
  646. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  647. {
  648. kData->param.data[j].type = PARAMETER_OUTPUT;
  649. needsCtrlOut = true;
  650. }
  651. else
  652. {
  653. kData->param.data[j].type = PARAMETER_INPUT;
  654. needsCtrlIn = true;
  655. }
  656. // extra parameter hints
  657. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  658. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  659. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  660. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  661. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  662. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  663. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  664. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  665. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  666. kData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  667. kData->param.ranges[j].min = min;
  668. kData->param.ranges[j].max = max;
  669. kData->param.ranges[j].def = def;
  670. kData->param.ranges[j].step = step;
  671. kData->param.ranges[j].stepSmall = stepSmall;
  672. kData->param.ranges[j].stepLarge = stepLarge;
  673. }
  674. if (needsCtrlIn)
  675. {
  676. portName.clear();
  677. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  678. {
  679. portName = fName;
  680. portName += ":";
  681. }
  682. portName += "event-in";
  683. portName.truncate(portNameSize);
  684. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  685. }
  686. if (needsCtrlOut)
  687. {
  688. portName.clear();
  689. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  690. {
  691. portName = fName;
  692. portName += ":";
  693. }
  694. portName += "event-out";
  695. portName.truncate(portNameSize);
  696. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  697. }
  698. // plugin hints
  699. fHints = 0x0;
  700. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  701. fHints |= PLUGIN_CAN_DRYWET;
  702. if (aOuts > 0)
  703. fHints |= PLUGIN_CAN_VOLUME;
  704. if (aOuts >= 2 && aOuts % 2 == 0)
  705. fHints |= PLUGIN_CAN_BALANCE;
  706. // native plugin hints
  707. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  708. fHints |= PLUGIN_IS_RTSAFE;
  709. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  710. fHints |= PLUGIN_IS_SYNTH;
  711. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  712. fHints |= PLUGIN_HAS_GUI;
  713. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  714. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  715. // extra plugin hints
  716. kData->extraHints = 0x0;
  717. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  718. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  719. // plugin options
  720. fOptions = 0x0;
  721. if (fDescriptor->name != nullptr)
  722. {
  723. if (std::strcmp(fDescriptor->name, "ZynAddSubFX") == 0)
  724. {
  725. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  726. }
  727. }
  728. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  729. if (forcedStereoIn || forcedStereoOut)
  730. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  731. if (mIns > 0)
  732. {
  733. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  734. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  735. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  736. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  737. }
  738. bufferSizeChanged(kData->engine->getBufferSize());
  739. reloadPrograms(true);
  740. carla_debug("NativePlugin::reload() - end");
  741. }
  742. void reloadPrograms(const bool init)
  743. {
  744. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  745. uint32_t i, oldCount = kData->midiprog.count;
  746. // Delete old programs
  747. kData->midiprog.clear();
  748. // Query new programs
  749. uint32_t count = 0;
  750. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr)
  751. count = fDescriptor->get_midi_program_count(fHandle);
  752. if (count > 0)
  753. kData->midiprog.createNew(count);
  754. // Update data
  755. for (i=0; i < kData->midiprog.count; i++)
  756. {
  757. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  758. CARLA_ASSERT(mpDesc != nullptr);
  759. CARLA_ASSERT(mpDesc->name != nullptr);
  760. kData->midiprog.data[i].bank = mpDesc->bank;
  761. kData->midiprog.data[i].program = mpDesc->program;
  762. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  763. }
  764. #ifndef BUILD_BRIDGE
  765. // Update OSC Names
  766. if (kData->engine->isOscControlRegistered())
  767. {
  768. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  769. for (i=0; i < kData->midiprog.count; i++)
  770. 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);
  771. }
  772. #endif
  773. if (init)
  774. {
  775. if (kData->midiprog.count > 0)
  776. setMidiProgram(0, false, false, false);
  777. }
  778. else
  779. {
  780. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  781. // Check if current program is invalid
  782. bool programChanged = false;
  783. if (kData->midiprog.count == oldCount+1)
  784. {
  785. // one midi program added, probably created by user
  786. kData->midiprog.current = oldCount;
  787. programChanged = true;
  788. }
  789. else if (kData->midiprog.current >= static_cast<int32_t>(kData->midiprog.count))
  790. {
  791. // current midi program > count
  792. kData->midiprog.current = 0;
  793. programChanged = true;
  794. }
  795. else if (kData->midiprog.current < 0 && kData->midiprog.count > 0)
  796. {
  797. // programs exist now, but not before
  798. kData->midiprog.current = 0;
  799. programChanged = true;
  800. }
  801. else if (kData->midiprog.current >= 0 && kData->midiprog.count == 0)
  802. {
  803. // programs existed before, but not anymore
  804. kData->midiprog.current = -1;
  805. programChanged = true;
  806. }
  807. if (programChanged)
  808. setMidiProgram(kData->midiprog.current, true, true, true);
  809. }
  810. }
  811. // -------------------------------------------------------------------
  812. // Plugin processing
  813. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  814. {
  815. uint32_t i, k;
  816. // --------------------------------------------------------------------------------------------------------
  817. // Check if active
  818. if (! kData->active)
  819. {
  820. // disable any output sound
  821. for (i=0; i < kData->audioOut.count; i++)
  822. carla_zeroFloat(outBuffer[i], frames);
  823. if (kData->activeBefore)
  824. {
  825. if (fDescriptor->deactivate != nullptr)
  826. {
  827. fDescriptor->deactivate(fHandle);
  828. if (fHandle2 != nullptr)
  829. fDescriptor->deactivate(fHandle2);
  830. }
  831. }
  832. kData->activeBefore = kData->active;
  833. return;
  834. }
  835. fMidiEventCount = 0;
  836. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*MAX_MIDI_EVENTS*2);
  837. // --------------------------------------------------------------------------------------------------------
  838. // Check if not active before
  839. if (! kData->activeBefore)
  840. {
  841. if (kData->event.portIn != nullptr)
  842. {
  843. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; k++)
  844. {
  845. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  846. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  847. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  848. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  849. }
  850. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  851. }
  852. if (fDescriptor->activate != nullptr)
  853. {
  854. fDescriptor->activate(fHandle);
  855. if (fHandle2 != nullptr)
  856. fDescriptor->activate(fHandle2);
  857. }
  858. }
  859. CARLA_PROCESS_CONTINUE_CHECK;
  860. // --------------------------------------------------------------------------------------------------------
  861. // Set TimeInfo
  862. const EngineTimeInfo& timeInfo = kData->engine->getTimeInfo();
  863. fTimeInfo.playing = timeInfo.playing;
  864. fTimeInfo.frame = timeInfo.frame;
  865. fTimeInfo.time = timeInfo.time;
  866. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  867. {
  868. fTimeInfo.bbt.valid = true;
  869. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  870. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  871. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  872. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  873. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  874. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  875. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  876. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  877. }
  878. else
  879. fTimeInfo.bbt.valid = false;
  880. CARLA_PROCESS_CONTINUE_CHECK;
  881. // --------------------------------------------------------------------------------------------------------
  882. // Event Input and Processing
  883. if (kData->event.portIn != nullptr && kData->activeBefore)
  884. {
  885. // ----------------------------------------------------------------------------------------------------
  886. // MIDI Input (External)
  887. if (kData->extNotes.mutex.tryLock())
  888. {
  889. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  890. {
  891. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  892. CARLA_ASSERT(note.channel >= 0);
  893. fMidiEvents[fMidiEventCount].port = 0;
  894. fMidiEvents[fMidiEventCount].time = 0;
  895. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  896. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  897. fMidiEvents[fMidiEventCount].data[1] = note.note;
  898. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  899. fMidiEventCount += 1;
  900. }
  901. kData->extNotes.mutex.unlock();
  902. } // End of MIDI Input (External)
  903. // ----------------------------------------------------------------------------------------------------
  904. // Event Input (System)
  905. bool allNotesOffSent = false;
  906. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  907. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  908. uint32_t startTime = 0;
  909. uint32_t timeOffset = 0;
  910. uint32_t nextBankId = 0;
  911. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  912. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  913. for (i=0; i < nEvents; i++)
  914. {
  915. const EngineEvent& event = kData->event.portIn->getEvent(i);
  916. time = event.time;
  917. if (time >= frames)
  918. continue;
  919. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  920. if (time > timeOffset && sampleAccurate)
  921. {
  922. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  923. {
  924. if (fMidiEventCount > 0)
  925. {
  926. //carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*fMidiEventCount);
  927. fMidiEventCount = 0;
  928. }
  929. nextBankId = 0;
  930. timeOffset = time;
  931. }
  932. else
  933. startTime += timeOffset;
  934. }
  935. // Control change
  936. switch (event.type)
  937. {
  938. case kEngineEventTypeNull:
  939. break;
  940. case kEngineEventTypeControl:
  941. {
  942. const EngineControlEvent& ctrlEvent = event.ctrl;
  943. switch (ctrlEvent.type)
  944. {
  945. case kEngineControlEventTypeNull:
  946. break;
  947. case kEngineControlEventTypeParameter:
  948. {
  949. // Control backend stuff
  950. if (event.channel == kData->ctrlChannel)
  951. {
  952. double value;
  953. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  954. {
  955. value = ctrlEvent.value;
  956. setDryWet(value, false, false);
  957. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  958. continue;
  959. }
  960. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  961. {
  962. value = ctrlEvent.value*127/100;
  963. setVolume(value, false, false);
  964. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  965. continue;
  966. }
  967. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  968. {
  969. double left, right;
  970. value = ctrlEvent.value/0.5 - 1.0;
  971. if (value < 0.0)
  972. {
  973. left = -1.0;
  974. right = (value*2)+1.0;
  975. }
  976. else if (value > 0.0)
  977. {
  978. left = (value*2)-1.0;
  979. right = 1.0;
  980. }
  981. else
  982. {
  983. left = -1.0;
  984. right = 1.0;
  985. }
  986. setBalanceLeft(left, false, false);
  987. setBalanceRight(right, false, false);
  988. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  989. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  990. continue;
  991. }
  992. }
  993. // Control plugin parameters
  994. for (k=0; k < kData->param.count; k++)
  995. {
  996. if (kData->param.data[k].midiChannel != event.channel)
  997. continue;
  998. if (kData->param.data[k].midiCC != ctrlEvent.param)
  999. continue;
  1000. if (kData->param.data[k].type != PARAMETER_INPUT)
  1001. continue;
  1002. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1003. continue;
  1004. double value;
  1005. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1006. {
  1007. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  1008. }
  1009. else
  1010. {
  1011. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  1012. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1013. value = std::rint(value);
  1014. }
  1015. setParameterValue(k, value, false, false, false);
  1016. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1017. }
  1018. break;
  1019. }
  1020. case kEngineControlEventTypeMidiBank:
  1021. if (event.channel == kData->ctrlChannel)
  1022. nextBankId = ctrlEvent.param;
  1023. break;
  1024. case kEngineControlEventTypeMidiProgram:
  1025. if (event.channel == kData->ctrlChannel)
  1026. {
  1027. const uint32_t nextProgramId = ctrlEvent.param;
  1028. for (k=0; k < kData->midiprog.count; k++)
  1029. {
  1030. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  1031. {
  1032. setMidiProgram(k, false, false, false);
  1033. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  1034. break;
  1035. }
  1036. }
  1037. }
  1038. break;
  1039. case kEngineControlEventTypeAllSoundOff:
  1040. if (event.channel == kData->ctrlChannel)
  1041. {
  1042. if (! allNotesOffSent)
  1043. sendMidiAllNotesOff();
  1044. if (fDescriptor->deactivate != nullptr)
  1045. {
  1046. fDescriptor->deactivate(fHandle);
  1047. if (fHandle2 != nullptr)
  1048. fDescriptor->deactivate(fHandle2);
  1049. }
  1050. if (fDescriptor->activate != nullptr)
  1051. {
  1052. fDescriptor->activate(fHandle);
  1053. if (fHandle2 != nullptr)
  1054. fDescriptor->activate(fHandle2);
  1055. }
  1056. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  1057. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  1058. allNotesOffSent = true;
  1059. }
  1060. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1061. continue;
  1062. fMidiEvents[fMidiEventCount].port = 0;
  1063. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1064. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1065. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1066. fMidiEvents[fMidiEventCount].data[2] = 0;
  1067. fMidiEventCount += 1;
  1068. break;
  1069. case kEngineControlEventTypeAllNotesOff:
  1070. if (event.channel == kData->ctrlChannel)
  1071. {
  1072. if (! allNotesOffSent)
  1073. sendMidiAllNotesOff();
  1074. allNotesOffSent = true;
  1075. }
  1076. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1077. continue;
  1078. fMidiEvents[fMidiEventCount].port = 0;
  1079. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1080. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1081. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1082. fMidiEvents[fMidiEventCount].data[2] = 0;
  1083. fMidiEventCount += 1;
  1084. break;
  1085. }
  1086. break;
  1087. }
  1088. case kEngineEventTypeMidi:
  1089. {
  1090. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1091. continue;
  1092. const EngineMidiEvent& midiEvent = event.midi;
  1093. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1094. uint8_t channel = event.channel;
  1095. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1096. continue;
  1097. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1098. continue;
  1099. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1100. continue;
  1101. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1102. continue;
  1103. // Fix bad note-off
  1104. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1105. status -= 0x10;
  1106. fMidiEvents[fMidiEventCount].port = 0;
  1107. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1108. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1109. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1110. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1111. fMidiEventCount += 1;
  1112. if (status == MIDI_STATUS_NOTE_ON)
  1113. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1114. else if (status == MIDI_STATUS_NOTE_OFF)
  1115. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0);
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. kData->postRtEvents.trySplice();
  1121. if (frames > timeOffset)
  1122. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1123. } // End of Event Input and Processing
  1124. // --------------------------------------------------------------------------------------------------------
  1125. // Plugin processing (no events)
  1126. else
  1127. {
  1128. processSingle(inBuffer, outBuffer, frames, 0);
  1129. } // End of Plugin processing (no events)
  1130. CARLA_PROCESS_CONTINUE_CHECK;
  1131. // --------------------------------------------------------------------------------------------------------
  1132. // Control and MIDI Output
  1133. if (fMidiOut.count > 0 || kData->event.portOut != nullptr)
  1134. {
  1135. float value, curValue;
  1136. for (k=0; k < kData->param.count; k++)
  1137. {
  1138. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1139. continue;
  1140. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1141. kData->param.ranges[k].fixValue(curValue);
  1142. if (kData->param.data[k].midiCC > 0)
  1143. {
  1144. value = kData->param.ranges[k].normalizeValue(curValue);
  1145. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1146. }
  1147. }
  1148. // reverse lookup MIDI events
  1149. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; k--)
  1150. {
  1151. if (fMidiEvents[k].data[0] == 0)
  1152. break;
  1153. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1154. const uint8_t port = fMidiEvents[k].port;
  1155. if (kData->event.portOut != nullptr)
  1156. kData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, 3);
  1157. else if (port < fMidiOut.count)
  1158. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, 3);
  1159. }
  1160. } // End of Control and MIDI Output
  1161. // --------------------------------------------------------------------------------------------------------
  1162. kData->activeBefore = kData->active;
  1163. }
  1164. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1165. {
  1166. uint32_t i, k;
  1167. // --------------------------------------------------------------------------------------------------------
  1168. // Try lock, silence otherwise
  1169. if (kData->engine->isOffline())
  1170. {
  1171. kData->mutex.lock();
  1172. }
  1173. else if (! kData->mutex.tryLock())
  1174. {
  1175. for (i=0; i < kData->audioOut.count; i++)
  1176. {
  1177. for (k=0; k < frames; k++)
  1178. outBuffer[i][k+timeOffset] = 0.0f;
  1179. }
  1180. return false;
  1181. }
  1182. for (i=0; i < kData->audioIn.count; i++)
  1183. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1184. for (i=0; i < kData->audioOut.count; i++)
  1185. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1186. fIsProcessing = true;
  1187. if (fHandle2 == nullptr)
  1188. {
  1189. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1190. }
  1191. else
  1192. {
  1193. fDescriptor->process(fHandle,
  1194. (kData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1195. (kData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1196. frames, fMidiEventCount, fMidiEvents);
  1197. fDescriptor->process(fHandle2,
  1198. (kData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1199. (kData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1200. frames, fMidiEventCount, fMidiEvents);
  1201. }
  1202. fIsProcessing = false;
  1203. fTimeInfo.frame += frames;
  1204. // --------------------------------------------------------------------------------------------------------
  1205. // Post-processing (dry/wet, volume and balance)
  1206. {
  1207. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1208. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1209. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1210. for (i=0; i < kData->audioOut.count; i++)
  1211. {
  1212. // Dry/Wet
  1213. if (doDryWet)
  1214. {
  1215. for (k=0; k < frames; k++)
  1216. {
  1217. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1218. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1219. }
  1220. }
  1221. // Balance
  1222. if (doBalance)
  1223. {
  1224. if (i % 2 == 0)
  1225. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1226. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1227. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1228. for (k=0; k < frames; k++)
  1229. {
  1230. if (i % 2 == 0)
  1231. {
  1232. // left
  1233. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1234. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1235. }
  1236. else
  1237. {
  1238. // right
  1239. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1240. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1241. }
  1242. }
  1243. }
  1244. // Volume (and buffer copy)
  1245. {
  1246. for (k=0; k < frames; k++)
  1247. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1248. }
  1249. }
  1250. } // End of Post-processing
  1251. // --------------------------------------------------------------------------------------------------------
  1252. kData->mutex.unlock();
  1253. return true;
  1254. }
  1255. void bufferSizeChanged(const uint32_t newBufferSize)
  1256. {
  1257. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1258. {
  1259. if (fAudioInBuffers[i] != nullptr)
  1260. delete[] fAudioInBuffers[i];
  1261. fAudioInBuffers[i] = new float[newBufferSize];
  1262. }
  1263. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1264. {
  1265. if (fAudioOutBuffers[i] != nullptr)
  1266. delete[] fAudioOutBuffers[i];
  1267. fAudioOutBuffers[i] = new float[newBufferSize];
  1268. }
  1269. }
  1270. // -------------------------------------------------------------------
  1271. // Post-poned events
  1272. void uiParameterChange(const uint32_t index, const float value)
  1273. {
  1274. CARLA_ASSERT(fDescriptor != nullptr);
  1275. CARLA_ASSERT(fHandle != nullptr);
  1276. CARLA_ASSERT(index < kData->param.count);
  1277. if (! fIsUiVisible)
  1278. return;
  1279. if (fDescriptor == nullptr || fHandle == nullptr)
  1280. return;
  1281. if (index >= kData->param.count)
  1282. return;
  1283. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1284. }
  1285. void uiMidiProgramChange(const uint32_t index)
  1286. {
  1287. CARLA_ASSERT(fDescriptor != nullptr);
  1288. CARLA_ASSERT(fHandle != nullptr);
  1289. CARLA_ASSERT(index < kData->midiprog.count);
  1290. if (! fIsUiVisible)
  1291. return;
  1292. if (fDescriptor == nullptr || fHandle == nullptr)
  1293. return;
  1294. if (index >= kData->midiprog.count)
  1295. return;
  1296. fDescriptor->ui_set_parameter_value(fHandle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1297. }
  1298. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  1299. {
  1300. CARLA_ASSERT(fDescriptor != nullptr);
  1301. CARLA_ASSERT(fHandle != nullptr);
  1302. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1303. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1304. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1305. if (! fIsUiVisible)
  1306. return;
  1307. if (fDescriptor == nullptr || fHandle == nullptr)
  1308. return;
  1309. if (channel >= MAX_MIDI_CHANNELS)
  1310. return;
  1311. if (note >= MAX_MIDI_NOTE)
  1312. return;
  1313. if (velo >= MAX_MIDI_VALUE)
  1314. return;
  1315. // TODO
  1316. }
  1317. void uiNoteOff(const uint8_t channel, const uint8_t note)
  1318. {
  1319. CARLA_ASSERT(fDescriptor != nullptr);
  1320. CARLA_ASSERT(fHandle != nullptr);
  1321. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1322. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1323. if (! fIsUiVisible)
  1324. return;
  1325. if (fDescriptor == nullptr || fHandle == nullptr)
  1326. return;
  1327. if (channel >= MAX_MIDI_CHANNELS)
  1328. return;
  1329. if (note >= MAX_MIDI_NOTE)
  1330. return;
  1331. // TODO
  1332. }
  1333. // -------------------------------------------------------------------
  1334. // Cleanup
  1335. void initBuffers()
  1336. {
  1337. fMidiIn.initBuffers(kData->engine);
  1338. fMidiOut.initBuffers(kData->engine);
  1339. CarlaPlugin::initBuffers();
  1340. }
  1341. void deleteBuffers()
  1342. {
  1343. carla_debug("NativePlugin::deleteBuffers() - start");
  1344. if (fAudioInBuffers != nullptr)
  1345. {
  1346. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1347. {
  1348. if (fAudioInBuffers[i] != nullptr)
  1349. {
  1350. delete[] fAudioInBuffers[i];
  1351. fAudioInBuffers[i] = nullptr;
  1352. }
  1353. }
  1354. delete[] fAudioInBuffers;
  1355. fAudioInBuffers = nullptr;
  1356. }
  1357. if (fAudioOutBuffers != nullptr)
  1358. {
  1359. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1360. {
  1361. if (fAudioOutBuffers[i] != nullptr)
  1362. {
  1363. delete[] fAudioOutBuffers[i];
  1364. fAudioOutBuffers[i] = nullptr;
  1365. }
  1366. }
  1367. delete[] fAudioOutBuffers;
  1368. fAudioOutBuffers = nullptr;
  1369. }
  1370. fMidiIn.clear();
  1371. fMidiOut.clear();
  1372. CarlaPlugin::deleteBuffers();
  1373. carla_debug("NativePlugin::deleteBuffers() - end");
  1374. }
  1375. // -------------------------------------------------------------------
  1376. protected:
  1377. uint32_t handleGetBufferSize()
  1378. {
  1379. return kData->engine->getBufferSize();
  1380. }
  1381. double handleGetSampleRate()
  1382. {
  1383. return kData->engine->getSampleRate();
  1384. }
  1385. const ::TimeInfo* handleGetTimeInfo()
  1386. {
  1387. CARLA_ASSERT(fIsProcessing);
  1388. return &fTimeInfo;
  1389. }
  1390. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1391. {
  1392. CARLA_ASSERT(fEnabled);
  1393. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1394. CARLA_ASSERT(event != nullptr);
  1395. CARLA_ASSERT(fIsProcessing);
  1396. if (! fEnabled)
  1397. return false;
  1398. if (fMidiOut.count == 0)
  1399. return false;
  1400. if (event == nullptr)
  1401. return false;
  1402. if (! fIsProcessing)
  1403. {
  1404. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1405. return false;
  1406. }
  1407. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1408. return false;
  1409. // reverse-find first free event, and put it there
  1410. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; i--)
  1411. {
  1412. if (fMidiEvents[i].data[0] == 0)
  1413. {
  1414. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1415. break;
  1416. }
  1417. }
  1418. return true;
  1419. }
  1420. void handleUiParameterChanged(const uint32_t index, const float value)
  1421. {
  1422. setParameterValue(index, value, false, true, true);
  1423. }
  1424. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1425. {
  1426. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1427. }
  1428. void handleUiClosed()
  1429. {
  1430. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1431. fIsUiVisible = false;
  1432. }
  1433. public:
  1434. static size_t getPluginCount()
  1435. {
  1436. return sPluginDescriptors.count();
  1437. }
  1438. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1439. {
  1440. CARLA_ASSERT(index < sPluginDescriptors.count());
  1441. if (index < sPluginDescriptors.count())
  1442. return sPluginDescriptors.getAt(index);
  1443. return nullptr;
  1444. }
  1445. static void registerPlugin(const PluginDescriptor* desc)
  1446. {
  1447. sPluginDescriptors.append(desc);
  1448. }
  1449. // -------------------------------------------------------------------
  1450. bool init(const char* const name, const char* const label)
  1451. {
  1452. CARLA_ASSERT(kData->engine != nullptr);
  1453. CARLA_ASSERT(kData->client == nullptr);
  1454. CARLA_ASSERT(label);
  1455. // ---------------------------------------------------------------
  1456. // get descriptor that matches label
  1457. // FIXME - use itenerator when available
  1458. for (size_t i=0; i < sPluginDescriptors.count(); i++)
  1459. {
  1460. fDescriptor = sPluginDescriptors.getAt(i);
  1461. if (fDescriptor == nullptr)
  1462. break;
  1463. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1464. break;
  1465. fDescriptor = nullptr;
  1466. }
  1467. if (fDescriptor == nullptr)
  1468. {
  1469. kData->engine->setLastError("Invalid internal plugin");
  1470. return false;
  1471. }
  1472. // ---------------------------------------------------------------
  1473. // get info
  1474. if (name != nullptr)
  1475. fName = kData->engine->getNewUniquePluginName(name);
  1476. else if (fDescriptor->name != nullptr)
  1477. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1478. else
  1479. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1480. // ---------------------------------------------------------------
  1481. // register client
  1482. kData->client = kData->engine->addClient(this);
  1483. if (kData->client == nullptr || ! kData->client->isOk())
  1484. {
  1485. kData->engine->setLastError("Failed to register plugin client");
  1486. return false;
  1487. }
  1488. // ---------------------------------------------------------------
  1489. // initialize plugin
  1490. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1491. if (fHandle == nullptr)
  1492. {
  1493. kData->engine->setLastError("Plugin failed to initialize");
  1494. return false;
  1495. }
  1496. return true;
  1497. }
  1498. class ScopedInitializer
  1499. {
  1500. public:
  1501. ScopedInitializer()
  1502. {
  1503. initDescriptors();
  1504. }
  1505. ~ScopedInitializer()
  1506. {
  1507. clearDescriptors();
  1508. }
  1509. };
  1510. private:
  1511. PluginHandle fHandle;
  1512. PluginHandle fHandle2;
  1513. HostDescriptor fHost;
  1514. const PluginDescriptor* fDescriptor;
  1515. bool fIsProcessing;
  1516. bool fIsUiVisible;
  1517. float** fAudioInBuffers;
  1518. float** fAudioOutBuffers;
  1519. uint32_t fMidiEventCount;
  1520. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1521. NativePluginMidiData fMidiIn;
  1522. NativePluginMidiData fMidiOut;
  1523. ::TimeInfo fTimeInfo;
  1524. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1525. // -------------------------------------------------------------------
  1526. static void initDescriptors()
  1527. {
  1528. #ifndef BUILD_BRIDGE
  1529. carla_register_native_plugin_bypass();
  1530. carla_register_native_plugin_midiSequencer();
  1531. carla_register_native_plugin_midiSplit();
  1532. carla_register_native_plugin_midiThrough();
  1533. carla_register_native_plugin_midiTranspose();
  1534. carla_register_native_plugin_nekofilter();
  1535. carla_register_native_plugin_3BandEQ();
  1536. carla_register_native_plugin_3BandSplitter();
  1537. carla_register_native_plugin_PingPongPan();
  1538. carla_register_native_plugin_Notes();
  1539. # ifdef WANT_AUDIOFILE
  1540. carla_register_native_plugin_audiofile();
  1541. # endif
  1542. # ifdef WANT_ZYNADDSUBFX
  1543. carla_register_native_plugin_zynaddsubfx();
  1544. # endif
  1545. #endif
  1546. }
  1547. static void clearDescriptors()
  1548. {
  1549. sPluginDescriptors.clear();
  1550. }
  1551. // -------------------------------------------------------------------
  1552. #define handlePtr ((NativePlugin*)handle)
  1553. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1554. {
  1555. return handlePtr->handleGetBufferSize();
  1556. }
  1557. static double carla_host_get_sample_rate(HostHandle handle)
  1558. {
  1559. return handlePtr->handleGetSampleRate();
  1560. }
  1561. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1562. {
  1563. return handlePtr->handleGetTimeInfo();
  1564. }
  1565. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1566. {
  1567. return handlePtr->handleWriteMidiEvent(event);
  1568. }
  1569. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1570. {
  1571. handlePtr->handleUiParameterChanged(index, value);
  1572. }
  1573. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1574. {
  1575. handlePtr->handleUiCustomDataChanged(key, value);
  1576. }
  1577. static void carla_host_ui_closed(HostHandle handle)
  1578. {
  1579. handlePtr->handleUiClosed();
  1580. }
  1581. #undef handlePtr
  1582. };
  1583. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1584. static const NativePlugin::ScopedInitializer _si;
  1585. CARLA_BACKEND_END_NAMESPACE
  1586. void carla_register_native_plugin(const PluginDescriptor* desc)
  1587. {
  1588. CARLA_BACKEND_USE_NAMESPACE
  1589. NativePlugin::registerPlugin(desc);
  1590. }
  1591. #else // WANT_NATIVE
  1592. # warning Building without Internal plugin support
  1593. #endif
  1594. CARLA_BACKEND_START_NAMESPACE
  1595. size_t CarlaPlugin::getNativePluginCount()
  1596. {
  1597. #ifdef WANT_NATIVE
  1598. return NativePlugin::getPluginCount();
  1599. #else
  1600. return 0;
  1601. #endif
  1602. }
  1603. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1604. {
  1605. #ifdef WANT_NATIVE
  1606. return NativePlugin::getPluginDescriptor(index);
  1607. #else
  1608. return nullptr;
  1609. // unused
  1610. (void)index;
  1611. #endif
  1612. }
  1613. // -----------------------------------------------------------------------
  1614. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1615. {
  1616. carla_debug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1617. #ifdef WANT_NATIVE
  1618. NativePlugin* const plugin = new NativePlugin(init.engine, init.id);
  1619. if (! plugin->init(init.name, init.label))
  1620. {
  1621. delete plugin;
  1622. return nullptr;
  1623. }
  1624. plugin->reload();
  1625. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1626. {
  1627. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1628. delete plugin;
  1629. return nullptr;
  1630. }
  1631. return plugin;
  1632. #else
  1633. init.engine->setLastError("Internal plugins not available");
  1634. return nullptr;
  1635. #endif
  1636. }
  1637. CARLA_BACKEND_END_NAMESPACE