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.

822 lines
24KB

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