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.

1849 lines
61KB

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