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.

855 lines
25KB

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