summaryrefslogtreecommitdiff
path: root/muse/synti/zynaddsubfx/Effects/EQ.C
blob: 8180a655cd8cf91e903c2ebe95133dfcf84e620e (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
/*
  ZynAddSubFX - a software synthesizer
 
  EQ.C - EQ effect
  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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "EQ.h"

EQ::EQ(int insertion_,REALTYPE *efxoutl_,REALTYPE *efxoutr_){
    efxoutl=efxoutl_;
    efxoutr=efxoutr_;
    insertion=insertion_;
    filterpars=NULL;
    
    for (int i=0;i<MAX_EQ_BANDS;i++){
	filter[i].Ptype=0;
	filter[i].Pfreq=64;
	filter[i].Pgain=64;
	filter[i].Pq=64;
	filter[i].Pstages=0;
	filter[i].l=new AnalogFilter(6,1000.0,1.0,0);
	filter[i].r=new AnalogFilter(6,1000.0,1.0,0);
    };
    //default values
    Ppreset=0;
    Pvolume=50;
    
    setpreset(Ppreset);    	   
    cleanup();
};

EQ::~EQ(){
};

/*
 * Cleanup the effect
 */
void EQ::cleanup(){
    for (int i=0;i<MAX_EQ_BANDS;i++){
	filter[i].l->cleanup();
	filter[i].r->cleanup();
    };
};



/*
 * Effect output
 */
void EQ::out(REALTYPE *smpsl,REALTYPE *smpsr){
    int i;
    for (i=0;i<SOUND_BUFFER_SIZE;i++){
	efxoutl[i]=smpsl[i]*volume;
	efxoutr[i]=smpsr[i]*volume;
    };
    
    for (i=0;i<MAX_EQ_BANDS;i++){
	if (filter[i].Ptype==0) continue;
	filter[i].l->filterout(efxoutl);
	filter[i].r->filterout(efxoutr);
    };
};


/*
 * Parameter control
 */
void EQ::setvolume(unsigned char Pvolume){
    this->Pvolume=Pvolume;

    outvolume=pow(0.005,(1.0-Pvolume/127.0))*10.0;
    if (insertion==0) {
	volume=1.0;
    } else {
	volume=outvolume;
    };

};


void EQ::setpreset(unsigned char npreset){
    const int PRESET_SIZE=1;
    const int NUM_PRESETS=2;
    unsigned char presets[NUM_PRESETS][PRESET_SIZE]={
	//EQ 1
	{67},
	//EQ 2
	{67}};

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


void EQ::changepar(int npar,unsigned char value){
    switch (npar){
	case 0: setvolume(value);
		break;
    };
    if (npar<10) return;
    
    int nb=(npar-10)/5;//number of the band (filter)
    if (nb>=MAX_EQ_BANDS) return;
    int bp=npar%5;//band paramenter
    
    REALTYPE tmp;
    switch(bp){
	case 0: if (value>9) value=0;//has to be changed if more filters will be added
	        filter[nb].Ptype=value;
		if (value!=0){
		    filter[nb].l->settype(value-1);
		    filter[nb].r->settype(value-1);
		};
		break;
	case 1: filter[nb].Pfreq=value;
		tmp=600.0*pow(30.0,(value-64.0)/64.0);
		filter[nb].l->setfreq(tmp);
		filter[nb].r->setfreq(tmp);
		break;
	case 2: filter[nb].Pgain=value;
		tmp=30.0*(value-64.0)/64.0;
		filter[nb].l->setgain(tmp);
		filter[nb].r->setgain(tmp);
		break;
	case 3: filter[nb].Pq=value;
		tmp=pow(30.0,(value-64.0)/64.0);
		filter[nb].l->setq(tmp);
		filter[nb].r->setq(tmp);
		break;
	case 4: if (value>=MAX_FILTER_STAGES) value=MAX_FILTER_STAGES-1;
		filter[nb].Pstages=value;
		filter[nb].l->setstages(value);
		filter[nb].r->setstages(value);
		break;
    };
};

unsigned char EQ::getpar(int npar){
    switch (npar){
	case 0: return(Pvolume);
		break;
    };

    if (npar<10) return(0);
    
    int nb=(npar-10)/5;//number of the band (filter)
    if (nb>=MAX_EQ_BANDS) return(0);
    int bp=npar%5;//band paramenter
    switch(bp){
	case 0: return(filter[nb].Ptype);
		break;
	case 1: return(filter[nb].Pfreq);
		break;
	case 2: return(filter[nb].Pgain);
		break;
	case 3: return(filter[nb].Pq);
		break;
	case 4: return(filter[nb].Pstages);
		break;
    };

    return(0);//in case of bogus parameter number
};




REALTYPE EQ::getfreqresponse(REALTYPE freq){
    REALTYPE resp=1.0;

    for (int i=0;i<MAX_EQ_BANDS;i++){
	if (filter[i].Ptype==0) continue;
	resp*=filter[i].l->H(freq);
    };
    return(rap2dB(resp*outvolume));
};