summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/synti/zynaddsubfx/Effects/EffectMgr.C
blob: efdb672a428c3f05ed8cb2a138577f90b01c12b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
  ZynAddSubFX - a software synthesizer
 
  EffectMgr.C - Effect manager, an interface betwen the program and effects
  Copyright (C) 2002-2005 Nasca Octavian Paul
  Author: Nasca Octavian Paul

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License 
  as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License (version 2) for more details.

  You should have received a copy of the GNU General Public License (version 2)
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

#include <stdlib.h>
#include <stdio.h>
#include "EffectMgr.h"
#include "../Misc/Master.h"

EffectMgr::EffectMgr(int insertion_,Master* master_){
    setpresettype("Peffect");
    efx=NULL;
    nefx=0;
    insertion=insertion_;
    master=master_;
    efxoutl=new REALTYPE[SOUND_BUFFER_SIZE];
    efxoutr=new REALTYPE[SOUND_BUFFER_SIZE];;
    for (int i=0;i<SOUND_BUFFER_SIZE;i++){
	efxoutl[i]=0.0;
	efxoutr[i]=0.0;
    };
    filterpars=NULL;
    dryonly=false;
    defaults();
};


EffectMgr::~EffectMgr(){
    if (efx!=NULL) delete (efx);
    delete (efxoutl);
    delete (efxoutr);
};

void EffectMgr::defaults(){
    changeeffect(0);
    setdryonly(false);
};

/*
 * Change the effect
 */
void EffectMgr::changeeffect(int nefx_){
    cleanup();
    if (nefx==nefx_) return;
    nefx=nefx_;
    for (int i=0;i<SOUND_BUFFER_SIZE;i++){
	efxoutl[i]=0.0;
	efxoutr[i]=0.0;
    };

    if (efx!=NULL) delete (efx);
    switch (nefx){
	case 1:efx=new Reverb(insertion,efxoutl,efxoutr);break;
	case 2:efx=new Echo(insertion,efxoutl,efxoutr);break;
	case 3:efx=new Chorus(insertion,efxoutl,efxoutr);break;
	case 4:efx=new Phaser(insertion,efxoutl,efxoutr);break;
	case 5:efx=new Alienwah(insertion,efxoutl,efxoutr);break;
	case 6:efx=new Distorsion(insertion,efxoutl,efxoutr);break;
	case 7:efx=new EQ(insertion,efxoutl,efxoutr);break;
	case 8:efx=new DynamicFilter(insertion,efxoutl,efxoutr);break;
	//put more effect here
	default:efx=NULL;break;//no effect (thru)
    };
    
    if (efx!=NULL) filterpars=efx->filterpars;
};

/*
 * Obtain the effect number
 */
int EffectMgr::geteffect(){
    return (nefx);
};

/*
 * Cleanup the current effect
 */
void EffectMgr::cleanup(){
    if (efx!=NULL) efx->cleanup();
};


/*
 * Get the preset of the current effect
 */
 
unsigned char EffectMgr::getpreset(){
    if (efx!=NULL) return(efx->Ppreset);
	else return(0);
};

/*
 * Change the preset of the current effect
 */
void EffectMgr::changepreset_nolock(unsigned char npreset){
    if (efx!=NULL) efx->setpreset(npreset);
};

/*
 * Change the preset of the current effect(with thread locking)
 */
void EffectMgr::changepreset(unsigned char npreset){
    master->lock();
    changepreset_nolock(npreset);
    master->unlock();
};


/*
 * Change a parameter of the current effect 
 */
void EffectMgr::seteffectpar_nolock(int npar,unsigned char value){
    if (efx==NULL) return;
    efx->changepar(npar,value);
};

/*
 * Change a parameter of the current effect (with thread locking)
 */
void EffectMgr::seteffectpar(int npar,unsigned char value){
    master->lock();
    seteffectpar_nolock(npar,value);
    master->unlock();
};

/*
 * Get a parameter of the current effect
 */
unsigned char EffectMgr::geteffectpar(int npar){
    if (efx==NULL) return(0);
    return(efx->getpar(npar));
};


/*
 * Apply the effect
 */
