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.

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