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.

540 lines
14KB

  1. /*
  2. * Carla Plugin Engine
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifdef CARLA_ENGINE_PLUGIN
  18. #include "carla_engine.hpp"
  19. #include "carla_plugin.hpp"
  20. #include "DistrhoPlugin.h"
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -----------------------------------------
  23. // Parameters
  24. static const unsigned char paramMap[] = {
  25. 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  26. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  27. 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  28. 0x50, 0x51, 0x52, 0x53, 0x54, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  29. };
  30. static const unsigned int paramVolume = 5;
  31. static const unsigned int paramBalance = 6;
  32. static const unsigned int paramPan = 8;
  33. static const unsigned int paramCount = sizeof(paramMap);
  34. static const unsigned int programCount = 128;
  35. // -------------------------------------------------------------------------------------------------------------------
  36. // Plugin Engine client
  37. class CarlaEnginePluginClient : public CarlaEngineClient
  38. {
  39. public:
  40. CarlaEnginePluginClient(const CarlaEngineType engineType, const ProcessMode processMode)
  41. : CarlaEngineClient(engineType, processMode)
  42. {
  43. }
  44. ~CarlaEnginePluginClient()
  45. {
  46. }
  47. const CarlaEngineBasePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput)
  48. {
  49. qDebug("CarlaEnginePluginClient::addPort(%i, \"%s\", %s)", portType, name, bool2str(isInput));
  50. switch (portType)
  51. {
  52. case CarlaEnginePortTypeNull:
  53. break;
  54. case CarlaEnginePortTypeAudio:
  55. return new CarlaEngineAudioPort(isInput, processMode);
  56. case CarlaEnginePortTypeControl:
  57. return new CarlaEngineControlPort(isInput, processMode);
  58. case CarlaEnginePortTypeMIDI:
  59. return new CarlaEngineMidiPort(isInput, processMode);
  60. }
  61. qCritical("CarlaEnginePluginClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  62. return nullptr;
  63. }
  64. };
  65. // -----------------------------------------
  66. class CarlaEnginePlugin : public CarlaEngine,
  67. public DISTRHO::Plugin
  68. {
  69. public:
  70. short idZyn;
  71. CarlaEnginePlugin()
  72. : CarlaEngine(),
  73. DISTRHO::Plugin(paramCount, programCount, 0)
  74. {
  75. qDebug("CarlaEnginePlugin::CarlaEnginePlugin()");
  76. // init parameters
  77. for (unsigned int i=0; i < paramCount; i++)
  78. paramBuffers[i] = 0.0f;
  79. paramBuffers[paramVolume] = 100.0f;
  80. paramBuffers[paramBalance] = 63.5f;
  81. paramBuffers[paramPan] = 63.5f;
  82. memcpy(prevParamBuffers, paramBuffers, sizeof(float)*paramCount);
  83. // set-up engine
  84. options.processMode = PROCESS_MODE_CONTINUOUS_RACK;
  85. options.forceStereo = true;
  86. options.preferPluginBridges = false;
  87. options.preferUiBridges = false;
  88. init("Carla");
  89. // Force thread start so we get some OSC usage
  90. // (liblo locks otherwise)
  91. //startCheckThread();
  92. // testing
  93. idZyn = addPlugin(PLUGIN_DSSI, "/usr/lib/dssi/sineshaper.so", nullptr, "ll-sineshaper");
  94. }
  95. ~CarlaEnginePlugin()
  96. {
  97. qDebug("CarlaEnginePlugin::~CarlaEnginePlugin()");
  98. removeAllPlugins();
  99. close();
  100. }
  101. // -------------------------------------
  102. // CarlaEngine virtual calls
  103. bool init(const char* const clientName)
  104. {
  105. qDebug("CarlaEnginePlugin::init(\"%s\")", clientName);
  106. bufferSize = d_bufferSize();
  107. sampleRate = d_sampleRate();
  108. name = clientName;
  109. name.toBasic();
  110. CarlaEngine::init(name);
  111. return true;
  112. }
  113. bool close()
  114. {
  115. qDebug("CarlaEnginePlugin::close()");
  116. CarlaEngine::close();
  117. return true;
  118. }
  119. bool isRunning() const
  120. {
  121. return true;
  122. }
  123. bool isOffline() const
  124. {
  125. return false;
  126. }
  127. CarlaEngineType type() const
  128. {
  129. return CarlaEngineTypeRtAudio;
  130. }
  131. CarlaEngineClient* addClient(CarlaPlugin* const)
  132. {
  133. return new CarlaEnginePluginClient(CarlaEngineTypeRtAudio, options.processMode);
  134. }
  135. protected:
  136. // ---------------------------------------------
  137. // DISTRHO Plugin Information
  138. const char* d_label()
  139. {
  140. return "Carla";
  141. }
  142. const char* d_maker()
  143. {
  144. return "falkTX";
  145. }
  146. const char* d_license()
  147. {
  148. return "GPL v2+";
  149. }
  150. uint32_t d_version()
  151. {
  152. return 0x0500;
  153. }
  154. long d_uniqueId()
  155. {
  156. return d_cconst('C', 'r', 'l', 'a');
  157. }
  158. // ---------------------------------------------
  159. // DISTRHO Plugin Init
  160. void d_initParameter(uint32_t index, DISTRHO::Parameter& parameter)
  161. {
  162. if (index >= paramCount)
  163. return;
  164. parameter.hints = DISTRHO::PARAMETER_IS_AUTOMABLE;
  165. parameter.ranges.def = 0.0f;
  166. parameter.ranges.min = 0.0f;
  167. parameter.ranges.max = 127.0f;
  168. if (index == paramVolume)
  169. parameter.ranges.def = 100.0f;
  170. else if (index == paramBalance)
  171. parameter.ranges.def = 63.5f;
  172. else if (index == paramPan)
  173. parameter.ranges.def = 63.5f;
  174. switch (paramMap[index])
  175. {
  176. case 0x01:
  177. parameter.name = "0x01 Modulation";
  178. break;
  179. case 0x02:
  180. parameter.name = "0x02 Breath";
  181. break;
  182. case 0x03:
  183. parameter.name = "0x03 (Undefined)";
  184. break;
  185. case 0x04:
  186. parameter.name = "0x04 Foot";
  187. break;
  188. case 0x05:
  189. parameter.name = "0x05 Portamento";
  190. break;
  191. case 0x07:
  192. parameter.name = "0x07 Volume";
  193. break;
  194. case 0x08:
  195. parameter.name = "0x08 Balance";
  196. break;
  197. case 0x09:
  198. parameter.name = "0x09 (Undefined)";
  199. break;
  200. case 0x0A:
  201. parameter.name = "0x0A Pan";
  202. break;
  203. case 0x0B:
  204. parameter.name = "0x0B Expression";
  205. break;
  206. case 0x0C:
  207. parameter.name = "0x0C FX Control 1";
  208. break;
  209. case 0x0D:
  210. parameter.name = "0x0D FX Control 2";
  211. break;
  212. case 0x0E:
  213. parameter.name = "0x0E (Undefined)";
  214. break;
  215. case 0x0F:
  216. parameter.name = "0x0F (Undefined)";
  217. break;
  218. case 0x10:
  219. parameter.name = "0x10 General Purpose 1";
  220. break;
  221. case 0x11:
  222. parameter.name = "0x11 General Purpose 2";
  223. break;
  224. case 0x12:
  225. parameter.name = "0x12 General Purpose 3";
  226. break;
  227. case 0x13:
  228. parameter.name = "0x13 General Purpose 4";
  229. break;
  230. case 0x14:
  231. parameter.name = "0x14 (Undefined)";
  232. break;
  233. case 0x15:
  234. parameter.name = "0x15 (Undefined)";
  235. break;
  236. case 0x16:
  237. parameter.name = "0x16 (Undefined)";
  238. break;
  239. case 0x17:
  240. parameter.name = "0x17 (Undefined)";
  241. break;
  242. case 0x18:
  243. parameter.name = "0x18 (Undefined)";
  244. break;
  245. case 0x19:
  246. parameter.name = "0x19 (Undefined)";
  247. break;
  248. case 0x1A:
  249. parameter.name = "0x1A (Undefined)";
  250. break;
  251. case 0x1B:
  252. parameter.name = "0x1B (Undefined)";
  253. break;
  254. case 0x1C:
  255. parameter.name = "0x1C (Undefined)";
  256. break;
  257. case 0x1D:
  258. parameter.name = "0x1D (Undefined)";
  259. break;
  260. case 0x1E:
  261. parameter.name = "0x1E (Undefined)";
  262. break;
  263. case 0x1F:
  264. parameter.name = "0x1F (Undefined)";
  265. break;
  266. case 0x46:
  267. parameter.name = "0x46 Control 1 [Variation]";
  268. break;
  269. case 0x47:
  270. parameter.name = "0x47 Control 2 [Timbre]";
  271. break;
  272. case 0x48:
  273. parameter.name = "0x48 Control 3 [Release]";
  274. break;
  275. case 0x49:
  276. parameter.name = "0x49 Control 4 [Attack]";
  277. break;
  278. case 0x4A:
  279. parameter.name = "0x4A Control 5 [Brightness]";
  280. break;
  281. case 0x4B:
  282. parameter.name = "0x4B Control 6 [Decay]";
  283. break;
  284. case 0x4C:
  285. parameter.name = "0x4C Control 7 [Vib Rate]";
  286. break;
  287. case 0x4D:
  288. parameter.name = "0x4D Control 8 [Vib Depth]";
  289. break;
  290. case 0x4E:
  291. parameter.name = "0x4E Control 9 [Vib Delay]";
  292. break;
  293. case 0x4F:
  294. parameter.name = "0x4F Control 10 [Undefined]";
  295. break;
  296. case 0x50:
  297. parameter.name = "0x50 General Purpose 5";
  298. break;
  299. case 0x51:
  300. parameter.name = "0x51 General Purpose 6";
  301. break;
  302. case 0x52:
  303. parameter.name = "0x52 General Purpose 7";
  304. break;
  305. case 0x53:
  306. parameter.name = "0x53 General Purpose 8";
  307. break;
  308. case 0x54:
  309. parameter.name = "0x54 Portamento Control";
  310. break;
  311. case 0x5B:
  312. parameter.name = "0x5B FX 1 Depth [Reverb]";
  313. break;
  314. case 0x5C:
  315. parameter.name = "0x5C FX 2 Depth [Tremolo]";
  316. break;
  317. case 0x5D:
  318. parameter.name = "0x5D FX 3 Depth [Chorus]";
  319. break;
  320. case 0x5E:
  321. parameter.name = "0x5E FX 4 Depth [Detune]";
  322. break;
  323. case 0x5F:
  324. parameter.name = "0x5F FX 5 Depth [Phaser]";
  325. break;
  326. }
  327. }
  328. void d_initProgramName(uint32_t index, d_string& programName)
  329. {
  330. programName = "Program #" + d_string(index);
  331. }
  332. void d_initStateKey(uint32_t index, d_string& stateKey)
  333. {
  334. Q_UNUSED(index);
  335. Q_UNUSED(stateKey);
  336. }
  337. // ---------------------------------------------
  338. // DISTRHO Plugin Internal data
  339. float d_parameterValue(uint32_t index)
  340. {
  341. if (index >= paramCount)
  342. return 0.0f;
  343. return paramBuffers[index];
  344. }
  345. void d_setParameterValue(uint32_t index, float value)
  346. {
  347. if (index >= paramCount)
  348. return;
  349. paramBuffers[index] = value;
  350. }
  351. void d_setProgram(uint32_t index)
  352. {
  353. if (index >= programCount)
  354. return;
  355. if (maxPluginNumber() == 0)
  356. return;
  357. if (CarlaPlugin* const plugin = getPlugin(0))
  358. {
  359. if (index > plugin->programCount())
  360. plugin->setProgram(index, true, true, false, true);
  361. }
  362. }
  363. void d_setState(const char* key, const char* value)
  364. {
  365. Q_UNUSED(key);
  366. Q_UNUSED(value);
  367. }
  368. // ---------------------------------------------
  369. // DISTRHO Plugin Process
  370. void d_activate()
  371. {
  372. //#if 0
  373. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  374. {
  375. CarlaPlugin* const plugin = getPluginUnchecked(i);
  376. if (plugin && plugin->enabled())
  377. plugin->setActive(true, true, false);
  378. }
  379. //#endif
  380. memcpy(prevParamBuffers, paramBuffers, sizeof(float)*paramCount);
  381. }
  382. void d_deactivate()
  383. {
  384. //#if 0
  385. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  386. {
  387. CarlaPlugin* const plugin = getPluginUnchecked(i);
  388. if (plugin && plugin->enabled())
  389. plugin->setActive(false, true, false);
  390. }
  391. //#endif
  392. }
  393. void d_run(float** inputs, float** outputs, uint32_t frames, uint32_t midiEventCount, const DISTRHO::MidiEvent* midiEvents)
  394. {
  395. if (maxPluginNumber() == 0)
  396. return;
  397. // create audio buffers
  398. float* inBuf[2] = { (float*)inputs[0], (float*)inputs[1] };
  399. float* outBuf[2] = { outputs[0], outputs[1] };
  400. // initialize control input
  401. memset(rackControlEventsIn, 0, sizeof(CarlaEngineControlEvent)*CarlaEngine::MAX_CONTROL_EVENTS);
  402. {
  403. uint32_t carlaEventIndex = 0;
  404. for (unsigned int i=0; i < paramCount; i++)
  405. {
  406. if (prevParamBuffers[i] == paramBuffers[i])
  407. continue;
  408. CarlaEngineControlEvent* const carlaEvent = &rackControlEventsIn[carlaEventIndex++];
  409. carlaEvent->type = CarlaEngineParameterChangeEvent;
  410. carlaEvent->parameter = paramMap[i];
  411. carlaEvent->value = paramBuffers[i]/127;
  412. }
  413. }
  414. memcpy(prevParamBuffers, paramBuffers, sizeof(float)*paramCount);
  415. // initialize midi input
  416. memset(rackMidiEventsIn, 0, sizeof(CarlaEngineMidiEvent)*CarlaEngine::MAX_MIDI_EVENTS);
  417. {
  418. const DISTRHO::MidiEvent* event;
  419. for (uint32_t i=0, j=0; j < midiEventCount; j++)
  420. {
  421. if (i == CarlaEngine::MAX_MIDI_EVENTS)
  422. break;
  423. event = &midiEvents[j];
  424. rackMidiEventsIn[i].time = event->frame;
  425. rackMidiEventsIn[i].size = 3;
  426. memcpy(rackMidiEventsIn[i].data, event->buffer, 3);
  427. i += 1;
  428. }
  429. }
  430. processRack(inBuf, outBuf, frames);
  431. }
  432. // ---------------------------------------------
  433. // Callbacks
  434. void d_bufferSizeChanged(uint32_t newBufferSize)
  435. {
  436. bufferSizeChanged(newBufferSize);
  437. }
  438. // ---------------------------------------------
  439. private:
  440. float paramBuffers[paramCount];
  441. float prevParamBuffers[paramCount];
  442. };
  443. CARLA_BACKEND_END_NAMESPACE
  444. // -------------------------------------------------
  445. START_NAMESPACE_DISTRHO
  446. Plugin* createPlugin()
  447. {
  448. return new CarlaBackend::CarlaEnginePlugin();
  449. }
  450. END_NAMESPACE_DISTRHO
  451. // -------------------------------------------------
  452. #include "DistrhoPluginMain.cpp"
  453. #endif // CARLA_ENGINE_PLUGIN