-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOfflineBufferProcesses.cpp
218 lines (189 loc) · 5.95 KB
/
OfflineBufferProcesses.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
// BufRev, an NRT buffer samples swapper
// Pierre Alexandre Tremblay, 2018
// A digital debris of the FluCoMa project, funded by the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (grant agreement No 725899)
#include "SC_PlugIn.h"
static InterfaceTable *ft;
void BufRev(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
float *data = buf->data;
int size = buf->samples;
int chans = buf->channels;
int frames = buf->frames;
int halfframe = frames/2;
float temp;
int loadd,hiadd;
for (int i = 0; i < halfframe; ++i) {
for (int j = 0; j < chans; ++j) {
hiadd = j+size-((i+1)*chans);
loadd = j+(i*chans);
temp = data[hiadd];
data[hiadd] = data[loadd];
data[loadd] = temp;
}
}
}
void BufAdd(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
float *data = buf->data;
int size = buf->samples;
float offset = msg->getf(0.0);
for (int i = 0; i < size; ++i)
data[i] += offset;
}
void BufGain(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
float *data = buf->data;
int size = buf->samples;
float gain = msg->getf(-1.0);
for (int i = 0; i < size; ++i)
data[i] *= gain;
}
void BufRemoveDC(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
float *data = buf->data;
int chans = buf->channels;
int frames = buf->frames;
float coeff = msg->getf(0.995);
float previn, in;
double prevout, out;
for (int j = 0; j < chans; ++j) {
previn = data[j];
prevout = previn;
for (int i = 1; i < frames; ++i) {
in = data[(i*chans)+j];
out = in - previn + coeff * prevout;
data[(i*chans)+j] = out;
prevout = out;
previn = in;
}
}
}
void BufChunkSwap(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
float *data = buf->data;
int chans = buf->channels;
int frames = buf->frames;
// reading the arguments with default values
int srcStartAt = msg->geti(0);
int dstStartAt = msg->geti(-1);
int numFrames = msg->geti(0);
// should default value happen, retrieve them
if (dstStartAt == -1)
dstStartAt = frames - 1; // defaults to last sample
if (numFrames == 0)
numFrames = (frames * -1) / 2;
// define end writing frames (inclusive) and the incrementor value
int AbsNumFrames = std::abs(numFrames);
int dstStep = numFrames == AbsNumFrames ? 1 : -1;
int srcStopAt = srcStartAt + AbsNumFrames - 1;
int dstStopAt = dstStartAt + numFrames - dstStep;
if (srcStartAt < 0 || srcStartAt >= frames) {
Print("chunkSwap is not happy because the source starting is outside the buffer.\n");
return;
}
else if (srcStopAt < 0 || srcStopAt >= frames) {
Print("chunkSwap is not happy because the source finishing is outside the buffer.\n");
return;
}
else if (dstStartAt < 0 || dstStartAt >= frames) {
Print("chunkSwap is not happy because the destination starting is outside the buffer.\n");
return;
}
else if (dstStopAt < 0 || dstStopAt >= frames) {
Print("chunkSwap is not happy because the destination finishing is outside the buffer.\n");
return;
}
float temp;
int srcAdd,dstAdd;
for (int i = srcStartAt; i <= srcStopAt; ++i) {
for (int j = 0; j < chans; ++j) {
dstAdd = j+(dstStartAt*chans);
srcAdd = j+(i*chans);
// Print("%d %d %d %d\n", i, j, hiadd, loadd);
temp = data[dstAdd];
data[dstAdd] = data[srcAdd];
data[srcAdd] = temp;
}
dstStartAt+= dstStep;
}
}
void BufWaveSetCopyTo(World *world, struct SndBuf *buf, struct sc_msg_iter *msg)
{
int frames1 = buf->frames;
int channels1 = buf->channels;
uint32 bufnum2 = msg->geti();
int repetitions = msg->geti();
if (bufnum2 >= world->mNumSndBufs){
Print("waveSetCopyTo is not happy because the source buffer does not exist.\n");
return;
}
SndBuf* buf2 = world->mSndBufs + bufnum2;
if (buf2->data == buf->data){
Print("waveSetCopyTo is not happy because the source buffer is the same as the destination buffer.\n");
return;
}
int frames2 = buf2->frames;
int channels2 = buf2->channels;
if (channels1 != channels2) {
Print("waveSetCopyTo is not happy because the source buffer has a different channel count than the destination buffer.\n");
return;
}
// checks if default value (0) or error in quantity and sets to a filling behaviour or at least one
if (repetitions < 1){
repetitions = int(frames1 / frames2);
if (repetitions < 1)
repetitions = 1;
}
//for each channels
for (int j=0;j<channels2;j++){
long lastXadd = -1; //set start frame as invalid address flag
short prevpol = (buf2->data[j] > 0); //set the previous sample polarity as the first sample
long writeindex = 0; // set the writing index at the start of the buffer
long wavesetlenght;
//interates through the source samples
for (int i=0;i<frames2;i++){
//if previously positive...
if (prevpol){
// Print("in1\n");
//... and currently negative ...
if (buf2->data[(i*channels2)+j] < 0.0) {
// Print("in1-1\n");
// ... flag as being now in negativeland and exit.
prevpol = 0;
}
} else {
// if previously in negativeland...
// Print("in2\n");
if (buf2->data[(i*channels2)+j] >= 0.0) {
// ...and now being zero or up, so we write
// Print("in2-2\n");
// check it is not the first zero crossing
if (lastXadd >=0) {
// check if the lenght will be too long for all repetitions
wavesetlenght = i - lastXadd;
if (((wavesetlenght*repetitions)+ writeindex) > frames1) break;
// write if enough place
for (int k = 0; k < repetitions; k++){
for (int l = 0; l < wavesetlenght; l++) {
buf->data[(writeindex*channels2)+j] = buf2->data[((lastXadd+l)*channels2)+j];
writeindex++;
}
}
}
// setting the flag and the new past
prevpol = 1;
lastXadd = i;
}
}
}
}
}
PluginLoad(OfflineBufferProcessesUGens) {
ft = inTable;
DefineBufGen("mul", BufGain);
DefineBufGen("add", BufAdd);
DefineBufGen("reverse", BufRev);
DefineBufGen("removeDC", BufRemoveDC);
DefineBufGen("chunkSwap", BufChunkSwap);
DefineBufGen("waveSetCopyTo", BufWaveSetCopyTo);
}