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.

301 lines
9.9KB

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