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.

Config.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Config.cpp - Configuration file functions
  4. Copyright (C) 2003-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 <cstdio>
  18. #include <cmath>
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <rtosc/ports.h>
  22. #include <rtosc/port-sugar.h>
  23. #include "Config.h"
  24. #include "globals.h"
  25. #include "XMLwrapper.h"
  26. #define rStdString(name, len, ...) \
  27. {STRINGIFY(name) "::s", rMap(length, len) rProp(parameter) DOC(__VA_ARGS__), NULL, rStringCb(name,len)}
  28. #define rStdStringCb(name, length) rBOIL_BEGIN \
  29. if(!strcmp("", args)) {\
  30. data.reply(loc, "s", obj->name); \
  31. } else { \
  32. strncpy(obj->name, rtosc_argument(msg, 0).s, length); \
  33. data.broadcast(loc, "s", obj->name);\
  34. rChangeCb \
  35. } rBOIL_END
  36. #if 1
  37. #define rObject Config
  38. static const rtosc::Ports ports = {
  39. //rString(cfg.LinuxOSSWaveOutDev),
  40. //rString(cfg.LinuxOSSSeqInDev),
  41. rParamI(cfg.SampleRate, "samples of audio per second"),
  42. rParamI(cfg.SoundBufferSize, "Size of processed audio buffer"),
  43. rParamI(cfg.OscilSize, "Size Of Oscillator Wavetable"),
  44. rToggle(cfg.SwapStereo, "Swap Left And Right Channels"),
  45. rToggle(cfg.BankUIAutoClose, "Automatic Closing of BackUI After Patch Selection"),
  46. rParamI(cfg.GzipCompression, "Level of Gzip Compression For Save Files"),
  47. rParamI(cfg.Interpolation, "Level of Interpolation, Linear/Cubic"),
  48. {"cfg.presetsDirList", rDoc("list of preset search directories"), 0,
  49. [](const char *msg, rtosc::RtData &d)
  50. {
  51. Config &c = *(Config*)d.obj;
  52. if(rtosc_narguments(msg) != 0) {
  53. std::string args = rtosc_argument_string(msg);
  54. //clear everything
  55. c.clearpresetsdirlist();
  56. for(int i=0; i<(int)args.size(); ++i)
  57. if(args[i] == 's')
  58. c.cfg.presetsDirList[i] = rtosc_argument(msg, i).s;
  59. }
  60. char types[MAX_BANK_ROOT_DIRS+1];
  61. rtosc_arg_t args[MAX_BANK_ROOT_DIRS];
  62. size_t pos = 0;
  63. //zero out data
  64. memset(types, 0, sizeof(types));
  65. memset(args, 0, sizeof(args));
  66. for(int i=0; i<MAX_BANK_ROOT_DIRS; ++i) {
  67. if(!c.cfg.presetsDirList[i].empty()) {
  68. types[pos] = 's';
  69. args[pos].s = c.cfg.presetsDirList[i].c_str();
  70. pos++;
  71. }
  72. }
  73. char buffer[1024*5];
  74. rtosc_amessage(buffer, sizeof(buffer), d.loc, types, args);
  75. d.reply(buffer);
  76. }},
  77. {"cfg.bankRootDirList", rDoc("list of bank search directories"), 0,
  78. [](const char *msg, rtosc::RtData &d)
  79. {
  80. Config &c = *(Config*)d.obj;
  81. if(rtosc_narguments(msg) != 0) {
  82. std::string args = rtosc_argument_string(msg);
  83. //clear everything
  84. c.clearbankrootdirlist();
  85. for(int i=0; i<(int)args.size(); ++i)
  86. if(args[i] == 's')
  87. c.cfg.bankRootDirList[i] = rtosc_argument(msg, i).s;
  88. }
  89. char types[MAX_BANK_ROOT_DIRS+1];
  90. rtosc_arg_t args[MAX_BANK_ROOT_DIRS];
  91. size_t pos = 0;
  92. //zero out data
  93. memset(types, 0, sizeof(types));
  94. memset(args, 0, sizeof(args));
  95. for(int i=0; i<MAX_BANK_ROOT_DIRS; ++i) {
  96. if(!c.cfg.bankRootDirList[i].empty()) {
  97. types[pos] = 's';
  98. args[pos].s = c.cfg.bankRootDirList[i].c_str();
  99. pos++;
  100. }
  101. }
  102. char buffer[1024*5];
  103. rtosc_amessage(buffer, sizeof(buffer), d.loc, types, args);
  104. d.reply(buffer);
  105. }},
  106. //rArrayS(cfg.bankRootDirList,MAX_BANK_ROOT_DIRS),
  107. //rString(cfg.currentBankDir),
  108. //rArrayS(cfg.presetsDirList,MAX_BANK_ROOT_DIRS),
  109. rToggle(cfg.CheckPADsynth, "Old Check For PADsynth functionality within a patch"),
  110. rToggle(cfg.IgnoreProgramChange, "Ignore MIDI Program Change Events"),
  111. rParamI(cfg.UserInterfaceMode, "Beginner/Advanced Mode Select"),
  112. rParamI(cfg.VirKeybLayout, "Keyboard Layout For Virtual Piano Keyboard"),
  113. //rParamS(cfg.LinuxALSAaudioDev),
  114. //rParamS(cfg.nameTag)
  115. {"cfg.OscilPower::i", rProp(parameter) rDoc("Size Of Oscillator Wavetable"), 0,
  116. [](const char *msg, rtosc::RtData &d)
  117. {
  118. Config &c = *(Config*)d.obj;
  119. if(rtosc_narguments(msg) == 0) {
  120. d.reply(d.loc, "i", (int)(log(c.cfg.OscilSize*1.0)/log(2.0)));
  121. return;
  122. }
  123. float val = powf(2.0, rtosc_argument(msg, 0).i);
  124. c.cfg.OscilSize = val;
  125. d.broadcast(d.loc, "i", (int)(log(c.cfg.OscilSize*1.0)/log(2.0)));
  126. }},
  127. };
  128. const rtosc::Ports &Config::ports = ::ports;
  129. #endif
  130. Config::Config()
  131. {}
  132. void Config::init()
  133. {
  134. maxstringsize = MAX_STRING_SIZE; //for ui
  135. //defaults
  136. cfg.SampleRate = 44100;
  137. cfg.SoundBufferSize = 256;
  138. cfg.OscilSize = 1024;
  139. cfg.SwapStereo = 0;
  140. cfg.oss_devs.linux_wave_out = new char[MAX_STRING_SIZE];
  141. snprintf(cfg.oss_devs.linux_wave_out, MAX_STRING_SIZE, "/dev/dsp");
  142. cfg.oss_devs.linux_seq_in = new char[MAX_STRING_SIZE];
  143. snprintf(cfg.oss_devs.linux_seq_in, MAX_STRING_SIZE, "/dev/sequencer");
  144. cfg.WindowsWaveOutId = 0;
  145. cfg.WindowsMidiInId = 0;
  146. cfg.BankUIAutoClose = 0;
  147. cfg.GzipCompression = 3;
  148. cfg.Interpolation = 0;
  149. cfg.CheckPADsynth = 1;
  150. cfg.IgnoreProgramChange = 0;
  151. cfg.UserInterfaceMode = 0;
  152. cfg.VirKeybLayout = 1;
  153. winwavemax = 1;
  154. winmidimax = 1;
  155. //try to find out how many input midi devices are there
  156. winmididevices = new winmidionedevice[winmidimax];
  157. for(int i = 0; i < winmidimax; ++i) {
  158. winmididevices[i].name = new char[MAX_STRING_SIZE];
  159. for(int j = 0; j < MAX_STRING_SIZE; ++j)
  160. winmididevices[i].name[j] = '\0';
  161. }
  162. //get the midi input devices name
  163. cfg.currentBankDir = "./testbnk";
  164. char filename[MAX_STRING_SIZE];
  165. getConfigFileName(filename, MAX_STRING_SIZE);
  166. readConfig(filename);
  167. if(cfg.bankRootDirList[0].empty()) {
  168. //banks
  169. cfg.bankRootDirList[0] = "~/banks";
  170. cfg.bankRootDirList[1] = "./";
  171. cfg.bankRootDirList[2] = "/usr/share/zynaddsubfx/banks";
  172. cfg.bankRootDirList[3] = "/usr/local/share/zynaddsubfx/banks";
  173. #ifdef __APPLE__
  174. cfg.bankRootDirList[4] = "../Resources/banks";
  175. #else
  176. cfg.bankRootDirList[4] = "../banks";
  177. #endif
  178. cfg.bankRootDirList[5] = "banks";
  179. }
  180. if(cfg.presetsDirList[0].empty()) {
  181. //presets
  182. cfg.presetsDirList[0] = "./";
  183. #ifdef __APPLE__
  184. cfg.presetsDirList[1] = "../Resources/presets";
  185. #else
  186. cfg.presetsDirList[1] = "../presets";
  187. #endif
  188. cfg.presetsDirList[2] = "presets";
  189. cfg.presetsDirList[3] = "/usr/share/zynaddsubfx/presets";
  190. cfg.presetsDirList[4] = "/usr/local/share/zynaddsubfx/presets";
  191. }
  192. cfg.LinuxALSAaudioDev = "default";
  193. cfg.nameTag = "";
  194. }
  195. Config::~Config()
  196. {
  197. delete [] cfg.oss_devs.linux_wave_out;
  198. delete [] cfg.oss_devs.linux_seq_in;
  199. for(int i = 0; i < winmidimax; ++i)
  200. delete [] winmididevices[i].name;
  201. delete [] winmididevices;
  202. }
  203. void Config::save() const
  204. {
  205. char filename[MAX_STRING_SIZE];
  206. getConfigFileName(filename, MAX_STRING_SIZE);
  207. saveConfig(filename);
  208. }
  209. void Config::clearbankrootdirlist()
  210. {
  211. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  212. cfg.bankRootDirList[i].clear();
  213. }
  214. void Config::clearpresetsdirlist()
  215. {
  216. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  217. cfg.presetsDirList[i].clear();
  218. }
  219. void Config::readConfig(const char *filename)
  220. {
  221. XMLwrapper xmlcfg;
  222. if(xmlcfg.loadXMLfile(filename) < 0)
  223. return;
  224. if(xmlcfg.enterbranch("CONFIGURATION")) {
  225. cfg.SampleRate = xmlcfg.getpar("sample_rate",
  226. cfg.SampleRate,
  227. 4000,
  228. 1024000);
  229. cfg.SoundBufferSize = xmlcfg.getpar("sound_buffer_size",
  230. cfg.SoundBufferSize,
  231. 16,
  232. 8192);
  233. cfg.OscilSize = xmlcfg.getpar("oscil_size",
  234. cfg.OscilSize,
  235. MAX_AD_HARMONICS * 2,
  236. 131072);
  237. cfg.SwapStereo = xmlcfg.getpar("swap_stereo",
  238. cfg.SwapStereo,
  239. 0,
  240. 1);
  241. cfg.BankUIAutoClose = xmlcfg.getpar("bank_window_auto_close",
  242. cfg.BankUIAutoClose,
  243. 0,
  244. 1);
  245. cfg.GzipCompression = xmlcfg.getpar("gzip_compression",
  246. cfg.GzipCompression,
  247. 0,
  248. 9);
  249. cfg.currentBankDir = xmlcfg.getparstr("bank_current", "");
  250. cfg.Interpolation = xmlcfg.getpar("interpolation",
  251. cfg.Interpolation,
  252. 0,
  253. 1);
  254. cfg.CheckPADsynth = xmlcfg.getpar("check_pad_synth",
  255. cfg.CheckPADsynth,
  256. 0,
  257. 1);
  258. cfg.IgnoreProgramChange = xmlcfg.getpar("ignore_program_change",
  259. cfg.IgnoreProgramChange,
  260. 0,
  261. 1);
  262. cfg.UserInterfaceMode = xmlcfg.getpar("user_interface_mode",
  263. cfg.UserInterfaceMode,
  264. 0,
  265. 2);
  266. cfg.VirKeybLayout = xmlcfg.getpar("virtual_keyboard_layout",
  267. cfg.VirKeybLayout,
  268. 0,
  269. 10);
  270. //get bankroot dirs
  271. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  272. if(xmlcfg.enterbranch("BANKROOT", i)) {
  273. cfg.bankRootDirList[i] = xmlcfg.getparstr("bank_root", "");
  274. xmlcfg.exitbranch();
  275. }
  276. //get preset root dirs
  277. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  278. if(xmlcfg.enterbranch("PRESETSROOT", i)) {
  279. cfg.presetsDirList[i] = xmlcfg.getparstr("presets_root", "");
  280. xmlcfg.exitbranch();
  281. }
  282. //linux stuff
  283. xmlcfg.getparstr("linux_oss_wave_out_dev",
  284. cfg.oss_devs.linux_wave_out,
  285. MAX_STRING_SIZE);
  286. xmlcfg.getparstr("linux_oss_seq_in_dev",
  287. cfg.oss_devs.linux_seq_in,
  288. MAX_STRING_SIZE);
  289. //windows stuff
  290. cfg.WindowsWaveOutId = xmlcfg.getpar("windows_wave_out_id",
  291. cfg.WindowsWaveOutId,
  292. 0,
  293. winwavemax);
  294. cfg.WindowsMidiInId = xmlcfg.getpar("windows_midi_in_id",
  295. cfg.WindowsMidiInId,
  296. 0,
  297. winmidimax);
  298. xmlcfg.exitbranch();
  299. }
  300. cfg.OscilSize = (int) powf(2, ceil(logf(cfg.OscilSize - 1.0f) / logf(2.0f)));
  301. }
  302. void Config::saveConfig(const char *filename) const
  303. {
  304. XMLwrapper *xmlcfg = new XMLwrapper();
  305. xmlcfg->beginbranch("CONFIGURATION");
  306. xmlcfg->addpar("sample_rate", cfg.SampleRate);
  307. xmlcfg->addpar("sound_buffer_size", cfg.SoundBufferSize);
  308. xmlcfg->addpar("oscil_size", cfg.OscilSize);
  309. xmlcfg->addpar("swap_stereo", cfg.SwapStereo);
  310. xmlcfg->addpar("bank_window_auto_close", cfg.BankUIAutoClose);
  311. xmlcfg->addpar("gzip_compression", cfg.GzipCompression);
  312. xmlcfg->addpar("check_pad_synth", cfg.CheckPADsynth);
  313. xmlcfg->addpar("ignore_program_change", cfg.IgnoreProgramChange);
  314. xmlcfg->addparstr("bank_current", cfg.currentBankDir);
  315. xmlcfg->addpar("user_interface_mode", cfg.UserInterfaceMode);
  316. xmlcfg->addpar("virtual_keyboard_layout", cfg.VirKeybLayout);
  317. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  318. if(!cfg.bankRootDirList[i].empty()) {
  319. xmlcfg->beginbranch("BANKROOT", i);
  320. xmlcfg->addparstr("bank_root", cfg.bankRootDirList[i]);
  321. xmlcfg->endbranch();
  322. }
  323. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  324. if(!cfg.presetsDirList[i].empty()) {
  325. xmlcfg->beginbranch("PRESETSROOT", i);
  326. xmlcfg->addparstr("presets_root", cfg.presetsDirList[i]);
  327. xmlcfg->endbranch();
  328. }
  329. xmlcfg->addpar("interpolation", cfg.Interpolation);
  330. //linux stuff
  331. xmlcfg->addparstr("linux_oss_wave_out_dev", cfg.oss_devs.linux_wave_out);
  332. xmlcfg->addparstr("linux_oss_seq_in_dev", cfg.oss_devs.linux_seq_in);
  333. //windows stuff
  334. xmlcfg->addpar("windows_wave_out_id", cfg.WindowsWaveOutId);
  335. xmlcfg->addpar("windows_midi_in_id", cfg.WindowsMidiInId);
  336. xmlcfg->endbranch();
  337. // for some reason (which one?), the gzip compression is bashed to 0
  338. xmlcfg->saveXMLfile(filename, 0);
  339. delete (xmlcfg);
  340. }
  341. void Config::getConfigFileName(char *name, int namesize) const
  342. {
  343. name[0] = 0;
  344. snprintf(name, namesize, "%s%s", getenv("HOME"), "/.zynaddsubfxXML.cfg");
  345. }