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.

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