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.

2010 lines
65KB

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