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.

541 lines
15KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. main.cpp - Main file of the synthesizer
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Copyright (C) 2012-2014 Mark McCurry
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <iostream>
  18. #include <fstream>
  19. #include <map>
  20. #include <cmath>
  21. #include <cctype>
  22. #include <algorithm>
  23. #include <signal.h>
  24. #include <unistd.h>
  25. #include <pthread.h>
  26. #include <getopt.h>
  27. #include <lo/lo.h>
  28. #include <rtosc/ports.h>
  29. #include <rtosc/thread-link.h>
  30. #include "Params/PADnoteParameters.h"
  31. #include "DSP/FFTwrapper.h"
  32. #include "Misc/PresetExtractor.h"
  33. #include "Misc/Master.h"
  34. #include "Misc/Part.h"
  35. #include "Misc/Util.h"
  36. //Nio System
  37. #include "Nio/Nio.h"
  38. //GUI System
  39. #include "UI/Connection.h"
  40. GUI::ui_handle_t gui;
  41. //Glue Layer
  42. #include "Misc/MiddleWare.h"
  43. MiddleWare *middleware;
  44. using namespace std;
  45. Master *master;
  46. int swaplr = 0; //1 for left-right swapping
  47. int Pexitprogram = 0; //if the UI set this to 1, the program will exit
  48. #if LASH
  49. #include "Misc/LASHClient.h"
  50. LASHClient *lash = NULL;
  51. #endif
  52. #if USE_NSM
  53. #include "UI/NSM.H"
  54. NSM_Client *nsm = 0;
  55. #endif
  56. char *instance_name = 0;
  57. void exitprogram(const Config &config);
  58. extern pthread_t main_thread;
  59. //cleanup on signaled exit
  60. void sigterm_exit(int /*sig*/)
  61. {
  62. if(Pexitprogram)
  63. exit(1);
  64. Pexitprogram = 1;
  65. }
  66. /*
  67. * Program initialisation
  68. */
  69. void initprogram(SYNTH_T synth, Config* config, int prefered_port)
  70. {
  71. middleware = new MiddleWare(std::move(synth), config, prefered_port);
  72. master = middleware->spawnMaster();
  73. master->swaplr = swaplr;
  74. signal(SIGINT, sigterm_exit);
  75. signal(SIGTERM, sigterm_exit);
  76. Nio::init(master->synth, config->cfg.oss_devs, master);
  77. }
  78. /*
  79. * Program exit
  80. */
  81. void exitprogram(const Config& config)
  82. {
  83. Nio::stop();
  84. config.save();
  85. GUI::destroyUi(gui);
  86. delete middleware;
  87. #if LASH
  88. if(lash)
  89. delete lash;
  90. #endif
  91. #if USE_NSM
  92. if(nsm)
  93. delete nsm;
  94. #endif
  95. FFT_cleanup();
  96. }
  97. int main(int argc, char *argv[])
  98. {
  99. main_thread = pthread_self();
  100. SYNTH_T synth;
  101. Config config;
  102. config.init();
  103. int noui = 0;
  104. cerr
  105. << "\nZynAddSubFX - Copyright (c) 2002-2013 Nasca Octavian Paul and others"
  106. << endl;
  107. cerr
  108. << " Copyright (c) 2009-2015 Mark McCurry [active maintainer]"
  109. << endl;
  110. cerr << "Compiled: " << __DATE__ << " " << __TIME__ << endl;
  111. cerr << "This program is free software (GNU GPL v2 or later) and \n";
  112. cerr << "it comes with ABSOLUTELY NO WARRANTY.\n" << endl;
  113. if(argc == 1)
  114. cerr << "Try 'zynaddsubfx --help' for command-line options." << endl;
  115. /* Get the settings from the Config*/
  116. synth.samplerate = config.cfg.SampleRate;
  117. synth.buffersize = config.cfg.SoundBufferSize;
  118. synth.oscilsize = config.cfg.OscilSize;
  119. swaplr = config.cfg.SwapStereo;
  120. Nio::preferedSampleRate(synth.samplerate);
  121. synth.alias(); //build aliases
  122. sprng(time(NULL));
  123. /* Parse command-line options */
  124. struct option opts[] = {
  125. {
  126. "load", 2, NULL, 'l'
  127. },
  128. {
  129. "load-instrument", 2, NULL, 'L'
  130. },
  131. {
  132. "sample-rate", 2, NULL, 'r'
  133. },
  134. {
  135. "buffer-size", 2, NULL, 'b'
  136. },
  137. {
  138. "oscil-size", 2, NULL, 'o'
  139. },
  140. {
  141. "swap", 2, NULL, 'S'
  142. },
  143. {
  144. "no-gui", 2, NULL, 'U'
  145. },
  146. {
  147. "dummy", 2, NULL, 'Y'
  148. },
  149. {
  150. "help", 2, NULL, 'h'
  151. },
  152. {
  153. "version", 2, NULL, 'v'
  154. },
  155. {
  156. "named", 1, NULL, 'N'
  157. },
  158. {
  159. "auto-connect", 0, NULL, 'a'
  160. },
  161. {
  162. "pid-in-client-name", 0, NULL, 'p'
  163. },
  164. {
  165. "prefered-port", 1, NULL, 'P',
  166. },
  167. {
  168. "output", 1, NULL, 'O'
  169. },
  170. {
  171. "input", 1, NULL, 'I'
  172. },
  173. {
  174. "exec-after-init", 1, NULL, 'e'
  175. },
  176. {
  177. "dump-oscdoc", 2, NULL, 'd'
  178. },
  179. {
  180. "ui-title", 1, NULL, 'u'
  181. },
  182. {
  183. 0, 0, 0, 0
  184. }
  185. };
  186. opterr = 0;
  187. int option_index = 0, opt, exitwithhelp = 0, exitwithversion = 0;
  188. int prefered_port = -1;
  189. string loadfile, loadinstrument, execAfterInit, ui_title;
  190. while(1) {
  191. int tmp = 0;
  192. /**\todo check this process for a small memory leak*/
  193. opt = getopt_long(argc,
  194. argv,
  195. "l:L:r:b:o:I:O:N:e:P:u:hvapSDUY",
  196. opts,
  197. &option_index);
  198. char *optarguments = optarg;
  199. #define GETOP(x) if(optarguments) \
  200. x = optarguments
  201. #define GETOPNUM(x) if(optarguments) \
  202. x = atoi(optarguments)
  203. if(opt == -1)
  204. break;
  205. switch(opt) {
  206. case 'h':
  207. exitwithhelp = 1;
  208. break;
  209. case 'v':
  210. exitwithversion = 1;
  211. break;
  212. case 'Y': /* this command a dummy command (has NO effect)
  213. and is used because I need for NSIS installer
  214. (NSIS sometimes forces a command line for a
  215. program, even if I don't need that; eg. when
  216. I want to add a icon to a shortcut.
  217. */
  218. break;
  219. case 'U':
  220. noui = 1;
  221. break;
  222. case 'l':
  223. GETOP(loadfile);
  224. break;
  225. case 'L':
  226. GETOP(loadinstrument);
  227. break;
  228. case 'r':
  229. GETOPNUM(synth.samplerate);
  230. if(synth.samplerate < 4000) {
  231. cerr << "ERROR:Incorrect sample rate: " << optarguments
  232. << endl;
  233. exit(1);
  234. }
  235. break;
  236. case 'b':
  237. GETOPNUM(synth.buffersize);
  238. if(synth.buffersize < 2) {
  239. cerr << "ERROR:Incorrect buffer size: " << optarguments
  240. << endl;
  241. exit(1);
  242. }
  243. break;
  244. case 'o':
  245. if(optarguments)
  246. synth.oscilsize = tmp = atoi(optarguments);
  247. if(synth.oscilsize < MAX_AD_HARMONICS * 2)
  248. synth.oscilsize = MAX_AD_HARMONICS * 2;
  249. synth.oscilsize =
  250. (int) powf(2,
  251. ceil(logf(synth.oscilsize - 1.0f) / logf(2.0f)));
  252. if(tmp != synth.oscilsize)
  253. cerr
  254. <<
  255. "synth.oscilsize is wrong (must be 2^n) or too small. Adjusting to "
  256. << synth.oscilsize << "." << endl;
  257. break;
  258. case 'S':
  259. swaplr = 1;
  260. break;
  261. case 'N':
  262. Nio::setPostfix(optarguments);
  263. break;
  264. case 'I':
  265. if(optarguments)
  266. Nio::setDefaultSource(optarguments);
  267. break;
  268. case 'O':
  269. if(optarguments)
  270. Nio::setDefaultSink(optarguments);
  271. break;
  272. case 'a':
  273. Nio::autoConnect = true;
  274. break;
  275. case 'p':
  276. Nio::pidInClientName = true;
  277. break;
  278. case 'P':
  279. if(optarguments)
  280. prefered_port = atoi(optarguments);
  281. break;
  282. case 'e':
  283. GETOP(execAfterInit);
  284. break;
  285. case 'd':
  286. if(optarguments)
  287. {
  288. rtosc::OscDocFormatter s;
  289. ofstream outfile(optarguments);
  290. s.prog_name = "ZynAddSubFX";
  291. s.p = &Master::ports;
  292. s.uri = "http://example.com/fake/";
  293. s.doc_origin = "http://example.com/fake/url.xml";
  294. s.author_first = "Mark";
  295. s.author_last = "McCurry";
  296. outfile << s;
  297. }
  298. break;
  299. case 'u':
  300. if(optarguments)
  301. ui_title = optarguments;
  302. break;
  303. case '?':
  304. cerr << "ERROR:Bad option or parameter.\n" << endl;
  305. exitwithhelp = 1;
  306. break;
  307. }
  308. }
  309. synth.alias();
  310. if(exitwithversion) {
  311. cout << "Version: " << VERSION << endl;
  312. return 0;
  313. }
  314. if(exitwithhelp != 0) {
  315. cout << "Usage: zynaddsubfx [OPTION]\n\n"
  316. << " -h , --help \t\t\t\t Display command-line help and exit\n"
  317. << " -v , --version \t\t\t Display version and exit\n"
  318. << " -l file, --load=FILE\t\t\t Loads a .xmz file\n"
  319. << " -L file, --load-instrument=FILE\t Loads a .xiz file\n"
  320. << " -r SR, --sample-rate=SR\t\t Set the sample rate SR\n"
  321. <<
  322. " -b BS, --buffer-size=SR\t\t Set the buffer size (granularity)\n"
  323. << " -o OS, --oscil-size=OS\t\t Set the ADsynth oscil. size\n"
  324. << " -S , --swap\t\t\t\t Swap Left <--> Right\n"
  325. <<
  326. " -U , --no-gui\t\t\t\t Run ZynAddSubFX without user interface\n"
  327. << " -N , --named\t\t\t\t Postfix IO Name when possible\n"
  328. << " -a , --auto-connect\t\t\t AutoConnect when using JACK\n"
  329. << " -p , --pid-in-client-name\t\t Append PID to (JACK) "
  330. "client name\n"
  331. << " -P , --preferred-port\t\t\t Preferred OSC Port\n"
  332. << " -O , --output\t\t\t\t Set Output Engine\n"
  333. << " -I , --input\t\t\t\t Set Input Engine\n"
  334. << " -e , --exec-after-init\t\t Run post-initialization script\n"
  335. << " -d , --dump-oscdoc=FILE\t\t Dump oscdoc xml to file\n"
  336. << " -u , --ui-title=TITLE\t\t Extend UI Window Titles\n"
  337. << endl;
  338. return 0;
  339. }
  340. cerr.precision(1);
  341. cerr << std::fixed;
  342. cerr << "\nSample Rate = \t\t" << synth.samplerate << endl;
  343. cerr << "Sound Buffer Size = \t" << synth.buffersize << " samples" << endl;
  344. cerr << "Internal latency = \t" << synth.dt() * 1000.0f << " ms" << endl;
  345. cerr << "ADsynth Oscil.Size = \t" << synth.oscilsize << " samples" << endl;
  346. initprogram(std::move(synth), &config, prefered_port);
  347. bool altered_master = false;
  348. if(!loadfile.empty()) {
  349. altered_master = true;
  350. const char *filename = loadfile.c_str();
  351. int tmp = master->loadXML(filename);
  352. if(tmp < 0) {
  353. cerr << "ERROR: Could not load master file " << loadfile
  354. << "." << endl;
  355. exit(1);
  356. }
  357. else {
  358. strncpy(master->last_xmz, filename, XMZ_PATH_MAX);
  359. master->last_xmz[XMZ_PATH_MAX-1] = 0;
  360. master->applyparameters();
  361. cout << "Master file loaded." << endl;
  362. }
  363. }
  364. if(!loadinstrument.empty()) {
  365. altered_master = true;
  366. int loadtopart = 0;
  367. int tmp = master->part[loadtopart]->loadXMLinstrument(
  368. loadinstrument.c_str());
  369. if(tmp < 0) {
  370. cerr << "ERROR: Could not load instrument file "
  371. << loadinstrument << '.' << endl;
  372. exit(1);
  373. }
  374. else {
  375. master->part[loadtopart]->applyparameters();
  376. master->part[loadtopart]->initialize_rt();
  377. cout << "Instrument file loaded." << endl;
  378. }
  379. }
  380. if(altered_master)
  381. middleware->updateResources(master);
  382. //Run the Nio system
  383. bool ioGood = Nio::start();
  384. if(!execAfterInit.empty()) {
  385. cout << "Executing user supplied command: " << execAfterInit << endl;
  386. if(system(execAfterInit.c_str()) == -1)
  387. cerr << "Command Failed..." << endl;
  388. }
  389. gui = NULL;
  390. //Capture Startup Responses
  391. typedef std::vector<const char *> wait_t;
  392. wait_t msg_waitlist;
  393. middleware->setUiCallback([](void*v,const char*msg) {
  394. wait_t &wait = *(wait_t*)v;
  395. size_t len = rtosc_message_length(msg, -1);
  396. char *copy = new char[len];
  397. memcpy(copy, msg, len);
  398. wait.push_back(copy);
  399. }, &msg_waitlist);
  400. if(!noui)
  401. gui = GUI::createUi(middleware->spawnUiApi(), &Pexitprogram);
  402. middleware->setUiCallback(GUI::raiseUi, gui);
  403. middleware->setIdleCallback([](void*){GUI::tickUi(gui);}, NULL);
  404. //Replay Startup Responses
  405. for(auto msg:msg_waitlist) {
  406. GUI::raiseUi(gui, msg);
  407. delete [] msg;
  408. }
  409. //set titles
  410. if(!ui_title.empty())
  411. GUI::raiseUi(gui, "/ui/title", "s", ui_title.c_str());
  412. if(!noui)
  413. {
  414. GUI::raiseUi(gui, "/show", "i", config.cfg.UserInterfaceMode);
  415. if(!ioGood)
  416. GUI::raiseUi(gui, "/alert", "s",
  417. "Default IO did not initialize.\nDefaulting to NULL backend.");
  418. }
  419. #if USE_NSM
  420. char *nsm_url = getenv("NSM_URL");
  421. if(nsm_url) {
  422. nsm = new NSM_Client;
  423. if(!nsm->init(nsm_url))
  424. nsm->announce("ZynAddSubFX", ":switch:", argv[0]);
  425. else {
  426. delete nsm;
  427. nsm = NULL;
  428. }
  429. }
  430. #endif
  431. #if USE_NSM
  432. if(!nsm)
  433. #endif
  434. {
  435. #if LASH
  436. lash = new LASHClient(&argc, &argv);
  437. GUI::raiseUi(gui, "/session-type", "s", "LASH");
  438. #endif
  439. }
  440. while(Pexitprogram == 0) {
  441. #if USE_NSM
  442. if(nsm) {
  443. nsm->check();
  444. goto done;
  445. }
  446. #endif
  447. #if LASH
  448. {
  449. string filename;
  450. switch(lash->checkevents(filename)) {
  451. case LASHClient::Save:
  452. GUI::raiseUi(gui, "/save-master", "s", filename.c_str());
  453. lash->confirmevent(LASHClient::Save);
  454. break;
  455. case LASHClient::Restore:
  456. GUI::raiseUi(gui, "/load-master", "s", filename.c_str());
  457. lash->confirmevent(LASHClient::Restore);
  458. break;
  459. case LASHClient::Quit:
  460. Pexitprogram = 1;
  461. default:
  462. break;
  463. }
  464. }
  465. #endif //LASH
  466. #if USE_NSM
  467. done:
  468. #endif
  469. GUI::tickUi(gui);
  470. middleware->tick();
  471. }
  472. exitprogram(config);
  473. return 0;
  474. }