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.

Microtonal.cpp 20KB

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