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.

1796 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. bool forcedStereoIn, forcedStereoOut;
  395. forcedStereoIn = forcedStereoOut = false;
  396. bool needsCtrlIn, needsCtrlOut;
  397. needsCtrlIn = needsCtrlOut = false;
  398. aIns = fDescriptor->audioIns;
  399. aOuts = fDescriptor->audioOuts;
  400. mIns = fDescriptor->midiIns;
  401. mOuts = fDescriptor->midiOuts;
  402. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  403. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  404. {
  405. if (fHandle2 == nullptr)
  406. fHandle2 = fDescriptor->instantiate(fDescriptor, &fHost);
  407. if (aIns == 1)
  408. {
  409. aIns = 2;
  410. forcedStereoIn = true;
  411. }
  412. if (aOuts == 1)
  413. {
  414. aOuts = 2;
  415. forcedStereoOut = true;
  416. }
  417. }
  418. if (aIns > 0)
  419. {
  420. kData->audioIn.createNew(aIns);
  421. fAudioInBuffers = new float*[aIns];
  422. for (uint32_t i=0; i < aIns; i++)
  423. fAudioInBuffers[i] = nullptr;
  424. }
  425. if (aOuts > 0)
  426. {
  427. kData->audioOut.createNew(aOuts);
  428. fAudioOutBuffers = new float*[aOuts];
  429. needsCtrlIn = true;
  430. for (uint32_t i=0; i < aOuts; i++)
  431. fAudioOutBuffers[i] = nullptr;
  432. }
  433. if (mIns > 0)
  434. {
  435. fMidiIn.createNew(mIns);
  436. needsCtrlIn = (mIns == 1);
  437. }
  438. if (mOuts > 0)
  439. {
  440. fMidiOut.createNew(mOuts);
  441. needsCtrlOut = (mOuts == 1);
  442. }
  443. if (params > 0)
  444. {
  445. kData->param.createNew(params);
  446. }
  447. const int portNameSize = kData->engine->maxPortNameSize();
  448. CarlaString portName;
  449. // Audio Ins
  450. for (j=0; j < aIns; j++)
  451. {
  452. portName.clear();
  453. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  454. {
  455. portName = fName;
  456. portName += ":";
  457. }
  458. if (aIns > 1)
  459. {
  460. portName += "input_";
  461. portName += CarlaString(j+1);
  462. }
  463. else
  464. portName += "input";
  465. portName.truncate(portNameSize);
  466. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  467. kData->audioIn.ports[j].rindex = j;
  468. if (forcedStereoIn)
  469. {
  470. portName += "_2";
  471. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  472. kData->audioIn.ports[1].rindex = j;
  473. break;
  474. }
  475. }
  476. // Audio Outs
  477. for (j=0; j < aOuts; j++)
  478. {
  479. portName.clear();
  480. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  481. {
  482. portName = fName;
  483. portName += ":";
  484. }
  485. if (aOuts > 1)
  486. {
  487. portName += "output_";
  488. portName += CarlaString(j+1);
  489. }
  490. else
  491. portName += "output";
  492. portName.truncate(portNameSize);
  493. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  494. kData->audioOut.ports[j].rindex = j;
  495. if (forcedStereoOut)
  496. {
  497. portName += "_2";
  498. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  499. kData->audioOut.ports[1].rindex = j;
  500. break;
  501. }
  502. }
  503. // MIDI Input (only if multiple)
  504. if (mIns > 1)
  505. {
  506. for (j=0; j < mIns; j++)
  507. {
  508. portName.clear();
  509. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  510. {
  511. portName = fName;
  512. portName += ":";
  513. }
  514. portName += "midi-in_";
  515. portName += CarlaString(j+1);
  516. portName.truncate(portNameSize);
  517. fMidiIn.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  518. fMidiIn.indexes[j] = j;
  519. }
  520. }
  521. // MIDI Output (only if multiple)
  522. if (mOuts > 1)
  523. {
  524. for (j=0; j < mOuts; j++)
  525. {
  526. portName.clear();
  527. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  528. {
  529. portName = fName;
  530. portName += ":";
  531. }
  532. portName += "midi-out_";
  533. portName += CarlaString(j+1);
  534. portName.truncate(portNameSize);
  535. fMidiOut.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  536. fMidiOut.indexes[j] = j;
  537. }
  538. }
  539. for (j=0; j < params; j++)
  540. {
  541. const ::Parameter* const paramInfo = fDescriptor->get_parameter_info(fHandle, j);
  542. kData->param.data[j].index = j;
  543. kData->param.data[j].rindex = j;
  544. kData->param.data[j].hints = 0x0;
  545. kData->param.data[j].midiChannel = 0;
  546. kData->param.data[j].midiCC = -1;
  547. float min, max, def, step, stepSmall, stepLarge;
  548. // min value
  549. min = paramInfo->ranges.min;
  550. // max value
  551. max = paramInfo->ranges.max;
  552. if (min > max)
  553. max = min;
  554. else if (max < min)
  555. min = max;
  556. if (max - min == 0.0f)
  557. {
  558. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  559. max = min + 0.1f;
  560. }
  561. // default value
  562. def = paramInfo->ranges.def;
  563. if (def < min)
  564. def = min;
  565. else if (def > max)
  566. def = max;
  567. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  568. {
  569. min *= sampleRate;
  570. max *= sampleRate;
  571. def *= sampleRate;
  572. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  573. }
  574. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  575. {
  576. step = max - min;
  577. stepSmall = step;
  578. stepLarge = step;
  579. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  580. }
  581. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  582. {
  583. step = 1.0f;
  584. stepSmall = 1.0f;
  585. stepLarge = 10.0f;
  586. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  587. }
  588. else
  589. {
  590. float range = max - min;
  591. step = range/100.0f;
  592. stepSmall = range/1000.0f;
  593. stepLarge = range/10.0f;
  594. }
  595. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  596. {
  597. kData->param.data[j].type = PARAMETER_OUTPUT;
  598. needsCtrlOut = true;
  599. }
  600. else
  601. {
  602. kData->param.data[j].type = PARAMETER_INPUT;
  603. needsCtrlIn = true;
  604. }
  605. // extra parameter hints
  606. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  607. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  608. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  609. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  610. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  611. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  612. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  613. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  614. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  615. kData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  616. kData->param.ranges[j].min = min;
  617. kData->param.ranges[j].max = max;
  618. kData->param.ranges[j].def = def;
  619. kData->param.ranges[j].step = step;
  620. kData->param.ranges[j].stepSmall = stepSmall;
  621. kData->param.ranges[j].stepLarge = stepLarge;
  622. }
  623. if (needsCtrlIn)
  624. {
  625. portName.clear();
  626. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  627. {
  628. portName = fName;
  629. portName += ":";
  630. }
  631. portName += "event-in";
  632. portName.truncate(portNameSize);
  633. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  634. }
  635. if (needsCtrlOut)
  636. {
  637. portName.clear();
  638. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  639. {
  640. portName = fName;
  641. portName += ":";
  642. }
  643. portName += "event-out";
  644. portName.truncate(portNameSize);
  645. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  646. }
  647. // plugin checks
  648. fHints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  649. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  650. fHints |= PLUGIN_CAN_DRYWET;
  651. if (aOuts > 0)
  652. fHints |= PLUGIN_CAN_VOLUME;
  653. if (aOuts >= 2 && aOuts%2 == 0)
  654. fHints |= PLUGIN_CAN_BALANCE;
  655. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  656. fHints |= PLUGIN_CAN_FORCE_STEREO;
  657. // native plugin hints
  658. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  659. fHints |= PLUGIN_IS_RTSAFE;
  660. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  661. fHints |= PLUGIN_IS_SYNTH;
  662. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  663. fHints |= PLUGIN_HAS_GUI;
  664. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  665. fHints |= PLUGIN_USES_SINGLE_THREAD;
  666. bufferSizeChanged(kData->engine->getBufferSize());
  667. reloadPrograms(true);
  668. kData->client->activate();
  669. carla_debug("NativePlugin::reload() - end");
  670. }
  671. void reloadPrograms(const bool init)
  672. {
  673. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  674. uint32_t i, oldCount = kData->midiprog.count;
  675. // Delete old programs
  676. kData->midiprog.clear();
  677. // Query new programs
  678. uint32_t count = 0;
  679. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr)
  680. count = fDescriptor->get_midi_program_count(fHandle);
  681. if (count > 0)
  682. kData->midiprog.createNew(count);
  683. // Update data
  684. for (i=0; i < kData->midiprog.count; i++)
  685. {
  686. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  687. CARLA_ASSERT(mpDesc != nullptr);
  688. CARLA_ASSERT(mpDesc->name != nullptr);
  689. kData->midiprog.data[i].bank = mpDesc->bank;
  690. kData->midiprog.data[i].program = mpDesc->program;
  691. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  692. }
  693. #ifndef BUILD_BRIDGE
  694. // Update OSC Names
  695. if (kData->engine->isOscControlRegistered())
  696. {
  697. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  698. for (i=0; i < kData->midiprog.count; i++)
  699. 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);
  700. }
  701. #endif
  702. if (init)
  703. {
  704. if (kData->midiprog.count > 0)
  705. setMidiProgram(0, false, false, false, true);
  706. }
  707. else
  708. {
  709. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  710. // Check if current program is invalid
  711. bool programChanged = false;
  712. if (kData->midiprog.count == oldCount+1)
  713. {
  714. // one midi program added, probably created by user
  715. kData->midiprog.current = oldCount;
  716. programChanged = true;
  717. }
  718. else if (kData->midiprog.current >= static_cast<int32_t>(kData->midiprog.count))
  719. {
  720. // current midi program > count
  721. kData->midiprog.current = 0;
  722. programChanged = true;
  723. }
  724. else if (kData->midiprog.current < 0 && kData->midiprog.count > 0)
  725. {
  726. // programs exist now, but not before
  727. kData->midiprog.current = 0;
  728. programChanged = true;
  729. }
  730. else if (kData->midiprog.current >= 0 && kData->midiprog.count == 0)
  731. {
  732. // programs existed before, but not anymore
  733. kData->midiprog.current = -1;
  734. programChanged = true;
  735. }
  736. if (programChanged)
  737. setMidiProgram(kData->midiprog.current, true, true, true, true);
  738. }
  739. }
  740. // -------------------------------------------------------------------
  741. // Plugin processing
  742. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  743. {
  744. uint32_t i, k;
  745. // --------------------------------------------------------------------------------------------------------
  746. // Check if active
  747. if (! kData->active)
  748. {
  749. // disable any output sound
  750. for (i=0; i < kData->audioOut.count; i++)
  751. carla_zeroFloat(outBuffer[i], frames);
  752. if (kData->activeBefore)
  753. {
  754. if (fDescriptor->deactivate != nullptr)
  755. {
  756. fDescriptor->deactivate(fHandle);
  757. if (fHandle2 != nullptr)
  758. fDescriptor->deactivate(fHandle2);
  759. }
  760. }
  761. kData->activeBefore = kData->active;
  762. return;
  763. }
  764. fMidiEventCount = 0;
  765. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*MAX_MIDI_EVENTS*2);
  766. // --------------------------------------------------------------------------------------------------------
  767. // Check if not active before
  768. if (! kData->activeBefore)
  769. {
  770. if (kData->event.portIn != nullptr)
  771. {
  772. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; k++)
  773. {
  774. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  775. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  776. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  777. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  778. }
  779. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  780. }
  781. if (fDescriptor->activate != nullptr)
  782. {
  783. fDescriptor->activate(fHandle);
  784. if (fHandle2 != nullptr)
  785. fDescriptor->activate(fHandle2);
  786. }
  787. }
  788. // --------------------------------------------------------------------------------------------------------
  789. // Event Input and Processing
  790. if (kData->event.portIn != nullptr && kData->activeBefore)
  791. {
  792. // ----------------------------------------------------------------------------------------------------
  793. // MIDI Input (External)
  794. if (kData->extNotes.mutex.tryLock())
  795. {
  796. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  797. {
  798. const ExternalMidiNote& note = kData->extNotes.data.getLast(true); // FIXME, should be first
  799. CARLA_ASSERT(note.channel >= 0);
  800. fMidiEvents[fMidiEventCount].port = 0;
  801. fMidiEvents[fMidiEventCount].time = 0;
  802. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  803. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  804. fMidiEvents[fMidiEventCount].data[1] = note.note;
  805. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  806. fMidiEventCount += 1;
  807. }
  808. kData->extNotes.mutex.unlock();
  809. } // End of MIDI Input (External)
  810. // ----------------------------------------------------------------------------------------------------
  811. // Event Input (System)
  812. bool allNotesOffSent = false;
  813. bool sampleAccurate = (fHints & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  814. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  815. uint32_t timeOffset = 0;
  816. uint32_t nextBankId = 0;
  817. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  818. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  819. for (i=0; i < nEvents; i++)
  820. {
  821. const EngineEvent& event = kData->event.portIn->getEvent(i);
  822. time = event.time - framesOffset;
  823. if (time >= frames)
  824. continue;
  825. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  826. if (time > timeOffset && sampleAccurate)
  827. {
  828. processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset);
  829. if (fMidiEventCount > 0)
  830. {
  831. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*fMidiEventCount);
  832. fMidiEventCount = 0;
  833. }
  834. nextBankId = 0;
  835. timeOffset = time;
  836. }
  837. // Control change
  838. switch (event.type)
  839. {
  840. case kEngineEventTypeNull:
  841. break;
  842. case kEngineEventTypeControl:
  843. {
  844. const EngineControlEvent& ctrlEvent = event.ctrl;
  845. switch (ctrlEvent.type)
  846. {
  847. case kEngineControlEventTypeNull:
  848. break;
  849. case kEngineControlEventTypeParameter:
  850. {
  851. // Control backend stuff
  852. if (event.channel == kData->ctrlInChannel)
  853. {
  854. double value;
  855. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  856. {
  857. value = ctrlEvent.value;
  858. setDryWet(value, false, false);
  859. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  860. continue;
  861. }
  862. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  863. {
  864. value = ctrlEvent.value*127/100;
  865. setVolume(value, false, false);
  866. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  867. continue;
  868. }
  869. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  870. {
  871. double left, right;
  872. value = ctrlEvent.value/0.5 - 1.0;
  873. if (value < 0.0)
  874. {
  875. left = -1.0;
  876. right = (value*2)+1.0;
  877. }
  878. else if (value > 0.0)
  879. {
  880. left = (value*2)-1.0;
  881. right = 1.0;
  882. }
  883. else
  884. {
  885. left = -1.0;
  886. right = 1.0;
  887. }
  888. setBalanceLeft(left, false, false);
  889. setBalanceRight(right, false, false);
  890. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  891. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  892. continue;
  893. }
  894. }
  895. // Control plugin parameters
  896. for (k=0; k < kData->param.count; k++)
  897. {
  898. if (kData->param.data[k].midiChannel != event.channel)
  899. continue;
  900. if (kData->param.data[k].midiCC != ctrlEvent.param)
  901. continue;
  902. if (kData->param.data[k].type != PARAMETER_INPUT)
  903. continue;
  904. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  905. continue;
  906. double value;
  907. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  908. {
  909. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  910. }
  911. else
  912. {
  913. // FIXME - ranges call for this
  914. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  915. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  916. value = std::rint(value);
  917. }
  918. setParameterValue(k, value, false, false, false);
  919. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  920. }
  921. break;
  922. }
  923. case kEngineControlEventTypeMidiBank:
  924. if (event.channel == kData->ctrlInChannel)
  925. nextBankId = ctrlEvent.param;
  926. break;
  927. case kEngineControlEventTypeMidiProgram:
  928. if (event.channel == kData->ctrlInChannel)
  929. {
  930. const uint32_t nextProgramId = ctrlEvent.param;
  931. for (k=0; k < kData->midiprog.count; k++)
  932. {
  933. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  934. {
  935. setMidiProgram(k, false, false, false, false);
  936. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  937. break;
  938. }
  939. }
  940. }
  941. break;
  942. case kEngineControlEventTypeAllSoundOff:
  943. if (event.channel == kData->ctrlInChannel)
  944. {
  945. if (! allNotesOffSent)
  946. sendMidiAllNotesOff();
  947. if (fDescriptor->deactivate != nullptr)
  948. {
  949. fDescriptor->deactivate(fHandle);
  950. if (fHandle2 != nullptr)
  951. fDescriptor->deactivate(fHandle2);
  952. }
  953. if (fDescriptor->activate != nullptr)
  954. {
  955. fDescriptor->activate(fHandle);
  956. if (fHandle2 != nullptr)
  957. fDescriptor->activate(fHandle2);
  958. }
  959. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  960. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  961. allNotesOffSent = true;
  962. }
  963. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  964. continue;
  965. fMidiEvents[fMidiEventCount].port = 0;
  966. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0 : time;
  967. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  968. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  969. fMidiEvents[fMidiEventCount].data[2] = 0;
  970. fMidiEventCount += 1;
  971. break;
  972. case kEngineControlEventTypeAllNotesOff:
  973. if (event.channel == kData->ctrlInChannel)
  974. {
  975. if (! allNotesOffSent)
  976. sendMidiAllNotesOff();
  977. allNotesOffSent = true;
  978. }
  979. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  980. continue;
  981. fMidiEvents[fMidiEventCount].port = 0;
  982. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0: time;
  983. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  984. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  985. fMidiEvents[fMidiEventCount].data[2] = 0;
  986. fMidiEventCount += 1;
  987. break;
  988. }
  989. break;
  990. }
  991. case kEngineEventTypeMidi:
  992. {
  993. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  994. continue;
  995. const EngineMidiEvent& midiEvent = event.midi;
  996. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  997. uint8_t channel = event.channel;
  998. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fHints & PLUGIN_OPTION_SELF_AUTOMATION) == 0)
  999. continue;
  1000. // Fix bad note-off (per DSSI spec)
  1001. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1002. status -= 0x10;
  1003. fMidiEvents[fMidiEventCount].port = 0;
  1004. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0 : time - timeOffset;
  1005. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1006. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1007. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1008. fMidiEventCount += 1;
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. kData->postRtEvents.trySplice();
  1014. if (frames > timeOffset)
  1015. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1016. } // End of Event Input and Processing
  1017. // --------------------------------------------------------------------------------------------------------
  1018. // Plugin processing (no events)
  1019. else
  1020. {
  1021. processSingle(inBuffer, outBuffer, frames, 0);
  1022. } // End of Plugin processing (no events)
  1023. CARLA_PROCESS_CONTINUE_CHECK;
  1024. // --------------------------------------------------------------------------------------------------------
  1025. // Post-processing (dry/wet, volume and balance)
  1026. {
  1027. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1028. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  1029. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1030. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1031. for (i=0; i < kData->audioOut.count; i++)
  1032. {
  1033. // Dry/Wet
  1034. if (doDryWet)
  1035. {
  1036. for (k=0; k < frames; k++)
  1037. {
  1038. bufValue = inBuffer[(kData->audioIn.count == 1) ? 0 : i][k];
  1039. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1040. }
  1041. }
  1042. // Balance
  1043. if (doBalance)
  1044. {
  1045. if (i % 2 == 0)
  1046. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1047. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1048. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1049. for (k=0; k < frames; k++)
  1050. {
  1051. if (i % 2 == 0)
  1052. {
  1053. // left
  1054. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1055. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  1056. }
  1057. else
  1058. {
  1059. // right
  1060. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  1061. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  1062. }
  1063. }
  1064. }
  1065. // Volume
  1066. if (doVolume)
  1067. {
  1068. for (k=0; k < frames; k++)
  1069. outBuffer[i][k] *= kData->postProc.volume;
  1070. }
  1071. }
  1072. } // End of Post-processing
  1073. CARLA_PROCESS_CONTINUE_CHECK;
  1074. // --------------------------------------------------------------------------------------------------------
  1075. // MIDI Output
  1076. if (fMidiOut.count > 0)
  1077. {
  1078. // reverse lookup
  1079. for (uint32_t i = (MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; i--)
  1080. {
  1081. if (fMidiEvents[i].data[0] == 0)
  1082. break;
  1083. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[i].data);
  1084. const uint8_t port = fMidiEvents[i].port;
  1085. if (port < fMidiOut.count)
  1086. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[i].time, channel, port, fMidiEvents[i].data, 3);
  1087. }
  1088. } // End of MIDI Output
  1089. CARLA_PROCESS_CONTINUE_CHECK;
  1090. // --------------------------------------------------------------------------------------------------------
  1091. // Control Output
  1092. if (kData->event.portOut != nullptr)
  1093. {
  1094. float value, curValue;
  1095. for (k=0; k < kData->param.count; k++)
  1096. {
  1097. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1098. continue;
  1099. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1100. kData->param.ranges[k].fixValue(curValue);
  1101. if (kData->param.data[k].midiCC > 0)
  1102. {
  1103. value = kData->param.ranges[k].normalizeValue(curValue);
  1104. kData->event.portOut->writeControlEvent(framesOffset, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1105. }
  1106. }
  1107. } // End of Control Output
  1108. // --------------------------------------------------------------------------------------------------------
  1109. kData->activeBefore = kData->active;
  1110. }
  1111. void processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1112. {
  1113. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1114. std::memcpy(fAudioInBuffers[i], inBuffer[i]+timeOffset, sizeof(float)*frames);
  1115. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1116. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1117. fIsProcessing = true;
  1118. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1119. if (fHandle2 != nullptr)
  1120. fDescriptor->process(fHandle2, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1121. fIsProcessing = false;
  1122. for (uint32_t i=0, k; i < kData->audioOut.count; i++)
  1123. {
  1124. for (k=0; k < frames; k++)
  1125. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1126. }
  1127. }
  1128. void bufferSizeChanged(const uint32_t newBufferSize)
  1129. {
  1130. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1131. {
  1132. if (fAudioInBuffers[i] != nullptr)
  1133. delete[] fAudioInBuffers[i];
  1134. fAudioInBuffers[i] = new float[newBufferSize];
  1135. }
  1136. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1137. {
  1138. if (fAudioOutBuffers[i] != nullptr)
  1139. delete[] fAudioOutBuffers[i];
  1140. fAudioOutBuffers[i] = new float[newBufferSize];
  1141. }
  1142. }
  1143. // -------------------------------------------------------------------
  1144. // Cleanup
  1145. void initBuffers()
  1146. {
  1147. fMidiIn.initBuffers(kData->engine);
  1148. fMidiOut.initBuffers(kData->engine);
  1149. CarlaPlugin::initBuffers();
  1150. }
  1151. void deleteBuffers()
  1152. {
  1153. carla_debug("NativePlugin::deleteBuffers() - start");
  1154. if (fAudioInBuffers != nullptr)
  1155. {
  1156. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1157. {
  1158. if (fAudioInBuffers[i] != nullptr)
  1159. {
  1160. delete[] fAudioInBuffers[i];
  1161. fAudioInBuffers[i] = nullptr;
  1162. }
  1163. }
  1164. delete[] fAudioInBuffers;
  1165. fAudioInBuffers = nullptr;
  1166. }
  1167. if (fAudioOutBuffers != nullptr)
  1168. {
  1169. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1170. {
  1171. if (fAudioOutBuffers[i] != nullptr)
  1172. {
  1173. delete[] fAudioOutBuffers[i];
  1174. fAudioOutBuffers[i] = nullptr;
  1175. }
  1176. }
  1177. delete[] fAudioOutBuffers;
  1178. fAudioOutBuffers = nullptr;
  1179. }
  1180. fMidiIn.clear();
  1181. fMidiOut.clear();
  1182. CarlaPlugin::deleteBuffers();
  1183. carla_debug("NativePlugin::deleteBuffers() - end");
  1184. }
  1185. // -------------------------------------------------------------------
  1186. protected:
  1187. uint32_t handleGetBufferSize()
  1188. {
  1189. return kData->engine->getBufferSize();
  1190. }
  1191. double handleGetSampleRate()
  1192. {
  1193. return kData->engine->getSampleRate();
  1194. }
  1195. const ::TimeInfo* handleGetTimeInfo()
  1196. {
  1197. // TODO
  1198. return nullptr;
  1199. }
  1200. bool handleWriteMidiEvent(const MidiEvent* const event)
  1201. {
  1202. CARLA_ASSERT(fEnabled);
  1203. CARLA_ASSERT(fMidiOut.count > 0);
  1204. CARLA_ASSERT(event != nullptr);
  1205. CARLA_ASSERT(fIsProcessing);
  1206. if (! fEnabled)
  1207. return false;
  1208. if (fMidiOut.count == 0)
  1209. return false;
  1210. if (event == nullptr)
  1211. return false;
  1212. if (! fIsProcessing)
  1213. {
  1214. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out events outside audio thread, ignoring", event);
  1215. return false;
  1216. }
  1217. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1218. return false;
  1219. // reverse-find first free event, and put it there
  1220. for (uint32_t i=fMidiEventCount-1; i >= fMidiEventCount; i--)
  1221. {
  1222. if (fMidiEvents[i].data[0] == 0)
  1223. {
  1224. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1225. break;
  1226. }
  1227. }
  1228. return true;
  1229. }
  1230. void handleUiParameterChanged(const uint32_t index, const float value)
  1231. {
  1232. setParameterValue(index, value, false, true, true);
  1233. }
  1234. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1235. {
  1236. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1237. }
  1238. void handleUiClosed()
  1239. {
  1240. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1241. }
  1242. public:
  1243. static size_t getPluginCount()
  1244. {
  1245. maybeFirstInit();
  1246. return sPluginDescriptors.size();
  1247. }
  1248. static const PluginDescriptor* getPlugin(const size_t index)
  1249. {
  1250. maybeFirstInit();
  1251. CARLA_ASSERT(index < sPluginDescriptors.size());
  1252. if (index < sPluginDescriptors.size())
  1253. return sPluginDescriptors[index];
  1254. return nullptr;
  1255. }
  1256. static void registerPlugin(const PluginDescriptor* desc)
  1257. {
  1258. sPluginDescriptors.push_back(desc);
  1259. }
  1260. static void maybeFirstInit()
  1261. {
  1262. if (! sFirstInit)
  1263. return;
  1264. sFirstInit = false;
  1265. #ifndef BUILD_BRIDGE
  1266. carla_register_native_plugin_bypass();
  1267. carla_register_native_plugin_midiSplit();
  1268. carla_register_native_plugin_midiThrough();
  1269. #if 0
  1270. carla_register_native_plugin_3BandEQ();
  1271. carla_register_native_plugin_3BandSplitter();
  1272. carla_register_native_plugin_PingPongPan();
  1273. #endif
  1274. # ifdef WANT_ZYNADDSUBFX
  1275. carla_register_native_plugin_zynaddsubfx();
  1276. # endif
  1277. #endif
  1278. }
  1279. // -------------------------------------------------------------------
  1280. bool init(const char* const name, const char* const label)
  1281. {
  1282. CARLA_ASSERT(kData->engine != nullptr);
  1283. CARLA_ASSERT(kData->client == nullptr);
  1284. CARLA_ASSERT(label);
  1285. // ---------------------------------------------------------------
  1286. // initialize native-plugins descriptors
  1287. maybeFirstInit();
  1288. // ---------------------------------------------------------------
  1289. // get descriptor that matches label
  1290. for (size_t i=0; i < sPluginDescriptors.size(); i++)
  1291. {
  1292. fDescriptor = sPluginDescriptors[i];
  1293. if (fDescriptor == nullptr)
  1294. break;
  1295. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1296. break;
  1297. fDescriptor = nullptr;
  1298. }
  1299. if (fDescriptor == nullptr)
  1300. {
  1301. kData->engine->setLastError("Invalid internal plugin");
  1302. return false;
  1303. }
  1304. // ---------------------------------------------------------------
  1305. // get info
  1306. if (name != nullptr)
  1307. fName = kData->engine->getNewUniquePluginName(name);
  1308. else if (fDescriptor->name != nullptr)
  1309. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1310. else
  1311. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1312. // ---------------------------------------------------------------
  1313. // register client
  1314. kData->client = kData->engine->addClient(this);
  1315. if (kData->client == nullptr || ! kData->client->isOk())
  1316. {
  1317. kData->engine->setLastError("Failed to register plugin client");
  1318. return false;
  1319. }
  1320. // ---------------------------------------------------------------
  1321. // initialize plugin
  1322. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1323. if (fHandle == nullptr)
  1324. {
  1325. kData->engine->setLastError("Plugin failed to initialize");
  1326. return false;
  1327. }
  1328. return true;
  1329. }
  1330. private:
  1331. PluginHandle fHandle;
  1332. PluginHandle fHandle2;
  1333. HostDescriptor fHost;
  1334. const PluginDescriptor* fDescriptor;
  1335. bool fIsProcessing;
  1336. float** fAudioInBuffers;
  1337. float** fAudioOutBuffers;
  1338. uint32_t fMidiEventCount;
  1339. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1340. NativePluginMidiData fMidiIn;
  1341. NativePluginMidiData fMidiOut;
  1342. static bool sFirstInit;
  1343. static std::vector<const PluginDescriptor*> sPluginDescriptors;
  1344. // -------------------------------------------------------------------
  1345. #define handlePtr ((NativePlugin*)handle)
  1346. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1347. {
  1348. return handlePtr->handleGetBufferSize();
  1349. }
  1350. static double carla_host_get_sample_rate(HostHandle handle)
  1351. {
  1352. return handlePtr->handleGetSampleRate();
  1353. }
  1354. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1355. {
  1356. return handlePtr->handleGetTimeInfo();
  1357. }
  1358. static bool carla_host_write_midi_event(HostHandle handle, const MidiEvent* event)
  1359. {
  1360. return handlePtr->handleWriteMidiEvent(event);
  1361. }
  1362. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1363. {
  1364. handlePtr->handleUiParameterChanged(index, value);
  1365. }
  1366. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1367. {
  1368. handlePtr->handleUiCustomDataChanged(key, value);
  1369. }
  1370. static void carla_host_ui_closed(HostHandle handle)
  1371. {
  1372. handlePtr->handleUiClosed();
  1373. }
  1374. #undef handlePtr
  1375. };
  1376. bool NativePlugin::sFirstInit = true;
  1377. std::vector<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1378. // -----------------------------------------------------------------------
  1379. size_t CarlaPlugin::getNativePluginCount()
  1380. {
  1381. return NativePlugin::getPluginCount();
  1382. }
  1383. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1384. {
  1385. return NativePlugin::getPlugin(index);
  1386. }
  1387. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1388. {
  1389. carla_debug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1390. NativePlugin* const plugin = new NativePlugin(init.engine, init.id);
  1391. if (! plugin->init(init.name, init.label))
  1392. {
  1393. delete plugin;
  1394. return nullptr;
  1395. }
  1396. plugin->reload();
  1397. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1398. {
  1399. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1400. delete plugin;
  1401. return nullptr;
  1402. }
  1403. plugin->registerToOscClient();
  1404. return plugin;
  1405. }
  1406. // -----------------------------------------------------------------------
  1407. CARLA_BACKEND_END_NAMESPACE
  1408. void carla_register_native_plugin(const PluginDescriptor* desc)
  1409. {
  1410. CARLA_BACKEND_USE_NAMESPACE
  1411. NativePlugin::registerPlugin(desc);
  1412. }
  1413. // -----------------------------------------------------------------------