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.

1665 lines
52KB

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