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.

809 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. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  199. if(chan == part[npart]->Prcvchn) {
  200. bank.loadfromslot(pgm, part[npart]);
  201. //Hack to get pad note parameters to update
  202. //this is not real time safe and makes assumptions about the calling
  203. //convention of this function...
  204. pthread_mutex_unlock(&mutex);
  205. part[npart]->applyparameters();
  206. pthread_mutex_lock(&mutex);
  207. }
  208. }
  209. void Master::vuUpdate(const float *outl, const float *outr)
  210. {
  211. //Peak computation (for vumeters)
  212. vu.outpeakl = 1e-12;
  213. vu.outpeakr = 1e-12;
  214. for(int i = 0; i < synth->buffersize; ++i) {
  215. if(fabs(outl[i]) > vu.outpeakl)
  216. vu.outpeakl = fabs(outl[i]);
  217. if(fabs(outr[i]) > vu.outpeakr)
  218. vu.outpeakr = fabs(outr[i]);
  219. }
  220. if((vu.outpeakl > 1.0f) || (vu.outpeakr > 1.0f))
  221. vu.clipped = 1;
  222. if(vu.maxoutpeakl < vu.outpeakl)
  223. vu.maxoutpeakl = vu.outpeakl;
  224. if(vu.maxoutpeakr < vu.outpeakr)
  225. vu.maxoutpeakr = vu.outpeakr;
  226. //RMS Peak computation (for vumeters)
  227. vu.rmspeakl = 1e-12;
  228. vu.rmspeakr = 1e-12;
  229. for(int i = 0; i < synth->buffersize; ++i) {
  230. vu.rmspeakl += outl[i] * outl[i];
  231. vu.rmspeakr += outr[i] * outr[i];
  232. }
  233. vu.rmspeakl = sqrt(vu.rmspeakl / synth->buffersize_f);
  234. vu.rmspeakr = sqrt(vu.rmspeakr / synth->buffersize_f);
  235. //Part Peak computation (for Part vumeters or fake part vumeters)
  236. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  237. vuoutpeakpart[npart] = 1.0e-12f;
  238. if(part[npart]->Penabled != 0) {
  239. float *outl = part[npart]->partoutl,
  240. *outr = part[npart]->partoutr;
  241. for(int i = 0; i < synth->buffersize; ++i) {
  242. float tmp = fabs(outl[i] + outr[i]);
  243. if(tmp > vuoutpeakpart[npart])
  244. vuoutpeakpart[npart] = tmp;
  245. }
  246. vuoutpeakpart[npart] *= volume;
  247. }
  248. else
  249. if(fakepeakpart[npart] > 1)
  250. fakepeakpart[npart]--;
  251. }
  252. }
  253. /*
  254. * Enable/Disable a part
  255. */
  256. void Master::partonoff(int npart, int what)
  257. {
  258. if(npart >= NUM_MIDI_PARTS)
  259. return;
  260. if(what == 0) { //disable part
  261. fakepeakpart[npart] = 0;
  262. part[npart]->Penabled = 0;
  263. part[npart]->cleanup();
  264. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  265. if(Pinsparts[nefx] == npart)
  266. insefx[nefx]->cleanup();
  267. ;
  268. }
  269. }
  270. else { //enabled
  271. part[npart]->Penabled = 1;
  272. fakepeakpart[npart] = 0;
  273. }
  274. }
  275. /*
  276. * Master audio out (the final sound)
  277. */
  278. void Master::AudioOut(float *outl, float *outr)
  279. {
  280. //Swaps the Left channel with Right Channel
  281. if(swaplr)
  282. swap(outl, outr);
  283. //clean up the output samples (should not be needed?)
  284. memset(outl, 0, synth->bufferbytes);
  285. memset(outr, 0, synth->bufferbytes);
  286. //Compute part samples and store them part[npart]->partoutl,partoutr
  287. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  288. if(part[npart]->Penabled != 0 && !pthread_mutex_trylock(&part[npart]->load_mutex)) {
  289. part[npart]->ComputePartSmps();
  290. pthread_mutex_unlock(&part[npart]->load_mutex);
  291. }
  292. }
  293. //Insertion effects
  294. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  295. if(Pinsparts[nefx] >= 0) {
  296. int efxpart = Pinsparts[nefx];
  297. if(part[efxpart]->Penabled)
  298. insefx[nefx]->out(part[efxpart]->partoutl,
  299. part[efxpart]->partoutr);
  300. }
  301. //Apply the part volumes and pannings (after insertion effects)
  302. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  303. if(part[npart]->Penabled == 0)
  304. continue;
  305. Stereo<float> newvol(part[npart]->volume),
  306. oldvol(part[npart]->oldvolumel,
  307. part[npart]->oldvolumer);
  308. float pan = part[npart]->panning;
  309. if(pan < 0.5f)
  310. newvol.l *= pan * 2.0f;
  311. else
  312. newvol.r *= (1.0f - pan) * 2.0f;
  313. //the volume or the panning has changed and needs interpolation
  314. if(ABOVE_AMPLITUDE_THRESHOLD(oldvol.l, newvol.l)
  315. || ABOVE_AMPLITUDE_THRESHOLD(oldvol.r, newvol.r)) {
  316. for(int i = 0; i < synth->buffersize; ++i) {
  317. Stereo<float> vol(INTERPOLATE_AMPLITUDE(oldvol.l, newvol.l,
  318. i, synth->buffersize),
  319. INTERPOLATE_AMPLITUDE(oldvol.r, newvol.r,
  320. i, synth->buffersize));
  321. part[npart]->partoutl[i] *= vol.l;
  322. part[npart]->partoutr[i] *= vol.r;
  323. }
  324. part[npart]->oldvolumel = newvol.l;
  325. part[npart]->oldvolumer = newvol.r;
  326. }
  327. else
  328. for(int i = 0; i < synth->buffersize; ++i) { //the volume did not changed
  329. part[npart]->partoutl[i] *= newvol.l;
  330. part[npart]->partoutr[i] *= newvol.r;
  331. }
  332. }
  333. //System effects
  334. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  335. if(sysefx[nefx]->geteffect() == 0)
  336. continue; //the effect is disabled
  337. float *tmpmixl = getTmpBuffer();
  338. float *tmpmixr = getTmpBuffer();
  339. //Clean up the samples used by the system effects
  340. memset(tmpmixl, 0, synth->bufferbytes);
  341. memset(tmpmixr, 0, synth->bufferbytes);
  342. //Mix the channels according to the part settings about System Effect
  343. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  344. //skip if the part has no output to effect
  345. if(Psysefxvol[nefx][npart] == 0)
  346. continue;
  347. //skip if the part is disabled
  348. if(part[npart]->Penabled == 0)
  349. continue;
  350. //the output volume of each part to system effect
  351. const float vol = sysefxvol[nefx][npart];
  352. for(int i = 0; i < synth->buffersize; ++i) {
  353. tmpmixl[i] += part[npart]->partoutl[i] * vol;
  354. tmpmixr[i] += part[npart]->partoutr[i] * vol;
  355. }
  356. }
  357. // system effect send to next ones
  358. for(int nefxfrom = 0; nefxfrom < nefx; ++nefxfrom)
  359. if(Psysefxsend[nefxfrom][nefx] != 0) {
  360. const float vol = sysefxsend[nefxfrom][nefx];
  361. for(int i = 0; i < synth->buffersize; ++i) {
  362. tmpmixl[i] += sysefx[nefxfrom]->efxoutl[i] * vol;
  363. tmpmixr[i] += sysefx[nefxfrom]->efxoutr[i] * vol;
  364. }
  365. }
  366. sysefx[nefx]->out(tmpmixl, tmpmixr);
  367. //Add the System Effect to sound output
  368. const float outvol = sysefx[nefx]->sysefxgetvolume();
  369. for(int i = 0; i < synth->buffersize; ++i) {
  370. outl[i] += tmpmixl[i] * outvol;
  371. outr[i] += tmpmixr[i] * outvol;
  372. }
  373. returnTmpBuffer(tmpmixl);
  374. returnTmpBuffer(tmpmixr);
  375. }
  376. //Mix all parts
  377. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  378. if(part[npart]->Penabled) //only mix active parts
  379. for(int i = 0; i < synth->buffersize; ++i) { //the volume did not changed
  380. outl[i] += part[npart]->partoutl[i];
  381. outr[i] += part[npart]->partoutr[i];
  382. }
  383. //Insertion effects for Master Out
  384. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  385. if(Pinsparts[nefx] == -2)
  386. insefx[nefx]->out(outl, outr);
  387. //Master Volume
  388. for(int i = 0; i < synth->buffersize; ++i) {
  389. outl[i] *= volume;
  390. outr[i] *= volume;
  391. }
  392. if(!pthread_mutex_trylock(&vumutex)) {
  393. vuUpdate(outl, outr);
  394. pthread_mutex_unlock(&vumutex);
  395. }
  396. //Shutup if it is asked (with fade-out)
  397. if(shutup) {
  398. for(int i = 0; i < synth->buffersize; ++i) {
  399. float tmp = (synth->buffersize_f - i) / synth->buffersize_f;
  400. outl[i] *= tmp;
  401. outr[i] *= tmp;
  402. }
  403. ShutUp();
  404. }
  405. //update the LFO's time
  406. LFOParams::time++;
  407. dump.inctick();
  408. }
  409. //TODO review the respective code from yoshimi for this
  410. //If memory serves correctly, libsamplerate was used
  411. void Master::GetAudioOutSamples(size_t nsamples,
  412. unsigned samplerate,
  413. float *outl,
  414. float *outr)
  415. {
  416. off_t out_off = 0;
  417. //Fail when resampling rather than doing a poor job
  418. if(synth->samplerate != samplerate) {
  419. printf("darn it: %d vs %d\n", synth->samplerate, samplerate);
  420. return;
  421. }
  422. while(nsamples) {
  423. //use all available samples
  424. if(nsamples >= smps) {
  425. memcpy(outl + out_off, bufl + off, sizeof(float) * smps);
  426. memcpy(outr + out_off, bufr + off, sizeof(float) * smps);
  427. nsamples -= smps;
  428. //generate samples
  429. AudioOut(bufl, bufr);
  430. off = 0;
  431. out_off += smps;
  432. smps = synth->buffersize;
  433. }
  434. else { //use some samples
  435. memcpy(outl + out_off, bufl + off, sizeof(float) * nsamples);
  436. memcpy(outr + out_off, bufr + off, sizeof(float) * nsamples);
  437. smps -= nsamples;
  438. off += nsamples;
  439. nsamples = 0;
  440. }
  441. }
  442. }
  443. Master::~Master()
  444. {
  445. delete []bufl;
  446. delete []bufr;
  447. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  448. delete part[npart];
  449. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  450. delete insefx[nefx];
  451. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  452. delete sysefx[nefx];
  453. delete fft;
  454. pthread_mutex_destroy(&mutex);
  455. pthread_mutex_destroy(&vumutex);
  456. }
  457. /*
  458. * Parameter control
  459. */
  460. void Master::setPvolume(char Pvolume_)
  461. {
  462. Pvolume = Pvolume_;
  463. volume = dB2rap((Pvolume - 96.0f) / 96.0f * 40.0f);
  464. }
  465. void Master::setPkeyshift(char Pkeyshift_)
  466. {
  467. Pkeyshift = Pkeyshift_;
  468. keyshift = (int)Pkeyshift - 64;
  469. }
  470. void Master::setPsysefxvol(int Ppart, int Pefx, char Pvol)
  471. {
  472. Psysefxvol[Pefx][Ppart] = Pvol;
  473. sysefxvol[Pefx][Ppart] = powf(0.1f, (1.0f - Pvol / 96.0f) * 2.0f);
  474. }
  475. void Master::setPsysefxsend(int Pefxfrom, int Pefxto, char Pvol)
  476. {
  477. Psysefxsend[Pefxfrom][Pefxto] = Pvol;
  478. sysefxsend[Pefxfrom][Pefxto] = powf(0.1f, (1.0f - Pvol / 96.0f) * 2.0f);
  479. }
  480. /*
  481. * Panic! (Clean up all parts and effects)
  482. */
  483. void Master::ShutUp()
  484. {
  485. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  486. part[npart]->cleanup();
  487. fakepeakpart[npart] = 0;
  488. }
  489. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx)
  490. insefx[nefx]->cleanup();
  491. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx)
  492. sysefx[nefx]->cleanup();
  493. vuresetpeaks();
  494. shutup = 0;
  495. }
  496. /*
  497. * Reset peaks and clear the "cliped" flag (for VU-meter)
  498. */
  499. void Master::vuresetpeaks()
  500. {
  501. pthread_mutex_lock(&vumutex);
  502. vu.outpeakl = 1e-9;
  503. vu.outpeakr = 1e-9;
  504. vu.maxoutpeakl = 1e-9;
  505. vu.maxoutpeakr = 1e-9;
  506. vu.clipped = 0;
  507. pthread_mutex_unlock(&vumutex);
  508. }
  509. vuData Master::getVuData()
  510. {
  511. vuData tmp;
  512. pthread_mutex_lock(&vumutex);
  513. tmp = vu;
  514. pthread_mutex_unlock(&vumutex);
  515. return tmp;
  516. }
  517. void Master::applyparameters(bool lockmutex)
  518. {
  519. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart)
  520. part[npart]->applyparameters(lockmutex);
  521. }
  522. void Master::add2XML(XMLwrapper *xml)
  523. {
  524. xml->addpar("volume", Pvolume);
  525. xml->addpar("key_shift", Pkeyshift);
  526. xml->addparbool("nrpn_receive", ctl.NRPN.receive);
  527. xml->beginbranch("MICROTONAL");
  528. microtonal.add2XML(xml);
  529. xml->endbranch();
  530. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  531. xml->beginbranch("PART", npart);
  532. part[npart]->add2XML(xml);
  533. xml->endbranch();
  534. }
  535. xml->beginbranch("SYSTEM_EFFECTS");
  536. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  537. xml->beginbranch("SYSTEM_EFFECT", nefx);
  538. xml->beginbranch("EFFECT");
  539. sysefx[nefx]->add2XML(xml);
  540. xml->endbranch();
  541. for(int pefx = 0; pefx < NUM_MIDI_PARTS; ++pefx) {
  542. xml->beginbranch("VOLUME", pefx);
  543. xml->addpar("vol", Psysefxvol[nefx][pefx]);
  544. xml->endbranch();
  545. }
  546. for(int tonefx = nefx + 1; tonefx < NUM_SYS_EFX; ++tonefx) {
  547. xml->beginbranch("SENDTO", tonefx);
  548. xml->addpar("send_vol", Psysefxsend[nefx][tonefx]);
  549. xml->endbranch();
  550. }
  551. xml->endbranch();
  552. }
  553. xml->endbranch();
  554. xml->beginbranch("INSERTION_EFFECTS");
  555. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  556. xml->beginbranch("INSERTION_EFFECT", nefx);
  557. xml->addpar("part", Pinsparts[nefx]);
  558. xml->beginbranch("EFFECT");
  559. insefx[nefx]->add2XML(xml);
  560. xml->endbranch();
  561. xml->endbranch();
  562. }
  563. xml->endbranch();
  564. }
  565. int Master::getalldata(char **data)
  566. {
  567. XMLwrapper *xml = new XMLwrapper();
  568. xml->beginbranch("MASTER");
  569. pthread_mutex_lock(&mutex);
  570. add2XML(xml);
  571. pthread_mutex_unlock(&mutex);
  572. xml->endbranch();
  573. *data = xml->getXMLdata();
  574. delete (xml);
  575. return strlen(*data) + 1;
  576. }
  577. void Master::putalldata(char *data, int /*size*/)
  578. {
  579. XMLwrapper *xml = new XMLwrapper();
  580. if(!xml->putXMLdata(data)) {
  581. delete (xml);
  582. return;
  583. }
  584. if(xml->enterbranch("MASTER") == 0)
  585. return;
  586. pthread_mutex_lock(&mutex);
  587. getfromXML(xml);
  588. pthread_mutex_unlock(&mutex);
  589. xml->exitbranch();
  590. delete (xml);
  591. }
  592. int Master::saveXML(const char *filename)
  593. {
  594. XMLwrapper *xml = new XMLwrapper();
  595. xml->beginbranch("MASTER");
  596. add2XML(xml);
  597. xml->endbranch();
  598. int result = xml->saveXMLfile(filename);
  599. delete (xml);
  600. return result;
  601. }
  602. int Master::loadXML(const char *filename)
  603. {
  604. XMLwrapper *xml = new XMLwrapper();
  605. if(xml->loadXMLfile(filename) < 0) {
  606. delete (xml);
  607. return -1;
  608. }
  609. if(xml->enterbranch("MASTER") == 0)
  610. return -10;
  611. getfromXML(xml);
  612. xml->exitbranch();
  613. delete (xml);
  614. return 0;
  615. }
  616. void Master::getfromXML(XMLwrapper *xml)
  617. {
  618. setPvolume(xml->getpar127("volume", Pvolume));
  619. setPkeyshift(xml->getpar127("key_shift", Pkeyshift));
  620. ctl.NRPN.receive = xml->getparbool("nrpn_receive", ctl.NRPN.receive);
  621. part[0]->Penabled = 0;
  622. for(int npart = 0; npart < NUM_MIDI_PARTS; ++npart) {
  623. if(xml->enterbranch("PART", npart) == 0)
  624. continue;
  625. part[npart]->getfromXML(xml);
  626. xml->exitbranch();
  627. }
  628. if(xml->enterbranch("MICROTONAL")) {
  629. microtonal.getfromXML(xml);
  630. xml->exitbranch();
  631. }
  632. sysefx[0]->changeeffect(0);
  633. if(xml->enterbranch("SYSTEM_EFFECTS")) {
  634. for(int nefx = 0; nefx < NUM_SYS_EFX; ++nefx) {
  635. if(xml->enterbranch("SYSTEM_EFFECT", nefx) == 0)
  636. continue;
  637. if(xml->enterbranch("EFFECT")) {
  638. sysefx[nefx]->getfromXML(xml);
  639. xml->exitbranch();
  640. }
  641. for(int partefx = 0; partefx < NUM_MIDI_PARTS; ++partefx) {
  642. if(xml->enterbranch("VOLUME", partefx) == 0)
  643. continue;
  644. setPsysefxvol(partefx, nefx,
  645. xml->getpar127("vol", Psysefxvol[partefx][nefx]));
  646. xml->exitbranch();
  647. }
  648. for(int tonefx = nefx + 1; tonefx < NUM_SYS_EFX; ++tonefx) {
  649. if(xml->enterbranch("SENDTO", tonefx) == 0)
  650. continue;
  651. setPsysefxsend(nefx, tonefx,
  652. xml->getpar127("send_vol",
  653. Psysefxsend[nefx][tonefx]));
  654. xml->exitbranch();
  655. }
  656. xml->exitbranch();
  657. }
  658. xml->exitbranch();
  659. }
  660. if(xml->enterbranch("INSERTION_EFFECTS")) {
  661. for(int nefx = 0; nefx < NUM_INS_EFX; ++nefx) {
  662. if(xml->enterbranch("INSERTION_EFFECT", nefx) == 0)
  663. continue;
  664. Pinsparts[nefx] = xml->getpar("part",
  665. Pinsparts[nefx],
  666. -2,
  667. NUM_MIDI_PARTS);
  668. if(xml->enterbranch("EFFECT")) {
  669. insefx[nefx]->getfromXML(xml);
  670. xml->exitbranch();
  671. }
  672. xml->exitbranch();
  673. }
  674. xml->exitbranch();
  675. }
  676. }