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.

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