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.

Part.cpp 47KB

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