summaryrefslogtreecommitdiff
path: root/attic/muse_qt4_evolution/synti/zynaddsubfx/Effects/Chorus.C
blob: fb40c93ba8cd73e23f30eeffd76b9ba4d0d72a56 (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
/*
  ZynAddSubFX - a software synthesizer
 
  Chorus.C - Chorus and Flange 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 <math.h>
#include "Chorus.h"
#include <stdio.h>

Chorus::Chorus(int insertion_,REALTYPE *efxoutl_,REALTYPE *efxoutr_){
    efxoutl=efxoutl_;
    efxoutr=efxoutr_;
    dlk=0;drk=0;
    maxdelay=(int)(MAX_CHORUS_DELAY/1000.0*SAMPLE_RATE);
    delayl=new REALTYPE[maxdelay];
    delayr=new REALTYPE[maxdelay];
    insertion=insertion_;

    filterpars=NULL;
    Ppreset=0;
    setpreset(Ppreset);

    lfo.effectlfoout(&lfol,&lfor);
    dl2=getdelay(lfol);
    dr2=getdelay(lfor);
    cleanup();
};

Chorus::~Chorus(){
    delete [] delayl;
    delete [] delayr;
};

/*
 * get the delay value in samples; xlfo is the current lfo value
 */
REALTYPE Chorus::getdelay(REALTYPE xlfo){
    REALTYPE result;
    if (Pflangemode==0){
	result=(delay+xlfo*depth)*SAMPLE_RATE;
    } else result=0;
    
    //check if it is too big delay(caused bu errornous setdelay() and setdepth()    
    if ((result+0.5)>=maxdelay) {
	fprintf(stderr,"%s","WARNING: Chorus.C::getdelay(..) too big delay (see setdelay and setdepth funcs.)\n");
	result=maxdelay-1.0;
    };
    return(result);
};

/*
 * Apply the effect
 */
void Chorus::out(REALTYPE *smpsl,REALTYPE *smpsr){
    int i;
    dl1=dl2;dr1=dr2;
    lfo.effectlfoout(&lfol,&lfor);
    
    dl2=getdelay(lfol);
    dr2=getdelay(lfor);

    for (i=0;i<SOUND_BUFFER_SIZE;i++){	
	REALTYPE inl=smpsl[i];
	REALTYPE inr=smpsr[i];
	//LRcross
	REALTYPE l=inl;
	REALTYPE r=inr;
	inl=l*(1.0-lrcross)+r*lrcross;
	inr=r*(1.0-lrcross)+l*lrcross;
	
	//Left channel

	//compute the delay in samples using linear interpolation between the lfo delays
	mdel=(dl1*(SOUND_BUFFER_SIZE-i)+dl2*i)/SOUND_BUFFER_SIZE;
	if (++dlk>=maxdelay) dlk=0;
	REALTYPE tmp=dlk-mdel+maxdelay*2.0;//where should I get the sample from

	F2I(tmp,dlhi);
	dlhi%=maxdelay;
	
	dlhi2=(dlhi-1+maxdelay)%maxdelay;
	dllo=1.0-fmod(tmp,1.0);
        efxoutl[i]=delayl[dlhi2]*dllo+delayl[dlhi]*(1.0-dllo);
	delayl[dlk]=inl+efxoutl[i]*fb;

	//Right channel

	//compute the delay in samples using linear interpolation between the lfo delays
	mdel=(dr1*(SOUND_BUFFER_SIZE-i)+dr2*i)/SOUND_BUFFER_SIZE;
	if (++drk>=maxdelay) drk=0;
	tmp=drk-mdel+maxdelay*2.0;//where should I get the sample from

	F2I(tmp,dlhi);
	dlhi%=maxdelay;	
	
	dlhi2=(dlhi-1+maxdelay)%maxdelay;
	dllo=1.0-fmod(tmp,1.0);
        efxoutr[i]=delayr[dlhi2]*dllo+delayr[dlhi]*(1.0-dllo);
	delayr[dlk]=inr+efxoutr[i]*fb;

    };

    if (Poutsub!=0)
	for (i=0;i<SOUND_BUFFER_SIZE;i++){
    	    efxoutl[i] *= -1.0;
            efxoutr[i] *= -1.0;
	};


    for (int i=0;i<SOUND_BUFFER_SIZE;i++){
	efxoutl[i]*=panning;
	efxoutr[i]*=(1.0-panning);
    };
};

/*
 * Cleanup the effect
 */
void Chorus::cleanup(){
    for (int i=0;i<maxdelay;i++){
	delayl[i]=0.0;
	delayr[i]=0.0;
    };
        
};

/*
 * Parameter control
 */
void Chorus::setdepth(unsigned char Pdepth){
    this->Pdepth=Pdepth;
    depth=(pow(8.0,(Pdepth/127.0)*2.0)-1.0)/1000.0;//seconds
};

void Chorus::setdelay(unsigned char Pdelay){
    this->Pdelay=Pdelay;
    delay=(pow(10.0,(Pdelay/127.0)*2.0)-1.0)/1000.0;//seconds
};

void Chorus::setfb(unsigned char Pfb){
    this->Pfb=Pfb;
    fb=(Pfb-64.0)/64.1;
};
void Chorus::setvolume(unsigned char Pvolume){
    this->Pvolume=Pvolume;
    outvolume=Pvolume/127.0;
    if (insertion==0) volume=1.0;
	else volume=outvolume;
};

void Chorus::setpanning(unsigned char Ppanning){
    this->Ppanning=Ppanning;
    panning=Ppanning/127.0;
};

void Chorus::setlrcross(unsigned char Plrcross){
    this->Plrcross=Plrcross;
    lrcross=Plrcross/127.0;
};

void Chorus::setpreset(unsigned char npreset){
    const int PRESET_SIZE=12;
    const int NUM_PRESETS=10;
    unsigned char presets[NUM_PRESETS][PRESET_SIZE]={
	//Chorus1
	{64,64,50,0,0,90,40,85,64,119,0,0},
	//Chorus2
	{64,64,45,0,0,98,56,90,64,19,0,0},
	//Chorus3
	{64,64,29,0,1,42,97,95,90,127,0,0},
	//Celeste1
	{64,64,26,0,0,42,115,18,90,127,0,0},
	//Celeste2
	{64,64,29,117,0,50,115,9,31,127,0,1},
	//Flange1
	{64,64,57,0,0,60,23,3,62,0,0,0},
	//Flange2
	{64,64,33,34,1,40,35,3,109,0,0,0},
	//Flange3
	{64,64,53,34,1,94,35,3,54,0,0,1},
	//Flange4
	{64,64,40,0,1,62,12,19,97,0,0,0},
	//Flange5
	{64,64,55,105,0,24,39,19,17,0,0,1}};

    if (npreset>=NUM_PRESETS) npreset=NUM_PRESETS-1;
    for (int n=0;n<PRESET_SIZE;n++) changepar(n,presets[npreset][n]);
    Ppreset=npreset;
};


void Chorus::changepar(int npar,unsigned char value){
    switch(npar){
	case 0:	setvolume(value);
	        break;
	case 1:	setpanning(value);
	        break;
	case 2:	lfo.Pfreq=value;
	        lfo.updateparams();
		break;	
	case 3:	lfo.Prandomness=value;
	        lfo.updateparams();
		break;	
	case 4:	lfo.PLFOtype=value;
	        lfo.updateparams();
		break;	
	case 5:	lfo.Pstereo=value;
	        lfo.updateparams();
		break;	
	case 6:	setdepth(value);
	        break;
	case 7:	setdelay(value);
	        break;
	case 8:	setfb(value);
	        break;
	case 9:	setlrcross(value);
	        break;
	case 10:if (value>1) value=1;
		Pflangemode=value;
	        break;
	case 11:if (value>1) value=1;
	        Poutsub=value;
		break;
    };
};

unsigned char Chorus::getpar(int npar){
    switch (npar){
	case 0:	return(Pvolume);
		break;
	case 1:	return(Ppanning);
		break;
	case 2:	return(lfo.Pfreq);
		break;
	case 3:	return(lfo.Prandomness);
		break;
	case 4:	return(lfo.PLFOtype);
		break;
	case 5:	return(lfo.Pstereo);
		break;
	case 6:	return(Pdepth);
		break;
	case 7:	return(Pdelay);
		break;
	case 8:	return(Pfb);
		break;
	case 9:	return(Plrcross);
		break;
	case 10:return(Pflangemode);
		break;
	case 11:return(Poutsub);
		break;
	default:return (0);
    };
    
};