void EffectMgr::out(REALTYPE *smpsl,REALTYPE *smpsr){
    int i;
    if (efx==NULL){
	if (insertion==0) 
	    for (i=0;i<SOUND_BUFFER_SIZE;i++){
	     smpsl[i]=0.0;smpsr[i]=0.0;
	     efxoutl[i]=0.0;efxoutr[i]=0.0;
	    };
	return;
    };
    for (i=0;i<SOUND_BUFFER_SIZE;i++){
	smpsl[i]+=denormalkillbuf[i];
	smpsr[i]+=denormalkillbuf[i];
	efxoutl[i]=0.0;
	efxoutr[i]=0.0;
    };
    efx->out(smpsl,smpsr);
    
    REALTYPE volume=efx->volume;
    
    if (nefx==7){//this is need only for the EQ effect
	for (i=0;i<SOUND_BUFFER_SIZE;i++){
	    smpsl[i]=efxoutl[i];
	    smpsr[i]=efxoutr[i];
	};
	return;
    };
    
    //Insertion effect
    if (insertion!=0) {
        REALTYPE v1,v2;
	if (volume<0.5) {
		v1=1.0;
		v2=volume*2.0;
	} else {
		v1=(1.0-volume)*2.0;
		v2=1.0;
    	};
	if ((nefx==1)||(nefx==2)) v2*=v2;//for Reverb and Echo, the wet function is not liniar
	
	if (dryonly){//this is used for instrument effect only
    	    for (i=0;i<SOUND_BUFFER_SIZE;i++){
		smpsl[i]*=v1;
		smpsr[i]*=v1;
		efxoutl[i]*=v2;
		efxoutr[i]*=v2;
	    };
	}else{//normal instrument/insertion effect 
    	    for (i=0;i<SOUND_BUFFER_SIZE;i++){
		smpsl[i]=smpsl[i]*v1+efxoutl[i]*v2;
		smpsr[i]=smpsr[i]*v1+efxoutr[i]*v2;
	    };
	};
    } else {//System effect
	for (i=0;i<SOUND_BUFFER_SIZE;i++){
	    efxoutl[i]*=2.0*volume;
	    efxoutr[i]*=2.0*volume;
	    smpsl[i]=efxoutl[i];
	    smpsr[i]=efxoutr[i];
	};
    };
    
};

/*
 * Get the effect volume for the system effect
 */
REALTYPE EffectMgr::sysefxgetvolume(){
    if (efx==NULL) return (1.0);
	else return(efx->outvolume);    
};


/*
 * Get the EQ response
 */
REALTYPE EffectMgr::getEQfreqresponse(REALTYPE freq){
    if (nefx==7) return(efx->getfreqresponse(freq));
	else return(0.0);
};


void EffectMgr::setdryonly(bool value){
    dryonly=value;
};

void EffectMgr::add2XML(XMLwrapper *xml){
    xml->addpar("type",geteffect());

    if ((efx==NULL)||(geteffect()==0)) return;
    xml->addpar("preset",efx->Ppreset);

    xml->beginbranch("EFFECT_PARAMETERS");
	for (int n=0;n<128;n++){
	    int par=geteffectpar(n);
	    if (par==0) continue;
	    xml->beginbranch("par_no",n);
		xml->addpar("par",par);
	    xml->endbranch();
	};
	if (filterpars!=NULL){
	    xml->beginbranch("FILTER");
		filterpars->add2XML(xml);
	    xml->endbranch();
	};
    xml->endbranch();
};

void EffectMgr::getfromXML(XMLwrapper *xml){
    changeeffect(xml->getpar127("type",geteffect()));

    if ((efx==NULL)||(geteffect()==0)) return;
    
    efx->Ppreset=xml->getpar127("preset",efx->Ppreset);

    if (xml->enterbranch("EFFECT_PARAMETERS")){
	for (int n=0;n<128;n++){
	    seteffectpar_nolock(n,0);//erase effect parameter
	    if (xml->enterbranch("par_no",n)==0) continue;

	    int par=geteffectpar(n);
	    seteffectpar_nolock(n,xml->getpar127("par",par));
	    xml->exitbranch();
	};
	if (filterpars!=NULL){
	    if (xml->enterbranch("FILTER")){
		filterpars->getfromXML(xml);
		xml->exitbranch();
	    };
	};
	xml->exitbranch();
    };
    cleanup();
};