-
Notifications
You must be signed in to change notification settings - Fork 2
/
effect_ensemble.cpp
226 lines (191 loc) · 7.18 KB
/
effect_ensemble.cpp
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
/* Audio Library for Teensy 3.X
* Copyright (c) 2019, Alexander Davis [email protected]
*
* Permission is hereby granted for non-commercial use only.
* You may not use this code in any product offered for sale without the
* expresss written approval of the author.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// My take on a string machine ensemble chorus sound like the Roland RS-202
// or Lowrey Symphonic Strings.
// It takes the approach of 6.0 Hz and 0.6 Hz sinewaves making up an LFO,
// which then modulates three delay lines 120 degrees apart in the LFO waveform.
#include <Arduino.h>
#include "effect_ensemble.h"
#include "utility/dspinst.h"
#include "arm_math.h"
AudioEffectEnsemble::AudioEffectEnsemble() : AudioStream(1, inputQueueArray)
{
memset(delayBuffer, 0, sizeof(delayBuffer));
memset(lfoTable, 0, sizeof(lfoTable));
// input index
inIndex = 0;
// output indexes
// default to center of buffer
outIndex1 = 512;
outIndex2 = 512;
outIndex3 = 512;
// lfo index
// seprated by thirds to approximate 120 degree phase relationship
lfoIndex1 = 0;
lfoIndex2 = 245;
lfoIndex3 = 490;
// lfo rate counter
lfoCount = 0;
// input index offset
offset1 = 0;
offset2 = 0;
offset3 = 0;
offsetIndex1 = 0;
offsetIndex2 = 0;
offsetIndex3 = 0;
offsetIndex1B = 0;
offsetIndex2B = 0;
offsetIndex3B = 0;
// generate the LFO wavetable
for (iC = 0; iC < LFO_SAMPLES; iC++)
{
lfoTable[iC] = round(((sin(((2.0 * M_PI)/LFO_SAMPLES) * iC) * LFO_RANGE) / 2.0) + (((sin(((20.0 * M_PI)/LFO_SAMPLES) * iC)) * LFO_RANGE) / 3.0));
}
return;
}
// TODO: move this to one of the data files, use in output_adat.cpp, output_tdm.cpp, etc
static const audio_block_t zeroblock = {
0, 0, 0, {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#if AUDIO_BLOCK_SAMPLES > 16
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 32
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 48
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 64
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 80
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 96
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
#if AUDIO_BLOCK_SAMPLES > 112
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
#endif
} };
void AudioEffectEnsemble::update(void)
{
const audio_block_t *block;
audio_block_t *outblock;
audio_block_t *outblockB;
uint16_t i;
outblock = allocate();
outblockB = allocate();
if ((!outblock) || (!outblockB)) {
audio_block_t *tmp = receiveReadOnly(0);
if (tmp) release(tmp);
return;
}
block = receiveReadOnly(0);
if (!block)
block = &zeroblock;
// buffer the incoming block
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++)
{
// wrap the input index
inIndex++;
if (inIndex > (ENSEMBLE_BUFFER_SIZE - 1))
inIndex = 0;
delayBuffer[inIndex] = block->data[i];
}
// re-load the block with the delayed data
for (i=0; i < AUDIO_BLOCK_SAMPLES; i++)
{
// advance the wavetable indexes every COUNTS_PER_LFO
// so the LFO modulates at the correct rate
lfoCount++;
if (lfoCount > COUNTS_PER_LFO)
{
// wrap the lfo index
lfoIndex1++;
if (lfoIndex1 > (LFO_SIZE - 1))
lfoIndex1 = 0;
lfoIndex2++;
if (lfoIndex2 > (LFO_SIZE - 1))
lfoIndex2 = 0;
lfoIndex3++;
if (lfoIndex3 > (LFO_SIZE - 1))
lfoIndex3 = 0;
// reset the counter
lfoCount = 0;
}
// wrap the output index
outIndex1++;
if (outIndex1 > (ENSEMBLE_BUFFER_SIZE - 1))
outIndex1 = 0;
outIndex2++;
if (outIndex2 > (ENSEMBLE_BUFFER_SIZE - 1))
outIndex2 = 0;
outIndex3++;
if (outIndex3 > (ENSEMBLE_BUFFER_SIZE - 1))
outIndex3 = 0;
// get the delay from the wavetable
offset1 = lfoTable[lfoIndex1];
offset2 = lfoTable[lfoIndex2];
offset3 = lfoTable[lfoIndex3];
// add the delay to the buffer index to get the delay index
offsetIndex1 = outIndex1 + offset1;
offsetIndex2 = outIndex2 + offset2;
offsetIndex3 = outIndex3 + offset3;
offsetIndex1B = outIndex1 + offset1 + PHASE_90;
offsetIndex2B = outIndex2 + offset2 + PHASE_90;
offsetIndex3B = outIndex3 + offset3 + PHASE_90;
// wrap the index if it goes past the end of the buffer
if (offsetIndex1 > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex1 = offsetIndex1 - ENSEMBLE_BUFFER_SIZE;
if (offsetIndex2 > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex2 = offsetIndex2 - ENSEMBLE_BUFFER_SIZE;
if (offsetIndex3 > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex3 = offsetIndex3 - ENSEMBLE_BUFFER_SIZE;
// wrap the index if it goes past the buffer the other way
if (offsetIndex1 < 0)
offsetIndex1 = ENSEMBLE_BUFFER_SIZE + offsetIndex1;
if (offsetIndex2 < 0)
offsetIndex2 = ENSEMBLE_BUFFER_SIZE + offsetIndex2;
if (offsetIndex3 < 0)
offsetIndex3 = ENSEMBLE_BUFFER_SIZE + offsetIndex3;
// wrap the index if it goes past the end of the buffer
if (offsetIndex1B > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex1B = offsetIndex1B - ENSEMBLE_BUFFER_SIZE;
if (offsetIndex2B > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex2B = offsetIndex2B - ENSEMBLE_BUFFER_SIZE;
if (offsetIndex3B > (ENSEMBLE_BUFFER_SIZE - 1))
offsetIndex3B = offsetIndex3B - ENSEMBLE_BUFFER_SIZE;
// wrap the index if it goes past the buffer the other way
if (offsetIndex1B < 0)
offsetIndex1B = ENSEMBLE_BUFFER_SIZE + offsetIndex1B;
if (offsetIndex2B < 0)
offsetIndex2B = ENSEMBLE_BUFFER_SIZE + offsetIndex2B;
if (offsetIndex3B < 0)
offsetIndex3B = ENSEMBLE_BUFFER_SIZE + offsetIndex3B;
// combine delayed samples into output
// add the delayed and scaled samples
outblock->data[i] = int(round((delayBuffer[offsetIndex1] + delayBuffer[offsetIndex2] + delayBuffer[offsetIndex3]) / 3.0));
outblockB->data[i] = int(round((delayBuffer[offsetIndex1B] + delayBuffer[offsetIndex2B] + delayBuffer[offsetIndex3B]) / 3.0));
}
transmit(outblock, 0);
transmit(outblockB, 1);
release(outblock);
release(outblockB);
if (block != &zeroblock) release((audio_block_t *)block);
return;
}