Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1795 lines
59KB

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