-
Notifications
You must be signed in to change notification settings - Fork 30
/
ChorusPatch.hpp
157 lines (123 loc) · 4.4 KB
/
ChorusPatch.hpp
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
#ifndef __ChorusPatch_hpp__
#define __ChorusPatch_hpp__
#include "StompBox.h"
#include "CircularBuffer.hpp"
#define CHORUS_PATCH_REQUEST_BUFFER_SIZE 32768
class ChorusPatch : public Patch {
private:
CircularBuffer delayBuffer;
int32_t oldReadIndex1;
int32_t oldReadIndex2;
int32_t oldReadIndex3;
float angle;
float maxDelay;
public:
ChorusPatch(){
registerParameter(PARAMETER_A, "RATE");
registerParameter(PARAMETER_B, "DEPTH");
registerParameter(PARAMETER_C, "WET/DRY ");
registerParameter(PARAMETER_D, "VOICES");
AudioBuffer* buffer = createMemoryBuffer(1, CHORUS_PATCH_REQUEST_BUFFER_SIZE);
delayBuffer.initialise(buffer->getSamples(0), buffer->getSize());
angle = 0.f;
maxDelay = 0.05 * delayBuffer.getSize();// 50ms, anything more and it will be a noticeable echo
}
void processAudio(AudioBuffer &buffer){
int numVoices = 1;
float twoVoices = 1.0/3;
float threeVoices = 2 * (1.0/3);
int32_t voiceWidth = 0;
float rate, depth, mix, voicesParam;
double sampleRate = getSampleRate();
rate = getParameterValue(PARAMETER_A) * 3 ;
depth = getParameterValue(PARAMETER_B) * maxDelay; // get the depth relative to the maximum delay
mix = getParameterValue(PARAMETER_C);
voicesParam = getParameterValue(PARAMETER_D);
float sineWave;
float* x = buffer.getSamples(0);
int size = buffer.getSize();
if (voicesParam < twoVoices)
{
numVoices = 1;
}
if(voicesParam >= twoVoices && voicesParam < threeVoices)
{
numVoices = 2;
voiceWidth = 20 * (threeVoices - voicesParam);
}
if(voicesParam >=threeVoices && voicesParam <= 1)
{
numVoices = 3;
voiceWidth = 20 * (1.0 - voicesParam);
}
//================LFO=================//
float inc = rate * (2 * M_PI) *buffer.getSize()/sampleRate ; // The incrementing sine is calculate once per block
sineWave = sinf(angle) * depth/2 ;
angle += inc;
if (angle > 2 * M_PI)
{
angle -= 2 * M_PI;
}
//===============================//
int32_t newReadIndex1 = 0;
int32_t newReadIndex2 = 0;
int32_t newReadIndex3 = 0;
switch (numVoices)
{
case 1:
{
newReadIndex1 = sineWave;
break;
}
case 2:
{
newReadIndex1 = sineWave;
newReadIndex2 = sineWave + voiceWidth;
if (newReadIndex2 > maxDelay)
{
newReadIndex2 = maxDelay - (newReadIndex2 - maxDelay);
}
break;
}
case 3:
{
newReadIndex1 = sineWave;
newReadIndex2 = sineWave + voiceWidth;
if (newReadIndex2 > maxDelay)
{
newReadIndex2 = maxDelay - (newReadIndex2 - maxDelay);
}
newReadIndex1 = sineWave;
newReadIndex2 = sineWave + voiceWidth;
if (newReadIndex2 > maxDelay)
{
newReadIndex2 = maxDelay - (newReadIndex2 - maxDelay);
}
newReadIndex3 = sineWave - voiceWidth;
if (newReadIndex3 < 0)
{
newReadIndex3 = 0 + voiceWidth;
}
break;
}
default:
{
newReadIndex1 = sineWave;
break;
}
}
newReadIndex1 = sineWave;
for (int n = 0; n < size; n++)
{
delayBuffer.write(x[n]);
float voice1 = (delayBuffer.read(maxDelay/2 + oldReadIndex1)*(size-1-n) + delayBuffer.read(maxDelay/2 + newReadIndex1)*n)/size;
float voice2 = (delayBuffer.read(maxDelay/2 + oldReadIndex2)*(size-1-n) + delayBuffer.read(maxDelay/2 + newReadIndex2)*n)/size;
float voice3 = (delayBuffer.read(maxDelay/2 + oldReadIndex3)*(size-1-n) + delayBuffer.read(maxDelay/2 + newReadIndex3)*n)/size;
x[n] = (voice1+voice2+voice3) * (1 - mix) + x[n] * mix;
}
oldReadIndex1 = newReadIndex1;
oldReadIndex2 = newReadIndex2;
oldReadIndex3 = newReadIndex3;
}
};
#endif // __ChorusPatch_hpp__