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.

582 lines
17KB

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