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.

812 lines
23KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Master.cpp - It sends Midi Messages to Parts, receives samples from parts,
  4. process them with system/insertion effects and mix them
  5. Copyright (C) 2002-2005 Nasca Octavian Paul
  6. Author: Nasca Octavian Paul
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of version 2 of the GNU General Public License
  9. as published by the Free Software Foundation.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License (version 2 or later) for more details.
  14. You should have received a copy of the GNU General Public License (version 2)
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "Master.h"
  19. #include "Part.h"
  20. #include "../Params/LFOParams.h"
  21. #include "../Effects/EffectMgr.h"
  22. #include "../DSP/FFTwrapper.h"
  23. #include <stdio.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <iostream>
  27. #include <algorithm>
  28. #include <cmath>
  29. #include <unistd.h>
  30. using namespace std;
  31. vuData::vuData(void)
  32. :outpeakl(0.0f), outpeakr(0.0f), maxoutpeakl(0.0f), maxoutpeakr(0.0f),
  33. rmspeakl(0.0f), rmspeakr(0.0f), clipped(0)
  34. {}
  35. static Master* masterInstance = NULL;
  36. Master::Master()
  37. {
  38. swaplr = 0;
  39. off = 0;
  40. smps = 0;
  41. bufl = new float[synth->buffersize];
  42. bufr = new float[synth->buffersize];
  43. pthread_mutex_init(&mutex, NULL);
  44. pthread_mutex_init(&vumutex, NULL);
  45. fft = new FFTwrapper(synth->oscilsize);
  46. shutup = 0;
  47. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  48. vuoutpeakpart[npart] = 1e-9;
  49. fakepeakpart[npart] = 0;
  50. }
  51. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  52. part[npart] = new Part(&microtonal, fft, &mutex);
  53. //Insertion Effects init
  54. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  55. insefx[nefx] = new EffectMgr(1, &mutex);
  56. //System Effects init
  57. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  58. sysefx[nefx] = new EffectMgr(0, &mutex);
  59. defaults();
  60. }
  61. void Master::defaults()
  62. {
  63. volume = 1.0f;
  64. setPvolume(80);
  65. setPkeyshift(64);
  66. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  67. part[npart]->defaults();
  68. part[npart]->Prcvchn = npart % NUM_MIDI_CHANNELS;
  69. }
  70. partonoff(0, 1); //enable the first part
  71. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  72. insefx[nefx]->defaults();
  73. Pinsparts[nefx] = -1;
  74. }
  75. //System Effects init
  76. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  77. sysefx[nefx]->defaults();
  78. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  79. setPsysefxvol(npart, nefx, 0);
  80. for(int nefxto = 0; nefxto < NUM_SYS_EFX; ++nefxto)
  81. setPsysefxsend(nefx, nefxto, 0);
  82. }
  83. microtonal.defaults();
  84. ShutUp();
  85. }
  86. bool Master::mutexLock(lockset request)
  87. {
  88. switch(request) {
  89. case MUTEX_TRYLOCK:
  90. return !pthread_mutex_trylock(&mutex);
  91. case MUTEX_LOCK:
  92. return !pthread_mutex_lock(&mutex);
  93. case MUTEX_UNLOCK:
  94. return !pthread_mutex_unlock(&mutex);
  95. }
  96. return false;
  97. }
  98. Master &Master::getInstance()
  99. {
  100. if (!masterInstance)
  101. masterInstance = new Master;
  102. return *masterInstance;
  103. }
  104. void Master::deleteInstance()
  105. {
  106. if (masterInstance)
  107. {
  108. delete masterInstance;
  109. masterInstance = NULL;
  110. }
  111. }
  112. /*
  113. * Note On Messages (velocity=0 for NoteOff)
  114. */
  115. void Master::noteOn(char chan, char note, char velocity)
  116. {
  117. if(velocity) {
  118. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  119. if(chan == part[npart]->Prcvchn) {
  120. fakepeakpart[npart] = velocity * 2;
  121. if(part[npart]->Penabled)
  122. part[npart]->NoteOn(note, velocity, keyshift);
  123. }
  124. }
  125. else
  126. this->noteOff(chan, note);
  127. HDDRecorder.triggernow();
  128. }
  129. /*
  130. * Note Off Messages
  131. */
  132. void Master::noteOff(char chan, char note)
  133. {
  134. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  135. if((chan == part[npart]->Prcvchn) && part[npart]->Penabled)
  136. part[npart]->NoteOff(note);
  137. }
  138. /*
  139. * Pressure Messages (velocity=0 for NoteOff)
  140. */
  141. void Master::polyphonicAftertouch(char chan, char note, char velocity)
  142. {
  143. if(velocity) {
  144. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  145. if(chan == part[npart]->Prcvchn)
  146. if(part[npart]->Penabled)
  147. part[npart]->PolyphonicAftertouch(note, velocity, keyshift);
  148. }
  149. else
  150. this->noteOff(chan, note);
  151. }
  152. /*
  153. * Controllers
  154. */
  155. void Master::setController(char chan, int type, int par)
  156. {
  157. if((type == C_dataentryhi) || (type == C_dataentrylo)
  158. || (type == C_nrpnhi) || (type == C_nrpnlo)) { //Process RPN and NRPN by the Master (ignore the chan)
  159. ctl.setparameternumber(type, par);
  160. int parhi = -1, parlo = -1, valhi = -1, vallo = -1;
  161. if(ctl.getnrpn(&parhi, &parlo, &valhi, &vallo) == 0) //this is NRPN
  162. //fprintf(stderr,"rcv. NRPN: %d %d %d %d\n",parhi,parlo,valhi,vallo);
  163. switch(parhi) {
  164. case 0x04: //System Effects
  165. if(parlo < NUM_SYS_EFX)
  166. sysefx[parlo]->seteffectpar_nolock(valhi, vallo);
  167. ;
  168. break;
  169. case 0x08: //Insertion Effects
  170. if(parlo < NUM_INS_EFX)
  171. insefx[parlo]->seteffectpar_nolock(valhi, vallo);
  172. ;
  173. break;
  174. }
  175. ;
  176. }
  177. else
  178. if(type == C_bankselectmsb) { // Change current bank
  179. if(((unsigned int)par < bank.banks.size())
  180. && (bank.banks[par].dir != bank.bankfiletitle))
  181. bank.loadbank(bank.banks[par].dir);
  182. }
  183. else { //other controllers
  184. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) //Send the controller to all part assigned to the channel
  185. if((chan == part[npart]->Prcvchn) && (part[npart]->Penabled != 0))
  186. part[npart]->SetController(type, par);
  187. ;
  188. if(type == C_allsoundsoff) { //cleanup insertion/system FX
  189. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  190. sysefx[nefx]->cleanup();
  191. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  192. insefx[nefx]->cleanup();
  193. }
  194. }
  195. }
  196. void Master::setProgram(char chan, unsigned int pgm)
  197. {
  198. if(config.cfg.IgnoreProgramChange)
  199. return;
  200. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  201. if(chan == part[npart]->Prcvchn) {
  202. bank.loadfromslot(pgm, part[npart]);
  203. //Hack to get pad note parameters to update
  204. //this is not real time safe and makes assumptions about the calling
  205. //convention of this function...
  206. pthread_mutex_unlock(&mutex);
  207. part[npart]->applyparameters();
  208. pthread_mutex_lock(&mutex);
  209. }
  210. }
  211. void Master::vuUpdate(const float *outl, const float *outr)
  212. {
  213. //Peak computation (for vumeters)
  214. vu.outpeakl = 1e-12;
  215. vu.outpeakr = 1e-12;
  216. for(int i = 0; i < synth->buffersize; ++i) {
  217. if(fabs(outl[i]) > vu.outpeakl)
  218. vu.outpeakl = fabs(outl[i]);
  219. if(fabs(outr[i]) > vu.outpeakr)
  220. vu.outpeakr = fabs(outr[i]);
  221. }
  222. if((vu.outpeakl > 1.0f) || (vu.outpeakr > 1.0f))
  223. vu.clipped = 1;
  224. if(vu.maxoutpeakl < vu.outpeakl)
  225. vu.maxoutpeakl = vu.outpeakl;
  226. if(vu.maxoutpeakr < vu.outpeakr)
  227. vu.maxoutpeakr = vu.outpeakr;
  228. //RMS Peak computation (for vumeters)
  229. vu.rmspeakl = 1e-12;
  230. vu.rmspeakr = 1e-12;
  231. for(int i = 0; i < synth->buffersize; ++i) {
  232. vu.rmspeakl += outl[i] * outl[i];
  233. vu.rmspeakr += outr[i] * outr[i];
  234. }
  235. vu.rmspeakl = sqrt(vu.rmspeakl / synth->buffersize_f);
  236. vu.rmspeakr = sqrt(vu.rmspeakr / synth->buffersize_f);
  237. //Part Peak computation (for Part vumeters or fake part vumeters)
  238. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  239. vuoutpeakpart[npart] = 1.0e-12f;
  240. if(part[npart]->Penabled != 0) {
  241. float *outl = part[npart]->partoutl,
  242. *outr = part[npart]->partoutr;
  243. for(int i = 0; i < synth->buffersize; ++i) {
  244. float tmp = fabs(outl[i] + outr[i]);
  245. if(tmp > vuoutpeakpart[npart])
  246. vuoutpeakpart[npart] = tmp;
  247. }
  248. vuoutpeakpart[npart] *= volume;
  249. }
  250. else
  251. if(fakepeakpart[npart] > 1)
  252. fakepeakpart[npart]--;
  253. }
  254. }
  255. /*
  256. * Enable/Disable a part
  257. */
  258. void Master::partonoff(int npart, int what)
  259. {
  260. if(npart >= NUM_MIDI_PARTS)
  261. return;
  262. if(what == 0) { //disable part
  263. fakepeakpart[npart] = 0;
  264. part[npart]->Penabled = 0;
  265. part[npart]->cleanup();
  266. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  267. if(Pinsparts[nefx] == npart)
  268. insefx[nefx]->cleanup();
  269. ;
  270. }
  271. }
  272. else { //enabled
  273. part[npart]->Penabled = 1;
  274. fakepeakpart[npart] = 0;
  275. }
  276. }
  277. /*
  278. * Master audio out (the final sound)
  279. */
  280. void Master::AudioOut(float *outl, float *outr)
  281. {
  282. //Swaps the Left channel with Right Channel
  283. if(swaplr)
  284. swap(outl, outr);
  285. //clean up the output samples (should not be needed?)
  286. memset(outl, 0, synth->bufferbytes);
  287. memset(outr, 0, synth->bufferbytes);
  288. //Compute part samples and store them part[npart]->partoutl,partoutr
  289. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  290. if(part[npart]->Penabled != 0 && !pthread_mutex_trylock(&part[npart]->load_mutex)) {
  291. part[npart]->ComputePartSmps();
  292. pthread_mutex_unlock(&part[npart]->load_mutex);
  293. }
  294. }
  295. //Insertion effects
  296. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  297. if(Pinsparts[nefx] >= 0) {
  298. int efxpart = Pinsparts[nefx];
  299. if(part[efxpart]->Penabled)
  300. insefx[nefx]->out(part[efxpart]->partoutl,
  301. part[efxpart]->partoutr);
  302. }
  303. //Apply the part volumes and pannings (after insertion effects)
  304. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  305. if(part[npart]->Penabled == 0)
  306. continue;
  307. Stereo<float> newvol(part[npart]->volume),
  308. oldvol(part[npart]->oldvolumel,
  309. part[npart]->oldvolumer);
  310. float pan = part[npart]->panning;
  311. if(pan < 0.5f)
  312. newvol.l *= pan * 2.0f;
  313. else
  314. newvol.r *= (1.0f - pan) * 2.0f;
  315. //the volume or the panning has changed and needs interpolation
  316. if(ABOVE_AMPLITUDE_THRESHOLD(oldvol.l, newvol.l)
  317. || ABOVE_AMPLITUDE_THRESHOLD(oldvol.r, newvol.r)) {
  318. for(int i = 0; i < synth->buffersize; ++i) {
  319. Stereo<float> vol(INTERPOLATE_AMPLITUDE(oldvol.l, newvol.l,
  320. i, synth->buffersize),
  321. INTERPOLATE_AMPLITUDE(oldvol.r, newvol.r,
  322. i, synth->buffersize));
  323. part[npart]->partoutl[i] *= vol.l;
  324. part[npart]->partoutr[i] *= vol.r;
  325. }
  326. part[npart]->oldvolumel = newvol.l;
  327. part[npart]->oldvolumer = newvol.r;
  328. }
  329. else
  330. for(int i = 0; i < synth->buffersize; ++i) { //the volume did not changed
  331. part[npart]->partoutl[i] *= newvol.l;
  332. part[npart]->partoutr[i] *= newvol.r;
  333. }
  334. }
  335. //System effects
  336. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  337. if(sysefx[nefx]->geteffect() == 0)
  338. continue; //the effect is disabled
  339. float *tmpmixl = getTmpBuffer();
  340. float *tmpmixr = getTmpBuffer();
  341. //Clean up the samples used by the system effects
  342. memset(tmpmixl, 0, synth->bufferbytes);
  343. memset(tmpmixr, 0, synth->bufferbytes);
  344. //Mix the channels according to the part settings about System Effect
  345. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  346. //skip if the part has no output to effect
  347. if(Psysefxvol[nefx][npart] == 0)
  348. continue;
  349. //skip if the part is disabled
  350. if(part[npart]->Penabled == 0)
  351. continue;
  352. //the output volume of each part to system effect
  353. const float vol = sysefxvol[nefx][npart];
  354. for(int i = 0; i < synth->buffersize; ++i) {
  355. tmpmixl[i] += part[npart]->partoutl[i] * vol;
  356. tmpmixr[i] += part[npart]->partoutr[i] * vol;
  357. }
  358. }
  359. // system effect send to next ones
  360. for(int nefxfrom = 0; nefxfrom < nefx; ++nefxfrom)
  361. if(Psysefxsend[nefxfrom][nefx] != 0) {
  362. const float vol = sysefxsend[nefxfrom][nefx];
  363. for(int i = 0; i < synth->buffersize; ++i) {
  364. tmpmixl[i] += sysefx[nefxfrom]->efxoutl[i] * vol;
  365. tmpmixr[i] += sysefx[nefxfrom]->efxoutr[i] * vol;
  366. }
  367. }
  368. sysefx[nefx]->out(tmpmixl, tmpmixr);
  369. //Add the System Effect to sound output
  370. const float outvol = sysefx[nefx]->sysefxgetvolume();
  371. for(int i = 0; i < synth->buffersize; ++i) {
  372. outl[i] += tmpmixl[i] * outvol;
  373. outr[i] += tmpmixr[i] * outvol;
  374. }
  375. returnTmpBuffer(tmpmixl);
  376. returnTmpBuffer(tmpmixr);
  377. }
  378. //Mix all parts
  379. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  380. if(part[npart]->Penabled) //only mix active parts
  381. for(int i = 0; i < synth->buffersize; ++i) { //the volume did not changed
  382. outl[i] += part[npart]->partoutl[i];
  383. outr[i] += part[npart]->partoutr[i];
  384. }
  385. //Insertion effects for Master Out
  386. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  387. if(Pinsparts[nefx] == -2)
  388. insefx[nefx]->out(outl, outr);
  389. //Master Volume
  390. for(int i = 0; i < synth->buffersize; ++i) {
  391. outl[i] *= volume;
  392. outr[i] *= volume;
  393. }
  394. if(!pthread_mutex_trylock(&vumutex)) {
  395. vuUpdate(outl, outr);
  396. pthread_mutex_unlock(&vumutex);
  397. }
  398. //Shutup if it is asked (with fade-out)
  399. if(shutup) {
  400. for(int i = 0; i < synth->buffersize; ++i) {
  401. float tmp = (synth->buffersize_f - i) / synth->buffersize_f;
  402. outl[i] *= tmp;
  403. outr[i] *= tmp;
  404. }
  405. ShutUp();
  406. }
  407. //update the LFO's time
  408. LFOParams::time++;
  409. dump.inctick();
  410. }
  411. //TODO review the respective code from yoshimi for this
  412. //If memory serves correctly, libsamplerate was used
  413. void Master::GetAudioOutSamples(size_t nsamples,
  414. unsigned samplerate,
  415. float *outl,
  416. float *outr)
  417. {
  418. off_t out_off = 0;
  419. //Fail when resampling rather than doing a poor job
  420. if(synth->samplerate != samplerate) {
  421. printf("darn it: %d vs %d\n", synth->samplerate, samplerate);
  422. return;
  423. }
  424. while(nsamples) {
  425. //use all available samples
  426. if(nsamples >= smps) {
  427. memcpy(outl + out_off, bufl + off, sizeof(float) * smps);
  428. memcpy(outr + out_off, bufr + off, sizeof(float) * smps);
  429. nsamples -= smps;
  430. //generate samples
  431. AudioOut(bufl, bufr);
  432. off = 0;
  433. out_off += smps;
  434. smps = synth->buffersize;
  435. }
  436. else { //use some samples
  437. memcpy(outl + out_off, bufl + off, sizeof(float) * nsamples);
  438. memcpy(outr + out_off, bufr + off, sizeof(float) * nsamples);
  439. smps -= nsamples;
  440. off += nsamples;
  441. nsamples = 0;
  442. }
  443. }
  444. }
  445. Master::~Master()
  446. {
  447. delete []bufl;
  448. delete []bufr;
  449. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  450. delete part[npart];
  451. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  452. delete insefx[nefx];
  453. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  454. delete sysefx[nefx];
  455. delete fft;
  456. pthread_mutex_destroy(&mutex);
  457. pthread_mutex_destroy(&vumutex);
  458. }
  459. /*
  460. * Parameter control
  461. */
  462. void Master::setPvolume(char Pvolume_)
  463. {
  464. Pvolume = Pvolume_;
  465. volume = dB2rap((Pvolume - 96.0f) / 96.0f * 40.0f);
  466. }
  467. void Master::setPkeyshift(char Pkeyshift_)
  468. {
  469. Pkeyshift = Pkeyshift_;
  470. keyshift = (int)Pkeyshift - 64;
  471. }
  472. void Master::setPsysefxvol(int Ppart, int Pefx, char Pvol)
  473. {
  474. Psysefxvol[Pefx][Ppart] = Pvol;
  475. sysefxvol[Pefx][Ppart] = powf(0.1f, (1.0f - Pvol / 96.0f) * 2.0f);
  476. }
  477. void Master::setPsysefxsend(int Pefxfrom, int Pefxto, char Pvol)
  478. {
  479. Psysefxsend[Pefxfrom][Pefxto] = Pvol;
  480. sysefxsend[Pefxfrom][Pefxto] = powf(0.1f, (1.0f - Pvol / 96.0f) * 2.0f);
  481. }
  482. /*
  483. * Panic! (Clean up all parts and effects)
  484. */
  485. void Master::ShutUp()
  486. {
  487. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  488. part[npart]->cleanup();
  489. fakepeakpart[npart] = 0;
  490. }
  491. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  492. insefx[nefx]->cleanup();
  493. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  494. sysefx[nefx]->cleanup();
  495. vuresetpeaks();
  496. shutup = 0;
  497. }
  498. /*
  499. * Reset peaks and clear the "cliped" flag (for VU-meter)
  500. */
  501. void Master::vuresetpeaks()
  502. {
  503. pthread_mutex_lock(&vumutex);
  504. vu.outpeakl = 1e-9;
  505. vu.outpeakr = 1e-9;
  506. vu.maxoutpeakl = 1e-9;
  507. vu.maxoutpeakr = 1e-9;
  508. vu.clipped = 0;
  509. pthread_mutex_unlock(&vumutex);
  510. }
  511. vuData Master::getVuData()
  512. {
  513. vuData tmp;
  514. pthread_mutex_lock(&vumutex);
  515. tmp = vu;
  516. pthread_mutex_unlock(&vumutex);
  517. return tmp;
  518. }
  519. void Master::applyparameters(bool lockmutex)
  520. {
  521. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  522. part[npart]->applyparameters(lockmutex);
  523. }
  524. void Master::add2XML(XMLwrapper *xml)
  525. {
  526. xml->addpar("volume", Pvolume);
  527. xml->addpar("key_shift", Pkeyshift);
  528. xml->addparbool("nrpn_receive", ctl.NRPN.receive);
  529. xml->beginbranch("MICROTONAL");
  530. microtonal.add2XML(xml);
  531. xml->endbranch();
  532. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  533. xml->beginbranch("PART", npart);
  534. part[npart]->add2XML(xml);
  535. xml->endbranch();
  536. }
  537. xml->beginbranch("SYSTEM_EFFECTS");
  538. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  539. xml->beginbranch("SYSTEM_EFFECT", nefx);
  540. xml->beginbranch("EFFECT");
  541. sysefx[nefx]->add2XML(xml);
  542. xml->endbranch();
  543. for(int pefx = 0; pefx < NUM_MIDI_PARTS; ++pefx) {
  544. xml->beginbranch("VOLUME", pefx);
  545. xml->addpar("vol", Psysefxvol[nefx][pefx]);
  546. xml->endbranch();
  547. }
  548. for(int tonefx = nefx + 1; tonefx < NUM_SYS_EFX; ++tonefx) {
  549. xml->beginbranch("SENDTO", tonefx);
  550. xml->addpar("send_vol", Psysefxsend[nefx][tonefx]);
  551. xml->endbranch();
  552. }
  553. xml->endbranch();
  554. }
  555. xml->endbranch();
  556. xml->beginbranch("INSERTION_EFFECTS");
  557. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  558. xml->beginbranch("INSERTION_EFFECT", nefx);
  559. xml->addpar("part", Pinsparts[nefx]);
  560. xml->beginbranch("EFFECT");
  561. insefx[nefx]->add2XML(xml);
  562. xml->endbranch();
  563. xml->endbranch();
  564. }
  565. xml->endbranch();
  566. }
  567. int Master::getalldata(char **data)
  568. {
  569. XMLwrapper *xml = new XMLwrapper();
  570. xml->beginbranch("MASTER");
  571. pthread_mutex_lock(&mutex);
  572. add2XML(xml);
  573. pthread_mutex_unlock(&mutex);
  574. xml->endbranch();
  575. *data = xml->getXMLdata();
  576. delete (xml);
  577. return strlen(*data) + 1;
  578. }
  579. void Master::putalldata(char *data, int /*size*/)
  580. {
  581. XMLwrapper *xml = new XMLwrapper();
  582. if(!xml->putXMLdata(data)) {
  583. delete (xml);
  584. return;
  585. }
  586. if(xml->enterbranch("MASTER") == 0)
  587. return;
  588. pthread_mutex_lock(&mutex);
  589. getfromXML(xml);
  590. pthread_mutex_unlock(&mutex);
  591. xml->exitbranch();
  592. delete (xml);
  593. }
  594. int Master::saveXML(const char *filename)
  595. {
  596. XMLwrapper *xml = new XMLwrapper();
  597. xml->beginbranch("MASTER");
  598. add2XML(xml);
  599. xml->endbranch();
  600. int result = xml->saveXMLfile(filename);
  601. delete (xml);
  602. return result;
  603. }
  604. int Master::loadXML(const char *filename)
  605. {
  606. XMLwrapper *xml = new XMLwrapper();
  607. if(xml->loadXMLfile(filename) < 0) {
  608. delete (xml);
  609. return -1;
  610. }
  611. if(xml->enterbranch("MASTER") == 0)
  612. return -10;
  613. getfromXML(xml);
  614. xml->exitbranch();
  615. delete (xml);
  616. return 0;
  617. }
  618. void Master::getfromXML(XMLwrapper *xml)
  619. {
  620. setPvolume(xml->getpar127("volume", Pvolume));
  621. setPkeyshift(xml->getpar127("key_shift", Pkeyshift));
  622. ctl.NRPN.receive = xml->getparbool("nrpn_receive", ctl.NRPN.receive);
  623. part[0]->Penabled = 0;
  624. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  625. if(xml->enterbranch("PART", npart) == 0)
  626. continue;
  627. part[npart]->getfromXML(xml);
  628. xml->exitbranch();
  629. }
  630. if(xml->enterbranch("MICROTONAL")) {
  631. microtonal.getfromXML(xml);
  632. xml->exitbranch();
  633. }
  634. sysefx[0]->changeeffect(0);
  635. if(xml->enterbranch("SYSTEM_EFFECTS")) {
  636. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  637. if(xml->enterbranch("SYSTEM_EFFECT", nefx) == 0)
  638. continue;
  639. if(xml->enterbranch("EFFECT")) {
  640. sysefx[nefx]->getfromXML(xml);
  641. xml->exitbranch();
  642. }
  643. for(int partefx = 0; partefx < NUM_MIDI_PARTS; ++partefx) {
  644. if(xml->enterbranch("VOLUME", partefx) == 0)
  645. continue;
  646. setPsysefxvol(partefx, nefx,
  647. xml->getpar127("vol", Psysefxvol[partefx][nefx]));
  648. xml->exitbranch();
  649. }
  650. for(int tonefx = nefx + 1; tonefx < NUM_SYS_EFX; ++tonefx) {
  651. if(xml->enterbranch("SENDTO", tonefx) == 0)
  652. continue;
  653. setPsysefxsend(nefx, tonefx,
  654. xml->getpar127("send_vol",
  655. Psysefxsend[nefx][tonefx]));
  656. xml->exitbranch();
  657. }
  658. xml->exitbranch();
  659. }
  660. xml->exitbranch();
  661. }
  662. if(xml->enterbranch("INSERTION_EFFECTS")) {
  663. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  664. if(xml->enterbranch("INSERTION_EFFECT", nefx) == 0)
  665. continue;
  666. Pinsparts[nefx] = xml->getpar("part",
  667. Pinsparts[nefx],
  668. -2,
  669. NUM_MIDI_PARTS);
  670. if(xml->enterbranch("EFFECT")) {
  671. insefx[nefx]->getfromXML(xml);
  672. xml->exitbranch();
  673. }
  674. xml->exitbranch();
  675. }
  676. xml->exitbranch();
  677. }
  678. }