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.

502 lines
13KB

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