Collection of tools useful for audio production
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.

1714 lines
53KB

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