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.

567 lines
16KB

  1. /*
  2. * Carla Plugin Engine (DISTRHO)
  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. #ifdef WANT_PLUGIN
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaStateUtils.hpp"
  20. #include "DistrhoPlugin.hpp"
  21. using DISTRHO::d_cconst;
  22. using DISTRHO::d_string;
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------
  25. // Parameters
  26. static const unsigned char kParamMap[] = {
  27. 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  28. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  29. 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  30. 0x50, 0x51, 0x52, 0x53, 0x54, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  31. };
  32. static const unsigned int kParamVolume = 5;
  33. static const unsigned int kParamBalance = 6;
  34. static const unsigned int kParamPan = 8;
  35. static const unsigned int kParamCount = sizeof(kParamMap);
  36. static const unsigned int kProgramCount = 128;
  37. static const unsigned int kStateCount = MAX_RACK_PLUGINS;
  38. // -----------------------------------------
  39. class CarlaEnginePlugin : public DISTRHO::Plugin,
  40. public CarlaEngine
  41. {
  42. public:
  43. CarlaEnginePlugin()
  44. : DISTRHO::Plugin(kParamCount, kProgramCount, kStateCount),
  45. CarlaEngine(),
  46. fParamBuffers{0.0f},
  47. fPrevParamBuffers{0.0f}
  48. {
  49. carla_debug("CarlaEnginePlugin::CarlaEnginePlugin()");
  50. // init parameters
  51. fParamBuffers[kParamVolume] = 100.0f;
  52. fParamBuffers[kParamBalance] = 63.5f;
  53. fParamBuffers[kParamPan] = 63.5f;
  54. fPrevParamBuffers[kParamVolume] = 100.0f;
  55. fPrevParamBuffers[kParamBalance] = 63.5f;
  56. fPrevParamBuffers[kParamPan] = 63.5f;
  57. // set-up engine
  58. fOptions.processMode = PROCESS_MODE_CONTINUOUS_RACK;
  59. fOptions.transportMode = TRANSPORT_MODE_PLUGIN;
  60. fOptions.forceStereo = true;
  61. fOptions.preferPluginBridges = false;
  62. fOptions.preferUiBridges = false;
  63. init("Carla");
  64. }
  65. ~CarlaEnginePlugin() override
  66. {
  67. carla_debug("CarlaEnginePlugin::~CarlaEnginePlugin()");
  68. setAboutToClose();
  69. removeAllPlugins();
  70. close();
  71. }
  72. protected:
  73. // -------------------------------------
  74. // Carla Engine virtual calls
  75. bool init(const char* const clientName) override
  76. {
  77. carla_debug("CarlaEnginePlugin::init(\"%s\")", clientName);
  78. fBufferSize = d_bufferSize();
  79. fSampleRate = d_sampleRate();
  80. CarlaEngine::init(clientName);
  81. return true;
  82. }
  83. bool close() override
  84. {
  85. carla_debug("CarlaEnginePlugin::close()");
  86. proccessPendingEvents();
  87. return CarlaEngine::close();
  88. }
  89. bool isRunning() const override
  90. {
  91. return true;
  92. }
  93. bool isOffline() const override
  94. {
  95. return false;
  96. }
  97. EngineType type() const override
  98. {
  99. return kEngineTypePlugin;
  100. }
  101. // ---------------------------------------------
  102. // DISTRHO Plugin Information
  103. const char* d_label() const override
  104. {
  105. return "Carla";
  106. }
  107. const char* d_maker() const override
  108. {
  109. return "falkTX";
  110. }
  111. const char* d_license() const override
  112. {
  113. return "GPL v2+";
  114. }
  115. uint32_t d_version() const override
  116. {
  117. return 0x0600;
  118. }
  119. long d_uniqueId() const override
  120. {
  121. return d_cconst('C', 'r', 'l', 'a');
  122. }
  123. // ---------------------------------------------
  124. // DISTRHO Plugin Init
  125. void d_initParameter(uint32_t index, DISTRHO::Parameter& parameter) override
  126. {
  127. if (index >= kParamCount)
  128. return;
  129. parameter.hints = DISTRHO::PARAMETER_IS_AUTOMABLE;
  130. parameter.ranges.def = 0.0f;
  131. parameter.ranges.min = 0.0f;
  132. parameter.ranges.max = 127.0f;
  133. switch (kParamMap[index])
  134. {
  135. case 0x01:
  136. parameter.name = "0x01 Modulation";
  137. break;
  138. case 0x02:
  139. parameter.name = "0x02 Breath";
  140. break;
  141. case 0x03:
  142. parameter.name = "0x03 (Undefined)";
  143. break;
  144. case 0x04:
  145. parameter.name = "0x04 Foot";
  146. break;
  147. case 0x05:
  148. parameter.name = "0x05 Portamento";
  149. break;
  150. case 0x07:
  151. parameter.name = "0x07 Volume";
  152. parameter.ranges.def = 100.0f;
  153. break;
  154. case 0x08:
  155. parameter.name = "0x08 Balance";
  156. parameter.ranges.def = 63.5f;
  157. break;
  158. case 0x09:
  159. parameter.name = "0x09 (Undefined)";
  160. break;
  161. case 0x0A:
  162. parameter.name = "0x0A Pan";
  163. parameter.ranges.def = 63.5f;
  164. break;
  165. case 0x0B:
  166. parameter.name = "0x0B Expression";
  167. break;
  168. case 0x0C:
  169. parameter.name = "0x0C FX Control 1";
  170. break;
  171. case 0x0D:
  172. parameter.name = "0x0D FX Control 2";
  173. break;
  174. case 0x0E:
  175. parameter.name = "0x0E (Undefined)";
  176. break;
  177. case 0x0F:
  178. parameter.name = "0x0F (Undefined)";
  179. break;
  180. case 0x10:
  181. parameter.name = "0x10 General Purpose 1";
  182. break;
  183. case 0x11:
  184. parameter.name = "0x11 General Purpose 2";
  185. break;
  186. case 0x12:
  187. parameter.name = "0x12 General Purpose 3";
  188. break;
  189. case 0x13:
  190. parameter.name = "0x13 General Purpose 4";
  191. break;
  192. case 0x14:
  193. parameter.name = "0x14 (Undefined)";
  194. break;
  195. case 0x15:
  196. parameter.name = "0x15 (Undefined)";
  197. break;
  198. case 0x16:
  199. parameter.name = "0x16 (Undefined)";
  200. break;
  201. case 0x17:
  202. parameter.name = "0x17 (Undefined)";
  203. break;
  204. case 0x18:
  205. parameter.name = "0x18 (Undefined)";
  206. break;
  207. case 0x19:
  208. parameter.name = "0x19 (Undefined)";
  209. break;
  210. case 0x1A:
  211. parameter.name = "0x1A (Undefined)";
  212. break;
  213. case 0x1B:
  214. parameter.name = "0x1B (Undefined)";
  215. break;
  216. case 0x1C:
  217. parameter.name = "0x1C (Undefined)";
  218. break;
  219. case 0x1D:
  220. parameter.name = "0x1D (Undefined)";
  221. break;
  222. case 0x1E:
  223. parameter.name = "0x1E (Undefined)";
  224. break;
  225. case 0x1F:
  226. parameter.name = "0x1F (Undefined)";
  227. break;
  228. case 0x46:
  229. parameter.name = "0x46 Control 1 [Variation]";
  230. break;
  231. case 0x47:
  232. parameter.name = "0x47 Control 2 [Timbre]";
  233. break;
  234. case 0x48:
  235. parameter.name = "0x48 Control 3 [Release]";
  236. break;
  237. case 0x49:
  238. parameter.name = "0x49 Control 4 [Attack]";
  239. break;
  240. case 0x4A:
  241. parameter.name = "0x4A Control 5 [Brightness]";
  242. break;
  243. case 0x4B:
  244. parameter.name = "0x4B Control 6 [Decay]";
  245. break;
  246. case 0x4C:
  247. parameter.name = "0x4C Control 7 [Vib Rate]";
  248. break;
  249. case 0x4D:
  250. parameter.name = "0x4D Control 8 [Vib Depth]";
  251. break;
  252. case 0x4E:
  253. parameter.name = "0x4E Control 9 [Vib Delay]";
  254. break;
  255. case 0x4F:
  256. parameter.name = "0x4F Control 10 [Undefined]";
  257. break;
  258. case 0x50:
  259. parameter.name = "0x50 General Purpose 5";
  260. break;
  261. case 0x51:
  262. parameter.name = "0x51 General Purpose 6";
  263. break;
  264. case 0x52:
  265. parameter.name = "0x52 General Purpose 7";
  266. break;
  267. case 0x53:
  268. parameter.name = "0x53 General Purpose 8";
  269. break;
  270. case 0x54:
  271. parameter.name = "0x54 Portamento Control";
  272. break;
  273. case 0x5B:
  274. parameter.name = "0x5B FX 1 Depth [Reverb]";
  275. break;
  276. case 0x5C:
  277. parameter.name = "0x5C FX 2 Depth [Tremolo]";
  278. break;
  279. case 0x5D:
  280. parameter.name = "0x5D FX 3 Depth [Chorus]";
  281. break;
  282. case 0x5E:
  283. parameter.name = "0x5E FX 4 Depth [Detune]";
  284. break;
  285. case 0x5F:
  286. parameter.name = "0x5F FX 5 Depth [Phaser]";
  287. break;
  288. default:
  289. parameter.name = "";
  290. break;
  291. }
  292. }
  293. void d_initProgramName(uint32_t index, d_string& programName) override
  294. {
  295. programName = "Program #" + d_string(index);
  296. }
  297. void d_initStateKey(uint32_t index, d_string& stateKey) override
  298. {
  299. stateKey = "Plugin #" + d_string(index);
  300. }
  301. // ---------------------------------------------
  302. // DISTRHO Plugin Internal data
  303. float d_parameterValue(uint32_t index) override
  304. {
  305. if (index >= kParamCount)
  306. return 0.0f;
  307. return fParamBuffers[index];
  308. }
  309. void d_setParameterValue(uint32_t index, float value) override
  310. {
  311. if (index >= kParamCount)
  312. return;
  313. fParamBuffers[index] = value;
  314. }
  315. void d_setProgram(uint32_t index) override
  316. {
  317. if (index >= kProgramCount)
  318. return;
  319. if (kData->curPluginCount == 0 || kData->plugins == nullptr)
  320. return;
  321. CarlaPlugin* const plugin(kData->plugins[0].plugin);
  322. if (plugin == nullptr || ! plugin->enabled())
  323. return;
  324. if (plugin->midiProgramCount() > 0)
  325. {
  326. if (index <= plugin->midiProgramCount())
  327. plugin->setMidiProgram(index, true, true, false);
  328. }
  329. else if (plugin->programCount() > 0 && plugin->type() != PLUGIN_LV2)
  330. {
  331. if (index <= plugin->programCount())
  332. plugin->setProgram(index, true, true, false);
  333. }
  334. }
  335. void d_setState(const char* key, const char* value) override
  336. {
  337. // TODO
  338. (void)key;
  339. (void)value;
  340. }
  341. // ---------------------------------------------
  342. // DISTRHO Plugin Process
  343. void d_activate() override
  344. {
  345. for (unsigned int i=0; i < kData->curPluginCount; ++i)
  346. {
  347. CarlaPlugin* const plugin(getPluginUnchecked(i));
  348. if (plugin != nullptr && plugin->enabled())
  349. plugin->setActive(true, true, false);
  350. }
  351. carla_copyFloat(fPrevParamBuffers, fParamBuffers, kParamCount);
  352. }
  353. void d_deactivate() override
  354. {
  355. for (unsigned int i=0; i < kData->curPluginCount; ++i)
  356. {
  357. CarlaPlugin* const plugin(getPluginUnchecked(i));
  358. if (plugin != nullptr && plugin->enabled())
  359. plugin->setActive(false, true, false);
  360. }
  361. // just in case
  362. proccessPendingEvents();
  363. }
  364. void d_run(float** inputs, float** outputs, uint32_t frames, uint32_t midiEventCount, const DISTRHO::MidiEvent* midiEvents) override
  365. {
  366. if (kData->curPluginCount == 0)
  367. {
  368. carla_zeroFloat(outputs[0], frames);
  369. carla_zeroFloat(outputs[1], frames);
  370. return proccessPendingEvents();
  371. }
  372. // ---------------------------------------------------------------
  373. // create audio buffers
  374. float* inBuf[2] = { inputs[0], inputs[1] };
  375. float* outBuf[2] = { outputs[0], outputs[1] };
  376. // ---------------------------------------------------------------
  377. // initialize input events
  378. carla_zeroStruct<EngineEvent>(kData->bufEvent.in, INTERNAL_EVENT_COUNT);
  379. {
  380. uint32_t engineEventIndex = 0;
  381. for (unsigned int i=0; i < kParamCount && engineEventIndex+midiEventCount < INTERNAL_EVENT_COUNT; ++i)
  382. {
  383. if (fParamBuffers[i] == fPrevParamBuffers[i])
  384. continue;
  385. EngineEvent& engineEvent(kData->bufEvent.in[engineEventIndex++]);
  386. engineEvent.clear();
  387. engineEvent.type = kEngineEventTypeControl;
  388. engineEvent.time = 0;
  389. engineEvent.channel = 0;
  390. engineEvent.ctrl.type = kEngineControlEventTypeParameter;
  391. engineEvent.ctrl.param = kParamMap[i];
  392. engineEvent.ctrl.value = fParamBuffers[i]/127.0f;
  393. fPrevParamBuffers[i] = fParamBuffers[i];
  394. }
  395. for (uint32_t i=0; i < midiEventCount && engineEventIndex < INTERNAL_EVENT_COUNT; ++i)
  396. {
  397. const DISTRHO::MidiEvent& midiEvent(midiEvents[i]);
  398. if (midiEvent.size > 4)
  399. continue;
  400. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.buf);
  401. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent.buf);
  402. // we don't want some events
  403. if (status == MIDI_STATUS_PROGRAM_CHANGE)
  404. continue;
  405. // handle note/sound off properly
  406. if (status == MIDI_STATUS_CONTROL_CHANGE)
  407. {
  408. const uint8_t control = midiEvent.buf[1];
  409. if (MIDI_IS_CONTROL_BANK_SELECT(control))
  410. continue;
  411. if (control == MIDI_CONTROL_ALL_SOUND_OFF || control == MIDI_CONTROL_ALL_NOTES_OFF)
  412. {
  413. EngineEvent& engineEvent(kData->bufEvent.in[engineEventIndex++]);
  414. engineEvent.clear();
  415. engineEvent.type = kEngineEventTypeControl;
  416. engineEvent.time = midiEvent.frame;
  417. engineEvent.channel = channel;
  418. engineEvent.ctrl.type = (control == MIDI_CONTROL_ALL_SOUND_OFF) ? kEngineControlEventTypeAllSoundOff : kEngineControlEventTypeAllNotesOff;
  419. engineEvent.ctrl.param = 0;
  420. engineEvent.ctrl.value = 0.0f;
  421. continue;
  422. }
  423. }
  424. EngineEvent& engineEvent(kData->bufEvent.in[engineEventIndex++]);
  425. engineEvent.clear();
  426. engineEvent.type = kEngineEventTypeMidi;
  427. engineEvent.time = midiEvent.frame;
  428. engineEvent.channel = channel;
  429. engineEvent.midi.data[0] = MIDI_GET_STATUS_FROM_DATA(midiEvent.buf);
  430. engineEvent.midi.data[1] = midiEvent.buf[1];
  431. engineEvent.midi.data[2] = midiEvent.buf[2];
  432. engineEvent.midi.data[3] = midiEvent.buf[3];
  433. engineEvent.midi.size = midiEvent.size;
  434. }
  435. }
  436. // ---------------------------------------------------------------
  437. // process
  438. processRack(inBuf, outBuf, frames);
  439. proccessPendingEvents();
  440. }
  441. // ---------------------------------------------
  442. // Callbacks
  443. void d_bufferSizeChanged(uint32_t newBufferSize) override
  444. {
  445. if (fBufferSize == newBufferSize)
  446. return;
  447. fBufferSize = newBufferSize;
  448. bufferSizeChanged(newBufferSize);
  449. }
  450. void d_sampleRateChanged(double newSampleRate) override
  451. {
  452. if (fSampleRate == newSampleRate)
  453. return;
  454. fSampleRate = newSampleRate;
  455. sampleRateChanged(newSampleRate);
  456. }
  457. // ---------------------------------------------
  458. private:
  459. float fParamBuffers[kParamCount];
  460. float fPrevParamBuffers[kParamCount];
  461. };
  462. CARLA_BACKEND_END_NAMESPACE
  463. // -------------------------------------------------
  464. START_NAMESPACE_DISTRHO
  465. Plugin* createPlugin()
  466. {
  467. return new CarlaBackend::CarlaEnginePlugin();
  468. }
  469. END_NAMESPACE_DISTRHO
  470. // -------------------------------------------------
  471. #include "DistrhoPluginMain.cpp"
  472. #endif // WANT_PLUGIN