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.

712 lines
20KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Microtonal.cpp - Tuning settings and microtonal capabilities
  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 <cmath>
  18. #include <cstring>
  19. #include <cstdio>
  20. #include <rtosc/ports.h>
  21. #include <rtosc/port-sugar.h>
  22. #include "XMLwrapper.h"
  23. #include "Util.h"
  24. #include "Microtonal.h"
  25. #define MAX_LINE_SIZE 80
  26. #define rObject Microtonal
  27. using namespace rtosc;
  28. /**
  29. * TODO
  30. * Consider how much of this should really exist on the rt side of things.
  31. * All the rt side needs is a function to map notes at various keyshifts to
  32. * frequencies, which does not require this many parameters...
  33. *
  34. * A good lookup table should be a good finalization of this
  35. */
  36. const rtosc::Ports Microtonal::ports = {
  37. rToggle(Pinvertupdown, "key mapping inverse"),
  38. rParamZyn(Pinvertupdowncenter, "center of the inversion"),
  39. rToggle(Penabled, "Enable for microtonal mode"),
  40. rParamZyn(PAnote, "The note for 'A'"),
  41. rParamF(PAfreq, "Frequency of the 'A' note"),
  42. rParamZyn(Pscaleshift, "UNDOCUMENTED"),
  43. rParamZyn(Pfirstkey, "First key to retune"),
  44. rParamZyn(Plastkey, "Last key to retune"),
  45. rParamZyn(Pmiddlenote, "Scale degree 0 note"),
  46. //TODO check to see if this should be exposed
  47. rParamZyn(Pmapsize, "UNDOCUMENTED"),
  48. rToggle(Pmappingenabled, "Mapping Enable"),
  49. rParams(Pmapping, "UNDOCUMENTED"),
  50. rParamZyn(Pglobalfinedetune, "Fine detune for all notes"),
  51. rString(Pname, MICROTONAL_MAX_NAME_LEN, "Microtonal Name"),
  52. rString(Pcomment, MICROTONAL_MAX_NAME_LEN, "Microtonal Name"),
  53. {"octavesize:", 0, 0, [](const char*, RtData &d)
  54. {
  55. Microtonal &m = *(Microtonal*)d.obj;
  56. d.reply(d.loc, "i", m.getoctavesize());
  57. }},
  58. };
  59. Microtonal::Microtonal()
  60. {
  61. defaults();
  62. }
  63. void Microtonal::defaults()
  64. {
  65. Pinvertupdown = 0;
  66. Pinvertupdowncenter = 60;
  67. octavesize = 12;
  68. Penabled = 0;
  69. PAnote = 69;
  70. PAfreq = 440.0f;
  71. Pscaleshift = 64;
  72. Pfirstkey = 0;
  73. Plastkey = 127;
  74. Pmiddlenote = 60;
  75. Pmapsize = 12;
  76. Pmappingenabled = 0;
  77. for(int i = 0; i < 128; ++i)
  78. Pmapping[i] = i;
  79. for(int i = 0; i < MAX_OCTAVE_SIZE; ++i) {
  80. octave[i].tuning = tmpoctave[i].tuning = powf(
  81. 2,
  82. (i % octavesize
  83. + 1) / 12.0f);
  84. octave[i].type = tmpoctave[i].type = 1;
  85. octave[i].x1 = tmpoctave[i].x1 = (i % octavesize + 1) * 100;
  86. octave[i].x2 = tmpoctave[i].x2 = 0;
  87. }
  88. octave[11].type = 2;
  89. octave[11].x1 = 2;
  90. octave[11].x2 = 1;
  91. for(int i = 0; i < MICROTONAL_MAX_NAME_LEN; ++i) {
  92. Pname[i] = '\0';
  93. Pcomment[i] = '\0';
  94. }
  95. snprintf((char *) Pname, MICROTONAL_MAX_NAME_LEN, "12tET");
  96. snprintf((char *) Pcomment,
  97. MICROTONAL_MAX_NAME_LEN,
  98. "Equal Temperament 12 notes per octave");
  99. Pglobalfinedetune = 64;
  100. }
  101. Microtonal::~Microtonal()
  102. {}
  103. /*
  104. * Get the size of the octave
  105. */
  106. unsigned char Microtonal::getoctavesize() const
  107. {
  108. if(Penabled != 0)
  109. return octavesize;
  110. else
  111. return 12;
  112. }
  113. /*
  114. * Get the frequency according the note number
  115. */
  116. float Microtonal::getnotefreq(int note, int keyshift) const
  117. {
  118. // in this function will appears many times things like this:
  119. // var=(a+b*100)%b
  120. // I had written this way because if I use var=a%b gives unwanted results when a<0
  121. // This is the same with divisions.
  122. if((Pinvertupdown != 0) && ((Pmappingenabled == 0) || (Penabled == 0)))
  123. note = (int) Pinvertupdowncenter * 2 - note;
  124. //compute global fine detune
  125. float globalfinedetunerap =
  126. powf(2.0f, (Pglobalfinedetune - 64.0f) / 1200.0f); //-64.0f .. 63.0f cents
  127. if(Penabled == 0) //12tET
  128. return powf(2.0f,
  129. (note - PAnote
  130. + keyshift) / 12.0f) * PAfreq * globalfinedetunerap;
  131. int scaleshift =
  132. ((int)Pscaleshift - 64 + (int) octavesize * 100) % octavesize;
  133. //compute the keyshift
  134. float rap_keyshift = 1.0f;
  135. if(keyshift != 0) {
  136. int kskey = (keyshift + (int)octavesize * 100) % octavesize;
  137. int ksoct = (keyshift + (int)octavesize * 100) / octavesize - 100;
  138. rap_keyshift = (kskey == 0) ? (1.0f) : (octave[kskey - 1].tuning);
  139. rap_keyshift *= powf(octave[octavesize - 1].tuning, ksoct);
  140. }
  141. //if the mapping is enabled
  142. if(Pmappingenabled) {
  143. if((note < Pfirstkey) || (note > Plastkey))
  144. return -1.0f;
  145. //Compute how many mapped keys are from middle note to reference note
  146. //and find out the proportion between the freq. of middle note and "A" note
  147. int tmp = PAnote - Pmiddlenote, minus = 0;
  148. if(tmp < 0) {
  149. tmp = -tmp;
  150. minus = 1;
  151. }
  152. int deltanote = 0;
  153. for(int i = 0; i < tmp; ++i)
  154. if(Pmapping[i % Pmapsize] >= 0)
  155. deltanote++;
  156. float rap_anote_middlenote =
  157. (deltanote ==
  158. 0) ? (1.0f) : (octave[(deltanote - 1) % octavesize].tuning);
  159. if(deltanote)
  160. rap_anote_middlenote *=
  161. powf(octave[octavesize - 1].tuning,
  162. (deltanote - 1) / octavesize);
  163. if(minus)
  164. rap_anote_middlenote = 1.0f / rap_anote_middlenote;
  165. //Convert from note (midi) to degree (note from the tunning)
  166. int degoct =
  167. (note - (int)Pmiddlenote + (int) Pmapsize
  168. * 200) / (int)Pmapsize - 200;
  169. int degkey = (note - Pmiddlenote + (int)Pmapsize * 100) % Pmapsize;
  170. degkey = Pmapping[degkey];
  171. if(degkey < 0)
  172. return -1.0f; //this key is not mapped
  173. //invert the keyboard upside-down if it is asked for
  174. //TODO: do the right way by using Pinvertupdowncenter
  175. if(Pinvertupdown != 0) {
  176. degkey = octavesize - degkey - 1;
  177. degoct = -degoct;
  178. }
  179. //compute the frequency of the note
  180. degkey = degkey + scaleshift;
  181. degoct += degkey / octavesize;
  182. degkey %= octavesize;
  183. float freq = (degkey == 0) ? (1.0f) : octave[degkey - 1].tuning;
  184. freq *= powf(octave[octavesize - 1].tuning, degoct);
  185. freq *= PAfreq / rap_anote_middlenote;
  186. freq *= globalfinedetunerap;
  187. if(scaleshift)
  188. freq /= octave[scaleshift - 1].tuning;
  189. return freq * rap_keyshift;
  190. }
  191. else { //if the mapping is disabled
  192. int nt = note - PAnote + scaleshift;
  193. int ntkey = (nt + (int)octavesize * 100) % octavesize;
  194. int ntoct = (nt - ntkey) / octavesize;
  195. float oct = octave[octavesize - 1].tuning;
  196. float freq =
  197. octave[(ntkey + octavesize - 1) % octavesize].tuning * powf(oct,
  198. ntoct)
  199. * PAfreq;
  200. if(!ntkey)
  201. freq /= oct;
  202. if(scaleshift)
  203. freq /= octave[scaleshift - 1].tuning;
  204. freq *= globalfinedetunerap;
  205. return freq * rap_keyshift;
  206. }
  207. }
  208. bool Microtonal::operator==(const Microtonal &micro) const
  209. {
  210. return !(*this != micro);
  211. }
  212. bool Microtonal::operator!=(const Microtonal &micro) const
  213. {
  214. //A simple macro to test equality MiCRotonal EQuals (not the perfect
  215. //approach, but good enough)
  216. #define MCREQ(x) if(x != micro.x) \
  217. return true
  218. //for floats
  219. #define FMCREQ(x) if(!((x < micro.x + 0.0001f) && (x > micro.x - 0.0001f))) \
  220. return true
  221. MCREQ(Pinvertupdown);
  222. MCREQ(Pinvertupdowncenter);
  223. MCREQ(octavesize);
  224. MCREQ(Penabled);
  225. MCREQ(PAnote);
  226. FMCREQ(PAfreq);
  227. MCREQ(Pscaleshift);
  228. MCREQ(Pfirstkey);
  229. MCREQ(Plastkey);
  230. MCREQ(Pmiddlenote);
  231. MCREQ(Pmapsize);
  232. MCREQ(Pmappingenabled);
  233. for(int i = 0; i < 128; ++i)
  234. MCREQ(Pmapping[i]);
  235. for(int i = 0; i < octavesize; ++i) {
  236. FMCREQ(octave[i].tuning);
  237. MCREQ(octave[i].type);
  238. MCREQ(octave[i].x1);
  239. MCREQ(octave[i].x2);
  240. }
  241. if(strcmp((const char *)this->Pname, (const char *)micro.Pname))
  242. return true;
  243. if(strcmp((const char *)this->Pcomment, (const char *)micro.Pcomment))
  244. return true;
  245. MCREQ(Pglobalfinedetune);
  246. return false;
  247. //undefine macros, as they are no longer needed
  248. #undef MCREQ
  249. #undef FMCREQ
  250. }
  251. /*
  252. * Convert a line to tunings; returns -1 if it ok
  253. */
  254. int Microtonal::linetotunings(unsigned int nline, const char *line)
  255. {
  256. int x1 = -1, x2 = -1, type = -1;
  257. float x = -1.0f, tmp, tuning = 1.0f;
  258. if(strstr(line, "/") == NULL) {
  259. if(strstr(line, ".") == NULL) { // M case (M=M/1)
  260. sscanf(line, "%d", &x1);
  261. x2 = 1;
  262. type = 2; //division
  263. }
  264. else { // float number case
  265. sscanf(line, "%f", &x);
  266. if(x < 0.000001f)
  267. return 1;
  268. type = 1; //float type(cents)
  269. }
  270. }
  271. else { // M/N case
  272. sscanf(line, "%d/%d", &x1, &x2);
  273. if((x1 < 0) || (x2 < 0))
  274. return 1;
  275. if(x2 == 0)
  276. x2 = 1;
  277. type = 2; //division
  278. }
  279. if(x1 <= 0)
  280. x1 = 1; //not allow zero frequency sounds (consider 0 as 1)
  281. //convert to float if the number are too big
  282. if((type == 2)
  283. && ((x1 > (128 * 128 * 128 - 1)) || (x2 > (128 * 128 * 128 - 1)))) {
  284. type = 1;
  285. x = ((float) x1) / x2;
  286. }
  287. switch(type) {
  288. case 1:
  289. x1 = (int) floor(x);
  290. tmp = fmod(x, 1.0f);
  291. x2 = (int) (floor(tmp * 1e6));
  292. tuning = powf(2.0f, x / 1200.0f);
  293. break;
  294. case 2:
  295. x = ((float)x1) / x2;
  296. tuning = x;
  297. break;
  298. }
  299. tmpoctave[nline].tuning = tuning;
  300. tmpoctave[nline].type = type;
  301. tmpoctave[nline].x1 = x1;
  302. tmpoctave[nline].x2 = x2;
  303. return -1; //ok
  304. }
  305. /*
  306. * Convert the text to tunnings
  307. */
  308. int Microtonal::texttotunings(const char *text)
  309. {
  310. unsigned int i, k = 0, nl = 0;
  311. char *lin;
  312. lin = new char[MAX_LINE_SIZE + 1];
  313. while(k < strlen(text)) {
  314. for(i = 0; i < MAX_LINE_SIZE; ++i) {
  315. lin[i] = text[k++];
  316. if(lin[i] < 0x20)
  317. break;
  318. }
  319. lin[i] = '\0';
  320. if(strlen(lin) == 0)
  321. continue;
  322. int err = linetotunings(nl, lin);
  323. if(err != -1) {
  324. delete [] lin;
  325. return nl; //Parse error
  326. }
  327. nl++;
  328. }
  329. delete [] lin;
  330. if(nl > MAX_OCTAVE_SIZE)
  331. nl = MAX_OCTAVE_SIZE;
  332. if(nl == 0)
  333. return -2; //the input is empty
  334. octavesize = nl;
  335. for(i = 0; i < octavesize; ++i) {
  336. octave[i].tuning = tmpoctave[i].tuning;
  337. octave[i].type = tmpoctave[i].type;
  338. octave[i].x1 = tmpoctave[i].x1;
  339. octave[i].x2 = tmpoctave[i].x2;
  340. }
  341. return -1; //ok
  342. }
  343. /*
  344. * Convert the text to mapping
  345. */
  346. void Microtonal::texttomapping(const char *text)
  347. {
  348. unsigned int i, k = 0;
  349. char *lin;
  350. lin = new char[MAX_LINE_SIZE + 1];
  351. for(i = 0; i < 128; ++i)
  352. Pmapping[i] = -1;
  353. int tx = 0;
  354. while(k < strlen(text)) {
  355. for(i = 0; i < MAX_LINE_SIZE; ++i) {
  356. lin[i] = text[k++];
  357. if(lin[i] < 0x20)
  358. break;
  359. }
  360. lin[i] = '\0';
  361. if(strlen(lin) == 0)
  362. continue;
  363. int tmp = 0;
  364. if(sscanf(lin, "%d", &tmp) == 0)
  365. tmp = -1;
  366. if(tmp < -1)
  367. tmp = -1;
  368. Pmapping[tx] = tmp;
  369. if((tx++) > 127)
  370. break;
  371. }
  372. delete [] lin;
  373. if(tx == 0)
  374. tx = 1;
  375. Pmapsize = tx;
  376. }
  377. /*
  378. * Convert tunning to text line
  379. */
  380. void Microtonal::tuningtoline(int n, char *line, int maxn)
  381. {
  382. if((n > octavesize) || (n > MAX_OCTAVE_SIZE)) {
  383. line[0] = '\0';
  384. return;
  385. }
  386. if(octave[n].type == 1)
  387. snprintf(line, maxn, "%d.%06d", octave[n].x1, octave[n].x2);
  388. if(octave[n].type == 2)
  389. snprintf(line, maxn, "%d/%d", octave[n].x1, octave[n].x2);
  390. }
  391. int Microtonal::loadline(FILE *file, char *line)
  392. {
  393. do {
  394. if(fgets(line, 500, file) == 0)
  395. return 1;
  396. } while(line[0] == '!');
  397. return 0;
  398. }
  399. /*
  400. * Loads the tunnings from a scl file
  401. */
  402. int Microtonal::loadscl(const char *filename)
  403. {
  404. FILE *file = fopen(filename, "r");
  405. char tmp[500];
  406. fseek(file, 0, SEEK_SET);
  407. //loads the short description
  408. if(loadline(file, &tmp[0]) != 0)
  409. return 2;
  410. for(int i = 0; i < 500; ++i)
  411. if(tmp[i] < 32)
  412. tmp[i] = 0;
  413. snprintf((char *) Pname, MICROTONAL_MAX_NAME_LEN, "%s", tmp);
  414. snprintf((char *) Pcomment, MICROTONAL_MAX_NAME_LEN, "%s", tmp);
  415. //loads the number of the notes
  416. if(loadline(file, &tmp[0]) != 0)
  417. return 2;
  418. int nnotes = MAX_OCTAVE_SIZE;
  419. sscanf(&tmp[0], "%d", &nnotes);
  420. if(nnotes > MAX_OCTAVE_SIZE)
  421. return 2;
  422. //load the tunnings
  423. for(int nline = 0; nline < nnotes; ++nline) {
  424. if(loadline(file, &tmp[0]) != 0)
  425. return 2;
  426. linetotunings(nline, &tmp[0]);
  427. }
  428. fclose(file);
  429. octavesize = nnotes;
  430. for(int i = 0; i < octavesize; ++i) {
  431. octave[i].tuning = tmpoctave[i].tuning;
  432. octave[i].type = tmpoctave[i].type;
  433. octave[i].x1 = tmpoctave[i].x1;
  434. octave[i].x2 = tmpoctave[i].x2;
  435. }
  436. return 0;
  437. }
  438. /*
  439. * Loads the mapping from a kbm file
  440. */
  441. int Microtonal::loadkbm(const char *filename)
  442. {
  443. FILE *file = fopen(filename, "r");
  444. int x;
  445. float tmpPAfreq = 440.0f;
  446. char tmp[500];
  447. fseek(file, 0, SEEK_SET);
  448. //loads the mapsize
  449. if(loadline(file, tmp) != 0 || sscanf(tmp, "%d", &x) == 0)
  450. return 2;
  451. Pmapsize = limit(x, 0, 127);
  452. //loads first MIDI note to retune
  453. if(loadline(file, tmp) != 0 || sscanf(tmp, "%d", &x) == 0)
  454. return 2;
  455. Pfirstkey = limit(x, 0, 127);
  456. //loads last MIDI note to retune
  457. if(loadline(file, tmp) != 0 || sscanf(tmp, "%d", &x) == 0)
  458. return 2;
  459. Plastkey = limit(x, 0, 127);
  460. //loads last the middle note where scale fro scale degree=0
  461. if(loadline(file, tmp) != 0 || sscanf(tmp, "%d", &x) == 0)
  462. return 2;
  463. Pmiddlenote = limit(x, 0, 127);
  464. //loads the reference note
  465. if(loadline(file, tmp) != 0 || sscanf(tmp, "%d", &x) == 0)
  466. return 2;
  467. PAnote = limit(x,0,127);
  468. //loads the reference freq.
  469. if(loadline(file, tmp) != 0 || sscanf(tmp, "%f", &tmpPAfreq) == 0)
  470. return 2;
  471. PAfreq = tmpPAfreq;
  472. //the scale degree(which is the octave) is not loaded,
  473. //it is obtained by the tunnings with getoctavesize() method
  474. if(loadline(file, &tmp[0]) != 0)
  475. return 2;
  476. //load the mappings
  477. if(Pmapsize != 0) {
  478. for(int nline = 0; nline < Pmapsize; ++nline) {
  479. if(loadline(file, tmp) != 0)
  480. return 2;
  481. if(sscanf(tmp, "%d", &x) == 0)
  482. x = -1;
  483. Pmapping[nline] = x;
  484. }
  485. Pmappingenabled = 1;
  486. }
  487. else {
  488. Pmappingenabled = 0;
  489. Pmapping[0] = 0;
  490. Pmapsize = 1;
  491. }
  492. fclose(file);
  493. return 0;
  494. }
  495. void Microtonal::add2XML(XMLwrapper *xml) const
  496. {
  497. xml->addparstr("name", (char *) Pname);
  498. xml->addparstr("comment", (char *) Pcomment);
  499. xml->addparbool("invert_up_down", Pinvertupdown);
  500. xml->addpar("invert_up_down_center", Pinvertupdowncenter);
  501. xml->addparbool("enabled", Penabled);
  502. xml->addpar("global_fine_detune", Pglobalfinedetune);
  503. xml->addpar("a_note", PAnote);
  504. xml->addparreal("a_freq", PAfreq);
  505. if((Penabled == 0) && (xml->minimal))
  506. return;
  507. xml->beginbranch("SCALE");
  508. xml->addpar("scale_shift", Pscaleshift);
  509. xml->addpar("first_key", Pfirstkey);
  510. xml->addpar("last_key", Plastkey);
  511. xml->addpar("middle_note", Pmiddlenote);
  512. xml->beginbranch("OCTAVE");
  513. xml->addpar("octave_size", octavesize);
  514. for(int i = 0; i < octavesize; ++i) {
  515. xml->beginbranch("DEGREE", i);
  516. if(octave[i].type == 1)
  517. xml->addparreal("cents", octave[i].tuning);
  518. ;
  519. if(octave[i].type == 2) {
  520. xml->addpar("numerator", octave[i].x1);
  521. xml->addpar("denominator", octave[i].x2);
  522. }
  523. xml->endbranch();
  524. }
  525. xml->endbranch();
  526. xml->beginbranch("KEYBOARD_MAPPING");
  527. xml->addpar("map_size", Pmapsize);
  528. xml->addpar("mapping_enabled", Pmappingenabled);
  529. for(int i = 0; i < Pmapsize; ++i) {
  530. xml->beginbranch("KEYMAP", i);
  531. xml->addpar("degree", Pmapping[i]);
  532. xml->endbranch();
  533. }
  534. xml->endbranch();
  535. xml->endbranch();
  536. }
  537. void Microtonal::getfromXML(XMLwrapper *xml)
  538. {
  539. xml->getparstr("name", (char *) Pname, MICROTONAL_MAX_NAME_LEN);
  540. xml->getparstr("comment", (char *) Pcomment, MICROTONAL_MAX_NAME_LEN);
  541. Pinvertupdown = xml->getparbool("invert_up_down", Pinvertupdown);
  542. Pinvertupdowncenter = xml->getpar127("invert_up_down_center",
  543. Pinvertupdowncenter);
  544. Penabled = xml->getparbool("enabled", Penabled);
  545. Pglobalfinedetune = xml->getpar127("global_fine_detune", Pglobalfinedetune);
  546. PAnote = xml->getpar127("a_note", PAnote);
  547. PAfreq = xml->getparreal("a_freq", PAfreq, 1.0f, 10000.0f);
  548. if(xml->enterbranch("SCALE")) {
  549. Pscaleshift = xml->getpar127("scale_shift", Pscaleshift);
  550. Pfirstkey = xml->getpar127("first_key", Pfirstkey);
  551. Plastkey = xml->getpar127("last_key", Plastkey);
  552. Pmiddlenote = xml->getpar127("middle_note", Pmiddlenote);
  553. if(xml->enterbranch("OCTAVE")) {
  554. octavesize = xml->getpar127("octave_size", octavesize);
  555. for(int i = 0; i < octavesize; ++i) {
  556. if(xml->enterbranch("DEGREE", i) == 0)
  557. continue;
  558. octave[i].x2 = 0;
  559. octave[i].tuning = xml->getparreal("cents", octave[i].tuning);
  560. octave[i].x1 = xml->getpar127("numerator", octave[i].x1);
  561. octave[i].x2 = xml->getpar127("denominator", octave[i].x2);
  562. if(octave[i].x2 != 0)
  563. octave[i].type = 2;
  564. else {
  565. octave[i].type = 1;
  566. //populate fields for display
  567. float x = logf(octave[i].tuning) / LOG_2 * 1200.0f;
  568. octave[i].x1 = (int) floor(x);
  569. octave[i].x2 = (int) (floor(fmodf(x, 1.0f) * 1e6));
  570. }
  571. xml->exitbranch();
  572. }
  573. xml->exitbranch();
  574. }
  575. if(xml->enterbranch("KEYBOARD_MAPPING")) {
  576. Pmapsize = xml->getpar127("map_size", Pmapsize);
  577. Pmappingenabled = xml->getpar127("mapping_enabled", Pmappingenabled);
  578. for(int i = 0; i < Pmapsize; ++i) {
  579. if(xml->enterbranch("KEYMAP", i) == 0)
  580. continue;
  581. Pmapping[i] = xml->getpar127("degree", Pmapping[i]);
  582. xml->exitbranch();
  583. }
  584. xml->exitbranch();
  585. }
  586. xml->exitbranch();
  587. }
  588. }
  589. int Microtonal::saveXML(const char *filename) const
  590. {
  591. XMLwrapper *xml = new XMLwrapper();
  592. xml->beginbranch("MICROTONAL");
  593. add2XML(xml);
  594. xml->endbranch();
  595. int result = xml->saveXMLfile(filename);
  596. delete (xml);
  597. return result;
  598. }
  599. int Microtonal::loadXML(const char *filename)
  600. {
  601. XMLwrapper *xml = new XMLwrapper();
  602. if(xml->loadXMLfile(filename) < 0) {
  603. delete (xml);
  604. return -1;
  605. }
  606. if(xml->enterbranch("MICROTONAL") == 0)
  607. return -10;
  608. getfromXML(xml);
  609. xml->exitbranch();
  610. delete (xml);
  611. return 0;
  612. }