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.

549 lines
14KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. main.cpp - Main file of the synthesizer
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  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 <FL/Fl.H>
  18. #include "UI/common.H"
  19. #include <iostream>
  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 "DSP/FFTwrapper.h"
  28. #include "Misc/Master.h"
  29. #include "Misc/Part.h"
  30. #include "Misc/Util.h"
  31. #include "Misc/Dump.h"
  32. extern Dump dump;
  33. //Nio System
  34. #include "Nio/Nio.h"
  35. #ifndef DISABLE_GUI
  36. #ifdef QT_GUI
  37. #include <QApplication>
  38. #include "masterui.h"
  39. QApplication *app;
  40. #elif defined FLTK_GUI
  41. #include "UI/MasterUI.h"
  42. #elif defined NTK_GUI
  43. #include "UI/MasterUI.h"
  44. #include <FL/Fl_Shared_Image.H>
  45. #include <FL/Fl_Tiled_Image.H>
  46. #include <FL/Fl_Dial.H>
  47. #endif // FLTK_GUI
  48. MasterUI *ui;
  49. #endif //DISABLE_GUI
  50. using namespace std;
  51. pthread_t thr4;
  52. Master *master;
  53. SYNTH_T *synth;
  54. int swaplr = 0; //1 for left-right swapping
  55. int Pexitprogram = 0; //if the UI set this to 1, the program will exit
  56. #if LASH
  57. #include "Misc/LASHClient.h"
  58. LASHClient *lash = NULL;
  59. #endif
  60. #if USE_NSM
  61. #include "UI/NSM.H"
  62. NSM_Client *nsm = 0;
  63. #endif
  64. char *instance_name = 0;
  65. void exitprogram();
  66. //cleanup on signaled exit
  67. void sigterm_exit(int /*sig*/)
  68. {
  69. Pexitprogram = 1;
  70. }
  71. #ifndef DISABLE_GUI
  72. #ifdef NTK_GUI
  73. static Fl_Tiled_Image *module_backdrop;
  74. #endif
  75. void
  76. set_module_parameters ( Fl_Widget *o )
  77. {
  78. #ifdef NTK_GUI
  79. o->box( FL_DOWN_FRAME );
  80. o->align( o->align() | FL_ALIGN_IMAGE_BACKDROP );
  81. o->color( FL_BLACK );
  82. o->image( module_backdrop );
  83. o->labeltype( FL_SHADOW_LABEL );
  84. #else
  85. o->box( FL_PLASTIC_UP_BOX );
  86. o->color( FL_CYAN );
  87. o->labeltype( FL_EMBOSSED_LABEL );
  88. #endif
  89. }
  90. #endif
  91. /*
  92. * Program initialisation
  93. */
  94. void initprogram(void)
  95. {
  96. cerr.precision(1);
  97. cerr << std::fixed;
  98. cerr << "\nSample Rate = \t\t" << synth->samplerate << endl;
  99. cerr << "Sound Buffer Size = \t" << synth->buffersize << " samples" << endl;
  100. cerr << "Internal latency = \t" << synth->buffersize_f * 1000.0f
  101. / synth->samplerate_f << " ms" << endl;
  102. cerr << "ADsynth Oscil.Size = \t" << synth->oscilsize << " samples" << endl;
  103. master = &Master::getInstance();
  104. master->swaplr = swaplr;
  105. signal(SIGINT, sigterm_exit);
  106. signal(SIGTERM, sigterm_exit);
  107. }
  108. /*
  109. * Program exit
  110. */
  111. void exitprogram()
  112. {
  113. //ensure that everything has stopped with the mutex wait
  114. pthread_mutex_lock(&master->mutex);
  115. pthread_mutex_unlock(&master->mutex);
  116. Nio::stop();
  117. #ifndef DISABLE_GUI
  118. delete ui;
  119. #endif
  120. #if LASH
  121. if(lash)
  122. delete lash;
  123. #endif
  124. #if USE_NSM
  125. if(nsm)
  126. delete nsm;
  127. #endif
  128. delete [] denormalkillbuf;
  129. FFT_cleanup();
  130. Master::deleteInstance();
  131. }
  132. int main(int argc, char *argv[])
  133. {
  134. synth = new SYNTH_T;
  135. config.init();
  136. dump.startnow();
  137. int noui = 0;
  138. cerr
  139. << "\nZynAddSubFX - Copyright (c) 2002-2011 Nasca Octavian Paul and others"
  140. << endl;
  141. cerr << "Compiled: " << __DATE__ << " " << __TIME__ << endl;
  142. cerr << "This program is free software (GNU GPL v.2 or later) and \n";
  143. cerr << "it comes with ABSOLUTELY NO WARRANTY.\n" << endl;
  144. if(argc == 1)
  145. cerr << "Try 'zynaddsubfx --help' for command-line options." << endl;
  146. /* Get the settings from the Config*/
  147. synth->samplerate = config.cfg.SampleRate;
  148. synth->buffersize = config.cfg.SoundBufferSize;
  149. synth->oscilsize = config.cfg.OscilSize;
  150. swaplr = config.cfg.SwapStereo;
  151. Nio::preferedSampleRate(synth->samplerate);
  152. synth->alias(); //build aliases
  153. sprng(time(NULL));
  154. /* Parse command-line options */
  155. struct option opts[] = {
  156. {
  157. "load", 2, NULL, 'l'
  158. },
  159. {
  160. "load-instrument", 2, NULL, 'L'
  161. },
  162. {
  163. "sample-rate", 2, NULL, 'r'
  164. },
  165. {
  166. "buffer-size", 2, NULL, 'b'
  167. },
  168. {
  169. "oscil-size", 2, NULL, 'o'
  170. },
  171. {
  172. "dump", 2, NULL, 'D'
  173. },
  174. {
  175. "swap", 2, NULL, 'S'
  176. },
  177. {
  178. "no-gui", 2, NULL, 'U'
  179. },
  180. {
  181. "dummy", 2, NULL, 'Y'
  182. },
  183. {
  184. "help", 2, NULL, 'h'
  185. },
  186. {
  187. "version", 2, NULL, 'v'
  188. },
  189. {
  190. "named", 1, NULL, 'N'
  191. },
  192. {
  193. "auto-connect", 0, NULL, 'a'
  194. },
  195. {
  196. "output", 1, NULL, 'O'
  197. },
  198. {
  199. "input", 1, NULL, 'I'
  200. },
  201. {
  202. "exec-after-init", 1, NULL, 'e'
  203. },
  204. {
  205. 0, 0, 0, 0
  206. }
  207. };
  208. opterr = 0;
  209. int option_index = 0, opt, exitwithhelp = 0, exitwithversion = 0;
  210. string loadfile, loadinstrument, execAfterInit;
  211. while(1) {
  212. int tmp = 0;
  213. /**\todo check this process for a small memory leak*/
  214. opt = getopt_long(argc,
  215. argv,
  216. "l:L:r:b:o:I:O:N:e:hvaSDUY",
  217. opts,
  218. &option_index);
  219. char *optarguments = optarg;
  220. #define GETOP(x) if(optarguments) \
  221. x = optarguments
  222. #define GETOPNUM(x) if(optarguments) \
  223. x = atoi(optarguments)
  224. if(opt == -1)
  225. break;
  226. switch(opt) {
  227. case 'h':
  228. exitwithhelp = 1;
  229. break;
  230. case 'v':
  231. exitwithversion = 1;
  232. break;
  233. case 'Y': /* this command a dummy command (has NO effect)
  234. and is used because I need for NSIS installer
  235. (NSIS sometimes forces a command line for a
  236. program, even if I don't need that; eg. when
  237. I want to add a icon to a shortcut.
  238. */
  239. break;
  240. case 'U':
  241. noui = 1;
  242. break;
  243. case 'l':
  244. GETOP(loadfile);
  245. break;
  246. case 'L':
  247. GETOP(loadinstrument);
  248. break;
  249. case 'r':
  250. GETOPNUM(synth->samplerate);
  251. if(synth->samplerate < 4000) {
  252. cerr << "ERROR:Incorrect sample rate: " << optarguments
  253. << endl;
  254. exit(1);
  255. }
  256. break;
  257. case 'b':
  258. GETOPNUM(synth->buffersize);
  259. if(synth->buffersize < 2) {
  260. cerr << "ERROR:Incorrect buffer size: " << optarguments
  261. << endl;
  262. exit(1);
  263. }
  264. break;
  265. case 'o':
  266. if(optarguments)
  267. synth->oscilsize = tmp = atoi(optarguments);
  268. if(synth->oscilsize < MAX_AD_HARMONICS * 2)
  269. synth->oscilsize = MAX_AD_HARMONICS * 2;
  270. synth->oscilsize =
  271. (int) powf(2,
  272. ceil(logf(synth->oscilsize - 1.0f) / logf(2.0f)));
  273. if(tmp != synth->oscilsize)
  274. cerr
  275. <<
  276. "synth->oscilsize is wrong (must be 2^n) or too small. Adjusting to "
  277. << synth->oscilsize << "." << endl;
  278. break;
  279. case 'S':
  280. swaplr = 1;
  281. break;
  282. case 'D':
  283. dump.startnow();
  284. break;
  285. case 'N':
  286. Nio::setPostfix(optarguments);
  287. break;
  288. case 'I':
  289. if(optarguments)
  290. Nio::setDefaultSource(optarguments);
  291. break;
  292. case 'O':
  293. if(optarguments)
  294. Nio::setDefaultSink(optarguments);
  295. break;
  296. case 'a':
  297. Nio::autoConnect = true;
  298. break;
  299. case 'e':
  300. GETOP(execAfterInit);
  301. break;
  302. case '?':
  303. cerr << "ERROR:Bad option or parameter.\n" << endl;
  304. exitwithhelp = 1;
  305. break;
  306. }
  307. }
  308. synth->alias();
  309. if(exitwithversion) {
  310. cout << "Version: " << VERSION << endl;
  311. return 0;
  312. }
  313. if(exitwithhelp != 0) {
  314. cout << "Usage: zynaddsubfx [OPTION]\n\n"
  315. << " -h , --help \t\t\t\t Display command-line help and exit\n"
  316. << " -v , --version \t\t\t Display version and exit\n"
  317. << " -l file, --load=FILE\t\t\t Loads a .xmz file\n"
  318. << " -L file, --load-instrument=FILE\t Loads a .xiz file\n"
  319. << " -r SR, --sample-rate=SR\t\t Set the sample rate SR\n"
  320. <<
  321. " -b BS, --buffer-size=SR\t\t Set the buffer size (granularity)\n"
  322. << " -o OS, --oscil-size=OS\t\t Set the ADsynth oscil. size\n"
  323. << " -S , --swap\t\t\t\t Swap Left <--> Right\n"
  324. << " -D , --dump\t\t\t\t Dumps midi note ON/OFF commands\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. << " -O , --output\t\t\t\t Set Output Engine\n"
  330. << " -I , --input\t\t\t\t Set Input Engine\n"
  331. << " -e , --exec-after-init\t\t Run post-initialization script\n"
  332. << endl;
  333. return 0;
  334. }
  335. //produce denormal buf
  336. denormalkillbuf = new float [synth->buffersize];
  337. for(int i = 0; i < synth->buffersize; ++i)
  338. denormalkillbuf[i] = (RND - 0.5f) * 1e-16;
  339. initprogram();
  340. if(!loadfile.empty()) {
  341. int tmp = master->loadXML(loadfile.c_str());
  342. if(tmp < 0) {
  343. cerr << "ERROR: Could not load master file " << loadfile
  344. << "." << endl;
  345. exit(1);
  346. }
  347. else {
  348. master->applyparameters();
  349. cout << "Master file loaded." << endl;
  350. }
  351. }
  352. if(!loadinstrument.empty()) {
  353. int loadtopart = 0;
  354. int tmp = master->part[loadtopart]->loadXMLinstrument(
  355. loadinstrument.c_str());
  356. if(tmp < 0) {
  357. cerr << "ERROR: Could not load instrument file "
  358. << loadinstrument << '.' << endl;
  359. exit(1);
  360. }
  361. else {
  362. master->part[loadtopart]->applyparameters();
  363. cout << "Instrument file loaded." << endl;
  364. }
  365. }
  366. //Run the Nio system
  367. bool ioGood = Nio::start();
  368. if(!execAfterInit.empty()) {
  369. cout << "Executing user supplied command: " << execAfterInit << endl;
  370. if(system(execAfterInit.c_str()) == -1)
  371. cerr << "Command Failed..." << endl;
  372. }
  373. #ifndef DISABLE_GUI
  374. #ifdef NTK_GUI
  375. fl_register_images();
  376. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  377. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/knob.png"))
  378. Fl_Dial::default_image(img);
  379. else
  380. Fl_Dial::default_image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/knob.png"));
  381. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/window_backdrop.png"))
  382. Fl::scheme_bg(new Fl_Tiled_Image(img));
  383. else
  384. Fl::scheme_bg(new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/window_backdrop.png")));
  385. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/module_backdrop.png"))
  386. module_backdrop = new Fl_Tiled_Image(img);
  387. else
  388. module_backdrop = new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/module_backdrop.png"));
  389. Fl::background( 50, 50, 50 );
  390. Fl::background2( 70, 70, 70 );
  391. Fl::foreground( 255,255,255 );
  392. #endif
  393. ui = new MasterUI(master, &Pexitprogram);
  394. if ( !noui)
  395. {
  396. ui->showUI();
  397. if(!ioGood)
  398. fl_alert(
  399. "Default IO did not initialize.\nDefaulting to NULL backend.");
  400. }
  401. #endif
  402. #ifndef DISABLE_GUI
  403. #if USE_NSM
  404. char *nsm_url = getenv("NSM_URL");
  405. if(nsm_url) {
  406. nsm = new NSM_Client;
  407. if(!nsm->init(nsm_url))
  408. nsm->announce("ZynAddSubFX", ":switch:", argv[0]);
  409. else {
  410. delete nsm;
  411. nsm = NULL;
  412. }
  413. }
  414. #endif
  415. #endif
  416. #if USE_NSM
  417. if(!nsm)
  418. #endif
  419. {
  420. #if LASH
  421. lash = new LASHClient(&argc, &argv);
  422. #ifndef DISABLE_GUI
  423. ui->sm_indicator1->value(1);
  424. ui->sm_indicator2->value(1);
  425. ui->sm_indicator1->tooltip("LASH");
  426. ui->sm_indicator2->tooltip("LASH");
  427. #endif
  428. #endif
  429. }
  430. while(Pexitprogram == 0) {
  431. #ifndef DISABLE_GUI
  432. #if USE_NSM
  433. if(nsm) {
  434. nsm->check();
  435. goto done;
  436. }
  437. #endif
  438. #if LASH
  439. {
  440. string filename;
  441. switch(lash->checkevents(filename)) {
  442. case LASHClient::Save:
  443. ui->do_save_master(filename.c_str());
  444. lash->confirmevent(LASHClient::Save);
  445. break;
  446. case LASHClient::Restore:
  447. ui->do_load_master(filename.c_str());
  448. lash->confirmevent(LASHClient::Restore);
  449. break;
  450. case LASHClient::Quit:
  451. Pexitprogram = 1;
  452. default:
  453. break;
  454. }
  455. }
  456. #endif //LASH
  457. #if USE_NSM
  458. done:
  459. #endif
  460. Fl::wait(0.02f);
  461. #else
  462. usleep(100000);
  463. #endif
  464. }
  465. exitprogram();
  466. return 0;
  467. }