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.

1388 lines
45KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Part.cpp - Part implementation
  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 "Part.h"
  18. #include "Microtonal.h"
  19. #include "Util.h"
  20. #include "XMLwrapper.h"
  21. #include "Allocator.h"
  22. #include "../Effects/EffectMgr.h"
  23. #include "../Params/ADnoteParameters.h"
  24. #include "../Params/SUBnoteParameters.h"
  25. #include "../Params/PADnoteParameters.h"
  26. #include "../Synth/Resonance.h"
  27. #include "../Synth/SynthNote.h"
  28. #include "../Synth/ADnote.h"
  29. #include "../Synth/SUBnote.h"
  30. #include "../Synth/PADnote.h"
  31. #include "../DSP/FFTwrapper.h"
  32. #include "../Misc/Util.h"
  33. #include <cstdlib>
  34. #include <cstdio>
  35. #include <cstring>
  36. #include <cassert>
  37. #include <rtosc/ports.h>
  38. #include <rtosc/port-sugar.h>
  39. using rtosc::Ports;
  40. using rtosc::RtData;
  41. #define rObject Part
  42. static const Ports partPorts = {
  43. rRecurs(kit, 16, "Kit"),//NUM_KIT_ITEMS
  44. rRecursp(partefx, 3, "Part Effect"),
  45. rRecur(ctl, "Controller"),
  46. rToggle(Penabled, "Part enable"),
  47. #undef rChangeCb
  48. #define rChangeCb obj->setPvolume(obj->Pvolume);
  49. rParamZyn(Pvolume, "Part Volume"),
  50. #undef rChangeCb
  51. #define rChangeCb obj->setkeylimit(obj->Pkeylimit);
  52. rParamI(Pkeylimit, rProp(parameter), rMap(min,0), rMap(max, POLYPHONY), "Key limit per part"),
  53. #undef rChangeCb
  54. #define rChangeCb
  55. rParamZyn(Pminkey, "Min Used Key"),
  56. rParamZyn(Pmaxkey, "Max Used Key"),
  57. rParamZyn(Pkeyshift, "Part keyshift"),
  58. rParamZyn(Prcvchn, "Active MIDI channel"),
  59. rParamZyn(Ppanning, "Set Panning"),
  60. rParamZyn(Pvelsns, "Velocity sensing"),
  61. rParamZyn(Pveloffs, "Velocity offset"),
  62. rToggle(Pnoteon, "If the channel accepts note on events"),
  63. //TODO FIXME Change to 0=OFF 1=MULTI 2=SINGLE
  64. rParamI(Pkitmode, "Kit mode enable"),
  65. rToggle(Pdrummode, "Drum mode enable"),
  66. rToggle(Ppolymode, "Polyphoney mode"),
  67. rToggle(Plegatomode, "Legato enable"),
  68. rParamZyn(info.Ptype, "Class of Instrument"),
  69. rString(info.Pauthor, MAX_INFO_TEXT_SIZE, "Instrument Author"),
  70. rString(info.Pcomments, MAX_INFO_TEXT_SIZE, "Instrument Comments"),
  71. rString(Pname, PART_MAX_NAME_LEN, "Kit User Specified Label"),
  72. rArray(Pefxroute, NUM_PART_EFX, "Effect Routing"),
  73. rArrayT(Pefxbypass, NUM_PART_EFX, "If an effect is bypassed"),
  74. {"captureMin:", NULL, NULL, [](const char *, RtData &r)
  75. {Part *p = (Part*)r.obj; p->Pminkey = p->lastnote;}},
  76. {"captureMax:", NULL, NULL, [](const char *, RtData &r)
  77. {Part *p = (Part*)r.obj; p->Pmaxkey = p->lastnote;}},
  78. {"polyType::c:i", NULL, NULL, [](const char *msg, RtData &d)
  79. {
  80. Part *p = (Part*)d.obj;
  81. if(!rtosc_narguments(msg)) {
  82. int res = 0;
  83. if(!p->Ppolymode)
  84. res = p->Plegatomode ? 2 : 1;
  85. d.reply(d.loc, "c", res);
  86. return;
  87. }
  88. int i = rtosc_argument(msg, 0).i;
  89. if(i == 0) {
  90. p->Ppolymode = 1;
  91. p->Plegatomode = 0;
  92. } else if(i==1) {
  93. p->Ppolymode = 0;
  94. p->Plegatomode = 0;
  95. } else {
  96. p->Ppolymode = 0;
  97. p->Plegatomode = 1;
  98. }}},
  99. //{"kit#16::T:F", "::Enables or disables kit item", 0,
  100. // [](const char *m, RtData &d) {
  101. // auto loc = d.loc;
  102. // Part *p = (Part*)d.obj;
  103. // unsigned kitid = -1;
  104. // //Note that this event will be captured before transmitted, thus
  105. // //reply/broadcast don't matter
  106. // for(int i=0; i<NUM_KIT_ITEMS; ++i) {
  107. // d.reply("/middleware/oscil", "siisb", loc, kitid, i,
  108. // "oscil", sizeof(void*),
  109. // p->kit[kitid]->adpars->voice[i]->OscilSmp);
  110. // d.reply("/middleware/oscil", "siisb", loc, kitid, i, "oscil-mod"
  111. // sizeof(void*),
  112. // p->kit[kitid]->adpars->voice[i]->somethingelse);
  113. // }
  114. // d.reply("/middleware/pad", "sib", loc, kitid,
  115. // sizeof(PADnoteParameters*),
  116. // p->kit[kitid]->padpars)
  117. // }}
  118. };
  119. #undef rObject
  120. #define rObject Part::Kit
  121. static const Ports kitPorts = {
  122. rRecurp(padpars, "Padnote parameters"),
  123. rRecurp(adpars, "Adnote parameters"),
  124. rRecurp(subpars, "Adnote parameters"),
  125. rToggle(Penabled, "Kit item enable"),
  126. rToggle(Pmuted, "Kit item mute"),
  127. rParamZyn(Pminkey, "Kit item min key"),
  128. rParamZyn(Pmaxkey, "Kit item max key"),
  129. rToggle(Padenabled, "ADsynth enable"),
  130. rToggle(Psubenabled, "SUBsynth enable"),
  131. rToggle(Ppadenabled, "PADsynth enable"),
  132. rParamZyn(Psendtoparteffect, "Effect Levels"),
  133. rString(Pname, PART_MAX_NAME_LEN, "Kit User Specified Label"),
  134. {"padpars-data:b", rProp(internal), 0,
  135. [](const char *msg, RtData &d) {
  136. rObject &o = *(rObject*)d.obj;
  137. assert(o.padpars == NULL);
  138. o.padpars = *(decltype(o.padpars)*)rtosc_argument(msg, 0).b.data;
  139. }},
  140. {"adpars-data:b", rProp(internal), 0,
  141. [](const char *msg, RtData &d) {
  142. rObject &o = *(rObject*)d.obj;
  143. assert(o.adpars == NULL);
  144. o.adpars = *(decltype(o.adpars)*)rtosc_argument(msg, 0).b.data;
  145. }},
  146. {"subpars-data:b", rProp(internal), 0,
  147. [](const char *msg, RtData &d) {
  148. rObject &o = *(rObject*)d.obj;
  149. assert(o.subpars == NULL);
  150. o.subpars = *(decltype(o.subpars)*)rtosc_argument(msg, 0).b.data;
  151. }},
  152. // [](
  153. };
  154. const Ports &Part::Kit::ports = kitPorts;
  155. const Ports &Part::ports = partPorts;
  156. Part::Part(Allocator &alloc, const SYNTH_T &synth_, Microtonal *microtonal_, FFTwrapper *fft_)
  157. :ctl(synth_), memory(alloc), synth(synth_)
  158. {
  159. microtonal = microtonal_;
  160. fft = fft_;
  161. partoutl = new float [synth.buffersize];
  162. partoutr = new float [synth.buffersize];
  163. monomemClear();
  164. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  165. kit[n].Pname = new char [PART_MAX_NAME_LEN];
  166. kit[n].adpars = nullptr;
  167. kit[n].subpars = nullptr;
  168. kit[n].padpars = nullptr;
  169. }
  170. kit[0].adpars = new ADnoteParameters(synth, fft);
  171. //Part's Insertion Effects init
  172. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  173. partefx[nefx] = new EffectMgr(memory, synth, 1);
  174. Pefxbypass[nefx] = false;
  175. }
  176. for(int n = 0; n < NUM_PART_EFX + 1; ++n) {
  177. partfxinputl[n] = new float [synth.buffersize];
  178. partfxinputr[n] = new float [synth.buffersize];
  179. }
  180. killallnotes = false;
  181. oldfreq = -1.0f;
  182. for(int i = 0; i < POLYPHONY; ++i) {
  183. partnote[i].status = KEY_OFF;
  184. partnote[i].note = -1;
  185. partnote[i].itemsplaying = 0;
  186. for(int j = 0; j < NUM_KIT_ITEMS; ++j) {
  187. partnote[i].kititem[j].adnote = NULL;
  188. partnote[i].kititem[j].subnote = NULL;
  189. partnote[i].kititem[j].padnote = NULL;
  190. }
  191. partnote[i].time = 0;
  192. }
  193. cleanup();
  194. Pname = new char[PART_MAX_NAME_LEN];
  195. oldvolumel = oldvolumer = 0.5f;
  196. lastnote = -1;
  197. lastpos = 0; // lastpos will store previously used NoteOn(...)'s pos.
  198. lastlegatomodevalid = false; // To store previous legatomodevalid value.
  199. defaults();
  200. }
  201. void Part::cloneTraits(Part &p) const
  202. {
  203. #define CLONE(x) p.x = this->x
  204. CLONE(Penabled);
  205. p.setPvolume(this->Pvolume);
  206. p.setPpanning(this->Ppanning);
  207. CLONE(Pminkey);
  208. CLONE(Pmaxkey);
  209. CLONE(Pkeyshift);
  210. CLONE(Prcvchn);
  211. CLONE(Pvelsns);
  212. CLONE(Pveloffs);
  213. CLONE(Pnoteon);
  214. CLONE(Ppolymode);
  215. CLONE(Plegatomode);
  216. CLONE(Pkeylimit);
  217. CLONE(ctl);
  218. }
  219. void Part::defaults()
  220. {
  221. Penabled = 0;
  222. Pminkey = 0;
  223. Pmaxkey = 127;
  224. Pnoteon = 1;
  225. Ppolymode = 1;
  226. Plegatomode = 0;
  227. setPvolume(96);
  228. Pkeyshift = 64;
  229. Prcvchn = 0;
  230. setPpanning(64);
  231. Pvelsns = 64;
  232. Pveloffs = 64;
  233. Pkeylimit = 15;
  234. defaultsinstrument();
  235. ctl.defaults();
  236. }
  237. void Part::defaultsinstrument()
  238. {
  239. ZERO(Pname, PART_MAX_NAME_LEN);
  240. info.Ptype = 0;
  241. ZERO(info.Pauthor, MAX_INFO_TEXT_SIZE + 1);
  242. ZERO(info.Pcomments, MAX_INFO_TEXT_SIZE + 1);
  243. Pkitmode = 0;
  244. Pdrummode = 0;
  245. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  246. kit[n].Penabled = false;
  247. kit[n].Pmuted = false;
  248. kit[n].Pminkey = 0;
  249. kit[n].Pmaxkey = 127;
  250. kit[n].Padenabled = false;
  251. kit[n].Psubenabled = false;
  252. kit[n].Ppadenabled = false;
  253. ZERO(kit[n].Pname, PART_MAX_NAME_LEN);
  254. kit[n].Psendtoparteffect = 0;
  255. if(n != 0)
  256. setkititemstatus(n, 0);
  257. }
  258. kit[0].Penabled = 1;
  259. kit[0].Padenabled = 1;
  260. kit[0].adpars->defaults();
  261. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  262. partefx[nefx]->defaults();
  263. Pefxroute[nefx] = 0; //route to next effect
  264. }
  265. }
  266. /*
  267. * Cleanup the part
  268. */
  269. void Part::cleanup(bool final_)
  270. {
  271. for(int k = 0; k < POLYPHONY; ++k)
  272. KillNotePos(k);
  273. for(int i = 0; i < synth.buffersize; ++i) {
  274. partoutl[i] = final_ ? 0.0f : denormalkillbuf[i];
  275. partoutr[i] = final_ ? 0.0f : denormalkillbuf[i];
  276. }
  277. ctl.resetall();
  278. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  279. partefx[nefx]->cleanup();
  280. for(int n = 0; n < NUM_PART_EFX + 1; ++n)
  281. for(int i = 0; i < synth.buffersize; ++i) {
  282. partfxinputl[n][i] = final_ ? 0.0f : denormalkillbuf[i];
  283. partfxinputr[n][i] = final_ ? 0.0f : denormalkillbuf[i];
  284. }
  285. }
  286. Part::~Part()
  287. {
  288. cleanup(true);
  289. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  290. delete kit[n].adpars;
  291. delete kit[n].subpars;
  292. delete kit[n].padpars;
  293. delete [] kit[n].Pname;
  294. }
  295. delete [] Pname;
  296. delete [] partoutl;
  297. delete [] partoutr;
  298. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  299. delete partefx[nefx];
  300. for(int n = 0; n < NUM_PART_EFX + 1; ++n) {
  301. delete [] partfxinputl[n];
  302. delete [] partfxinputr[n];
  303. }
  304. }
  305. /*
  306. * Note On Messages
  307. */
  308. void Part::NoteOn(unsigned char note,
  309. unsigned char velocity,
  310. int masterkeyshift)
  311. {
  312. // Legato and MonoMem used vars:
  313. int posb = POLYPHONY - 1; // Just a dummy initial value.
  314. bool legatomodevalid = false; //true when legato mode is determined applicable.
  315. bool doinglegato = false; // true when we determined we do a legato note.
  316. bool ismonofirstnote = false; /*(In Mono/Legato) true when we determined
  317. no other notes are held down or sustained.*/
  318. int lastnotecopy = lastnote; //Useful after lastnote has been changed.
  319. if(!Pnoteon || !inRange(note, Pminkey, Pmaxkey))
  320. return;
  321. // MonoMem stuff:
  322. if(!Ppolymode) { // If Poly is off
  323. monomemPush(note); // Add note to the list.
  324. monomem[note].velocity = velocity; // Store this note's velocity.
  325. monomem[note].mkeyshift = masterkeyshift; /* Store masterkeyshift too*/
  326. if((partnote[lastpos].status != KEY_PLAYING)
  327. && (partnote[lastpos].status != KEY_RELEASED_AND_SUSTAINED))
  328. ismonofirstnote = true; // No other keys are held or sustained.
  329. } else if(!monomemEmpty())
  330. monomemClear();
  331. lastnote = note;
  332. int pos = -1;
  333. for(int i = 0; i < POLYPHONY; ++i)
  334. if(partnote[i].status == KEY_OFF) {
  335. pos = i;
  336. break;
  337. }
  338. if(Plegatomode && !Pdrummode) {
  339. if(Ppolymode != 0) {
  340. fprintf(
  341. stderr,
  342. "ZynAddSubFX WARNING: Poly and Legato modes are both On, that should not happen ! ... Disabling Legato mode ! - (Part.cpp::NoteOn(..))\n");
  343. Plegatomode = 0;
  344. }
  345. else {
  346. // Legato mode is on and applicable.
  347. legatomodevalid = true;
  348. if((not ismonofirstnote) && (lastlegatomodevalid)) {
  349. // At least one other key is held or sustained, and the
  350. // previous note was played while in valid legato mode.
  351. doinglegato = true; // So we'll do a legato note.
  352. pos = lastpos; // A legato note uses same pos as previous..
  353. posb = lastposb; // .. same goes for posb.
  354. }
  355. else {
  356. // Legato mode is valid, but this is only a first note.
  357. for(int i = 0; i < POLYPHONY; ++i)
  358. if((partnote[i].status == KEY_PLAYING)
  359. || (partnote[i].status == KEY_RELEASED_AND_SUSTAINED))
  360. ReleaseNotePos(i);
  361. // Set posb
  362. posb = (pos + 1) % POLYPHONY; //We really want it (if the following fails)
  363. for(int i = 0; i < POLYPHONY; ++i)
  364. if((partnote[i].status == KEY_OFF) && (pos != i)) {
  365. posb = i;
  366. break;
  367. }
  368. }
  369. lastposb = posb; // Keep a trace of used posb
  370. }
  371. }
  372. else // Legato mode is either off or non-applicable.
  373. if(!Ppolymode) { //if the mode is 'mono' turn off all other notes
  374. for(int i = 0; i < POLYPHONY; ++i)
  375. if(partnote[i].status == KEY_PLAYING)
  376. ReleaseNotePos(i);
  377. ReleaseSustainedKeys();
  378. }
  379. lastlegatomodevalid = legatomodevalid;
  380. if(pos == -1)
  381. fprintf(stderr,
  382. "%s",
  383. "NOTES TOO MANY (> POLYPHONY) - (Part.cpp::NoteOn(..))\n");
  384. else {
  385. //start the note
  386. partnote[pos].status = KEY_PLAYING;
  387. partnote[pos].note = note;
  388. if(legatomodevalid) {
  389. partnote[posb].status = KEY_PLAYING;
  390. partnote[posb].note = note;
  391. }
  392. //this computes the velocity sensing of the part
  393. float vel = VelF(velocity / 127.0f, Pvelsns);
  394. //compute the velocity offset
  395. vel = limit(vel + (Pveloffs - 64.0f) / 64.0f, 0.0f, 1.0f);
  396. //compute the keyshift
  397. int partkeyshift = (int)Pkeyshift - 64;
  398. int keyshift = masterkeyshift + partkeyshift;
  399. //initialise note frequency
  400. float notebasefreq;
  401. if(Pdrummode == 0) {
  402. notebasefreq = microtonal->getnotefreq(note, keyshift);
  403. if(notebasefreq < 0.0f)
  404. return;//the key is no mapped
  405. }
  406. else
  407. notebasefreq = 440.0f * powf(2.0f, (note - 69.0f) / 12.0f);
  408. //Portamento
  409. if(oldfreq < 1.0f)
  410. oldfreq = notebasefreq;//this is only the first note is played
  411. // For Mono/Legato: Force Portamento Off on first
  412. // notes. That means it is required that the previous note is
  413. // still held down or sustained for the Portamento to activate
  414. // (that's like Legato).
  415. bool portamento = false;
  416. if(Ppolymode || !ismonofirstnote)
  417. // I added a third argument to the
  418. // ctl.initportamento(...) function to be able
  419. // to tell it if we're doing a legato note.
  420. portamento = ctl.initportamento(oldfreq, notebasefreq, doinglegato);
  421. if(portamento)
  422. ctl.portamento.noteusing = pos;
  423. oldfreq = notebasefreq;
  424. lastpos = pos; // Keep a trace of used pos.
  425. if(doinglegato) {
  426. // Do Legato note
  427. if(Pkitmode == 0) { // "normal mode" legato note
  428. auto note1 = partnote[pos].kititem[0];
  429. auto note2 = partnote[posb].kititem[0];
  430. LegatoParams pars = {notebasefreq, vel, portamento, note, true};
  431. if(kit[0].Padenabled && note1.adnote && note2.adnote) {
  432. note1.adnote->legatonote(pars);
  433. note2.adnote->legatonote(pars);
  434. }
  435. if(kit[0].Psubenabled && note1.subnote && note2.subnote) {
  436. note1.subnote->legatonote(pars);
  437. note2.subnote->legatonote(pars);
  438. }
  439. if(kit[0].Ppadenabled && note1.padnote && note2.padnote) {
  440. note1.padnote->legatonote(pars);
  441. note2.padnote->legatonote(pars);
  442. }
  443. }
  444. else { // "kit mode" legato note
  445. int ci = 0;
  446. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  447. //Make sure the key is valid and not across multiple ranges
  448. if(kit[item].Pmuted || !inRange(note, kit[item].Pminkey, kit[item].Pmaxkey)
  449. || !inRange((unsigned char)lastnotecopy, kit[item].Pminkey, kit[item].Pmaxkey))
  450. continue;
  451. auto note1 = partnote[pos].kititem[ci];
  452. auto note2 = partnote[posb].kititem[ci];
  453. LegatoParams pars = {notebasefreq, vel, portamento, note, true};
  454. note1.sendtoparteffect = limit((int)kit[item].Psendtoparteffect, 0, NUM_PART_EFX);
  455. note2.sendtoparteffect = limit((int)kit[item].Psendtoparteffect, 0, NUM_PART_EFX);
  456. if(kit[item].Padenabled && kit[item].adpars && note1.adnote && note2.adnote) {
  457. note1.adnote->legatonote(pars);
  458. note2.adnote->legatonote(pars);
  459. }
  460. if(kit[item].Psubenabled && kit[item].subpars && note1.subnote && note2.subnote) {
  461. note1.subnote->legatonote(pars);
  462. note2.subnote->legatonote(pars);
  463. }
  464. if(kit[item].Ppadenabled && kit[item].padpars && note1.padnote && note2.padnote) {
  465. note1.padnote->legatonote(pars);
  466. note2.padnote->legatonote(pars);
  467. }
  468. if(kit[item].adpars || kit[item].subpars || kit[item].padpars) {
  469. ci++;
  470. if((kit[item].Padenabled || kit[item].Psubenabled || kit[item].Ppadenabled) && (Pkitmode == 2))
  471. break;
  472. }
  473. }
  474. if(ci == 0) {
  475. // No legato were performed at all, so pretend nothing happened:
  476. monomemPop(monomemBack()); // Remove last note from the list.
  477. lastnote = lastnotecopy; // Set lastnote back to previous value.
  478. }
  479. }
  480. return; // Ok, Legato note done, return.
  481. }
  482. partnote[pos].itemsplaying = 0;
  483. if(legatomodevalid)
  484. partnote[posb].itemsplaying = 0;
  485. if(Pkitmode == 0) { //init the notes for the "normal mode"
  486. partnote[pos].kititem[0].sendtoparteffect = 0;
  487. SynthParams pars{memory, ctl, synth, notebasefreq, vel, (bool) portamento, note, false};
  488. if(kit[0].Padenabled)
  489. partnote[pos].kititem[0].adnote =
  490. memory.alloc<ADnote>(kit[0].adpars, pars);
  491. if(kit[0].Psubenabled)
  492. partnote[pos].kititem[0].subnote =
  493. memory.alloc<SUBnote>(kit[0].subpars, pars);
  494. if(kit[0].Ppadenabled)
  495. partnote[pos].kititem[0].padnote =
  496. memory.alloc<PADnote>(kit[0].padpars, pars);
  497. if(kit[0].Padenabled || kit[0].Psubenabled || kit[0].Ppadenabled)
  498. partnote[pos].itemsplaying++;
  499. // Spawn another note (but silent) if legatomodevalid==true
  500. if(legatomodevalid) {
  501. partnote[posb].kititem[0].sendtoparteffect = 0;
  502. pars.quiet = true;
  503. if(kit[0].Padenabled)
  504. partnote[posb].kititem[0].adnote =
  505. memory.alloc<ADnote>(kit[0].adpars, pars);
  506. if(kit[0].Psubenabled)
  507. partnote[posb].kititem[0].subnote =
  508. memory.alloc<SUBnote>(kit[0].subpars, pars);
  509. if(kit[0].Ppadenabled)
  510. partnote[posb].kititem[0].padnote =
  511. memory.alloc<PADnote>(kit[0].padpars, pars);
  512. if(kit[0].Padenabled || kit[0].Psubenabled || kit[0].Ppadenabled)
  513. partnote[posb].itemsplaying++;
  514. }
  515. }
  516. else //init the notes for the "kit mode"
  517. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  518. if(kit[item].Pmuted || !inRange(note, kit[item].Pminkey, kit[item].Pmaxkey))
  519. continue;
  520. int ci = partnote[pos].itemsplaying; //ci=current item
  521. auto &note1 = partnote[pos].kititem[ci];
  522. //if this parameter is 127 for "unprocessed"
  523. note1.sendtoparteffect = limit((int)kit[item].Psendtoparteffect, 0, NUM_PART_EFX);
  524. SynthParams pars{memory, ctl, synth, notebasefreq, vel, (bool) portamento, note, false};
  525. if(kit[item].adpars && kit[item].Padenabled)
  526. note1.adnote =
  527. memory.alloc<ADnote>(kit[item].adpars, pars);
  528. if(kit[item].subpars && kit[item].Psubenabled)
  529. note1.subnote =
  530. memory.alloc<SUBnote>(kit[item].subpars, pars);
  531. if(kit[item].padpars && kit[item].Ppadenabled)
  532. note1.padnote =
  533. memory.alloc<PADnote>(kit[item].padpars, pars);
  534. // Spawn another note (but silent) if legatomodevalid==true
  535. if(legatomodevalid) {
  536. auto &note2 = partnote[pos].kititem[ci];
  537. note2.sendtoparteffect = limit((int)kit[item].Psendtoparteffect, 0, NUM_PART_EFX);
  538. pars.quiet = true;
  539. if(kit[item].adpars && kit[item].Padenabled)
  540. note2.adnote =
  541. memory.alloc<ADnote>(kit[item].adpars, pars);
  542. if(kit[item].subpars && kit[item].Psubenabled)
  543. note2.subnote =
  544. memory.alloc<SUBnote>(kit[item].subpars, pars);
  545. if(kit[item].padpars && kit[item].Ppadenabled)
  546. note2.padnote =
  547. memory.alloc<PADnote>(kit[item].padpars, pars);
  548. if(kit[item].adpars || kit[item].subpars || kit[item].padpars)
  549. partnote[posb].itemsplaying++;
  550. }
  551. if(kit[item].adpars || kit[item].subpars) {
  552. partnote[pos].itemsplaying++;
  553. if((kit[item].Padenabled || kit[item].Psubenabled || kit[item].Ppadenabled) && (Pkitmode == 2))
  554. break;
  555. }
  556. }
  557. }
  558. //this only release the keys if there is maximum number of keys allowed
  559. setkeylimit(Pkeylimit);
  560. }
  561. /*
  562. * Note Off Messages
  563. */
  564. void Part::NoteOff(unsigned char note) //release the key
  565. {
  566. // This note is released, so we remove it from the list.
  567. if(!monomemEmpty())
  568. monomemPop(note);
  569. for(int i = POLYPHONY - 1; i >= 0; i--) //first note in, is first out if there are same note multiple times
  570. if((partnote[i].status == KEY_PLAYING) && (partnote[i].note == note)) {
  571. if(!ctl.sustain.sustain) { //the sustain pedal is not pushed
  572. if(!Ppolymode && !monomemEmpty())
  573. MonoMemRenote();//Play most recent still active note
  574. else
  575. ReleaseNotePos(i);
  576. }
  577. else //the sustain pedal is pushed
  578. partnote[i].status = KEY_RELEASED_AND_SUSTAINED;
  579. }
  580. }
  581. void Part::PolyphonicAftertouch(unsigned char note,
  582. unsigned char velocity,
  583. int masterkeyshift)
  584. {
  585. (void) masterkeyshift;
  586. if(!Pnoteon || !inRange(note, Pminkey, Pmaxkey) || Pdrummode)
  587. return;
  588. // MonoMem stuff:
  589. if(!Ppolymode) // if Poly is off
  590. monomem[note].velocity = velocity; // Store this note's velocity.
  591. for(int i = 0; i < POLYPHONY; ++i)
  592. if((partnote[i].note == note) && (partnote[i].status == KEY_PLAYING)) {
  593. /* update velocity */
  594. // compute the velocity offset
  595. float vel = VelF(velocity / 127.0f, Pvelsns) + (Pveloffs - 64.0f) / 64.0f;
  596. vel = limit(vel, 0.0f, 1.0f);
  597. if(!Pkitmode) { // "normal mode"
  598. if(kit[0].Padenabled && partnote[i].kititem[0].adnote)
  599. partnote[i].kititem[0].adnote->setVelocity(vel);
  600. if(kit[0].Psubenabled && partnote[i].kititem[0].subnote)
  601. partnote[i].kititem[0].subnote->setVelocity(vel);
  602. if(kit[0].Ppadenabled && partnote[i].kititem[0].padnote)
  603. partnote[i].kititem[0].padnote->setVelocity(vel);
  604. }
  605. else // "kit mode"
  606. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  607. if(kit[item].Pmuted || !inRange(note, kit[item].Pminkey, kit[item].Pmaxkey))
  608. continue;
  609. if(kit[item].Padenabled && partnote[i].kititem[item].adnote)
  610. partnote[i].kititem[item].adnote->setVelocity(vel);
  611. if(kit[item].Psubenabled && partnote[i].kititem[item].subnote)
  612. partnote[i].kititem[item].subnote->setVelocity(vel);
  613. if(kit[item].Ppadenabled && partnote[i].kititem[item].padnote)
  614. partnote[i].kititem[item].padnote->setVelocity(vel);
  615. }
  616. }
  617. }
  618. /*
  619. * Controllers
  620. */
  621. void Part::SetController(unsigned int type, int par)
  622. {
  623. switch(type) {
  624. case C_pitchwheel:
  625. ctl.setpitchwheel(par);
  626. break;
  627. case C_expression:
  628. ctl.setexpression(par);
  629. setPvolume(Pvolume); //update the volume
  630. break;
  631. case C_portamento:
  632. ctl.setportamento(par);
  633. break;
  634. case C_panning:
  635. ctl.setpanning(par);
  636. setPpanning(Ppanning); //update the panning
  637. break;
  638. case C_filtercutoff:
  639. ctl.setfiltercutoff(par);
  640. break;
  641. case C_filterq:
  642. ctl.setfilterq(par);
  643. break;
  644. case C_bandwidth:
  645. ctl.setbandwidth(par);
  646. break;
  647. case C_modwheel:
  648. ctl.setmodwheel(par);
  649. break;
  650. case C_fmamp:
  651. ctl.setfmamp(par);
  652. break;
  653. case C_volume:
  654. ctl.setvolume(par);
  655. if(ctl.volume.receive != 0)
  656. volume = ctl.volume.volume;
  657. else
  658. setPvolume(Pvolume);
  659. break;
  660. case C_sustain:
  661. ctl.setsustain(par);
  662. if(ctl.sustain.sustain == 0)
  663. ReleaseSustainedKeys();
  664. break;
  665. case C_allsoundsoff:
  666. AllNotesOff(); //Panic
  667. break;
  668. case C_resetallcontrollers:
  669. ctl.resetall();
  670. ReleaseSustainedKeys();
  671. if(ctl.volume.receive != 0)
  672. volume = ctl.volume.volume;
  673. else
  674. setPvolume(Pvolume);
  675. setPvolume(Pvolume); //update the volume
  676. setPpanning(Ppanning); //update the panning
  677. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  678. if(kit[item].adpars == NULL)
  679. continue;
  680. kit[item].adpars->GlobalPar.Reson->
  681. sendcontroller(C_resonance_center, 1.0f);
  682. kit[item].adpars->GlobalPar.Reson->
  683. sendcontroller(C_resonance_bandwidth, 1.0f);
  684. }
  685. //more update to add here if I add controllers
  686. break;
  687. case C_allnotesoff:
  688. ReleaseAllKeys();
  689. break;
  690. case C_resonance_center:
  691. ctl.setresonancecenter(par);
  692. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  693. if(kit[item].adpars == NULL)
  694. continue;
  695. kit[item].adpars->GlobalPar.Reson->
  696. sendcontroller(C_resonance_center,
  697. ctl.resonancecenter.relcenter);
  698. }
  699. break;
  700. case C_resonance_bandwidth:
  701. ctl.setresonancebw(par);
  702. kit[0].adpars->GlobalPar.Reson->
  703. sendcontroller(C_resonance_bandwidth, ctl.resonancebandwidth.relbw);
  704. break;
  705. }
  706. }
  707. /*
  708. * Release the sustained keys
  709. */
  710. void Part::ReleaseSustainedKeys()
  711. {
  712. // Let's call MonoMemRenote() on some conditions:
  713. if(Ppolymode == 0 && !monomemEmpty())
  714. if(monomemBack() != lastnote) // Sustain controller manipulation would cause repeated same note respawn without this check.
  715. MonoMemRenote(); // To play most recent still held note.
  716. for(int i = 0; i < POLYPHONY; ++i)
  717. if(partnote[i].status == KEY_RELEASED_AND_SUSTAINED)
  718. ReleaseNotePos(i);
  719. }
  720. /*
  721. * Release all keys
  722. */
  723. void Part::ReleaseAllKeys()
  724. {
  725. for(int i = 0; i < POLYPHONY; ++i)
  726. if((partnote[i].status != KEY_RELEASED)
  727. && (partnote[i].status != KEY_OFF)) //thanks to Frank Neumann
  728. ReleaseNotePos(i);
  729. }
  730. // Call NoteOn(...) with the most recent still held key as new note
  731. // (Made for Mono/Legato).
  732. void Part::MonoMemRenote()
  733. {
  734. unsigned char mmrtempnote = monomemBack(); // Last list element.
  735. monomemPop(mmrtempnote); // We remove it, will be added again in NoteOn(...).
  736. if(Pnoteon == 0)
  737. ReleaseNotePos(lastpos);
  738. else
  739. NoteOn(mmrtempnote, monomem[mmrtempnote].velocity,
  740. monomem[mmrtempnote].mkeyshift);
  741. }
  742. /*
  743. * Release note at position
  744. */
  745. void Part::ReleaseNotePos(int pos)
  746. {
  747. for(int j = 0; j < NUM_KIT_ITEMS; ++j) {
  748. if(partnote[pos].kititem[j].adnote)
  749. partnote[pos].kititem[j].adnote->releasekey();
  750. if(partnote[pos].kititem[j].subnote)
  751. partnote[pos].kititem[j].subnote->releasekey();
  752. if(partnote[pos].kititem[j].padnote)
  753. partnote[pos].kititem[j].padnote->releasekey();
  754. }
  755. partnote[pos].status = KEY_RELEASED;
  756. }
  757. /*
  758. * Kill note at position
  759. */
  760. void Part::KillNotePos(int pos)
  761. {
  762. partnote[pos].status = KEY_OFF;
  763. partnote[pos].note = -1;
  764. partnote[pos].time = 0;
  765. partnote[pos].itemsplaying = 0;
  766. for(int j = 0; j < NUM_KIT_ITEMS; ++j) {
  767. memory.dealloc(partnote[pos].kititem[j].adnote);
  768. memory.dealloc(partnote[pos].kititem[j].subnote);
  769. memory.dealloc(partnote[pos].kititem[j].padnote);
  770. }
  771. if(pos == ctl.portamento.noteusing) {
  772. ctl.portamento.noteusing = -1;
  773. ctl.portamento.used = 0;
  774. }
  775. }
  776. /*
  777. * Set Part's key limit
  778. */
  779. void Part::setkeylimit(unsigned char Pkeylimit)
  780. {
  781. this->Pkeylimit = Pkeylimit;
  782. int keylimit = Pkeylimit;
  783. if(keylimit == 0)
  784. keylimit = POLYPHONY - 5;
  785. //release old keys if the number of notes>keylimit
  786. if(Ppolymode != 0) {
  787. int notecount = 0;
  788. for(int i = 0; i < POLYPHONY; ++i)
  789. if((partnote[i].status == KEY_PLAYING) || (partnote[i].status == KEY_RELEASED_AND_SUSTAINED))
  790. notecount++;
  791. int oldestnotepos = -1;
  792. if(notecount > keylimit) //find out the oldest note
  793. for(int i = 0; i < POLYPHONY; ++i) {
  794. int maxtime = 0;
  795. if(((partnote[i].status == KEY_PLAYING) || (partnote[i].status == KEY_RELEASED_AND_SUSTAINED)) && (partnote[i].time > maxtime)) {
  796. maxtime = partnote[i].time;
  797. oldestnotepos = i;
  798. }
  799. }
  800. if(oldestnotepos != -1)
  801. ReleaseNotePos(oldestnotepos);
  802. }
  803. }
  804. /*
  805. * Prepare all notes to be turned off
  806. */
  807. void Part::AllNotesOff()
  808. {
  809. killallnotes = true;
  810. }
  811. void Part::RunNote(unsigned int k)
  812. {
  813. unsigned noteplay = 0;
  814. for(int item = 0; item < partnote[k].itemsplaying; ++item) {
  815. int sendcurrenttofx = partnote[k].kititem[item].sendtoparteffect;
  816. for(unsigned type = 0; type < 3; ++type) {
  817. //Select a note
  818. SynthNote **note = NULL;
  819. if(type == 0)
  820. note = &partnote[k].kititem[item].adnote;
  821. else if(type == 1)
  822. note = &partnote[k].kititem[item].subnote;
  823. else if(type == 2)
  824. note = &partnote[k].kititem[item].padnote;
  825. //Process if it exists
  826. if(!(*note))
  827. continue;
  828. noteplay++;
  829. float tmpoutr[synth.buffersize];
  830. float tmpoutl[synth.buffersize];
  831. (*note)->noteout(&tmpoutl[0], &tmpoutr[0]);
  832. if((*note)->finished())
  833. memory.dealloc(*note);
  834. for(int i = 0; i < synth.buffersize; ++i) { //add the note to part(mix)
  835. partfxinputl[sendcurrenttofx][i] += tmpoutl[i];
  836. partfxinputr[sendcurrenttofx][i] += tmpoutr[i];
  837. }
  838. }
  839. }
  840. //Kill note if there is no synth on that note
  841. if(!noteplay)
  842. KillNotePos(k);
  843. }
  844. /*
  845. * Compute Part samples and store them in the partoutl[] and partoutr[]
  846. */
  847. void Part::ComputePartSmps()
  848. {
  849. for(unsigned nefx = 0; nefx < NUM_PART_EFX + 1; ++nefx)
  850. for(int i = 0; i < synth.buffersize; ++i) {
  851. partfxinputl[nefx][i] = 0.0f;
  852. partfxinputr[nefx][i] = 0.0f;
  853. }
  854. for(unsigned k = 0; k < POLYPHONY; ++k) {
  855. if(partnote[k].status == KEY_OFF)
  856. continue;
  857. partnote[k].time++;
  858. //get the sampledata of the note and kill it if it's finished
  859. RunNote(k);
  860. }
  861. //Apply part's effects and mix them
  862. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  863. if(!Pefxbypass[nefx]) {
  864. partefx[nefx]->out(partfxinputl[nefx], partfxinputr[nefx]);
  865. if(Pefxroute[nefx] == 2)
  866. for(int i = 0; i < synth.buffersize; ++i) {
  867. partfxinputl[nefx + 1][i] += partefx[nefx]->efxoutl[i];
  868. partfxinputr[nefx + 1][i] += partefx[nefx]->efxoutr[i];
  869. }
  870. }
  871. int routeto = ((Pefxroute[nefx] == 0) ? nefx + 1 : NUM_PART_EFX);
  872. for(int i = 0; i < synth.buffersize; ++i) {
  873. partfxinputl[routeto][i] += partfxinputl[nefx][i];
  874. partfxinputr[routeto][i] += partfxinputr[nefx][i];
  875. }
  876. }
  877. for(int i = 0; i < synth.buffersize; ++i) {
  878. partoutl[i] = partfxinputl[NUM_PART_EFX][i];
  879. partoutr[i] = partfxinputr[NUM_PART_EFX][i];
  880. }
  881. if(killallnotes) {
  882. for(int i = 0; i < synth.buffersize; ++i) {
  883. float tmp = (synth.buffersize_f - i) / synth.buffersize_f;
  884. partoutl[i] *= tmp;
  885. partoutr[i] *= tmp;
  886. }
  887. for(int k = 0; k < POLYPHONY; ++k)
  888. KillNotePos(k);
  889. killallnotes = false;
  890. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  891. partefx[nefx]->cleanup();
  892. }
  893. ctl.updateportamento();
  894. }
  895. /*
  896. * Parameter control
  897. */
  898. void Part::setPvolume(char Pvolume_)
  899. {
  900. Pvolume = Pvolume_;
  901. volume =
  902. dB2rap((Pvolume - 96.0f) / 96.0f * 40.0f) * ctl.expression.relvolume;
  903. }
  904. void Part::setPpanning(char Ppanning_)
  905. {
  906. Ppanning = Ppanning_;
  907. panning = limit(Ppanning / 127.0f + ctl.panning.pan, 0.0f, 1.0f);
  908. }
  909. /*
  910. * Enable or disable a kit item
  911. */
  912. void Part::setkititemstatus(unsigned kititem, bool Penabled_)
  913. {
  914. //nonexistent kit item and the first kit item is always enabled
  915. if((kititem == 0) || (kititem >= NUM_KIT_ITEMS))
  916. return;
  917. Kit &kkit = kit[kititem];
  918. //no need to update if
  919. if(kkit.Penabled == Penabled_)
  920. return;
  921. kkit.Penabled = Penabled_;
  922. if(!Penabled_) {
  923. delete kkit.adpars;
  924. delete kkit.subpars;
  925. delete kkit.padpars;
  926. kkit.Pname[0] = '\0';
  927. //Reset notes s.t. stale buffers will not get read
  928. for(int k = 0; k < POLYPHONY; ++k)
  929. KillNotePos(k);
  930. }
  931. else {
  932. //All parameters must be NULL in this case
  933. assert(!(kkit.adpars || kkit.subpars || kkit.padpars));
  934. kkit.adpars = new ADnoteParameters(synth, fft);
  935. kkit.subpars = new SUBnoteParameters();
  936. kkit.padpars = new PADnoteParameters(synth, fft);
  937. }
  938. }
  939. void Part::add2XMLinstrument(XMLwrapper *xml)
  940. {
  941. xml->beginbranch("INFO");
  942. xml->addparstr("name", (char *)Pname);
  943. xml->addparstr("author", (char *)info.Pauthor);
  944. xml->addparstr("comments", (char *)info.Pcomments);
  945. xml->addpar("type", info.Ptype);
  946. xml->endbranch();
  947. xml->beginbranch("INSTRUMENT_KIT");
  948. xml->addpar("kit_mode", Pkitmode);
  949. xml->addparbool("drum_mode", Pdrummode);
  950. for(int i = 0; i < NUM_KIT_ITEMS; ++i) {
  951. xml->beginbranch("INSTRUMENT_KIT_ITEM", i);
  952. xml->addparbool("enabled", kit[i].Penabled);
  953. if(kit[i].Penabled != 0) {
  954. xml->addparstr("name", (char *)kit[i].Pname);
  955. xml->addparbool("muted", kit[i].Pmuted);
  956. xml->addpar("min_key", kit[i].Pminkey);
  957. xml->addpar("max_key", kit[i].Pmaxkey);
  958. xml->addpar("send_to_instrument_effect", kit[i].Psendtoparteffect);
  959. xml->addparbool("add_enabled", kit[i].Padenabled);
  960. if(kit[i].Padenabled && kit[i].adpars) {
  961. xml->beginbranch("ADD_SYNTH_PARAMETERS");
  962. kit[i].adpars->add2XML(xml);
  963. xml->endbranch();
  964. }
  965. xml->addparbool("sub_enabled", kit[i].Psubenabled);
  966. if(kit[i].Psubenabled && kit[i].subpars) {
  967. xml->beginbranch("SUB_SYNTH_PARAMETERS");
  968. kit[i].subpars->add2XML(xml);
  969. xml->endbranch();
  970. }
  971. xml->addparbool("pad_enabled", kit[i].Ppadenabled);
  972. if(kit[i].Ppadenabled && kit[i].padpars) {
  973. xml->beginbranch("PAD_SYNTH_PARAMETERS");
  974. kit[i].padpars->add2XML(xml);
  975. xml->endbranch();
  976. }
  977. }
  978. xml->endbranch();
  979. }
  980. xml->endbranch();
  981. xml->beginbranch("INSTRUMENT_EFFECTS");
  982. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  983. xml->beginbranch("INSTRUMENT_EFFECT", nefx);
  984. xml->beginbranch("EFFECT");
  985. partefx[nefx]->add2XML(xml);
  986. xml->endbranch();
  987. xml->addpar("route", Pefxroute[nefx]);
  988. partefx[nefx]->setdryonly(Pefxroute[nefx] == 2);
  989. xml->addparbool("bypass", Pefxbypass[nefx]);
  990. xml->endbranch();
  991. }
  992. xml->endbranch();
  993. }
  994. void Part::add2XML(XMLwrapper *xml)
  995. {
  996. //parameters
  997. xml->addparbool("enabled", Penabled);
  998. if((Penabled == 0) && (xml->minimal))
  999. return;
  1000. xml->addpar("volume", Pvolume);
  1001. xml->addpar("panning", Ppanning);
  1002. xml->addpar("min_key", Pminkey);
  1003. xml->addpar("max_key", Pmaxkey);
  1004. xml->addpar("key_shift", Pkeyshift);
  1005. xml->addpar("rcv_chn", Prcvchn);
  1006. xml->addpar("velocity_sensing", Pvelsns);
  1007. xml->addpar("velocity_offset", Pveloffs);
  1008. xml->addparbool("note_on", Pnoteon);
  1009. xml->addparbool("poly_mode", Ppolymode);
  1010. xml->addpar("legato_mode", Plegatomode);
  1011. xml->addpar("key_limit", Pkeylimit);
  1012. xml->beginbranch("INSTRUMENT");
  1013. add2XMLinstrument(xml);
  1014. xml->endbranch();
  1015. xml->beginbranch("CONTROLLER");
  1016. ctl.add2XML(xml);
  1017. xml->endbranch();
  1018. }
  1019. int Part::saveXML(const char *filename)
  1020. {
  1021. XMLwrapper xml;
  1022. xml.beginbranch("INSTRUMENT");
  1023. add2XMLinstrument(&xml);
  1024. xml.endbranch();
  1025. int result = xml.saveXMLfile(filename);
  1026. return result;
  1027. }
  1028. int Part::loadXMLinstrument(const char *filename)
  1029. {
  1030. XMLwrapper xml;
  1031. if(xml.loadXMLfile(filename) < 0) {
  1032. return -1;
  1033. }
  1034. if(xml.enterbranch("INSTRUMENT") == 0)
  1035. return -10;
  1036. getfromXMLinstrument(&xml);
  1037. xml.exitbranch();
  1038. return 0;
  1039. }
  1040. void Part::applyparameters(void)
  1041. {
  1042. applyparameters([]{return false;});
  1043. }
  1044. void Part::applyparameters(std::function<bool()> do_abort)
  1045. {
  1046. for(int n = 0; n < NUM_KIT_ITEMS; ++n)
  1047. if(kit[n].Ppadenabled && kit[n].padpars)
  1048. kit[n].padpars->applyparameters(do_abort);
  1049. }
  1050. void Part::initialize_rt(void)
  1051. {
  1052. for(int i=0; i<NUM_PART_EFX; ++i)
  1053. partefx[i]->init();
  1054. }
  1055. void Part::kill_rt(void)
  1056. {
  1057. for(int i=0; i<NUM_PART_EFX; ++i)
  1058. partefx[i]->kill();
  1059. for(int k = 0; k < POLYPHONY; ++k)
  1060. KillNotePos(k);
  1061. }
  1062. void Part::monomemPush(char note)
  1063. {
  1064. for(int i=0; i<256; ++i)
  1065. if(monomemnotes[i]==note)
  1066. return;
  1067. for(int i=254;i>=0; --i)
  1068. monomemnotes[i+1] = monomemnotes[i];
  1069. monomemnotes[0] = note;
  1070. }
  1071. void Part::monomemPop(char note)
  1072. {
  1073. int note_pos=-1;
  1074. for(int i=0; i<256; ++i)
  1075. if(monomemnotes[i]==note)
  1076. note_pos = i;
  1077. if(note_pos != -1) {
  1078. for(int i=note_pos; i<256; ++i)
  1079. monomemnotes[i] = monomemnotes[i+1];
  1080. monomemnotes[255] = -1;
  1081. }
  1082. }
  1083. char Part::monomemBack(void) const
  1084. {
  1085. return monomemnotes[0];
  1086. }
  1087. bool Part::monomemEmpty(void) const
  1088. {
  1089. return monomemnotes[0] == -1;
  1090. }
  1091. void Part::monomemClear(void)
  1092. {
  1093. for(int i=0; i<256; ++i)
  1094. monomemnotes[i] = -1;
  1095. }
  1096. void Part::getfromXMLinstrument(XMLwrapper *xml)
  1097. {
  1098. if(xml->enterbranch("INFO")) {
  1099. xml->getparstr("name", (char *)Pname, PART_MAX_NAME_LEN);
  1100. xml->getparstr("author", (char *)info.Pauthor, MAX_INFO_TEXT_SIZE);
  1101. xml->getparstr("comments", (char *)info.Pcomments, MAX_INFO_TEXT_SIZE);
  1102. info.Ptype = xml->getpar("type", info.Ptype, 0, 16);
  1103. xml->exitbranch();
  1104. }
  1105. if(xml->enterbranch("INSTRUMENT_KIT")) {
  1106. Pkitmode = xml->getpar127("kit_mode", Pkitmode);
  1107. Pdrummode = xml->getparbool("drum_mode", Pdrummode);
  1108. setkititemstatus(0, 0);
  1109. for(int i = 0; i < NUM_KIT_ITEMS; ++i) {
  1110. if(xml->enterbranch("INSTRUMENT_KIT_ITEM", i) == 0)
  1111. continue;
  1112. setkititemstatus(i, xml->getparbool("enabled", kit[i].Penabled));
  1113. if(kit[i].Penabled == 0) {
  1114. xml->exitbranch();
  1115. continue;
  1116. }
  1117. xml->getparstr("name", (char *)kit[i].Pname, PART_MAX_NAME_LEN);
  1118. kit[i].Pmuted = xml->getparbool("muted", kit[i].Pmuted);
  1119. kit[i].Pminkey = xml->getpar127("min_key", kit[i].Pminkey);
  1120. kit[i].Pmaxkey = xml->getpar127("max_key", kit[i].Pmaxkey);
  1121. kit[i].Psendtoparteffect = xml->getpar127(
  1122. "send_to_instrument_effect",
  1123. kit[i].Psendtoparteffect);
  1124. kit[i].Padenabled = xml->getparbool("add_enabled",
  1125. kit[i].Padenabled);
  1126. if(xml->enterbranch("ADD_SYNTH_PARAMETERS")) {
  1127. if(!kit[i].adpars)
  1128. kit[i].adpars = new ADnoteParameters(synth, fft);
  1129. kit[i].adpars->getfromXML(xml);
  1130. xml->exitbranch();
  1131. }
  1132. kit[i].Psubenabled = xml->getparbool("sub_enabled",
  1133. kit[i].Psubenabled);
  1134. if(xml->enterbranch("SUB_SYNTH_PARAMETERS")) {
  1135. if(!kit[i].subpars)
  1136. kit[i].subpars = new SUBnoteParameters();
  1137. kit[i].subpars->getfromXML(xml);
  1138. xml->exitbranch();
  1139. }
  1140. kit[i].Ppadenabled = xml->getparbool("pad_enabled",
  1141. kit[i].Ppadenabled);
  1142. if(xml->enterbranch("PAD_SYNTH_PARAMETERS")) {
  1143. if(!kit[i].padpars)
  1144. kit[i].padpars = new PADnoteParameters(synth, fft);
  1145. kit[i].padpars->getfromXML(xml);
  1146. xml->exitbranch();
  1147. }
  1148. xml->exitbranch();
  1149. }
  1150. xml->exitbranch();
  1151. }
  1152. if(xml->enterbranch("INSTRUMENT_EFFECTS")) {
  1153. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  1154. if(xml->enterbranch("INSTRUMENT_EFFECT", nefx) == 0)
  1155. continue;
  1156. if(xml->enterbranch("EFFECT")) {
  1157. partefx[nefx]->getfromXML(xml);
  1158. xml->exitbranch();
  1159. }
  1160. Pefxroute[nefx] = xml->getpar("route",
  1161. Pefxroute[nefx],
  1162. 0,
  1163. NUM_PART_EFX);
  1164. partefx[nefx]->setdryonly(Pefxroute[nefx] == 2);
  1165. Pefxbypass[nefx] = xml->getparbool("bypass", Pefxbypass[nefx]);
  1166. xml->exitbranch();
  1167. }
  1168. xml->exitbranch();
  1169. }
  1170. }
  1171. void Part::getfromXML(XMLwrapper *xml)
  1172. {
  1173. Penabled = xml->getparbool("enabled", Penabled);
  1174. setPvolume(xml->getpar127("volume", Pvolume));
  1175. setPpanning(xml->getpar127("panning", Ppanning));
  1176. Pminkey = xml->getpar127("min_key", Pminkey);
  1177. Pmaxkey = xml->getpar127("max_key", Pmaxkey);
  1178. Pkeyshift = xml->getpar127("key_shift", Pkeyshift);
  1179. Prcvchn = xml->getpar127("rcv_chn", Prcvchn);
  1180. Pvelsns = xml->getpar127("velocity_sensing", Pvelsns);
  1181. Pveloffs = xml->getpar127("velocity_offset", Pveloffs);
  1182. Pnoteon = xml->getparbool("note_on", Pnoteon);
  1183. Ppolymode = xml->getparbool("poly_mode", Ppolymode);
  1184. Plegatomode = xml->getparbool("legato_mode", Plegatomode); //older versions
  1185. if(!Plegatomode)
  1186. Plegatomode = xml->getpar127("legato_mode", Plegatomode);
  1187. Pkeylimit = xml->getpar127("key_limit", Pkeylimit);
  1188. if(xml->enterbranch("INSTRUMENT")) {
  1189. getfromXMLinstrument(xml);
  1190. xml->exitbranch();
  1191. }
  1192. if(xml->enterbranch("CONTROLLER")) {
  1193. ctl.getfromXML(xml);
  1194. xml->exitbranch();
  1195. }
  1196. }