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.

1724 lines
55KB

  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 "carla_plugin_internal.hpp"
  18. #include "carla_native.h"
  19. CARLA_BACKEND_START_NAMESPACE
  20. #if 0
  21. struct NativePluginMidiData {
  22. uint32_t count;
  23. uint32_t* indexes;
  24. CarlaEngineMidiPort** ports;
  25. NativePluginMidiData()
  26. : count(0),
  27. indexes(nullptr),
  28. ports(nullptr) {}
  29. };
  30. #endif
  31. class NativePlugin : public CarlaPlugin
  32. {
  33. public:
  34. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  35. : CarlaPlugin(engine, id)
  36. {
  37. qDebug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  38. fHandle = nullptr;
  39. fHandle2 = nullptr;
  40. fDescriptor = nullptr;
  41. fHost.handle = this;
  42. fHost.get_buffer_size = carla_host_get_buffer_size;
  43. fHost.get_sample_rate = carla_host_get_sample_rate;
  44. fHost.get_time_info = carla_host_get_time_info;
  45. fHost.write_midi_event = carla_host_write_midi_event;
  46. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  47. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  48. fHost.ui_closed = carla_host_ui_closed;
  49. fIsProcessing = false;
  50. //midiEventCount = 0;
  51. //memset(midiEvents, 0, sizeof(::MidiEvent) * MAX_MIDI_EVENTS * 2);
  52. }
  53. ~NativePlugin()
  54. {
  55. qDebug("NativePlugin::~NativePlugin()");
  56. if (fDescriptor != nullptr)
  57. {
  58. if (fDescriptor->deactivate != nullptr && kData->activeBefore)
  59. {
  60. if (fHandle != nullptr)
  61. fDescriptor->deactivate(fHandle);
  62. if (fHandle2 != nullptr)
  63. fDescriptor->deactivate(fHandle2);
  64. }
  65. if (fDescriptor->cleanup != nullptr)
  66. {
  67. if (fHandle != nullptr)
  68. fDescriptor->cleanup(fHandle);
  69. if (fHandle2 != nullptr)
  70. fDescriptor->cleanup(fHandle2);
  71. }
  72. fHandle = nullptr;
  73. fHandle2 = nullptr;
  74. fDescriptor = nullptr;
  75. }
  76. }
  77. // -------------------------------------------------------------------
  78. // Information (base)
  79. virtual PluginType type() const
  80. {
  81. return PLUGIN_INTERNAL;
  82. }
  83. PluginCategory category()
  84. {
  85. CARLA_ASSERT(fDescriptor != nullptr);
  86. if (fDescriptor != nullptr)
  87. return static_cast<PluginCategory>(fDescriptor->category);
  88. return getPluginCategoryFromName(fName);
  89. }
  90. // -------------------------------------------------------------------
  91. // Information (count)
  92. #if 0
  93. uint32_t midiInCount()
  94. {
  95. return mIn.count;
  96. }
  97. uint32_t midiOutCount()
  98. {
  99. return mOut.count;
  100. }
  101. #endif
  102. uint32_t parameterScalePointCount(const uint32_t parameterId) const
  103. {
  104. CARLA_ASSERT(fDescriptor != nullptr);
  105. CARLA_ASSERT(fHandle != nullptr);
  106. CARLA_ASSERT(parameterId < kData->param.count);
  107. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  108. {
  109. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  110. return param->scalePointCount;
  111. }
  112. return 0;
  113. }
  114. // -------------------------------------------------------------------
  115. // Information (per-plugin data)
  116. float getParameterValue(const uint32_t parameterId)
  117. {
  118. CARLA_ASSERT(fDescriptor != nullptr);
  119. CARLA_ASSERT(fHandle != nullptr);
  120. CARLA_ASSERT(parameterId < kData->param.count);
  121. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_value != nullptr)
  122. return fDescriptor->get_parameter_value(fHandle, parameterId);
  123. return 0.0f;
  124. }
  125. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  126. {
  127. CARLA_ASSERT(fDescriptor != nullptr);
  128. CARLA_ASSERT(fHandle != nullptr);
  129. CARLA_ASSERT(parameterId < kData->param.count);
  130. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  131. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  132. {
  133. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  134. {
  135. const ParameterScalePoint& scalePoint = param->scalePoints[scalePointId];
  136. return scalePoint.value;
  137. }
  138. }
  139. return 0.0f;
  140. }
  141. void getLabel(char* const strBuf)
  142. {
  143. CARLA_ASSERT(fDescriptor != nullptr);
  144. if (fDescriptor != nullptr && fDescriptor->label != nullptr)
  145. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  146. else
  147. CarlaPlugin::getLabel(strBuf);
  148. }
  149. void getMaker(char* const strBuf)
  150. {
  151. CARLA_ASSERT(fDescriptor != nullptr);
  152. if (fDescriptor != nullptr && fDescriptor->maker != nullptr)
  153. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  154. else
  155. CarlaPlugin::getMaker(strBuf);
  156. }
  157. void getCopyright(char* const strBuf)
  158. {
  159. CARLA_ASSERT(fDescriptor != nullptr);
  160. if (fDescriptor != nullptr && fDescriptor->copyright != nullptr)
  161. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  162. else
  163. CarlaPlugin::getCopyright(strBuf);
  164. }
  165. void getRealName(char* const strBuf)
  166. {
  167. CARLA_ASSERT(fDescriptor != nullptr);
  168. if (fDescriptor != nullptr && fDescriptor->name != nullptr)
  169. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  170. else
  171. CarlaPlugin::getRealName(strBuf);
  172. }
  173. void getParameterName(const uint32_t parameterId, char* const strBuf)
  174. {
  175. CARLA_ASSERT(fDescriptor != nullptr);
  176. CARLA_ASSERT(fHandle != nullptr);
  177. CARLA_ASSERT(parameterId < kData->param.count);
  178. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  179. {
  180. const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId);
  181. if (param != nullptr && param->name != nullptr)
  182. {
  183. std::strncpy(strBuf, param->name, STR_MAX);
  184. return;
  185. }
  186. }
  187. CarlaPlugin::getParameterName(parameterId, strBuf);
  188. }
  189. void getParameterText(const uint32_t parameterId, char* const strBuf)
  190. {
  191. CARLA_ASSERT(fDescriptor != nullptr);
  192. CARLA_ASSERT(fHandle != nullptr);
  193. CARLA_ASSERT(parameterId < kData->param.count);
  194. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_text != nullptr)
  195. {
  196. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId))
  197. {
  198. std::strncpy(strBuf, text, STR_MAX);
  199. return;
  200. }
  201. }
  202. CarlaPlugin::getParameterText(parameterId, strBuf);
  203. }
  204. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  205. {
  206. CARLA_ASSERT(fDescriptor != nullptr);
  207. CARLA_ASSERT(fHandle != nullptr);
  208. CARLA_ASSERT(parameterId < kData->param.count);
  209. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  210. {
  211. const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId);
  212. if (param != nullptr && param->unit != nullptr)
  213. {
  214. std::strncpy(strBuf, param->unit, STR_MAX);
  215. return;
  216. }
  217. }
  218. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  219. }
  220. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  221. {
  222. CARLA_ASSERT(fDescriptor != nullptr);
  223. CARLA_ASSERT(fHandle != nullptr);
  224. CARLA_ASSERT(parameterId < kData->param.count);
  225. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  226. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->get_parameter_info != nullptr)
  227. {
  228. if (const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  229. {
  230. const ParameterScalePoint& scalePoint = param->scalePoints[scalePointId];
  231. if (scalePoint.label != nullptr)
  232. {
  233. std::strncpy(strBuf, scalePoint.label, STR_MAX);
  234. return;
  235. }
  236. }
  237. }
  238. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  239. }
  240. // -------------------------------------------------------------------
  241. // Set data (plugin-specific stuff)
  242. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  243. {
  244. CARLA_ASSERT(fDescriptor != nullptr);
  245. CARLA_ASSERT(fHandle != nullptr);
  246. CARLA_ASSERT(parameterId < kData->param.count);
  247. const float fixedValue = kData->param.fixValue(parameterId, value);
  248. if (fDescriptor != nullptr && fHandle != nullptr && parameterId < kData->param.count && fDescriptor->set_parameter_value != nullptr)
  249. {
  250. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  251. if (fHandle2 != nullptr)
  252. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  253. }
  254. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  255. }
  256. #if 0
  257. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui)
  258. {
  259. CARLA_ASSERT(descriptor);
  260. CARLA_ASSERT(handle);
  261. CARLA_ASSERT(type);
  262. CARLA_ASSERT(key);
  263. CARLA_ASSERT(value);
  264. if (! type)
  265. return qCritical("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is not string", type, key, value, bool2str(sendGui));
  266. if (! key)
  267. return qCritical("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - key is null", type, key, value, bool2str(sendGui));
  268. if (! value)
  269. return qCritical("Nativelugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - value is null", type, key, value, bool2str(sendGui));
  270. if (descriptor && handle)
  271. {
  272. if (descriptor->set_custom_data)
  273. {
  274. descriptor->set_custom_data(handle, key, value);
  275. if (h2) descriptor->set_custom_data(h2, key, value);
  276. }
  277. // FIXME - only if gui was started before
  278. if (sendGui && descriptor->ui_set_custom_data)
  279. descriptor->ui_set_custom_data(handle, key, value);
  280. }
  281. CarlaPlugin::setCustomData(type, key, value, sendGui);
  282. }
  283. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  284. {
  285. CARLA_ASSERT(fDescriptor != nullptr);
  286. CARLA_ASSERT(fHandle != nullptr);
  287. CARLA_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  288. if (index < -1)
  289. index = -1;
  290. else if (index > (int32_t)midiprog.count)
  291. return;
  292. if (descriptor && handle && index >= 0)
  293. {
  294. if (x_engine->isOffline())
  295. {
  296. const CarlaEngine::ScopedLocker m(x_engine, block);
  297. descriptor->set_midi_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  298. if (h2) descriptor->set_midi_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  299. }
  300. else
  301. {
  302. const ScopedDisabler m(this, block);
  303. descriptor->set_midi_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  304. if (h2) descriptor->set_midi_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  305. }
  306. }
  307. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  308. }
  309. #endif
  310. // -------------------------------------------------------------------
  311. // Set gui stuff
  312. void showGui(const bool yesNo)
  313. {
  314. CARLA_ASSERT(fDescriptor != nullptr);
  315. CARLA_ASSERT(fHandle != nullptr);
  316. if (fDescriptor != nullptr && fHandle != nullptr && fDescriptor->ui_show != nullptr)
  317. fDescriptor->ui_show(fHandle, yesNo);
  318. }
  319. void idleGui()
  320. {
  321. CARLA_ASSERT(fDescriptor != nullptr);
  322. CARLA_ASSERT(fHandle != nullptr);
  323. if (fDescriptor != nullptr && fHandle != nullptr && fDescriptor->ui_idle != nullptr)
  324. fDescriptor->ui_idle(fHandle);
  325. }
  326. // -------------------------------------------------------------------
  327. // Plugin state
  328. void reload()
  329. {
  330. qDebug("NativePlugin::reload() - start");
  331. CARLA_ASSERT(fDescriptor != nullptr);
  332. #if 0
  333. const ProcessMode processMode(x_engine->getOptions().processMode);
  334. // Safely disable plugin for reload
  335. const ScopedDisabler m(this);
  336. if (x_client->isActive())
  337. x_client->deactivate();
  338. // Remove client ports
  339. removeClientPorts();
  340. // Delete old data
  341. deleteBuffers();
  342. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  343. aIns = aOuts = mIns = mOuts = params = 0;
  344. const double sampleRate = x_engine->getSampleRate();
  345. aIns = descriptor->audioIns;
  346. aOuts = descriptor->audioOuts;
  347. mIns = descriptor->midiIns;
  348. mOuts = descriptor->midiOuts;
  349. params = descriptor->get_parameter_count ? descriptor->get_parameter_count(handle) : 0;
  350. bool forcedStereoIn, forcedStereoOut;
  351. forcedStereoIn = forcedStereoOut = false;
  352. if (x_engine->getOptions().forceStereo && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1 && ! h2)
  353. {
  354. h2 = descriptor->instantiate(descriptor, &host);
  355. if (aIns == 1)
  356. {
  357. aIns = 2;
  358. forcedStereoIn = true;
  359. }
  360. if (aOuts == 1)
  361. {
  362. aOuts = 2;
  363. forcedStereoOut = true;
  364. }
  365. }
  366. if (aIns > 0)
  367. {
  368. aIn.ports = new CarlaEngineAudioPort*[aIns];
  369. aIn.rindexes = new uint32_t[aIns];
  370. }
  371. if (aOuts > 0)
  372. {
  373. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  374. aOut.rindexes = new uint32_t[aOuts];
  375. }
  376. if (mIns > 0)
  377. {
  378. mIn.ports = new CarlaEngineMidiPort*[mIns];
  379. mIn.indexes = new uint32_t[mIns];
  380. }
  381. if (mOuts > 0)
  382. {
  383. mOut.ports = new CarlaEngineMidiPort*[mOuts];
  384. mOut.indexes = new uint32_t[mOuts];
  385. }
  386. if (params > 0)
  387. {
  388. param.data = new ParameterData[params];
  389. param.ranges = new ParameterRanges[params];
  390. }
  391. const int portNameSize = x_engine->maxPortNameSize() - 2;
  392. char portName[portNameSize];
  393. bool needsCtrlIn = false;
  394. bool needsCtrlOut = false;
  395. // Audio Ins
  396. for (j=0; j < descriptor->audioIns; j++)
  397. {
  398. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  399. sprintf(portName, "%s:input_%02i", m_name, j+1);
  400. else
  401. sprintf(portName, "input_%02i", j+1);
  402. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  403. aIn.rindexes[j] = j;
  404. if (forcedStereoIn)
  405. {
  406. strcat(portName, "_");
  407. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  408. aIn.rindexes[1] = j;
  409. }
  410. }
  411. // Audio Outs
  412. for (j=0; j < descriptor->audioOuts; j++)
  413. {
  414. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  415. sprintf(portName, "%s:output_%02i", m_name, j+1);
  416. else
  417. sprintf(portName, "output_%02i", j+1);
  418. qDebug("Audio Out #%i", j);
  419. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  420. aOut.rindexes[j] = j;
  421. needsCtrlIn = true;
  422. if (forcedStereoOut)
  423. {
  424. strcat(portName, "_");
  425. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  426. aOut.rindexes[1] = j;
  427. }
  428. }
  429. // MIDI Input
  430. for (j=0; j < mIns; j++)
  431. {
  432. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  433. sprintf(portName, "%s:midi-in_%02i", m_name, j+1);
  434. else
  435. sprintf(portName, "midi-in_%02i", j+1);
  436. mIn.ports[j] = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  437. mIn.indexes[j] = j;
  438. }
  439. // MIDI Output
  440. for (j=0; j < mOuts; j++)
  441. {
  442. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  443. sprintf(portName, "%s:midi-out_%02i", m_name, j+1);
  444. else
  445. sprintf(portName, "midi-out_%02i", j+1);
  446. mOut.ports[j] = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  447. mOut.indexes[j] = j;
  448. }
  449. for (j=0; j < params; j++)
  450. {
  451. if (! descriptor->get_parameter_info)
  452. break;
  453. const ::Parameter* const paramInfo = descriptor->get_parameter_info(handle, j);
  454. //const uint32_t paramHints = param->hints;
  455. //const bool paramOutput = paramHins;
  456. param.data[j].index = j;
  457. param.data[j].rindex = j;
  458. param.data[j].hints = 0;
  459. param.data[j].midiChannel = 0;
  460. param.data[j].midiCC = -1;
  461. double min, max, def, step, stepSmall, stepLarge;
  462. // min value
  463. min = paramInfo->ranges.min;
  464. // max value
  465. max = paramInfo->ranges.max;
  466. if (min > max)
  467. max = min;
  468. else if (max < min)
  469. min = max;
  470. if (max - min == 0.0)
  471. {
  472. qWarning("Broken plugin parameter: max - min == 0");
  473. max = min + 0.1;
  474. }
  475. // default value
  476. def = paramInfo->ranges.def;
  477. if (def < min)
  478. def = min;
  479. else if (def > max)
  480. def = max;
  481. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  482. {
  483. min *= sampleRate;
  484. max *= sampleRate;
  485. def *= sampleRate;
  486. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  487. }
  488. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  489. {
  490. step = max - min;
  491. stepSmall = step;
  492. stepLarge = step;
  493. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  494. }
  495. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  496. {
  497. step = 1.0;
  498. stepSmall = 1.0;
  499. stepLarge = 10.0;
  500. param.data[j].hints |= PARAMETER_IS_INTEGER;
  501. }
  502. else
  503. {
  504. double range = max - min;
  505. step = range/100.0;
  506. stepSmall = range/1000.0;
  507. stepLarge = range/10.0;
  508. }
  509. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  510. {
  511. param.data[j].type = PARAMETER_OUTPUT;
  512. needsCtrlOut = true;
  513. }
  514. else
  515. {
  516. param.data[j].type = PARAMETER_INPUT;
  517. needsCtrlIn = true;
  518. }
  519. // extra parameter hints
  520. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  521. param.data[j].hints |= PARAMETER_IS_ENABLED;
  522. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  523. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  524. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  525. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  526. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  527. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  528. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  529. param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  530. param.ranges[j].min = min;
  531. param.ranges[j].max = max;
  532. param.ranges[j].def = def;
  533. param.ranges[j].step = step;
  534. param.ranges[j].stepSmall = stepSmall;
  535. param.ranges[j].stepLarge = stepLarge;
  536. }
  537. if (needsCtrlIn)
  538. {
  539. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  540. {
  541. strcpy(portName, m_name);
  542. strcat(portName, ":control-in");
  543. }
  544. else
  545. strcpy(portName, "control-in");
  546. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  547. }
  548. if (needsCtrlOut)
  549. {
  550. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  551. {
  552. strcpy(portName, m_name);
  553. strcat(portName, ":control-out");
  554. }
  555. else
  556. strcpy(portName, "control-out");
  557. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  558. }
  559. aIn.count = aIns;
  560. aOut.count = aOuts;
  561. mIn.count = mIns;
  562. mOut.count = mOuts;
  563. param.count = params;
  564. // plugin checks
  565. fHints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  566. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  567. fHints |= PLUGIN_CAN_DRYWET;
  568. if (aOuts > 0)
  569. fHints |= PLUGIN_CAN_VOLUME;
  570. if (aOuts >= 2 && aOuts%2 == 0)
  571. fHints |= PLUGIN_CAN_BALANCE;
  572. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  573. fHints |= PLUGIN_CAN_FORCE_STEREO;
  574. #endif
  575. // native plugin hints
  576. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  577. fHints |= PLUGIN_IS_RTSAFE;
  578. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  579. fHints |= PLUGIN_IS_SYNTH;
  580. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  581. fHints |= PLUGIN_HAS_GUI;
  582. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  583. fHints |= PLUGIN_USES_SINGLE_THREAD;
  584. reloadPrograms(true);
  585. kData->client->activate();
  586. qDebug("NativePlugin::reload() - end");
  587. }
  588. #if 0
  589. void reloadPrograms(const bool init)
  590. {
  591. qDebug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  592. uint32_t i, oldCount = midiprog.count;
  593. // Delete old programs
  594. if (midiprog.count > 0)
  595. {
  596. for (i=0; i < midiprog.count; i++)
  597. {
  598. if (midiprog.data[i].name)
  599. free((void*)midiprog.data[i].name);
  600. }
  601. delete[] midiprog.data;
  602. }
  603. midiprog.count = (descriptor->get_midi_program_count && descriptor->get_midi_program_info) ? descriptor->get_midi_program_count(handle) : 0;
  604. midiprog.data = nullptr;
  605. if (midiprog.count > 0)
  606. midiprog.data = new MidiProgramData[midiprog.count];
  607. // Update data
  608. for (i=0; i < midiprog.count; i++)
  609. {
  610. const ::MidiProgram* const mpDesc = descriptor->get_midi_program_info(handle, i);
  611. CARLA_ASSERT(mpDesc);
  612. CARLA_ASSERT(mpDesc->name);
  613. midiprog.data[i].bank = mpDesc->bank;
  614. midiprog.data[i].program = mpDesc->program;
  615. midiprog.data[i].name = strdup(mpDesc->name);
  616. }
  617. // Update OSC Names
  618. if (x_engine->isOscControlRegistered())
  619. {
  620. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  621. for (i=0; i < midiprog.count; i++)
  622. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  623. }
  624. if (init)
  625. {
  626. if (midiprog.count > 0)
  627. setMidiProgram(0, false, false, false, true);
  628. }
  629. else
  630. {
  631. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0, nullptr);
  632. // Check if current program is invalid
  633. bool programChanged = false;
  634. if (midiprog.count == oldCount+1)
  635. {
  636. // one midi program added, probably created by user
  637. midiprog.current = oldCount;
  638. programChanged = true;
  639. }
  640. else if (midiprog.current >= (int32_t)midiprog.count)
  641. {
  642. // current midi program > count
  643. midiprog.current = 0;
  644. programChanged = true;
  645. }
  646. else if (midiprog.current < 0 && midiprog.count > 0)
  647. {
  648. // programs exist now, but not before
  649. midiprog.current = 0;
  650. programChanged = true;
  651. }
  652. else if (midiprog.current >= 0 && midiprog.count == 0)
  653. {
  654. // programs existed before, but not anymore
  655. midiprog.current = -1;
  656. programChanged = true;
  657. }
  658. if (programChanged)
  659. setMidiProgram(midiprog.current, true, true, true, true);
  660. }
  661. }
  662. // -------------------------------------------------------------------
  663. // Plugin processing
  664. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  665. {
  666. uint32_t i, k;
  667. double aInsPeak[2] = { 0.0 };
  668. double aOutsPeak[2] = { 0.0 };
  669. // reset MIDI
  670. midiEventCount = 0;
  671. memset(midiEvents, 0, sizeof(::MidiEvent) * MAX_MIDI_EVENTS * 2);
  672. CARLA_PROCESS_CONTINUE_CHECK;
  673. // --------------------------------------------------------------------------------------------------------
  674. // Input VU
  675. if (aIn.count > 0 && x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  676. {
  677. if (aIn.count == 1)
  678. {
  679. for (k=0; k < frames; k++)
  680. {
  681. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  682. aInsPeak[0] = std::abs(inBuffer[0][k]);
  683. }
  684. }
  685. else if (aIn.count > 1)
  686. {
  687. for (k=0; k < frames; k++)
  688. {
  689. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  690. aInsPeak[0] = std::abs(inBuffer[0][k]);
  691. if (std::abs(inBuffer[1][k]) > aInsPeak[1])
  692. aInsPeak[1] = std::abs(inBuffer[1][k]);
  693. }
  694. }
  695. }
  696. CARLA_PROCESS_CONTINUE_CHECK;
  697. // --------------------------------------------------------------------------------------------------------
  698. // Parameters Input [Automation]
  699. if (param.portCin && m_active && m_activeBefore)
  700. {
  701. bool allNotesOffSent = false;
  702. const CarlaEngineControlEvent* cinEvent;
  703. uint32_t time, nEvents = param.portCin->getEventCount();
  704. uint32_t nextBankId = 0;
  705. if (midiprog.current >= 0 && midiprog.count > 0)
  706. nextBankId = midiprog.data[midiprog.current].bank;
  707. for (i=0; i < nEvents; i++)
  708. {
  709. cinEvent = param.portCin->getEvent(i);
  710. if (! cinEvent)
  711. continue;
  712. time = cinEvent->time - framesOffset;
  713. if (time >= frames)
  714. continue;
  715. // Control change
  716. switch (cinEvent->type)
  717. {
  718. case CarlaEngineNullEvent:
  719. break;
  720. case CarlaEngineParameterChangeEvent:
  721. {
  722. double value;
  723. // Control backend stuff
  724. if (cinEvent->channel == m_ctrlInChannel)
  725. {
  726. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  727. {
  728. value = cinEvent->value;
  729. setDryWet(value, false, false);
  730. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  731. continue;
  732. }
  733. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  734. {
  735. value = cinEvent->value*127/100;
  736. setVolume(value, false, false);
  737. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  738. continue;
  739. }
  740. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  741. {
  742. double left, right;
  743. value = cinEvent->value/0.5 - 1.0;
  744. if (value < 0.0)
  745. {
  746. left = -1.0;
  747. right = (value*2)+1.0;
  748. }
  749. else if (value > 0.0)
  750. {
  751. left = (value*2)-1.0;
  752. right = 1.0;
  753. }
  754. else
  755. {
  756. left = -1.0;
  757. right = 1.0;
  758. }
  759. setBalanceLeft(left, false, false);
  760. setBalanceRight(right, false, false);
  761. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  762. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  763. continue;
  764. }
  765. }
  766. // Control plugin parameters
  767. for (k=0; k < param.count; k++)
  768. {
  769. if (param.data[k].midiChannel != cinEvent->channel)
  770. continue;
  771. if (param.data[k].midiCC != cinEvent->parameter)
  772. continue;
  773. if (param.data[k].type != PARAMETER_INPUT)
  774. continue;
  775. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  776. {
  777. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  778. {
  779. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  780. }
  781. else
  782. {
  783. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  784. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  785. value = rint(value);
  786. }
  787. setParameterValue(k, value, false, false, false);
  788. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  789. }
  790. }
  791. break;
  792. }
  793. case CarlaEngineMidiBankChangeEvent:
  794. if (cinEvent->channel == m_ctrlInChannel)
  795. nextBankId = rint(cinEvent->value);
  796. break;
  797. case CarlaEngineMidiProgramChangeEvent:
  798. if (cinEvent->channel == m_ctrlInChannel)
  799. {
  800. uint32_t nextProgramId = rint(cinEvent->value);
  801. for (k=0; k < midiprog.count; k++)
  802. {
  803. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  804. {
  805. setMidiProgram(k, false, false, false, false);
  806. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  807. break;
  808. }
  809. }
  810. }
  811. break;
  812. case CarlaEngineAllSoundOffEvent:
  813. if (cinEvent->channel == m_ctrlInChannel)
  814. {
  815. if (mIn.count > 0 && ! allNotesOffSent)
  816. sendMidiAllNotesOff();
  817. if (descriptor->deactivate)
  818. {
  819. descriptor->deactivate(handle);
  820. if (h2) descriptor->deactivate(h2);
  821. }
  822. if (descriptor->activate)
  823. {
  824. descriptor->activate(handle);
  825. if (h2) descriptor->activate(h2);
  826. }
  827. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  828. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  829. allNotesOffSent = true;
  830. }
  831. break;
  832. case CarlaEngineAllNotesOffEvent:
  833. if (cinEvent->channel == m_ctrlInChannel)
  834. {
  835. if (mIn.count > 0 && ! allNotesOffSent)
  836. sendMidiAllNotesOff();
  837. allNotesOffSent = true;
  838. }
  839. break;
  840. }
  841. }
  842. } // End of Parameters Input
  843. CARLA_PROCESS_CONTINUE_CHECK;
  844. // --------------------------------------------------------------------------------------------------------
  845. // MIDI Input
  846. if (mIn.count > 0 && m_active && m_activeBefore)
  847. {
  848. // ----------------------------------------------------------------------------------------------------
  849. // MIDI Input (External)
  850. {
  851. engineMidiLock();
  852. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  853. {
  854. if (extMidiNotes[i].channel < 0)
  855. break;
  856. ::MidiEvent* const midiEvent = &midiEvents[midiEventCount];
  857. memset(midiEvent, 0, sizeof(::MidiEvent));
  858. midiEvent->data[0] = uint8_t(extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + extMidiNotes[i].channel;
  859. midiEvent->data[1] = extMidiNotes[i].note;
  860. midiEvent->data[2] = extMidiNotes[i].velo;
  861. extMidiNotes[i].channel = -1; // mark as invalid
  862. midiEventCount += 1;
  863. }
  864. engineMidiUnlock();
  865. } // End of MIDI Input (External)
  866. CARLA_PROCESS_CONTINUE_CHECK;
  867. // ----------------------------------------------------------------------------------------------------
  868. // MIDI Input (System)
  869. for (i=0; i < mIn.count; i++)
  870. {
  871. if (! mIn.ports[i])
  872. continue;
  873. const CarlaEngineMidiEvent* minEvent;
  874. uint32_t time, nEvents = mIn.ports[i]->getEventCount();
  875. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  876. {
  877. minEvent = mIn.ports[i]->getEvent(k);
  878. if (! minEvent)
  879. continue;
  880. time = minEvent->time - framesOffset;
  881. if (time >= frames)
  882. continue;
  883. uint8_t status = minEvent->data[0];
  884. uint8_t channel = status & 0x0F;
  885. // Fix bad note-off
  886. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  887. status -= 0x10;
  888. ::MidiEvent* const midiEvent = &midiEvents[midiEventCount];
  889. memset(midiEvent, 0, sizeof(::MidiEvent));
  890. midiEvent->port = i;
  891. midiEvent->time = minEvent->time;
  892. if (MIDI_IS_STATUS_NOTE_OFF(status))
  893. {
  894. uint8_t note = minEvent->data[1];
  895. midiEvent->data[0] = status;
  896. midiEvent->data[1] = note;
  897. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  898. }
  899. else if (MIDI_IS_STATUS_NOTE_ON(status))
  900. {
  901. uint8_t note = minEvent->data[1];
  902. uint8_t velo = minEvent->data[2];
  903. midiEvent->data[0] = status;
  904. midiEvent->data[1] = note;
  905. midiEvent->data[2] = velo;
  906. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  907. }
  908. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  909. {
  910. uint8_t note = minEvent->data[1];
  911. uint8_t pressure = minEvent->data[2];
  912. midiEvent->data[0] = status;
  913. midiEvent->data[1] = note;
  914. midiEvent->data[2] = pressure;
  915. }
  916. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  917. {
  918. uint8_t pressure = minEvent->data[1];
  919. midiEvent->data[0] = status;
  920. midiEvent->data[1] = pressure;
  921. }
  922. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  923. {
  924. uint8_t lsb = minEvent->data[1];
  925. uint8_t msb = minEvent->data[2];
  926. midiEvent->data[0] = status;
  927. midiEvent->data[1] = lsb;
  928. midiEvent->data[2] = msb;
  929. }
  930. else
  931. continue;
  932. midiEventCount += 1;
  933. }
  934. } // End of MIDI Input (System)
  935. } // End of MIDI Input
  936. CARLA_PROCESS_CONTINUE_CHECK;
  937. // --------------------------------------------------------------------------------------------------------
  938. // Plugin processing
  939. uint32_t midiEventCountBefore = midiEventCount;
  940. if (m_active)
  941. {
  942. if (! m_activeBefore)
  943. {
  944. if (mIn.count > 0)
  945. {
  946. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  947. {
  948. memset(&midiEvents[k], 0, sizeof(::MidiEvent));
  949. midiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  950. midiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  951. memset(&midiEvents[k*2], 0, sizeof(::MidiEvent));
  952. midiEvents[k*2].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  953. midiEvents[k*2].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  954. }
  955. midiEventCount = MAX_MIDI_CHANNELS*2;
  956. }
  957. if (descriptor->activate)
  958. {
  959. descriptor->activate(handle);
  960. if (h2) descriptor->activate(h2);
  961. }
  962. }
  963. isProcessing = true;
  964. if (h2)
  965. {
  966. descriptor->process(handle, inBuffer? &inBuffer[0] : nullptr, outBuffer? &outBuffer[0] : nullptr, frames, midiEventCountBefore, midiEvents);
  967. descriptor->process(h2, inBuffer? &inBuffer[1] : nullptr, outBuffer? &outBuffer[1] : nullptr, frames, midiEventCountBefore, midiEvents);
  968. }
  969. else
  970. descriptor->process(handle, inBuffer, outBuffer, frames, midiEventCountBefore, midiEvents);
  971. isProcessing = false;
  972. }
  973. else
  974. {
  975. if (m_activeBefore)
  976. {
  977. if (descriptor->deactivate)
  978. {
  979. descriptor->deactivate(handle);
  980. if (h2) descriptor->deactivate(h2);
  981. }
  982. }
  983. }
  984. CARLA_PROCESS_CONTINUE_CHECK;
  985. // --------------------------------------------------------------------------------------------------------
  986. // Post-processing (dry/wet, volume and balance)
  987. if (m_active)
  988. {
  989. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  990. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  991. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  992. double bal_rangeL, bal_rangeR;
  993. float bufValue, oldBufLeft[do_balance ? frames : 0];
  994. for (i=0; i < aOut.count; i++)
  995. {
  996. // Dry/Wet
  997. if (do_drywet)
  998. {
  999. for (k=0; k < frames; k++)
  1000. {
  1001. bufValue = (aIn.count == 1) ? inBuffer[0][k] : inBuffer[i][k];
  1002. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1003. }
  1004. }
  1005. // Balance
  1006. if (do_balance)
  1007. {
  1008. if (i%2 == 0)
  1009. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1010. bal_rangeL = (x_balanceLeft+1.0)/2;
  1011. bal_rangeR = (x_balanceRight+1.0)/2;
  1012. for (k=0; k < frames; k++)
  1013. {
  1014. if (i%2 == 0)
  1015. {
  1016. // left output
  1017. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1018. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1019. }
  1020. else
  1021. {
  1022. // right
  1023. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1024. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1025. }
  1026. }
  1027. }
  1028. // Volume
  1029. if (do_volume)
  1030. {
  1031. for (k=0; k < frames; k++)
  1032. outBuffer[i][k] *= x_volume;
  1033. }
  1034. // Output VU
  1035. if (x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  1036. {
  1037. for (k=0; i < 2 && k < frames; k++)
  1038. {
  1039. if (std::abs(outBuffer[i][k]) > aOutsPeak[i])
  1040. aOutsPeak[i] = std::abs(outBuffer[i][k]);
  1041. }
  1042. }
  1043. }
  1044. }
  1045. else
  1046. {
  1047. // disable any output sound if not active
  1048. for (i=0; i < aOut.count; i++)
  1049. carla_zeroF(outBuffer[i], frames);
  1050. aOutsPeak[0] = 0.0;
  1051. aOutsPeak[1] = 0.0;
  1052. } // End of Post-processing
  1053. CARLA_PROCESS_CONTINUE_CHECK;
  1054. // --------------------------------------------------------------------------------------------------------
  1055. // MIDI Output
  1056. if (mOut.count > 0 && m_active)
  1057. {
  1058. uint8_t data[3] = { 0 };
  1059. for (uint32_t i = midiEventCountBefore; i < midiEventCount; i++)
  1060. {
  1061. data[0] = midiEvents[i].data[0];
  1062. data[1] = midiEvents[i].data[1];
  1063. data[2] = midiEvents[i].data[2];
  1064. // Fix bad note-off
  1065. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  1066. data[0] -= 0x10;
  1067. const uint32_t port = midiEvents[i].port;
  1068. if (port < mOut.count)
  1069. mOut.ports[port]->writeEvent(midiEvents[i].time, data, 3);
  1070. }
  1071. } // End of MIDI Output
  1072. // --------------------------------------------------------------------------------------------------------
  1073. // Control Output
  1074. if (param.portCout && m_active)
  1075. {
  1076. double value, valueControl;
  1077. for (k=0; k < param.count; k++)
  1078. {
  1079. if (param.data[k].type == PARAMETER_OUTPUT)
  1080. {
  1081. value = descriptor->get_parameter_value(handle, param.data[k].rindex);
  1082. if (param.data[k].midiCC > 0)
  1083. {
  1084. valueControl = (value - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1085. param.portCout->writeEvent(CarlaEngineParameterChangeEvent, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, valueControl);
  1086. }
  1087. }
  1088. }
  1089. } // End of Control Output
  1090. CARLA_PROCESS_CONTINUE_CHECK;
  1091. // --------------------------------------------------------------------------------------------------------
  1092. // Peak Values
  1093. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1094. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1095. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1096. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1097. m_activeBefore = m_active;
  1098. }
  1099. // -------------------------------------------------------------------
  1100. // Cleanup
  1101. void removeClientPorts()
  1102. {
  1103. qDebug("NativePlugin::removeClientPorts() - start");
  1104. for (uint32_t i=0; i < mIn.count; i++)
  1105. {
  1106. delete mIn.ports[i];
  1107. mIn.ports[i] = nullptr;
  1108. }
  1109. for (uint32_t i=0; i < mOut.count; i++)
  1110. {
  1111. delete mOut.ports[i];
  1112. mOut.ports[i] = nullptr;
  1113. }
  1114. CarlaPlugin::removeClientPorts();
  1115. qDebug("NativePlugin::removeClientPorts() - end");
  1116. }
  1117. void initBuffers()
  1118. {
  1119. for (uint32_t i=0; i < mIn.count; i++)
  1120. {
  1121. if (mIn.ports[i])
  1122. mIn.ports[i]->initBuffer(x_engine);
  1123. }
  1124. for (uint32_t i=0; i < mOut.count; i++)
  1125. {
  1126. if (mOut.ports[i])
  1127. mOut.ports[i]->initBuffer(x_engine);
  1128. }
  1129. CarlaPlugin::initBuffers();
  1130. }
  1131. void deleteBuffers()
  1132. {
  1133. qDebug("NativePlugin::deleteBuffers() - start");
  1134. if (mIn.count > 0)
  1135. {
  1136. delete[] mIn.ports;
  1137. delete[] mIn.indexes;
  1138. }
  1139. if (mOut.count > 0)
  1140. {
  1141. delete[] mOut.ports;
  1142. delete[] mOut.indexes;
  1143. }
  1144. mIn.count = 0;
  1145. mIn.ports = nullptr;
  1146. mIn.indexes = nullptr;
  1147. mOut.count = 0;
  1148. mOut.ports = nullptr;
  1149. mOut.indexes = nullptr;
  1150. CarlaPlugin::deleteBuffers();
  1151. qDebug("NativePlugin::deleteBuffers() - end");
  1152. }
  1153. // -------------------------------------------------------------------
  1154. #endif
  1155. protected:
  1156. uint32_t handleGetBufferSize()
  1157. {
  1158. return kData->engine->getBufferSize();
  1159. }
  1160. double handleGetSampleRate()
  1161. {
  1162. return kData->engine->getSampleRate();
  1163. }
  1164. const TimeInfo* handleGetTimeInfo()
  1165. {
  1166. // TODO
  1167. return nullptr;
  1168. }
  1169. bool handleWriteMidiEvent(const MidiEvent* const event)
  1170. {
  1171. CARLA_ASSERT(fEnabled);
  1172. //CARLA_ASSERT(mOut.count > 0);
  1173. CARLA_ASSERT(fIsProcessing);
  1174. CARLA_ASSERT(event != nullptr);
  1175. if (! fEnabled)
  1176. return false;
  1177. //if (mOut.count == 0)
  1178. // return false;
  1179. if (! fIsProcessing)
  1180. {
  1181. qCritical("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out events outside audio thread, ignoring", event);
  1182. return false;
  1183. }
  1184. //if (midiEventCount >= MAX_MIDI_EVENTS*2)
  1185. // return false;
  1186. //memcpy(&midiEvents[midiEventCount], event, sizeof(::MidiEvent));
  1187. //midiEventCount += 1;
  1188. return true;
  1189. }
  1190. void handleUiParameterChanged(const uint32_t index, const float value)
  1191. {
  1192. setParameterValue(index, value, false, true, true);
  1193. }
  1194. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1195. {
  1196. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1197. }
  1198. void handleUiClosed()
  1199. {
  1200. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1201. }
  1202. public:
  1203. static size_t getPluginCount()
  1204. {
  1205. maybeFirstInit();
  1206. return sPluginDescriptors.size();
  1207. }
  1208. static const PluginDescriptor* getPlugin(const size_t index)
  1209. {
  1210. maybeFirstInit();
  1211. CARLA_ASSERT(index < sPluginDescriptors.size());
  1212. if (index < sPluginDescriptors.size())
  1213. return sPluginDescriptors[index];
  1214. return nullptr;
  1215. }
  1216. static void registerPlugin(const PluginDescriptor* desc)
  1217. {
  1218. sPluginDescriptors.push_back(desc);
  1219. }
  1220. static void maybeFirstInit()
  1221. {
  1222. if (! sFirstInit)
  1223. return;
  1224. sFirstInit = false;
  1225. #ifndef BUILD_BRIDGE
  1226. carla_register_native_plugin_bypass();
  1227. carla_register_native_plugin_midiSplit();
  1228. carla_register_native_plugin_midiThrough();
  1229. carla_register_native_plugin_3BandEQ();
  1230. carla_register_native_plugin_3BandSplitter();
  1231. carla_register_native_plugin_PingPongPan();
  1232. # ifdef WANT_ZYNADDSUBFX
  1233. carla_register_native_plugin_zynaddsubfx();
  1234. # endif
  1235. #endif
  1236. }
  1237. // -------------------------------------------------------------------
  1238. bool init(const char* const name, const char* const label)
  1239. {
  1240. CARLA_ASSERT(kData->engine != nullptr);
  1241. CARLA_ASSERT(kData->client == nullptr);
  1242. CARLA_ASSERT(label);
  1243. // ---------------------------------------------------------------
  1244. // initialize native-plugins descriptors
  1245. maybeFirstInit();
  1246. // ---------------------------------------------------------------
  1247. // get descriptor that matches label
  1248. for (size_t i=0; i < sPluginDescriptors.size(); i++)
  1249. {
  1250. fDescriptor = sPluginDescriptors[i];
  1251. if (fDescriptor == nullptr)
  1252. break;
  1253. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1254. break;
  1255. fDescriptor = nullptr;
  1256. }
  1257. if (fDescriptor == nullptr)
  1258. {
  1259. kData->engine->setLastError("Invalid internal plugin");
  1260. return false;
  1261. }
  1262. // ---------------------------------------------------------------
  1263. // get info
  1264. if (name != nullptr)
  1265. fName = kData->engine->getNewUniquePluginName(name);
  1266. else if (fDescriptor->name != nullptr)
  1267. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1268. else
  1269. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1270. // ---------------------------------------------------------------
  1271. // register client
  1272. kData->client = kData->engine->addClient(this);
  1273. if (kData->client == nullptr || ! kData->client->isOk())
  1274. {
  1275. kData->engine->setLastError("Failed to register plugin client");
  1276. return false;
  1277. }
  1278. // ---------------------------------------------------------------
  1279. // initialize plugin
  1280. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1281. if (fHandle == nullptr)
  1282. {
  1283. kData->engine->setLastError("Plugin failed to initialize");
  1284. return false;
  1285. }
  1286. return true;
  1287. }
  1288. private:
  1289. PluginHandle fHandle;
  1290. PluginHandle fHandle2;
  1291. HostDescriptor fHost;
  1292. const PluginDescriptor* fDescriptor;
  1293. bool fIsProcessing;
  1294. #if 0
  1295. NativePluginMidiData mIn;
  1296. NativePluginMidiData mOut;
  1297. uint32_t midiEventCount;
  1298. ::MidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1299. #endif
  1300. static bool sFirstInit;
  1301. static std::vector<const PluginDescriptor*> sPluginDescriptors;
  1302. // -------------------------------------------------------------------
  1303. #define handlePtr ((NativePlugin*)handle)
  1304. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1305. {
  1306. return handlePtr->handleGetBufferSize();
  1307. }
  1308. static double carla_host_get_sample_rate(HostHandle handle)
  1309. {
  1310. return handlePtr->handleGetSampleRate();
  1311. }
  1312. static const TimeInfo* carla_host_get_time_info(HostHandle handle)
  1313. {
  1314. return handlePtr->handleGetTimeInfo();
  1315. }
  1316. static bool carla_host_write_midi_event(HostHandle handle, const MidiEvent* event)
  1317. {
  1318. return handlePtr->handleWriteMidiEvent(event);
  1319. }
  1320. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1321. {
  1322. handlePtr->handleUiParameterChanged(index, value);
  1323. }
  1324. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1325. {
  1326. handlePtr->handleUiCustomDataChanged(key, value);
  1327. }
  1328. static void carla_host_ui_closed(HostHandle handle)
  1329. {
  1330. handlePtr->handleUiClosed();
  1331. }
  1332. #undef handlePtr
  1333. };
  1334. bool NativePlugin::sFirstInit = true;
  1335. std::vector<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1336. // -----------------------------------------------------------------------
  1337. size_t CarlaPlugin::getNativePluginCount()
  1338. {
  1339. return NativePlugin::getPluginCount();
  1340. }
  1341. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1342. {
  1343. return NativePlugin::getPlugin(index);
  1344. }
  1345. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1346. {
  1347. qDebug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1348. NativePlugin* const plugin = new NativePlugin(init.engine, init.id);
  1349. if (! plugin->init(init.name, init.label))
  1350. {
  1351. delete plugin;
  1352. return nullptr;
  1353. }
  1354. plugin->reload();
  1355. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1356. {
  1357. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1358. delete plugin;
  1359. return nullptr;
  1360. }
  1361. plugin->registerToOscClient();
  1362. return plugin;
  1363. }
  1364. // -----------------------------------------------------------------------
  1365. CARLA_BACKEND_END_NAMESPACE
  1366. void carla_register_native_plugin(const PluginDescriptor* desc)
  1367. {
  1368. CARLA_BACKEND_USE_NAMESPACE
  1369. NativePlugin::registerPlugin(desc);
  1370. }
  1371. // -----------------------------------------------------------------------