-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDspClip.cpp
75 lines (67 loc) · 2.51 KB
/
DspClip.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
/*
* Copyright 2009,2010,2011,2012 Reality Jockey, Ltd.
* http://rjdj.me/
*
* This file is part of ZenGarden.
*
* ZenGarden is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ZenGarden 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ZenGarden. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ArrayArithmetic.h"
#include "DspClip.h"
class PdGraph;
MessageObject *DspClip::newObject(PdMessage *initMessage, PdGraph *graph) {
return new DspClip(initMessage, graph);
}
DspClip::DspClip(PdMessage *initMessage, PdGraph *graph) : DspObject(3, 1, 0, 1, graph) {
lowerBound = initMessage->isFloat(0) ? initMessage->getFloat(0) : -1.0f;
upperBound = initMessage->isFloat(1) ? initMessage->getFloat(1) : 1.0f;
processFunction = &processScalar;
processFunctionNoMessage = &processScalar;
}
DspClip::~DspClip() {
// nothing to do
}
std::string DspClip::toString() {
char str[snprintf(NULL, 0, "%s %g %g", getObjectLabel(), lowerBound, upperBound)+1];
snprintf(str, sizeof(str), "%s %g %g", getObjectLabel(), lowerBound, upperBound);
return str;
}
void DspClip::processMessage(int inletIndex, PdMessage *message) {
switch (inletIndex) {
case 1: if (message->isFloat(0)) lowerBound = message->getFloat(0); break; // set the lower bound
case 2: if (message->isFloat(0)) upperBound = message->getFloat(0); break; // set the upper bound
default: break;
}
}
void DspClip::processScalar(DspObject *dspObject, int fromIndex, int toIndex) {
DspClip *d = reinterpret_cast<DspClip *>(dspObject);
#if __APPLE__
vDSP_vclip(d->dspBufferAtInlet[0]+fromIndex, 1, &(d->lowerBound), &(d->upperBound),
d->dspBufferAtOutlet[0]+fromIndex, 1, toIndex-fromIndex);
#else
float *in = d->dspBufferAtInlet[0];
float *out = d->dspBufferAtOutlet[0];
for (int i = fromIndex; i < toIndex; i++) {
if (in[i] < d->lowerBound) {
out[i] = d->lowerBound;
} else if (in[i] > d->upperBound) {
out[i] = d->upperBound;
} else {
out[i] = in[i];
}
}
#endif
}