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.

686 lines
18KB

  1. /*
  2. * Carla Native Plugins
  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. // for UINT32_MAX
  18. #define __STDC_LIMIT_MACROS
  19. #include <cstdint>
  20. #include "CarlaNative.hpp"
  21. #include "CarlaMIDI.h"
  22. #include "CarlaString.hpp"
  23. #include "RtList.hpp"
  24. #include <QtCore/QThread>
  25. #include "zynaddsubfx/Misc/Master.h"
  26. #include "zynaddsubfx/Misc/Util.h"
  27. #ifdef WANT_ZYNADDSUBFX_UI
  28. #include "zynaddsubfx/UI/common.H"
  29. #include "zynaddsubfx/UI/MasterUI.h"
  30. #include <FL/Fl_Shared_Image.H>
  31. #include <FL/Fl_Tiled_Image.H>
  32. #include <FL/Fl_Dial.H>
  33. #include <FL/Fl_Theme.H>
  34. #endif
  35. #include <ctime>
  36. #include <set>
  37. #include <string>
  38. // Dummy variables and functions for linking purposes
  39. const char* instance_name = nullptr;
  40. class WavFile;
  41. namespace Nio {
  42. bool start(void){return 1;}
  43. void stop(void){}
  44. bool setSource(std::string){return true;}
  45. bool setSink(std::string){return true;}
  46. std::set<std::string> getSources(void){return std::set<std::string>();}
  47. std::set<std::string> getSinks(void){return std::set<std::string>();}
  48. std::string getSource(void){return "";}
  49. std::string getSink(void){return "";}
  50. void waveNew(WavFile*){}
  51. void waveStart(void){}
  52. void waveStop(void){}
  53. void waveEnd(void){}
  54. }
  55. SYNTH_T* synth = nullptr;
  56. #ifdef WANT_ZYNADDSUBFX_UI
  57. #define PIXMAP_PATH "/usr/share/zynaddsubfx/pixmaps"
  58. #define SOURCE_DIR "/usr/share/zynaddsubfx/examples"
  59. static Fl_Tiled_Image* gModuleBackdrop = nullptr;
  60. void set_module_parameters(Fl_Widget* o)
  61. {
  62. CARLA_ASSERT(gModuleBackdrop != nullptr);
  63. o->box(FL_DOWN_FRAME);
  64. o->align(o->align() | FL_ALIGN_IMAGE_BACKDROP);
  65. o->color(FL_BLACK);
  66. o->image(gModuleBackdrop);
  67. o->labeltype(FL_SHADOW_LABEL);
  68. }
  69. #endif
  70. class ZynAddSubFxPlugin : public PluginDescriptorClass
  71. {
  72. public:
  73. enum Parameters {
  74. PARAMETER_COUNT = 0
  75. };
  76. ZynAddSubFxPlugin(const HostDescriptor* const host)
  77. : PluginDescriptorClass(host),
  78. kMaster(new Master()),
  79. kSampleRate(getSampleRate()),
  80. fThread(kMaster, host)
  81. {
  82. fThread.start();
  83. maybeInitPrograms(kMaster);
  84. //fThread.waitForStarted();
  85. }
  86. ~ZynAddSubFxPlugin()
  87. {
  88. //ensure that everything has stopped
  89. pthread_mutex_lock(&kMaster->mutex);
  90. pthread_mutex_unlock(&kMaster->mutex);
  91. fThread.stop();
  92. fThread.wait();
  93. delete kMaster;
  94. }
  95. protected:
  96. // -------------------------------------------------------------------
  97. // Plugin parameter calls
  98. uint32_t getParameterCount()
  99. {
  100. return PARAMETER_COUNT;
  101. }
  102. const Parameter* getParameterInfo(const uint32_t index)
  103. {
  104. CARLA_ASSERT(index < getParameterCount());
  105. //if (index >= PARAMETER_COUNT)
  106. return nullptr;
  107. static Parameter param;
  108. param.ranges.step = PARAMETER_RANGES_DEFAULT_STEP;
  109. param.ranges.stepSmall = PARAMETER_RANGES_DEFAULT_STEP_SMALL;
  110. param.ranges.stepLarge = PARAMETER_RANGES_DEFAULT_STEP_LARGE;
  111. param.scalePointCount = 0;
  112. param.scalePoints = nullptr;
  113. switch (index)
  114. {
  115. #if 0
  116. case PARAMETER_MASTER:
  117. param.hints = PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE;
  118. param.name = "Master Volume";
  119. param.unit = nullptr;
  120. param.ranges.min = 0.0f;
  121. param.ranges.max = 100.0f;
  122. param.ranges.def = 100.0f;
  123. break;
  124. #endif
  125. }
  126. return &param;
  127. }
  128. float getParameterValue(const uint32_t index)
  129. {
  130. CARLA_ASSERT(index < getParameterCount());
  131. switch (index)
  132. {
  133. #if 0
  134. case PARAMETER_MASTER:
  135. return kMaster->Pvolume;
  136. #endif
  137. default:
  138. return 0.0f;
  139. }
  140. }
  141. // -------------------------------------------------------------------
  142. // Plugin midi-program calls
  143. uint32_t getMidiProgramCount()
  144. {
  145. return sPrograms.count();
  146. }
  147. const MidiProgram* getMidiProgramInfo(const uint32_t index)
  148. {
  149. CARLA_ASSERT(index < getMidiProgramCount());
  150. if (index >= sPrograms.count())
  151. return nullptr;
  152. const ProgramInfo* const pInfo(sPrograms.getAt(index));
  153. static MidiProgram midiProgram;
  154. midiProgram.bank = pInfo->bank;
  155. midiProgram.program = pInfo->prog;
  156. midiProgram.name = pInfo->name;
  157. return &midiProgram;
  158. }
  159. // -------------------------------------------------------------------
  160. // Plugin state calls
  161. void setParameterValue(const uint32_t index, const float value)
  162. {
  163. CARLA_ASSERT(index < getParameterCount());
  164. switch (index)
  165. {
  166. }
  167. return;
  168. // unused, TODO
  169. (void)value;
  170. }
  171. void setMidiProgram(const uint32_t bank, const uint32_t program)
  172. {
  173. if (bank >= kMaster->bank.banks.size())
  174. return;
  175. if (program >= BANK_SIZE)
  176. return;
  177. bool isOffline = false;
  178. if (isOffline)
  179. loadProgram(kMaster, bank, program);
  180. else
  181. fThread.loadLater(bank, program);
  182. }
  183. // -------------------------------------------------------------------
  184. // Plugin process calls
  185. void activate()
  186. {
  187. // broken
  188. //for (int i=0; i < NUM_MIDI_PARTS; i++)
  189. // kMaster->setController(0, MIDI_CONTROL_ALL_SOUND_OFF, 0);
  190. }
  191. void process(float**, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const MidiEvent* const midiEvents)
  192. {
  193. if (pthread_mutex_trylock(&kMaster->mutex) != 0)
  194. {
  195. carla_zeroFloat(outBuffer[0], frames);
  196. carla_zeroFloat(outBuffer[1], frames);
  197. return;
  198. }
  199. for (uint32_t i=0; i < midiEventCount; i++)
  200. {
  201. const MidiEvent* const midiEvent = &midiEvents[i];
  202. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  203. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  204. if (MIDI_IS_STATUS_NOTE_OFF(status))
  205. {
  206. const uint8_t note = midiEvent->data[1];
  207. kMaster->noteOff(channel, note);
  208. }
  209. else if (MIDI_IS_STATUS_NOTE_ON(status))
  210. {
  211. const uint8_t note = midiEvent->data[1];
  212. const uint8_t velo = midiEvent->data[2];
  213. kMaster->noteOn(channel, note, velo);
  214. }
  215. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  216. {
  217. const uint8_t note = midiEvent->data[1];
  218. const uint8_t pressure = midiEvent->data[2];
  219. kMaster->polyphonicAftertouch(channel, note, pressure);
  220. }
  221. }
  222. kMaster->GetAudioOutSamples(frames, kSampleRate, outBuffer[0], outBuffer[1]);
  223. pthread_mutex_unlock(&kMaster->mutex);
  224. }
  225. #ifdef WANT_ZYNADDSUBFX_UI
  226. // -------------------------------------------------------------------
  227. // Plugin UI calls
  228. void uiShow(const bool show)
  229. {
  230. if (show)
  231. fThread.uiShow();
  232. else
  233. fThread.uiHide();
  234. }
  235. #endif
  236. // -------------------------------------------------------------------
  237. // Plugin state calls
  238. char* getState()
  239. {
  240. config.save();
  241. char* data = nullptr;
  242. kMaster->getalldata(&data);
  243. return data;
  244. }
  245. void setState(const char* const data)
  246. {
  247. fThread.stopLoadLater();
  248. kMaster->putalldata((char*)data, 0);
  249. kMaster->applyparameters(true);
  250. }
  251. // -------------------------------------------------------------------
  252. private:
  253. struct ProgramInfo {
  254. uint32_t bank;
  255. uint32_t prog;
  256. const char* name;
  257. ProgramInfo(uint32_t bank_, uint32_t prog_, const char* name_)
  258. : bank(bank_),
  259. prog(prog_),
  260. name(carla_strdup(name_)) {}
  261. ~ProgramInfo()
  262. {
  263. if (name != nullptr)
  264. {
  265. delete[] name;
  266. name = nullptr;
  267. }
  268. }
  269. ProgramInfo() = delete;
  270. ProgramInfo(ProgramInfo&) = delete;
  271. ProgramInfo(const ProgramInfo&) = delete;
  272. };
  273. class ZynThread : public QThread
  274. {
  275. public:
  276. ZynThread(Master* const master, const HostDescriptor* const host)
  277. : kMaster(master),
  278. kHost(host),
  279. #ifdef WANT_ZYNADDSUBFX_UI
  280. fUi(nullptr),
  281. fUiClosed(0),
  282. fNextUiAction(-1),
  283. #endif
  284. fQuit(false),
  285. fChangeProgram(false),
  286. fNextBank(0),
  287. fNextProgram(0)
  288. {
  289. }
  290. ~ZynThread()
  291. {
  292. // must be closed by now
  293. #ifdef WANT_ZYNADDSUBFX_UI
  294. CARLA_ASSERT(fUi == nullptr);
  295. #endif
  296. CARLA_ASSERT(fQuit);
  297. }
  298. void loadLater(const uint32_t bank, const uint32_t program)
  299. {
  300. fNextBank = bank;
  301. fNextProgram = program;
  302. fChangeProgram = true;
  303. }
  304. void stopLoadLater()
  305. {
  306. fChangeProgram = false;
  307. fNextBank = 0;
  308. fNextProgram = 0;
  309. }
  310. void stop()
  311. {
  312. fQuit = true;
  313. quit();
  314. }
  315. #ifdef WANT_ZYNADDSUBFX_UI
  316. void uiShow()
  317. {
  318. fNextUiAction = 1;
  319. }
  320. void uiHide()
  321. {
  322. fNextUiAction = 0;
  323. }
  324. #endif
  325. protected:
  326. void run()
  327. {
  328. while (! fQuit)
  329. {
  330. #ifdef WANT_ZYNADDSUBFX_UI
  331. Fl::lock();
  332. if (fNextUiAction == 1)
  333. {
  334. static bool initialized = false;
  335. if (! initialized)
  336. {
  337. initialized = true;
  338. fl_register_images();
  339. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  340. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/knob.png"))
  341. Fl_Dial::default_image(img);
  342. else
  343. Fl_Dial::default_image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/knob.png"));
  344. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/window_backdrop.png"))
  345. Fl::scheme_bg(new Fl_Tiled_Image(img));
  346. else
  347. Fl::scheme_bg(new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/window_backdrop.png")));
  348. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/module_backdrop.png"))
  349. gModuleBackdrop = new Fl_Tiled_Image(img);
  350. else
  351. gModuleBackdrop = new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/module_backdrop.png"));
  352. Fl::background(50, 50, 50);
  353. Fl::background2(70, 70, 70);
  354. Fl::foreground(255, 255, 255);
  355. Fl_Theme::set("Cairo");
  356. }
  357. CARLA_ASSERT(fUi == nullptr);
  358. if (fUi == nullptr)
  359. {
  360. fUiClosed = 0;
  361. fUi = new MasterUI(kMaster, &fUiClosed);
  362. //fUi->npartcounter->callback(_npartcounterCallback, this);
  363. fUi->showUI();
  364. }
  365. }
  366. else if (fNextUiAction == 0)
  367. {
  368. CARLA_ASSERT(fUi != nullptr);
  369. if (fUi != nullptr)
  370. {
  371. delete fUi;
  372. fUi = nullptr;
  373. }
  374. }
  375. fNextUiAction = -1;
  376. if (fUiClosed != 0)
  377. {
  378. fUiClosed = 0;
  379. fNextUiAction = 0;
  380. kHost->ui_closed(kHost->handle);
  381. }
  382. Fl::check();
  383. Fl::unlock();
  384. #endif
  385. if (fChangeProgram)
  386. {
  387. fChangeProgram = false;
  388. loadProgram(kMaster, fNextBank, fNextProgram);
  389. fNextBank = 0;
  390. fNextProgram = 0;
  391. }
  392. carla_msleep(15);
  393. }
  394. #ifdef WANT_ZYNADDSUBFX_UI
  395. if (fQuit && fUi != nullptr)
  396. {
  397. Fl::lock();
  398. delete fUi;
  399. fUi = nullptr;
  400. Fl::check();
  401. Fl::unlock();
  402. }
  403. #endif
  404. }
  405. #ifdef WANT_ZYNADDSUBFX_UI
  406. void handlePartCounterCallback(Fl_Widget* widget)
  407. {
  408. carla_stdout("handlePartCounterCallback(%p)", widget);
  409. }
  410. static void _npartcounterCallback(Fl_Widget* widget, void* ptr)
  411. {
  412. ((ZynThread*)ptr)->handlePartCounterCallback(widget);
  413. }
  414. #endif
  415. private:
  416. Master* const kMaster;
  417. const HostDescriptor* const kHost;
  418. #ifdef WANT_ZYNADDSUBFX_UI
  419. MasterUI* fUi;
  420. int fUiClosed;
  421. int fNextUiAction;
  422. #endif
  423. bool fQuit;
  424. bool fChangeProgram;
  425. uint32_t fNextBank;
  426. uint32_t fNextProgram;
  427. };
  428. Master* const kMaster;
  429. const unsigned kSampleRate;
  430. ZynThread fThread;
  431. static int sInstanceCount;
  432. static NonRtList<ProgramInfo*> sPrograms;
  433. public:
  434. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
  435. {
  436. if (sInstanceCount++ == 0)
  437. {
  438. CARLA_ASSERT(synth == nullptr);
  439. CARLA_ASSERT(denormalkillbuf == nullptr);
  440. synth = new SYNTH_T();
  441. synth->buffersize = host->get_buffer_size(host->handle);
  442. synth->samplerate = host->get_sample_rate(host->handle);
  443. synth->alias();
  444. config.init();
  445. config.cfg.SoundBufferSize = synth->buffersize;
  446. config.cfg.SampleRate = synth->samplerate;
  447. config.cfg.GzipCompression = 0;
  448. sprng(std::time(nullptr));
  449. denormalkillbuf = new float[synth->buffersize];
  450. for (int i=0; i < synth->buffersize; i++)
  451. denormalkillbuf[i] = (RND - 0.5f) * 1e-16;
  452. Master::getInstance();
  453. }
  454. return new ZynAddSubFxPlugin(host);
  455. }
  456. static void _cleanup(PluginHandle handle)
  457. {
  458. delete (ZynAddSubFxPlugin*)handle;
  459. if (--sInstanceCount == 0)
  460. {
  461. CARLA_ASSERT(synth != nullptr);
  462. CARLA_ASSERT(denormalkillbuf != nullptr);
  463. Master::deleteInstance();
  464. delete[] denormalkillbuf;
  465. denormalkillbuf = nullptr;
  466. delete synth;
  467. synth = nullptr;
  468. }
  469. }
  470. static void maybeInitPrograms(Master* const master)
  471. {
  472. static bool doSearch = true;
  473. if (! doSearch)
  474. return;
  475. doSearch = false;
  476. pthread_mutex_lock(&master->mutex);
  477. // refresh banks
  478. master->bank.rescanforbanks();
  479. for (uint32_t i=0, size = master->bank.banks.size(); i < size; i++)
  480. {
  481. if (master->bank.banks[i].dir.empty())
  482. continue;
  483. master->bank.loadbank(master->bank.banks[i].dir);
  484. for (unsigned int instrument = 0; instrument < BANK_SIZE; instrument++)
  485. {
  486. const std::string insName(master->bank.getname(instrument));
  487. if (insName.empty() || insName[0] == '\0' || insName[0] == ' ')
  488. continue;
  489. sPrograms.append(new ProgramInfo(i, instrument, insName.c_str()));
  490. }
  491. }
  492. pthread_mutex_unlock(&master->mutex);
  493. }
  494. static void loadProgram(Master* const master, const uint32_t bank, const uint32_t program)
  495. {
  496. const std::string& bankdir(master->bank.banks[bank].dir);
  497. if (! bankdir.empty())
  498. {
  499. pthread_mutex_lock(&master->mutex);
  500. master->bank.loadbank(bankdir);
  501. for (int i=0; i < NUM_MIDI_PARTS; i++)
  502. master->bank.loadfromslot(program, master->part[i]);
  503. master->applyparameters(false);
  504. pthread_mutex_unlock(&master->mutex);
  505. }
  506. }
  507. static void clearPrograms()
  508. {
  509. for (auto it = sPrograms.begin(); it.valid(); it.next())
  510. {
  511. ProgramInfo* const programInfo(*it);
  512. delete programInfo;
  513. }
  514. sPrograms.clear();
  515. }
  516. private:
  517. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
  518. };
  519. int ZynAddSubFxPlugin::sInstanceCount = 0;
  520. NonRtList<ZynAddSubFxPlugin::ProgramInfo*> ZynAddSubFxPlugin::sPrograms;
  521. struct ProgramsDestructor {
  522. ProgramsDestructor() {}
  523. ~ProgramsDestructor()
  524. {
  525. ZynAddSubFxPlugin::clearPrograms();
  526. }
  527. } programsDestructor;
  528. // -----------------------------------------------------------------------
  529. static const PluginDescriptor zynAddSubFxDesc = {
  530. /* category */ PLUGIN_CATEGORY_SYNTH,
  531. #ifdef WANT_ZYNADDSUBFX_UI
  532. /* hints */ static_cast<PluginHints>(PLUGIN_IS_SYNTH|PLUGIN_HAS_GUI/*|PLUGIN_USES_SINGLE_THREAD*/|PLUGIN_USES_STATE),
  533. #else
  534. /* hints */ static_cast<PluginHints>(PLUGIN_IS_SYNTH|PLUGIN_USES_STATE),
  535. #endif
  536. /* audioIns */ 2,
  537. /* audioOuts */ 2,
  538. /* midiIns */ 1,
  539. /* midiOuts */ 0,
  540. /* paramIns */ ZynAddSubFxPlugin::PARAMETER_COUNT,
  541. /* paramOuts */ 0,
  542. /* name */ "ZynAddSubFX",
  543. /* label */ "zynaddsubfx",
  544. /* maker */ "falkTX",
  545. /* copyright */ "GNU GPL v2+",
  546. PluginDescriptorFILL(ZynAddSubFxPlugin)
  547. };
  548. // -----------------------------------------------------------------------
  549. void carla_register_native_plugin_zynaddsubfx()
  550. {
  551. carla_register_native_plugin(&zynAddSubFxDesc);
  552. }
  553. // -----------------------------------------------------------------------