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.

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