Collection of tools useful for audio production
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.

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