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.

792 lines
21KB

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