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.

1990 lines
64KB

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