-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphaser.cpp
More file actions
125 lines (105 loc) · 3.36 KB
/
phaser.cpp
File metadata and controls
125 lines (105 loc) · 3.36 KB
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
/**
******************************************************************************
* @file phaser.c
* @author Xavier Halgand, thanks to Ross Bencina and music-dsp.org guys !
* @version
* @date december 2012
* @brief
******************************************************************************
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA.
*/
#include "phaser.h"
/*---------------------------------------------------------------------*/
#define MAX_RATE 7.f // in Hz
#define MIN_RATE 0.02f // in Hz
/*This defines the phaser stages
that is the number of allpass filters
*/
#define PH_STAGES 6
/*---------------------------------------------------------------------*/
static float old[PH_STAGES];
static float f_min, f_max;
static float swrate;
static float wet;
static float dmin, dmax; //range
static float fb; //feedback
static float lfoPhase;
static float lfoInc;
static float a1;
static float zm1;
/*---------------------------------------------------------------------*/
void PhaserInit(void) {
f_min = 200.f;
f_max = 1700.f;
swrate = 0.1f;
fb = 0.7f;
wet = 0.3f;
dmin = 2 * f_min / SAMPLERATE;
dmax = 2 * f_max / SAMPLERATE;
lfoInc = _2PI * swrate / SAMPLERATE;
}
/*---------------------------------------------------------------------*/
void Phaser_Rate_set(uint8_t val) {
swrate = (MAX_RATE - MIN_RATE) / MIDI_MAX * val + MIN_RATE;
lfoInc = _2PI * swrate / SAMPLERATE;
}
/*---------------------------------------------------------------------*/
void Phaser_Feedback_set(uint8_t val) {
fb = 0.999f * val / MIDI_MAX;
}
/*---------------------------------------------------------------------*/
void Phaser_Wet_set(uint8_t val) {
wet = val / MIDI_MAX;
}
/*---------------------------------------------------------------------*/
void PhaserRate(float rate) {
swrate = rate;
lfoInc = _2PI * swrate / SAMPLERATE;
}
/*---------------------------------------------------------------------*/
void PhaserFeedback(float fdb) {
fb = fdb;
}
/*---------------------------------------------------------------------*/
static float allpass(float yin, int ind) {
float yout;
yout = -yin * a1 + old[ind];
old[ind] = yout * a1 + yin;
return yout;
}
/*---------------------------------------------------------------------*/
float Phaser_compute(float xin) {
float yout;
int i;
float d;
//calculate and update phaser sweep lfo...
d = dmin
+ (dmax - dmin)
* ((sinetable[lrintf(ALPHA * lfoPhase)] + 1.f) * 0.5f);
lfoPhase += lfoInc;
if (lfoPhase >= _2PI)
lfoPhase -= _2PI;
//update filter coeffs
a1 = (1.f - d) / (1.f + d);
//calculate output
yout = allpass(xin + zm1 * fb, 0);
for (i = 1; i < PH_STAGES; i++) {
yout = allpass(yout, i);
}
zm1 = yout;
yout = (1 - wet) * xin + wet * yout;
return yout;
}