Collection of tools useful for audio production
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.

534 lines
14KB

  1. /*
  2. * Carla Backend
  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. #include "carla_engine.hpp"
  18. #include "carla_plugin.hpp"
  19. #include "DistrhoPlugin.h"
  20. CARLA_BACKEND_START_NAMESPACE
  21. // -----------------------------------------
  22. // Parameters
  23. static const unsigned char paramMap[] = {
  24. 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  25. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
  26. 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  27. 0x50, 0x51, 0x52, 0x53, 0x54, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  28. };
  29. static const unsigned int paramVolume = 5;
  30. static const unsigned int paramBalance = 6;
  31. static const unsigned int paramPan = 8;
  32. static const unsigned int paramCount = sizeof(paramMap);
  33. static const unsigned int programCount = 128;
  34. // -------------------------------------------------------------------------------------------------------------------
  35. // Plugin Engine client
  36. class CarlaEnginePluginClient : public CarlaEngineClient
  37. {
  38. public:
  39. CarlaEnginePluginClient(const CarlaEngineType engineType, const ProcessMode processMode)
  40. : CarlaEngineClient(engineType, processMode)
  41. {
  42. }
  43. ~CarlaEnginePluginClient()
  44. {
  45. }
  46. const CarlaEngineBasePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput)
  47. {
  48. qDebug("CarlaEnginePluginClient::addPort(%i, \"%s\", %s)", portType, name, bool2str(isInput));
  49. switch (portType)
  50. {
  51. case CarlaEnginePortTypeNull:
  52. break;
  53. case CarlaEnginePortTypeAudio:
  54. return new CarlaEngineAudioPort(isInput, processMode);
  55. case CarlaEnginePortTypeControl:
  56. return new CarlaEngineControlPort(isInput, processMode);
  57. case CarlaEnginePortTypeMIDI:
  58. return new CarlaEngineMidiPort(isInput, processMode);
  59. }
  60. qCritical("CarlaEnginePluginClient::addPort(%i, \"%s\", %s) - invalid type", 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. options.processMode = PROCESS_MODE_CONTINUOUS_RACK;
  84. options.forceStereo = true;
  85. options.preferPluginBridges = false;
  86. options.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. bufferSize = d_bufferSize();
  106. sampleRate = d_sampleRate();
  107. name = clientName;
  108. name.toBasic();
  109. CarlaEngine::init(name);
  110. return true;
  111. }
  112. bool close()
  113. {
  114. qDebug("CarlaEnginePlugin::close()");
  115. CarlaEngine::close();
  116. return true;
  117. }
  118. bool isOffline() const
  119. {
  120. return false;
  121. }
  122. bool isRunning() const
  123. {
  124. return true;
  125. }
  126. CarlaEngineType type() const
  127. {
  128. return CarlaEngineTypeRtAudio;
  129. }
  130. CarlaEngineClient* addClient(CarlaPlugin* const)
  131. {
  132. return new CarlaEnginePluginClient(CarlaEngineTypeRtAudio, options.processMode);
  133. }
  134. protected:
  135. // ---------------------------------------------
  136. // DISTRHO Plugin Information
  137. const char* d_label()
  138. {
  139. return "Carla";
  140. }
  141. const char* d_maker()
  142. {
  143. return "falkTX";
  144. }
  145. const char* d_license()
  146. {
  147. return "GPL v2+";
  148. }
  149. uint32_t d_version()
  150. {
  151. return 0x0500;
  152. }
  153. long d_uniqueId()
  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 (CarlaPlugin* const plugin = getPlugin(0))
  357. {
  358. if (index > plugin->programCount())
  359. plugin->setProgram(index, true, true, false, true);
  360. }
  361. }
  362. void d_setState(const char* key, const char* value)
  363. {
  364. Q_UNUSED(key);
  365. Q_UNUSED(value);
  366. }
  367. // ---------------------------------------------
  368. // DISTRHO Plugin Process
  369. void d_activate()
  370. {
  371. //#if 0
  372. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  373. {
  374. CarlaPlugin* const plugin = getPluginUnchecked(i);
  375. if (plugin && plugin->enabled())
  376. plugin->setActive(true, true, false);
  377. }
  378. //#endif
  379. memcpy(prevParamBuffers, paramBuffers, sizeof(float)*paramCount);
  380. }
  381. void d_deactivate()
  382. {
  383. //#if 0
  384. for (unsigned short i=0, max=maxPluginNumber(); i < max; i++)
  385. {
  386. CarlaPlugin* const plugin = getPluginUnchecked(i);
  387. if (plugin && plugin->enabled())
  388. plugin->setActive(false, true, false);
  389. }
  390. //#endif
  391. }
  392. void d_run(const float** inputs, float** outputs, uint32_t frames, uint32_t midiEventCount, const DISTRHO::MidiEvent* midiEvents)
  393. {
  394. if (maxPluginNumber() == 0)
  395. return;
  396. // create audio buffers
  397. float* inBuf[2] = { (float*)inputs[0], (float*)inputs[1] };
  398. float* outBuf[2] = { outputs[0], outputs[1] };
  399. // initialize control input
  400. memset(rackControlEventsIn, 0, sizeof(CarlaEngineControlEvent)*MAX_ENGINE_CONTROL_EVENTS);
  401. {
  402. uint32_t carlaEventIndex = 0;
  403. for (unsigned int i=0; i < paramCount; i++)
  404. {
  405. if (prevParamBuffers[i] == paramBuffers[i])
  406. continue;
  407. CarlaEngineControlEvent* const carlaEvent = &rackControlEventsIn[carlaEventIndex++];
  408. carlaEvent->type = CarlaEngineParameterChangeEvent;
  409. carlaEvent->controller = paramMap[i];
  410. carlaEvent->value = paramBuffers[i]/127;
  411. }
  412. }
  413. memcpy(prevParamBuffers, paramBuffers, sizeof(float)*paramCount);
  414. // initialize midi input
  415. memset(rackMidiEventsIn, 0, sizeof(CarlaEngineMidiEvent)*MAX_ENGINE_MIDI_EVENTS);
  416. {
  417. const DISTRHO::MidiEvent* event;
  418. for (uint32_t i=0, j=0; j < midiEventCount; j++)
  419. {
  420. if (i == MAX_ENGINE_MIDI_EVENTS)
  421. break;
  422. event = &midiEvents[j];
  423. rackMidiEventsIn[i].time = event->frame;
  424. rackMidiEventsIn[i].size = 3;
  425. memcpy(rackMidiEventsIn[i].data, event->buffer, 3);
  426. i += 1;
  427. }
  428. }
  429. processRack(inBuf, outBuf, frames);
  430. }
  431. // ---------------------------------------------
  432. // Callbacks
  433. void d_bufferSizeChanged(uint32_t newBufferSize)
  434. {
  435. bufferSizeChanged(newBufferSize);
  436. }
  437. // ---------------------------------------------
  438. private:
  439. float paramBuffers[paramCount];
  440. float prevParamBuffers[paramCount];
  441. };
  442. CARLA_BACKEND_END_NAMESPACE
  443. // -------------------------------------------------
  444. START_NAMESPACE_DISTRHO
  445. Plugin* createPlugin()
  446. {
  447. return new CarlaBackend::CarlaEnginePlugin();
  448. }
  449. END_NAMESPACE_DISTRHO
  450. #include "DistrhoPluginMain.cpp"