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.

548 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. }
  131. int main(int argc, char *argv[])
  132. {
  133. synth = new SYNTH_T;
  134. config.init();
  135. dump.startnow();
  136. int noui = 0;
  137. cerr
  138. << "\nZynAddSubFX - Copyright (c) 2002-2011 Nasca Octavian Paul and others"
  139. << endl;
  140. cerr << "Compiled: " << __DATE__ << " " << __TIME__ << endl;
  141. cerr << "This program is free software (GNU GPL v.2 or later) and \n";
  142. cerr << "it comes with ABSOLUTELY NO WARRANTY.\n" << endl;
  143. if(argc == 1)
  144. cerr << "Try 'zynaddsubfx --help' for command-line options." << endl;
  145. /* Get the settings from the Config*/
  146. synth->samplerate = config.cfg.SampleRate;
  147. synth->buffersize = config.cfg.SoundBufferSize;
  148. synth->oscilsize = config.cfg.OscilSize;
  149. swaplr = config.cfg.SwapStereo;
  150. Nio::preferedSampleRate(synth->samplerate);
  151. synth->alias(); //build aliases
  152. sprng(time(NULL));
  153. /* Parse command-line options */
  154. struct option opts[] = {
  155. {
  156. "load", 2, NULL, 'l'
  157. },
  158. {
  159. "load-instrument", 2, NULL, 'L'
  160. },
  161. {
  162. "sample-rate", 2, NULL, 'r'
  163. },
  164. {
  165. "buffer-size", 2, NULL, 'b'
  166. },
  167. {
  168. "oscil-size", 2, NULL, 'o'
  169. },
  170. {
  171. "dump", 2, NULL, 'D'
  172. },
  173. {
  174. "swap", 2, NULL, 'S'
  175. },
  176. {
  177. "no-gui", 2, NULL, 'U'
  178. },
  179. {
  180. "dummy", 2, NULL, 'Y'
  181. },
  182. {
  183. "help", 2, NULL, 'h'
  184. },
  185. {
  186. "version", 2, NULL, 'v'
  187. },
  188. {
  189. "named", 1, NULL, 'N'
  190. },
  191. {
  192. "auto-connect", 0, NULL, 'a'
  193. },
  194. {
  195. "output", 1, NULL, 'O'
  196. },
  197. {
  198. "input", 1, NULL, 'I'
  199. },
  200. {
  201. "exec-after-init", 1, NULL, 'e'
  202. },
  203. {
  204. 0, 0, 0, 0
  205. }
  206. };
  207. opterr = 0;
  208. int option_index = 0, opt, exitwithhelp = 0, exitwithversion = 0;
  209. string loadfile, loadinstrument, execAfterInit;
  210. while(1) {
  211. int tmp = 0;
  212. /**\todo check this process for a small memory leak*/
  213. opt = getopt_long(argc,
  214. argv,
  215. "l:L:r:b:o:I:O:N:e:hvaSDUY",
  216. opts,
  217. &option_index);
  218. char *optarguments = optarg;
  219. #define GETOP(x) if(optarguments) \
  220. x = optarguments
  221. #define GETOPNUM(x) if(optarguments) \
  222. x = atoi(optarguments)
  223. if(opt == -1)
  224. break;
  225. switch(opt) {
  226. case 'h':
  227. exitwithhelp = 1;
  228. break;
  229. case 'v':
  230. exitwithversion = 1;
  231. break;
  232. case 'Y': /* this command a dummy command (has NO effect)
  233. and is used because I need for NSIS installer
  234. (NSIS sometimes forces a command line for a
  235. program, even if I don't need that; eg. when
  236. I want to add a icon to a shortcut.
  237. */
  238. break;
  239. case 'U':
  240. noui = 1;
  241. break;
  242. case 'l':
  243. GETOP(loadfile);
  244. break;
  245. case 'L':
  246. GETOP(loadinstrument);
  247. break;
  248. case 'r':
  249. GETOPNUM(synth->samplerate);
  250. if(synth->samplerate < 4000) {
  251. cerr << "ERROR:Incorrect sample rate: " << optarguments
  252. << endl;
  253. exit(1);
  254. }
  255. break;
  256. case 'b':
  257. GETOPNUM(synth->buffersize);
  258. if(synth->buffersize < 2) {
  259. cerr << "ERROR:Incorrect buffer size: " << optarguments
  260. << endl;
  261. exit(1);
  262. }
  263. break;
  264. case 'o':
  265. if(optarguments)
  266. synth->oscilsize = tmp = atoi(optarguments);
  267. if(synth->oscilsize < MAX_AD_HARMONICS * 2)
  268. synth->oscilsize = MAX_AD_HARMONICS * 2;
  269. synth->oscilsize =
  270. (int) powf(2,
  271. ceil(logf(synth->oscilsize - 1.0f) / logf(2.0f)));
  272. if(tmp != synth->oscilsize)
  273. cerr
  274. <<
  275. "synth->oscilsize is wrong (must be 2^n) or too small. Adjusting to "
  276. << synth->oscilsize << "." << endl;
  277. break;
  278. case 'S':
  279. swaplr = 1;
  280. break;
  281. case 'D':
  282. dump.startnow();
  283. break;
  284. case 'N':
  285. Nio::setPostfix(optarguments);
  286. break;
  287. case 'I':
  288. if(optarguments)
  289. Nio::setDefaultSource(optarguments);
  290. break;
  291. case 'O':
  292. if(optarguments)
  293. Nio::setDefaultSink(optarguments);
  294. break;
  295. case 'a':
  296. Nio::autoConnect = true;
  297. break;
  298. case 'e':
  299. GETOP(execAfterInit);
  300. break;
  301. case '?':
  302. cerr << "ERROR:Bad option or parameter.\n" << endl;
  303. exitwithhelp = 1;
  304. break;
  305. }
  306. }
  307. synth->alias();
  308. if(exitwithversion) {
  309. cout << "Version: " << VERSION << endl;
  310. return 0;
  311. }
  312. if(exitwithhelp != 0) {
  313. cout << "Usage: zynaddsubfx [OPTION]\n\n"
  314. << " -h , --help \t\t\t\t Display command-line help and exit\n"
  315. << " -v , --version \t\t\t Display version and exit\n"
  316. << " -l file, --load=FILE\t\t\t Loads a .xmz file\n"
  317. << " -L file, --load-instrument=FILE\t Loads a .xiz file\n"
  318. << " -r SR, --sample-rate=SR\t\t Set the sample rate SR\n"
  319. <<
  320. " -b BS, --buffer-size=SR\t\t Set the buffer size (granularity)\n"
  321. << " -o OS, --oscil-size=OS\t\t Set the ADsynth oscil. size\n"
  322. << " -S , --swap\t\t\t\t Swap Left <--> Right\n"
  323. << " -D , --dump\t\t\t\t Dumps midi note ON/OFF commands\n"
  324. <<
  325. " -U , --no-gui\t\t\t\t Run ZynAddSubFX without user interface\n"
  326. << " -N , --named\t\t\t\t Postfix IO Name when possible\n"
  327. << " -a , --auto-connect\t\t\t AutoConnect when using JACK\n"
  328. << " -O , --output\t\t\t\t Set Output Engine\n"
  329. << " -I , --input\t\t\t\t Set Input Engine\n"
  330. << " -e , --exec-after-init\t\t Run post-initialization script\n"
  331. << endl;
  332. return 0;
  333. }
  334. //produce denormal buf
  335. denormalkillbuf = new float [synth->buffersize];
  336. for(int i = 0; i < synth->buffersize; ++i)
  337. denormalkillbuf[i] = (RND - 0.5f) * 1e-16;
  338. initprogram();
  339. if(!loadfile.empty()) {
  340. int tmp = master->loadXML(loadfile.c_str());
  341. if(tmp < 0) {
  342. cerr << "ERROR: Could not load master file " << loadfile
  343. << "." << endl;
  344. exit(1);
  345. }
  346. else {
  347. master->applyparameters();
  348. cout << "Master file loaded." << endl;
  349. }
  350. }
  351. if(!loadinstrument.empty()) {
  352. int loadtopart = 0;
  353. int tmp = master->part[loadtopart]->loadXMLinstrument(
  354. loadinstrument.c_str());
  355. if(tmp < 0) {
  356. cerr << "ERROR: Could not load instrument file "
  357. << loadinstrument << '.' << endl;
  358. exit(1);
  359. }
  360. else {
  361. master->part[loadtopart]->applyparameters();
  362. cout << "Instrument file loaded." << endl;
  363. }
  364. }
  365. //Run the Nio system
  366. bool ioGood = Nio::start();
  367. if(!execAfterInit.empty()) {
  368. cout << "Executing user supplied command: " << execAfterInit << endl;
  369. if(system(execAfterInit.c_str()) == -1)
  370. cerr << "Command Failed..." << endl;
  371. }
  372. #ifndef DISABLE_GUI
  373. #ifdef NTK_GUI
  374. fl_register_images();
  375. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  376. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/knob.png"))
  377. Fl_Dial::default_image(img);
  378. else
  379. Fl_Dial::default_image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/knob.png"));
  380. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/window_backdrop.png"))
  381. Fl::scheme_bg(new Fl_Tiled_Image(img));
  382. else
  383. Fl::scheme_bg(new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/window_backdrop.png")));
  384. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/module_backdrop.png"))
  385. module_backdrop = new Fl_Tiled_Image(img);
  386. else
  387. module_backdrop = new Fl_Tiled_Image(Fl_Shared_Image::get(SOURCE_DIR "/../pixmaps/module_backdrop.png"));
  388. Fl::background( 50, 50, 50 );
  389. Fl::background2( 70, 70, 70 );
  390. Fl::foreground( 255,255,255 );
  391. #endif
  392. ui = new MasterUI(master, &Pexitprogram);
  393. if ( !noui)
  394. {
  395. ui->showUI();
  396. if(!ioGood)
  397. fl_alert(
  398. "Default IO did not initialize.\nDefaulting to NULL backend.");
  399. }
  400. #endif
  401. #ifndef DISABLE_GUI
  402. #if USE_NSM
  403. char *nsm_url = getenv("NSM_URL");
  404. if(nsm_url) {
  405. nsm = new NSM_Client;
  406. if(!nsm->init(nsm_url))
  407. nsm->announce("ZynAddSubFX", ":switch:", argv[0]);
  408. else {
  409. delete nsm;
  410. nsm = NULL;
  411. }
  412. }
  413. #endif
  414. #endif
  415. #if USE_NSM
  416. if(!nsm)
  417. #endif
  418. {
  419. #if LASH
  420. lash = new LASHClient(&argc, &argv);
  421. #ifndef DISABLE_GUI
  422. ui->sm_indicator1->value(1);
  423. ui->sm_indicator2->value(1);
  424. ui->sm_indicator1->tooltip("LASH");
  425. ui->sm_indicator2->tooltip("LASH");
  426. #endif
  427. #endif
  428. }
  429. while(Pexitprogram == 0) {
  430. #ifndef DISABLE_GUI
  431. #if USE_NSM
  432. if(nsm) {
  433. nsm->check();
  434. goto done;
  435. }
  436. #endif
  437. #if LASH
  438. {
  439. string filename;
  440. switch(lash->checkevents(filename)) {
  441. case LASHClient::Save:
  442. ui->do_save_master(filename.c_str());
  443. lash->confirmevent(LASHClient::Save);
  444. break;
  445. case LASHClient::Restore:
  446. ui->do_load_master(filename.c_str());
  447. lash->confirmevent(LASHClient::Restore);
  448. break;
  449. case LASHClient::Quit:
  450. Pexitprogram = 1;
  451. default:
  452. break;
  453. }
  454. }
  455. #endif //LASH
  456. #if USE_NSM
  457. done:
  458. #endif
  459. Fl::wait(0.02f);
  460. #else
  461. usleep(100000);
  462. #endif
  463. }
  464. exitprogram();
  465. return 0;
  466. }