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 35KB

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