Collection of tools useful for audio production
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.

798 lines
22KB

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