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.

1149 lines
34KB

  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->setPpanning(obj->Ppanning);
  52. rParamZyn(Ppanning, "Set Panning"),
  53. #undef rChangeCb
  54. #define rChangeCb obj->setkeylimit(obj->Pkeylimit);
  55. rParamI(Pkeylimit, rProp(parameter), rMap(min,0), rMap(max, POLYPHONY), "Key limit per part"),
  56. #undef rChangeCb
  57. #define rChangeCb
  58. rParamZyn(Pminkey, "Min Used Key"),
  59. rParamZyn(Pmaxkey, "Max Used Key"),
  60. rParamZyn(Pkeyshift, "Part keyshift"),
  61. rParamZyn(Prcvchn, "Active MIDI channel"),
  62. rParamZyn(Pvelsns, "Velocity sensing"),
  63. rParamZyn(Pveloffs, "Velocity offset"),
  64. rToggle(Pnoteon, "If the channel accepts note on events"),
  65. //TODO FIXME Change to 0=OFF 1=MULTI 2=SINGLE
  66. rParamI(Pkitmode, "Kit mode enable"),
  67. rToggle(Pdrummode, "Drum mode enable\n"
  68. "When drum mode is enabled all keys are mapped to 12tET and legato is disabled"),
  69. rToggle(Ppolymode, "Polyphoney mode"),
  70. rToggle(Plegatomode, "Legato enable"),
  71. rParamZyn(info.Ptype, "Class of Instrument"),
  72. rString(info.Pauthor, MAX_INFO_TEXT_SIZE, "Instrument Author"),
  73. rString(info.Pcomments, MAX_INFO_TEXT_SIZE, "Instrument Comments"),
  74. rString(Pname, PART_MAX_NAME_LEN, "Kit User Specified Label"),
  75. rArray(Pefxroute, NUM_PART_EFX, "Effect Routing"),
  76. rArrayT(Pefxbypass, NUM_PART_EFX, "If an effect is bypassed"),
  77. {"captureMin:", rDoc("Capture minimum valid note"), NULL,
  78. [](const char *, RtData &r)
  79. {Part *p = (Part*)r.obj; p->Pminkey = p->lastnote;}},
  80. {"captureMax:", rDoc("Capture maximum valid note"), NULL,
  81. [](const char *, RtData &r)
  82. {Part *p = (Part*)r.obj; p->Pmaxkey = p->lastnote;}},
  83. {"polyType::c:i", rProp(parameter) rOptions(Polyphonic, Monophonic, Legato)
  84. rDoc("synthesis polyphony type"), NULL,
  85. [](const char *msg, RtData &d)
  86. {
  87. Part *p = (Part*)d.obj;
  88. if(!rtosc_narguments(msg)) {
  89. int res = 0;
  90. if(!p->Ppolymode)
  91. res = p->Plegatomode ? 2 : 1;
  92. d.reply(d.loc, "c", res);
  93. return;
  94. }
  95. int i = rtosc_argument(msg, 0).i;
  96. if(i == 0) {
  97. p->Ppolymode = 1;
  98. p->Plegatomode = 0;
  99. } else if(i==1) {
  100. p->Ppolymode = 0;
  101. p->Plegatomode = 0;
  102. } else {
  103. p->Ppolymode = 0;
  104. p->Plegatomode = 1;
  105. }}},
  106. {"clear:", rProp(internal) rDoc("Reset Part To Defaults"), 0,
  107. [](const char *, RtData &d)
  108. {
  109. //XXX todo forward this event for middleware to handle
  110. //Part *p = (Part*)d.obj;
  111. //p->defaults();
  112. //char part_loc[128];
  113. //strcpy(part_loc, d.loc);
  114. //char *end = strrchr(part_loc, '/');
  115. //if(end)
  116. // end[1] = 0;
  117. //d.broadcast("/damage", "s", part_loc);
  118. }},
  119. //{"kit#16::T:F", "::Enables or disables kit item", 0,
  120. // [](const char *m, RtData &d) {
  121. // auto loc = d.loc;
  122. // Part *p = (Part*)d.obj;
  123. // unsigned kitid = -1;
  124. // //Note that this event will be captured before transmitted, thus
  125. // //reply/broadcast don't matter
  126. // for(int i=0; i<NUM_KIT_ITEMS; ++i) {
  127. // d.reply("/middleware/oscil", "siisb", loc, kitid, i,
  128. // "oscil", sizeof(void*),
  129. // p->kit[kitid]->adpars->voice[i]->OscilSmp);
  130. // d.reply("/middleware/oscil", "siisb", loc, kitid, i, "oscil-mod"
  131. // sizeof(void*),
  132. // p->kit[kitid]->adpars->voice[i]->somethingelse);
  133. // }
  134. // d.reply("/middleware/pad", "sib", loc, kitid,
  135. // sizeof(PADnoteParameters*),
  136. // p->kit[kitid]->padpars)
  137. // }}
  138. };
  139. #undef rObject
  140. #define rObject Part::Kit
  141. static const Ports kitPorts = {
  142. rRecurp(padpars, "Padnote parameters"),
  143. rRecurp(adpars, "Adnote parameters"),
  144. rRecurp(subpars, "Adnote parameters"),
  145. rToggle(Penabled, "Kit item enable"),
  146. rToggle(Pmuted, "Kit item mute"),
  147. rParamZyn(Pminkey, "Kit item min key"),
  148. rParamZyn(Pmaxkey, "Kit item max key"),
  149. rToggle(Padenabled, "ADsynth enable"),
  150. rToggle(Psubenabled, "SUBsynth enable"),
  151. rToggle(Ppadenabled, "PADsynth enable"),
  152. rParamZyn(Psendtoparteffect, "Effect Levels"),
  153. rString(Pname, PART_MAX_NAME_LEN, "Kit User Specified Label"),
  154. {"captureMin:", rDoc("Capture minimum valid note"), NULL,
  155. [](const char *, RtData &r)
  156. {Part::Kit *p = (Part::Kit*)r.obj; p->Pminkey = p->parent->lastnote;}},
  157. {"captureMax:", rDoc("Capture maximum valid note"), NULL, [](const char *, RtData &r)
  158. {Part::Kit *p = (Part::Kit*)r.obj; p->Pmaxkey = p->parent->lastnote;}},
  159. {"padpars-data:b", rProp(internal) rDoc("Set PADsynth data pointer"), 0,
  160. [](const char *msg, RtData &d) {
  161. rObject &o = *(rObject*)d.obj;
  162. assert(o.padpars == NULL);
  163. o.padpars = *(decltype(o.padpars)*)rtosc_argument(msg, 0).b.data;
  164. }},
  165. {"adpars-data:b", rProp(internal) rDoc("Set ADsynth data pointer"), 0,
  166. [](const char *msg, RtData &d) {
  167. rObject &o = *(rObject*)d.obj;
  168. assert(o.adpars == NULL);
  169. o.adpars = *(decltype(o.adpars)*)rtosc_argument(msg, 0).b.data;
  170. }},
  171. {"subpars-data:b", rProp(internal) rDoc("Set SUBsynth data pointer"), 0,
  172. [](const char *msg, RtData &d) {
  173. rObject &o = *(rObject*)d.obj;
  174. assert(o.subpars == NULL);
  175. o.subpars = *(decltype(o.subpars)*)rtosc_argument(msg, 0).b.data;
  176. }},
  177. // [](
  178. };
  179. const Ports &Part::Kit::ports = kitPorts;
  180. const Ports &Part::ports = partPorts;
  181. Part::Part(Allocator &alloc, const SYNTH_T &synth_, const AbsTime &time_,
  182. const int &gzip_compression, const int &interpolation,
  183. Microtonal *microtonal_, FFTwrapper *fft_)
  184. :Pdrummode(false),
  185. Ppolymode(true),
  186. Plegatomode(false),
  187. partoutl(new float[synth_.buffersize]),
  188. partoutr(new float[synth_.buffersize]),
  189. ctl(synth_),
  190. microtonal(microtonal_),
  191. fft(fft_),
  192. memory(alloc),
  193. synth(synth_),
  194. time(time_),
  195. gzip_compression(gzip_compression),
  196. interpolation(interpolation)
  197. {
  198. monomemClear();
  199. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  200. kit[n].parent = this;
  201. kit[n].Pname = new char [PART_MAX_NAME_LEN];
  202. kit[n].adpars = nullptr;
  203. kit[n].subpars = nullptr;
  204. kit[n].padpars = nullptr;
  205. }
  206. kit[0].adpars = new ADnoteParameters(synth, fft);
  207. //Part's Insertion Effects init
  208. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  209. partefx[nefx] = new EffectMgr(memory, synth, 1);
  210. Pefxbypass[nefx] = false;
  211. }
  212. assert(partefx[0]);
  213. for(int n = 0; n < NUM_PART_EFX + 1; ++n) {
  214. partfxinputl[n] = new float [synth.buffersize];
  215. partfxinputr[n] = new float [synth.buffersize];
  216. }
  217. killallnotes = false;
  218. oldfreq = -1.0f;
  219. cleanup();
  220. Pname = new char[PART_MAX_NAME_LEN];
  221. oldvolumel = oldvolumer = 0.5f;
  222. lastnote = -1;
  223. defaults();
  224. assert(partefx[0]);
  225. }
  226. void Part::cloneTraits(Part &p) const
  227. {
  228. #define CLONE(x) p.x = this->x
  229. CLONE(Penabled);
  230. p.setPvolume(this->Pvolume);
  231. p.setPpanning(this->Ppanning);
  232. CLONE(Pminkey);
  233. CLONE(Pmaxkey);
  234. CLONE(Pkeyshift);
  235. CLONE(Prcvchn);
  236. CLONE(Pvelsns);
  237. CLONE(Pveloffs);
  238. CLONE(Pnoteon);
  239. CLONE(Ppolymode);
  240. CLONE(Plegatomode);
  241. CLONE(Pkeylimit);
  242. CLONE(ctl);
  243. }
  244. void Part::defaults()
  245. {
  246. Penabled = 0;
  247. Pminkey = 0;
  248. Pmaxkey = 127;
  249. Pnoteon = 1;
  250. Ppolymode = 1;
  251. Plegatomode = 0;
  252. setPvolume(96);
  253. Pkeyshift = 64;
  254. Prcvchn = 0;
  255. setPpanning(64);
  256. Pvelsns = 64;
  257. Pveloffs = 64;
  258. Pkeylimit = 15;
  259. defaultsinstrument();
  260. ctl.defaults();
  261. }
  262. void Part::defaultsinstrument()
  263. {
  264. ZERO(Pname, PART_MAX_NAME_LEN);
  265. info.Ptype = 0;
  266. ZERO(info.Pauthor, MAX_INFO_TEXT_SIZE + 1);
  267. ZERO(info.Pcomments, MAX_INFO_TEXT_SIZE + 1);
  268. Pkitmode = 0;
  269. Pdrummode = 0;
  270. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  271. kit[n].Penabled = false;
  272. kit[n].Pmuted = false;
  273. kit[n].Pminkey = 0;
  274. kit[n].Pmaxkey = 127;
  275. kit[n].Padenabled = false;
  276. kit[n].Psubenabled = false;
  277. kit[n].Ppadenabled = false;
  278. ZERO(kit[n].Pname, PART_MAX_NAME_LEN);
  279. kit[n].Psendtoparteffect = 0;
  280. if(n != 0)
  281. setkititemstatus(n, 0);
  282. }
  283. kit[0].Penabled = 1;
  284. kit[0].Padenabled = 1;
  285. kit[0].adpars->defaults();
  286. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  287. partefx[nefx]->defaults();
  288. Pefxroute[nefx] = 0; //route to next effect
  289. }
  290. }
  291. /*
  292. * Cleanup the part
  293. */
  294. void Part::cleanup(bool final_)
  295. {
  296. notePool.killAllNotes();
  297. for(int i = 0; i < synth.buffersize; ++i) {
  298. partoutl[i] = final_ ? 0.0f : synth.denormalkillbuf[i];
  299. partoutr[i] = final_ ? 0.0f : synth.denormalkillbuf[i];
  300. }
  301. ctl.resetall();
  302. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  303. partefx[nefx]->cleanup();
  304. for(int n = 0; n < NUM_PART_EFX + 1; ++n)
  305. for(int i = 0; i < synth.buffersize; ++i) {
  306. partfxinputl[n][i] = final_ ? 0.0f : synth.denormalkillbuf[i];
  307. partfxinputr[n][i] = final_ ? 0.0f : synth.denormalkillbuf[i];
  308. }
  309. }
  310. Part::~Part()
  311. {
  312. cleanup(true);
  313. for(int n = 0; n < NUM_KIT_ITEMS; ++n) {
  314. delete kit[n].adpars;
  315. delete kit[n].subpars;
  316. delete kit[n].padpars;
  317. delete [] kit[n].Pname;
  318. }
  319. delete [] Pname;
  320. delete [] partoutl;
  321. delete [] partoutr;
  322. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  323. delete partefx[nefx];
  324. for(int n = 0; n < NUM_PART_EFX + 1; ++n) {
  325. delete [] partfxinputl[n];
  326. delete [] partfxinputr[n];
  327. }
  328. }
  329. void assert_kit_sanity(const Part::Kit *kits)
  330. {
  331. for(int i=0; i<NUM_KIT_ITEMS; ++i) {
  332. //an enabled kit must have a corresponding parameter object
  333. assert(!kits[i].Padenabled || kits[i].adpars);
  334. assert(!kits[i].Ppadenabled || kits[i].padpars);
  335. assert(!kits[i].Psubenabled || kits[i].subpars);
  336. }
  337. }
  338. /*
  339. * Note On Messages
  340. */
  341. void Part::NoteOn(unsigned char note,
  342. unsigned char velocity,
  343. int masterkeyshift)
  344. {
  345. //Verify Basic Mode and sanity
  346. const bool isRunningNote = notePool.existsRunningNote();
  347. const bool doingLegato = isRunningNote && isLegatoMode() &&
  348. lastlegatomodevalid;
  349. if(!Pnoteon || !inRange(note, Pminkey, Pmaxkey))
  350. return;
  351. verifyKeyMode();
  352. assert_kit_sanity(kit);
  353. //Preserve Note Stack
  354. if(isMonoMode() || isLegatoMode()) {
  355. monomemPush(note);
  356. monomem[note].velocity = velocity;
  357. monomem[note].mkeyshift = masterkeyshift;
  358. } else if(!monomemEmpty())
  359. monomemClear();
  360. //Mono/Legato Release old notes
  361. if(isMonoMode() || (isLegatoMode() && !doingLegato))
  362. notePool.releasePlayingNotes();
  363. lastlegatomodevalid = isLegatoMode();
  364. //Compute Note Parameters
  365. const float vel = getVelocity(velocity, Pvelsns, Pveloffs);
  366. const int partkeyshift = (int)Pkeyshift - 64;
  367. const int keyshift = masterkeyshift + partkeyshift;
  368. const float notebasefreq = getBaseFreq(note, keyshift);
  369. if(notebasefreq < 0)
  370. return;
  371. //Portamento
  372. lastnote = note;
  373. if(oldfreq < 1.0f)
  374. oldfreq = notebasefreq;//this is only the first note is played
  375. // For Mono/Legato: Force Portamento Off on first
  376. // notes. That means it is required that the previous note is
  377. // still held down or sustained for the Portamento to activate
  378. // (that's like Legato).
  379. bool portamento = false;
  380. if(Ppolymode || isRunningNote)
  381. portamento = ctl.initportamento(oldfreq, notebasefreq, doingLegato);
  382. oldfreq = notebasefreq;
  383. //Adjust Existing Notes
  384. if(doingLegato) {
  385. LegatoParams pars = {notebasefreq, vel, portamento, note, true};
  386. notePool.applyLegato(pars);
  387. return;
  388. }
  389. //Create New Notes
  390. for(uint8_t i = 0; i < NUM_KIT_ITEMS; ++i) {
  391. auto &item = kit[i];
  392. if(Pkitmode != 0 && !item.validNote(note))
  393. continue;
  394. SynthParams pars{memory, ctl, synth, time, notebasefreq, vel,
  395. portamento, note, false};
  396. const int sendto = Pkitmode ? item.sendto() : 0;
  397. if(item.Padenabled)
  398. notePool.insertNote(note, sendto,
  399. {memory.alloc<ADnote>(kit[i].adpars, pars), 0, i});
  400. if(item.Psubenabled)
  401. notePool.insertNote(note, sendto,
  402. {memory.alloc<SUBnote>(kit[i].subpars, pars), 1, i});
  403. if(item.Ppadenabled)
  404. notePool.insertNote(note, sendto,
  405. {memory.alloc<PADnote>(kit[i].padpars, pars, interpolation), 2, i});
  406. //Partial Kit Use
  407. if(isNonKit() || (isSingleKit() && item.active()))
  408. break;
  409. }
  410. if(isLegatoMode())
  411. notePool.upgradeToLegato();
  412. //Enforce the key limit
  413. setkeylimit(Pkeylimit);
  414. }
  415. /*
  416. * Note Off Messages
  417. */
  418. void Part::NoteOff(unsigned char note) //release the key
  419. {
  420. // This note is released, so we remove it from the list.
  421. if(!monomemEmpty())
  422. monomemPop(note);
  423. for(auto &desc:notePool.activeDesc()) {
  424. if(desc.note != note)
  425. continue;
  426. if(!ctl.sustain.sustain) { //the sustain pedal is not pushed
  427. if((isMonoMode() || isLegatoMode()) && !monomemEmpty())
  428. MonoMemRenote();//Play most recent still active note
  429. else
  430. notePool.release(desc);
  431. }
  432. else //the sustain pedal is pushed
  433. desc.status = KEY_RELEASED_AND_SUSTAINED;
  434. }
  435. }
  436. void Part::PolyphonicAftertouch(unsigned char note,
  437. unsigned char velocity,
  438. int masterkeyshift)
  439. {
  440. (void) masterkeyshift;
  441. if(!Pnoteon || !inRange(note, Pminkey, Pmaxkey) || Pdrummode)
  442. return;
  443. // MonoMem stuff:
  444. if(!Ppolymode) // if Poly is off
  445. monomem[note].velocity = velocity; // Store this note's velocity.
  446. const float vel = getVelocity(velocity, Pvelsns, Pveloffs);
  447. for(auto &d:notePool.activeDesc()) {
  448. if(d.note == note && d.status == KEY_PLAYING)
  449. for(auto &s:notePool.activeNotes(d))
  450. s.note->setVelocity(vel);
  451. }
  452. }
  453. /*
  454. * Controllers
  455. */
  456. void Part::SetController(unsigned int type, int par)
  457. {
  458. switch(type) {
  459. case C_pitchwheel:
  460. ctl.setpitchwheel(par);
  461. break;
  462. case C_expression:
  463. ctl.setexpression(par);
  464. setPvolume(Pvolume); //update the volume
  465. break;
  466. case C_portamento:
  467. ctl.setportamento(par);
  468. break;
  469. case C_panning:
  470. ctl.setpanning(par);
  471. setPpanning(Ppanning); //update the panning
  472. break;
  473. case C_filtercutoff:
  474. ctl.setfiltercutoff(par);
  475. break;
  476. case C_filterq:
  477. ctl.setfilterq(par);
  478. break;
  479. case C_bandwidth:
  480. ctl.setbandwidth(par);
  481. break;
  482. case C_modwheel:
  483. ctl.setmodwheel(par);
  484. break;
  485. case C_fmamp:
  486. ctl.setfmamp(par);
  487. break;
  488. case C_volume:
  489. ctl.setvolume(par);
  490. if(ctl.volume.receive != 0)
  491. volume = ctl.volume.volume;
  492. else
  493. setPvolume(Pvolume);
  494. break;
  495. case C_sustain:
  496. ctl.setsustain(par);
  497. if(ctl.sustain.sustain == 0)
  498. ReleaseSustainedKeys();
  499. break;
  500. case C_allsoundsoff:
  501. AllNotesOff(); //Panic
  502. break;
  503. case C_resetallcontrollers:
  504. ctl.resetall();
  505. ReleaseSustainedKeys();
  506. if(ctl.volume.receive != 0)
  507. volume = ctl.volume.volume;
  508. else
  509. setPvolume(Pvolume);
  510. setPvolume(Pvolume); //update the volume
  511. setPpanning(Ppanning); //update the panning
  512. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  513. if(kit[item].adpars == NULL)
  514. continue;
  515. kit[item].adpars->GlobalPar.Reson->
  516. sendcontroller(C_resonance_center, 1.0f);
  517. kit[item].adpars->GlobalPar.Reson->
  518. sendcontroller(C_resonance_bandwidth, 1.0f);
  519. }
  520. //more update to add here if I add controllers
  521. break;
  522. case C_allnotesoff:
  523. ReleaseAllKeys();
  524. break;
  525. case C_resonance_center:
  526. ctl.setresonancecenter(par);
  527. for(int item = 0; item < NUM_KIT_ITEMS; ++item) {
  528. if(kit[item].adpars == NULL)
  529. continue;
  530. kit[item].adpars->GlobalPar.Reson->
  531. sendcontroller(C_resonance_center,
  532. ctl.resonancecenter.relcenter);
  533. }
  534. break;
  535. case C_resonance_bandwidth:
  536. ctl.setresonancebw(par);
  537. kit[0].adpars->GlobalPar.Reson->
  538. sendcontroller(C_resonance_bandwidth, ctl.resonancebandwidth.relbw);
  539. break;
  540. }
  541. }
  542. /*
  543. * Release the sustained keys
  544. */
  545. void Part::ReleaseSustainedKeys()
  546. {
  547. // Let's call MonoMemRenote() on some conditions:
  548. if((isMonoMode() || isLegatoMode()) && !monomemEmpty())
  549. if(monomemBack() != lastnote) // Sustain controller manipulation would cause repeated same note respawn without this check.
  550. MonoMemRenote(); // To play most recent still held note.
  551. for(auto &d:notePool.activeDesc())
  552. if(d.status == KEY_RELEASED_AND_SUSTAINED)
  553. for(auto &s:notePool.activeNotes(d))
  554. s.note->releasekey();
  555. }
  556. /*
  557. * Release all keys
  558. */
  559. void Part::ReleaseAllKeys()
  560. {
  561. for(auto &d:notePool.activeDesc())
  562. if(d.status != KEY_RELEASED)
  563. for(auto &s:notePool.activeNotes(d))
  564. s.note->releasekey();
  565. }
  566. // Call NoteOn(...) with the most recent still held key as new note
  567. // (Made for Mono/Legato).
  568. void Part::MonoMemRenote()
  569. {
  570. unsigned char mmrtempnote = monomemBack(); // Last list element.
  571. monomemPop(mmrtempnote); // We remove it, will be added again in NoteOn(...).
  572. NoteOn(mmrtempnote, monomem[mmrtempnote].velocity,
  573. monomem[mmrtempnote].mkeyshift);
  574. }
  575. float Part::getBaseFreq(int note, int keyshift) const
  576. {
  577. if(Pdrummode)
  578. return 440.0f * powf(2.0f, (note - 69.0f) / 12.0f);
  579. else
  580. return microtonal->getnotefreq(note, keyshift);
  581. }
  582. float Part::getVelocity(uint8_t velocity, uint8_t velocity_sense,
  583. uint8_t velocity_offset) const
  584. {
  585. //compute sense function
  586. const float vel = VelF(velocity / 127.0f, velocity_sense);
  587. //compute the velocity offset
  588. return limit(vel + (velocity_offset - 64.0f) / 64.0f, 0.0f, 1.0f);
  589. }
  590. void Part::verifyKeyMode(void)
  591. {
  592. if(Plegatomode && !Pdrummode && Ppolymode) {
  593. fprintf(stderr,
  594. "WARNING: Poly & Legato modes are On, that shouldn't happen\n"
  595. "Disabling Legato mode...\n"
  596. "(Part.cpp::NoteOn(..))\n");
  597. Plegatomode = 0;
  598. }
  599. }
  600. /*
  601. * Set Part's key limit
  602. */
  603. void Part::setkeylimit(unsigned char Pkeylimit_)
  604. {
  605. Pkeylimit = Pkeylimit_;
  606. int keylimit = Pkeylimit;
  607. if(keylimit == 0)
  608. keylimit = POLYPHONY - 5;
  609. if(notePool.getRunningNotes() > keylimit)
  610. notePool.enforceKeyLimit(keylimit);
  611. }
  612. /*
  613. * Prepare all notes to be turned off
  614. */
  615. void Part::AllNotesOff()
  616. {
  617. killallnotes = true;
  618. }
  619. /*
  620. * Compute Part samples and store them in the partoutl[] and partoutr[]
  621. */
  622. void Part::ComputePartSmps()
  623. {
  624. assert(partefx[0]);
  625. for(unsigned nefx = 0; nefx < NUM_PART_EFX + 1; ++nefx) {
  626. memset(partfxinputl[nefx], 0, synth.bufferbytes);
  627. memset(partfxinputr[nefx], 0, synth.bufferbytes);
  628. }
  629. for(auto &d:notePool.activeDesc()) {
  630. d.age++;
  631. for(auto &s:notePool.activeNotes(d)) {
  632. float tmpoutr[synth.buffersize];
  633. float tmpoutl[synth.buffersize];
  634. auto &note = *s.note;
  635. note.noteout(&tmpoutl[0], &tmpoutr[0]);
  636. for(int i = 0; i < synth.buffersize; ++i) { //add the note to part(mix)
  637. partfxinputl[d.sendto][i] += tmpoutl[i];
  638. partfxinputr[d.sendto][i] += tmpoutr[i];
  639. }
  640. if(note.finished())
  641. notePool.kill(s);
  642. }
  643. }
  644. //Apply part's effects and mix them
  645. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  646. if(!Pefxbypass[nefx]) {
  647. partefx[nefx]->out(partfxinputl[nefx], partfxinputr[nefx]);
  648. if(Pefxroute[nefx] == 2)
  649. for(int i = 0; i < synth.buffersize; ++i) {
  650. partfxinputl[nefx + 1][i] += partefx[nefx]->efxoutl[i];
  651. partfxinputr[nefx + 1][i] += partefx[nefx]->efxoutr[i];
  652. }
  653. }
  654. int routeto = ((Pefxroute[nefx] == 0) ? nefx + 1 : NUM_PART_EFX);
  655. for(int i = 0; i < synth.buffersize; ++i) {
  656. partfxinputl[routeto][i] += partfxinputl[nefx][i];
  657. partfxinputr[routeto][i] += partfxinputr[nefx][i];
  658. }
  659. }
  660. for(int i = 0; i < synth.buffersize; ++i) {
  661. partoutl[i] = partfxinputl[NUM_PART_EFX][i];
  662. partoutr[i] = partfxinputr[NUM_PART_EFX][i];
  663. }
  664. if(killallnotes) {
  665. for(int i = 0; i < synth.buffersize; ++i) {
  666. float tmp = (synth.buffersize_f - i) / synth.buffersize_f;
  667. partoutl[i] *= tmp;
  668. partoutr[i] *= tmp;
  669. }
  670. notePool.killAllNotes();
  671. monomemClear();
  672. killallnotes = false;
  673. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx)
  674. partefx[nefx]->cleanup();
  675. }
  676. ctl.updateportamento();
  677. }
  678. /*
  679. * Parameter control
  680. */
  681. void Part::setPvolume(char Pvolume_)
  682. {
  683. Pvolume = Pvolume_;
  684. volume =
  685. dB2rap((Pvolume - 96.0f) / 96.0f * 40.0f) * ctl.expression.relvolume;
  686. }
  687. void Part::setPpanning(char Ppanning_)
  688. {
  689. Ppanning = Ppanning_;
  690. panning = limit(Ppanning / 127.0f + ctl.panning.pan, 0.0f, 1.0f);
  691. }
  692. /*
  693. * Enable or disable a kit item
  694. */
  695. void Part::setkititemstatus(unsigned kititem, bool Penabled_)
  696. {
  697. //nonexistent kit item and the first kit item is always enabled
  698. if((kititem == 0) || (kititem >= NUM_KIT_ITEMS))
  699. return;
  700. Kit &kkit = kit[kititem];
  701. //no need to update if
  702. if(kkit.Penabled == Penabled_)
  703. return;
  704. kkit.Penabled = Penabled_;
  705. if(!Penabled_) {
  706. delete kkit.adpars;
  707. delete kkit.subpars;
  708. delete kkit.padpars;
  709. kkit.Pname[0] = '\0';
  710. notePool.killAllNotes();
  711. }
  712. else {
  713. //All parameters must be NULL in this case
  714. assert(!(kkit.adpars || kkit.subpars || kkit.padpars));
  715. kkit.adpars = new ADnoteParameters(synth, fft);
  716. kkit.subpars = new SUBnoteParameters();
  717. kkit.padpars = new PADnoteParameters(synth, fft);
  718. }
  719. }
  720. void Part::add2XMLinstrument(XMLwrapper *xml)
  721. {
  722. xml->beginbranch("INFO");
  723. xml->addparstr("name", (char *)Pname);
  724. xml->addparstr("author", (char *)info.Pauthor);
  725. xml->addparstr("comments", (char *)info.Pcomments);
  726. xml->addpar("type", info.Ptype);
  727. xml->endbranch();
  728. xml->beginbranch("INSTRUMENT_KIT");
  729. xml->addpar("kit_mode", Pkitmode);
  730. xml->addparbool("drum_mode", Pdrummode);
  731. for(int i = 0; i < NUM_KIT_ITEMS; ++i) {
  732. xml->beginbranch("INSTRUMENT_KIT_ITEM", i);
  733. xml->addparbool("enabled", kit[i].Penabled);
  734. if(kit[i].Penabled != 0) {
  735. xml->addparstr("name", (char *)kit[i].Pname);
  736. xml->addparbool("muted", kit[i].Pmuted);
  737. xml->addpar("min_key", kit[i].Pminkey);
  738. xml->addpar("max_key", kit[i].Pmaxkey);
  739. xml->addpar("send_to_instrument_effect", kit[i].Psendtoparteffect);
  740. xml->addparbool("add_enabled", kit[i].Padenabled);
  741. if(kit[i].Padenabled && kit[i].adpars) {
  742. xml->beginbranch("ADD_SYNTH_PARAMETERS");
  743. kit[i].adpars->add2XML(xml);
  744. xml->endbranch();
  745. }
  746. xml->addparbool("sub_enabled", kit[i].Psubenabled);
  747. if(kit[i].Psubenabled && kit[i].subpars) {
  748. xml->beginbranch("SUB_SYNTH_PARAMETERS");
  749. kit[i].subpars->add2XML(xml);
  750. xml->endbranch();
  751. }
  752. xml->addparbool("pad_enabled", kit[i].Ppadenabled);
  753. if(kit[i].Ppadenabled && kit[i].padpars) {
  754. xml->beginbranch("PAD_SYNTH_PARAMETERS");
  755. kit[i].padpars->add2XML(xml);
  756. xml->endbranch();
  757. }
  758. }
  759. xml->endbranch();
  760. }
  761. xml->endbranch();
  762. xml->beginbranch("INSTRUMENT_EFFECTS");
  763. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  764. xml->beginbranch("INSTRUMENT_EFFECT", nefx);
  765. xml->beginbranch("EFFECT");
  766. partefx[nefx]->add2XML(xml);
  767. xml->endbranch();
  768. xml->addpar("route", Pefxroute[nefx]);
  769. partefx[nefx]->setdryonly(Pefxroute[nefx] == 2);
  770. xml->addparbool("bypass", Pefxbypass[nefx]);
  771. xml->endbranch();
  772. }
  773. xml->endbranch();
  774. }
  775. void Part::add2XML(XMLwrapper *xml)
  776. {
  777. //parameters
  778. xml->addparbool("enabled", Penabled);
  779. if((Penabled == 0) && (xml->minimal))
  780. return;
  781. xml->addpar("volume", Pvolume);
  782. xml->addpar("panning", Ppanning);
  783. xml->addpar("min_key", Pminkey);
  784. xml->addpar("max_key", Pmaxkey);
  785. xml->addpar("key_shift", Pkeyshift);
  786. xml->addpar("rcv_chn", Prcvchn);
  787. xml->addpar("velocity_sensing", Pvelsns);
  788. xml->addpar("velocity_offset", Pveloffs);
  789. xml->addparbool("note_on", Pnoteon);
  790. xml->addparbool("poly_mode", Ppolymode);
  791. xml->addpar("legato_mode", Plegatomode);
  792. xml->addpar("key_limit", Pkeylimit);
  793. xml->beginbranch("INSTRUMENT");
  794. add2XMLinstrument(xml);
  795. xml->endbranch();
  796. xml->beginbranch("CONTROLLER");
  797. ctl.add2XML(xml);
  798. xml->endbranch();
  799. }
  800. int Part::saveXML(const char *filename)
  801. {
  802. XMLwrapper xml;
  803. xml.beginbranch("INSTRUMENT");
  804. add2XMLinstrument(&xml);
  805. xml.endbranch();
  806. int result = xml.saveXMLfile(filename, gzip_compression);
  807. return result;
  808. }
  809. int Part::loadXMLinstrument(const char *filename)
  810. {
  811. XMLwrapper xml;
  812. if(xml.loadXMLfile(filename) < 0) {
  813. return -1;
  814. }
  815. if(xml.enterbranch("INSTRUMENT") == 0)
  816. return -10;
  817. getfromXMLinstrument(&xml);
  818. xml.exitbranch();
  819. return 0;
  820. }
  821. void Part::applyparameters(void)
  822. {
  823. applyparameters([]{return false;});
  824. }
  825. void Part::applyparameters(std::function<bool()> do_abort)
  826. {
  827. for(int n = 0; n < NUM_KIT_ITEMS; ++n)
  828. if(kit[n].Ppadenabled && kit[n].padpars)
  829. kit[n].padpars->applyparameters(do_abort);
  830. }
  831. void Part::initialize_rt(void)
  832. {
  833. for(int i=0; i<NUM_PART_EFX; ++i)
  834. partefx[i]->init();
  835. }
  836. void Part::kill_rt(void)
  837. {
  838. for(int i=0; i<NUM_PART_EFX; ++i)
  839. partefx[i]->kill();
  840. notePool.killAllNotes();
  841. }
  842. void Part::monomemPush(char note)
  843. {
  844. for(int i=0; i<256; ++i)
  845. if(monomemnotes[i]==note)
  846. return;
  847. for(int i=254;i>=0; --i)
  848. monomemnotes[i+1] = monomemnotes[i];
  849. monomemnotes[0] = note;
  850. }
  851. void Part::monomemPop(char note)
  852. {
  853. int note_pos=-1;
  854. for(int i=0; i<256; ++i)
  855. if(monomemnotes[i]==note)
  856. note_pos = i;
  857. if(note_pos != -1) {
  858. for(int i=note_pos; i<256; ++i)
  859. monomemnotes[i] = monomemnotes[i+1];
  860. monomemnotes[255] = -1;
  861. }
  862. }
  863. char Part::monomemBack(void) const
  864. {
  865. return monomemnotes[0];
  866. }
  867. bool Part::monomemEmpty(void) const
  868. {
  869. return monomemnotes[0] == -1;
  870. }
  871. void Part::monomemClear(void)
  872. {
  873. for(int i=0; i<256; ++i)
  874. monomemnotes[i] = -1;
  875. }
  876. void Part::getfromXMLinstrument(XMLwrapper *xml)
  877. {
  878. if(xml->enterbranch("INFO")) {
  879. xml->getparstr("name", (char *)Pname, PART_MAX_NAME_LEN);
  880. xml->getparstr("author", (char *)info.Pauthor, MAX_INFO_TEXT_SIZE);
  881. xml->getparstr("comments", (char *)info.Pcomments, MAX_INFO_TEXT_SIZE);
  882. info.Ptype = xml->getpar("type", info.Ptype, 0, 16);
  883. xml->exitbranch();
  884. }
  885. if(xml->enterbranch("INSTRUMENT_KIT")) {
  886. Pkitmode = xml->getpar127("kit_mode", Pkitmode);
  887. Pdrummode = xml->getparbool("drum_mode", Pdrummode);
  888. setkititemstatus(0, 0);
  889. for(int i = 0; i < NUM_KIT_ITEMS; ++i) {
  890. if(xml->enterbranch("INSTRUMENT_KIT_ITEM", i) == 0)
  891. continue;
  892. setkititemstatus(i, xml->getparbool("enabled", kit[i].Penabled));
  893. if(kit[i].Penabled == 0) {
  894. xml->exitbranch();
  895. continue;
  896. }
  897. xml->getparstr("name", (char *)kit[i].Pname, PART_MAX_NAME_LEN);
  898. kit[i].Pmuted = xml->getparbool("muted", kit[i].Pmuted);
  899. kit[i].Pminkey = xml->getpar127("min_key", kit[i].Pminkey);
  900. kit[i].Pmaxkey = xml->getpar127("max_key", kit[i].Pmaxkey);
  901. kit[i].Psendtoparteffect = xml->getpar127(
  902. "send_to_instrument_effect",
  903. kit[i].Psendtoparteffect);
  904. kit[i].Padenabled = xml->getparbool("add_enabled",
  905. kit[i].Padenabled);
  906. if(xml->enterbranch("ADD_SYNTH_PARAMETERS")) {
  907. if(!kit[i].adpars)
  908. kit[i].adpars = new ADnoteParameters(synth, fft);
  909. kit[i].adpars->getfromXML(xml);
  910. xml->exitbranch();
  911. }
  912. kit[i].Psubenabled = xml->getparbool("sub_enabled",
  913. kit[i].Psubenabled);
  914. if(xml->enterbranch("SUB_SYNTH_PARAMETERS")) {
  915. if(!kit[i].subpars)
  916. kit[i].subpars = new SUBnoteParameters();
  917. kit[i].subpars->getfromXML(xml);
  918. xml->exitbranch();
  919. }
  920. kit[i].Ppadenabled = xml->getparbool("pad_enabled",
  921. kit[i].Ppadenabled);
  922. if(xml->enterbranch("PAD_SYNTH_PARAMETERS")) {
  923. if(!kit[i].padpars)
  924. kit[i].padpars = new PADnoteParameters(synth, fft);
  925. kit[i].padpars->getfromXML(xml);
  926. xml->exitbranch();
  927. }
  928. xml->exitbranch();
  929. }
  930. xml->exitbranch();
  931. }
  932. if(xml->enterbranch("INSTRUMENT_EFFECTS")) {
  933. for(int nefx = 0; nefx < NUM_PART_EFX; ++nefx) {
  934. if(xml->enterbranch("INSTRUMENT_EFFECT", nefx) == 0)
  935. continue;
  936. if(xml->enterbranch("EFFECT")) {
  937. partefx[nefx]->getfromXML(xml);
  938. xml->exitbranch();
  939. }
  940. Pefxroute[nefx] = xml->getpar("route",
  941. Pefxroute[nefx],
  942. 0,
  943. NUM_PART_EFX);
  944. partefx[nefx]->setdryonly(Pefxroute[nefx] == 2);
  945. Pefxbypass[nefx] = xml->getparbool("bypass", Pefxbypass[nefx]);
  946. xml->exitbranch();
  947. }
  948. xml->exitbranch();
  949. }
  950. }
  951. void Part::getfromXML(XMLwrapper *xml)
  952. {
  953. Penabled = xml->getparbool("enabled", Penabled);
  954. setPvolume(xml->getpar127("volume", Pvolume));
  955. setPpanning(xml->getpar127("panning", Ppanning));
  956. Pminkey = xml->getpar127("min_key", Pminkey);
  957. Pmaxkey = xml->getpar127("max_key", Pmaxkey);
  958. Pkeyshift = xml->getpar127("key_shift", Pkeyshift);
  959. Prcvchn = xml->getpar127("rcv_chn", Prcvchn);
  960. Pvelsns = xml->getpar127("velocity_sensing", Pvelsns);
  961. Pveloffs = xml->getpar127("velocity_offset", Pveloffs);
  962. Pnoteon = xml->getparbool("note_on", Pnoteon);
  963. Ppolymode = xml->getparbool("poly_mode", Ppolymode);
  964. Plegatomode = xml->getparbool("legato_mode", Plegatomode); //older versions
  965. if(!Plegatomode)
  966. Plegatomode = xml->getpar127("legato_mode", Plegatomode);
  967. Pkeylimit = xml->getpar127("key_limit", Pkeylimit);
  968. if(xml->enterbranch("INSTRUMENT")) {
  969. getfromXMLinstrument(xml);
  970. xml->exitbranch();
  971. }
  972. if(xml->enterbranch("CONTROLLER")) {
  973. ctl.getfromXML(xml);
  974. xml->exitbranch();
  975. }
  976. }
  977. bool Part::Kit::active(void) const
  978. {
  979. return Padenabled || Psubenabled || Ppadenabled;
  980. }
  981. uint8_t Part::Kit::sendto(void) const
  982. {
  983. return limit((int)Psendtoparteffect, 0, NUM_PART_EFX);
  984. }
  985. bool Part::Kit::validNote(char note) const
  986. {
  987. return !Pmuted && inRange((uint8_t)note, Pminkey, Pmaxkey);
  988. }