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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <stdio.h>
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "Config.h"
  22. #include "XMLwrapper.h"
  23. using namespace std;
  24. Config::Config()
  25. {}
  26. void Config::init()
  27. {
  28. maxstringsize = MAX_STRING_SIZE; //for ui
  29. //defaults
  30. cfg.SampleRate = 44100;
  31. cfg.SoundBufferSize = 256;
  32. cfg.OscilSize = 1024;
  33. cfg.SwapStereo = 0;
  34. cfg.LinuxOSSWaveOutDev = new char[MAX_STRING_SIZE];
  35. snprintf(cfg.LinuxOSSWaveOutDev, MAX_STRING_SIZE, "/dev/dsp");
  36. cfg.LinuxOSSSeqInDev = new char[MAX_STRING_SIZE];
  37. snprintf(cfg.LinuxOSSSeqInDev, MAX_STRING_SIZE, "/dev/sequencer");
  38. cfg.DumpFile = "zynaddsubfx_dump.txt";
  39. cfg.WindowsWaveOutId = 0;
  40. cfg.WindowsMidiInId = 0;
  41. cfg.BankUIAutoClose = 0;
  42. cfg.DumpNotesToFile = 0;
  43. cfg.DumpAppend = 1;
  44. cfg.GzipCompression = 3;
  45. cfg.Interpolation = 0;
  46. cfg.CheckPADsynth = 1;
  47. cfg.IgnoreProgramChange = 0;
  48. cfg.UserInterfaceMode = 0;
  49. cfg.VirKeybLayout = 1;
  50. winwavemax = 1;
  51. winmidimax = 1;
  52. //try to find out how many input midi devices are there
  53. winmididevices = new winmidionedevice[winmidimax];
  54. for(int i = 0; i < winmidimax; ++i) {
  55. winmididevices[i].name = new char[MAX_STRING_SIZE];
  56. for(int j = 0; j < MAX_STRING_SIZE; ++j)
  57. winmididevices[i].name[j] = '\0';
  58. }
  59. //get the midi input devices name
  60. cfg.currentBankDir = "./testbnk";
  61. char filename[MAX_STRING_SIZE];
  62. getConfigFileName(filename, MAX_STRING_SIZE);
  63. readConfig(filename);
  64. if(cfg.bankRootDirList[0].empty()) {
  65. //banks
  66. cfg.bankRootDirList[0] = "~/banks";
  67. cfg.bankRootDirList[1] = "./";
  68. cfg.bankRootDirList[2] = "/usr/share/zynaddsubfx/banks";
  69. cfg.bankRootDirList[3] = "/usr/local/share/zynaddsubfx/banks";
  70. #ifdef __APPLE__
  71. cfg.bankRootDirList[4] = "../Resources/banks";
  72. #else
  73. cfg.bankRootDirList[4] = "../banks";
  74. #endif
  75. cfg.bankRootDirList[5] = "banks";
  76. }
  77. if(cfg.presetsDirList[0].empty()) {
  78. //presets
  79. cfg.presetsDirList[0] = "./";
  80. #ifdef __APPLE__
  81. cfg.presetsDirList[1] = "../Resources/presets";
  82. #else
  83. cfg.presetsDirList[1] = "../presets";
  84. #endif
  85. cfg.presetsDirList[2] = "presets";
  86. cfg.presetsDirList[3] = "/usr/share/zynaddsubfx/presets";
  87. cfg.presetsDirList[4] = "/usr/local/share/zynaddsubfx/presets";
  88. }
  89. cfg.LinuxALSAaudioDev = "default";
  90. cfg.nameTag = "";
  91. }
  92. Config::~Config()
  93. {
  94. delete [] cfg.LinuxOSSWaveOutDev;
  95. delete [] cfg.LinuxOSSSeqInDev;
  96. for(int i = 0; i < winmidimax; ++i)
  97. delete [] winmididevices[i].name;
  98. delete [] winmididevices;
  99. }
  100. void Config::save()
  101. {
  102. char filename[MAX_STRING_SIZE];
  103. getConfigFileName(filename, MAX_STRING_SIZE);
  104. saveConfig(filename);
  105. }
  106. void Config::clearbankrootdirlist()
  107. {
  108. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  109. cfg.bankRootDirList[i].clear();
  110. }
  111. void Config::clearpresetsdirlist()
  112. {
  113. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  114. cfg.presetsDirList[i].clear();
  115. }
  116. void Config::readConfig(const char *filename)
  117. {
  118. XMLwrapper xmlcfg;
  119. if(xmlcfg.loadXMLfile(filename) < 0)
  120. return;
  121. if(xmlcfg.enterbranch("CONFIGURATION")) {
  122. cfg.SampleRate = xmlcfg.getpar("sample_rate",
  123. cfg.SampleRate,
  124. 4000,
  125. 1024000);
  126. cfg.SoundBufferSize = xmlcfg.getpar("sound_buffer_size",
  127. cfg.SoundBufferSize,
  128. 16,
  129. 8192);
  130. cfg.OscilSize = xmlcfg.getpar("oscil_size",
  131. cfg.OscilSize,
  132. MAX_AD_HARMONICS * 2,
  133. 131072);
  134. cfg.SwapStereo = xmlcfg.getpar("swap_stereo",
  135. cfg.SwapStereo,
  136. 0,
  137. 1);
  138. cfg.BankUIAutoClose = xmlcfg.getpar("bank_window_auto_close",
  139. cfg.BankUIAutoClose,
  140. 0,
  141. 1);
  142. cfg.DumpNotesToFile = xmlcfg.getpar("dump_notes_to_file",
  143. cfg.DumpNotesToFile,
  144. 0,
  145. 1);
  146. cfg.DumpAppend = xmlcfg.getpar("dump_append",
  147. cfg.DumpAppend,
  148. 0,
  149. 1);
  150. cfg.DumpFile = xmlcfg.getparstr("dump_file", "");
  151. cfg.GzipCompression = xmlcfg.getpar("gzip_compression",
  152. cfg.GzipCompression,
  153. 0,
  154. 9);
  155. cfg.currentBankDir = xmlcfg.getparstr("bank_current", "");
  156. cfg.Interpolation = xmlcfg.getpar("interpolation",
  157. cfg.Interpolation,
  158. 0,
  159. 1);
  160. cfg.CheckPADsynth = xmlcfg.getpar("check_pad_synth",
  161. cfg.CheckPADsynth,
  162. 0,
  163. 1);
  164. cfg.IgnoreProgramChange = xmlcfg.getpar("ignore_program_change",
  165. cfg.IgnoreProgramChange,
  166. 0,
  167. 1);
  168. cfg.UserInterfaceMode = xmlcfg.getpar("user_interface_mode",
  169. cfg.UserInterfaceMode,
  170. 0,
  171. 2);
  172. cfg.VirKeybLayout = xmlcfg.getpar("virtual_keyboard_layout",
  173. cfg.VirKeybLayout,
  174. 0,
  175. 10);
  176. //get bankroot dirs
  177. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  178. if(xmlcfg.enterbranch("BANKROOT", i)) {
  179. cfg.bankRootDirList[i] = xmlcfg.getparstr("bank_root", "");
  180. xmlcfg.exitbranch();
  181. }
  182. //get preset root dirs
  183. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  184. if(xmlcfg.enterbranch("PRESETSROOT", i)) {
  185. cfg.presetsDirList[i] = xmlcfg.getparstr("presets_root", "");
  186. xmlcfg.exitbranch();
  187. }
  188. //linux stuff
  189. xmlcfg.getparstr("linux_oss_wave_out_dev",
  190. cfg.LinuxOSSWaveOutDev,
  191. MAX_STRING_SIZE);
  192. xmlcfg.getparstr("linux_oss_seq_in_dev",
  193. cfg.LinuxOSSSeqInDev,
  194. MAX_STRING_SIZE);
  195. //windows stuff
  196. cfg.WindowsWaveOutId = xmlcfg.getpar("windows_wave_out_id",
  197. cfg.WindowsWaveOutId,
  198. 0,
  199. winwavemax);
  200. cfg.WindowsMidiInId = xmlcfg.getpar("windows_midi_in_id",
  201. cfg.WindowsMidiInId,
  202. 0,
  203. winmidimax);
  204. xmlcfg.exitbranch();
  205. }
  206. cfg.OscilSize = (int) powf(2, ceil(logf(cfg.OscilSize - 1.0f) / logf(2.0f)));
  207. }
  208. void Config::saveConfig(const char *filename)
  209. {
  210. XMLwrapper *xmlcfg = new XMLwrapper();
  211. xmlcfg->beginbranch("CONFIGURATION");
  212. xmlcfg->addpar("sample_rate", cfg.SampleRate);
  213. xmlcfg->addpar("sound_buffer_size", cfg.SoundBufferSize);
  214. xmlcfg->addpar("oscil_size", cfg.OscilSize);
  215. xmlcfg->addpar("swap_stereo", cfg.SwapStereo);
  216. xmlcfg->addpar("bank_window_auto_close", cfg.BankUIAutoClose);
  217. xmlcfg->addpar("dump_notes_to_file", cfg.DumpNotesToFile);
  218. xmlcfg->addpar("dump_append", cfg.DumpAppend);
  219. xmlcfg->addparstr("dump_file", cfg.DumpFile);
  220. xmlcfg->addpar("gzip_compression", cfg.GzipCompression);
  221. xmlcfg->addpar("check_pad_synth", cfg.CheckPADsynth);
  222. xmlcfg->addpar("ignore_program_change", cfg.IgnoreProgramChange);
  223. xmlcfg->addparstr("bank_current", cfg.currentBankDir);
  224. xmlcfg->addpar("user_interface_mode", cfg.UserInterfaceMode);
  225. xmlcfg->addpar("virtual_keyboard_layout", cfg.VirKeybLayout);
  226. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  227. if(!cfg.bankRootDirList[i].empty()) {
  228. xmlcfg->beginbranch("BANKROOT", i);
  229. xmlcfg->addparstr("bank_root", cfg.bankRootDirList[i]);
  230. xmlcfg->endbranch();
  231. }
  232. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i)
  233. if(!cfg.presetsDirList[i].empty()) {
  234. xmlcfg->beginbranch("PRESETSROOT", i);
  235. xmlcfg->addparstr("presets_root", cfg.presetsDirList[i]);
  236. xmlcfg->endbranch();
  237. }
  238. xmlcfg->addpar("interpolation", cfg.Interpolation);
  239. //linux stuff
  240. xmlcfg->addparstr("linux_oss_wave_out_dev", cfg.LinuxOSSWaveOutDev);
  241. xmlcfg->addparstr("linux_oss_seq_in_dev", cfg.LinuxOSSSeqInDev);
  242. //windows stuff
  243. xmlcfg->addpar("windows_wave_out_id", cfg.WindowsWaveOutId);
  244. xmlcfg->addpar("windows_midi_in_id", cfg.WindowsMidiInId);
  245. xmlcfg->endbranch();
  246. int tmp = cfg.GzipCompression;
  247. cfg.GzipCompression = 0;
  248. xmlcfg->saveXMLfile(filename);
  249. cfg.GzipCompression = tmp;
  250. delete (xmlcfg);
  251. }
  252. void Config::getConfigFileName(char *name, int namesize)
  253. {
  254. name[0] = 0;
  255. snprintf(name, namesize, "%s%s", getenv("HOME"), "/.zynaddsubfxXML.cfg");
  256. }