File: TestUGens.cpp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (243 lines) | stat: -rw-r--r-- 7,344 bytes parent folder | download | duplicates (3)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 *  TestUGens.cpp
 *  Plugins
 *  Copyright (c) 2007 Scott Wilson <i@scottwilson.ca>. All rights reserved.
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 *  Created by Scott Wilson on 22/06/2007.
 *  Modified by James Harkins on 28/07/2007.
 *
 *
 */

#include "SC_PlugIn.h"
#include <cstdio>

//////////////////////////////////////////////////////////////////////////////////////////////////

static InterfaceTable* ft;

struct CheckBadValues : public Unit {
    long sameCount;
    int prevclass;
};

struct Sanitize : public Unit {};

// declare unit generator functions
static void CheckBadValues_Ctor(CheckBadValues* unit);
static void CheckBadValues_next(CheckBadValues* unit, int inNumSamples);

static const char* CheckBadValues_fpclassString(int fpclass);
inline int CheckBadValues_fold_fpclasses(int fpclass);

static void Sanitize_Ctor(Sanitize* unit);
static void Sanitize_next_aa(Sanitize* unit, int inNumSamples);
static void Sanitize_next_ak(Sanitize* unit, int inNumSamples);
static void Sanitize_next_kk(Sanitize* unit, int inNumSamples);

//////////////////////////////////////////////////////////////////////////////////////////////////

void CheckBadValues_Ctor(CheckBadValues* unit) {
    unit->prevclass = FP_NORMAL;
    unit->sameCount = 0;
    SETCALC(CheckBadValues_next);
    CheckBadValues_next(unit, 1);
}


void CheckBadValues_next(CheckBadValues* unit, int inNumSamples) {
    float* in = ZIN(0);
    float* out = ZOUT(0);
    float id = ZIN0(1);
    int post = (int)ZIN0(2);

    float samp;
    int classification;

    switch (post) {
    case 1: // post a line on every bad value
        LOOP1(
            inNumSamples, samp = ZXP(in); classification = std::fpclassify(samp); switch (classification) {
                case FP_INFINITE:
                    Print("Infinite number found in Synth %d, ID: %d\n", unit->mParent->mNode.mID, (int)id);
                    ZXP(out) = 2;
                    break;
                case FP_NAN:
                    Print("NaN found in Synth %d, ID: %d\n", unit->mParent->mNode.mID, (int)id);
                    ZXP(out) = 1;
                    break;
                case FP_SUBNORMAL:
                    Print("Denormal found in Synth %d, ID: %d\n", unit->mParent->mNode.mID, (int)id);
                    ZXP(out) = 3;
                    break;
                default:
                    ZXP(out) = 0;
            };);
        break;
    case 2:
        LOOP1(
            inNumSamples, samp = ZXP(in); classification = CheckBadValues_fold_fpclasses(std::fpclassify(samp));
            if (classification != unit->prevclass) {
                if (unit->sameCount == 0) {
                    Print("CheckBadValues: %s found in Synth %d, ID %d\n", CheckBadValues_fpclassString(classification),
                          unit->mParent->mNode.mID, (int)id);
                } else {
                    Print("CheckBadValues: %s found in Synth %d, ID %d (previous %d values were %s)\n",
                          CheckBadValues_fpclassString(classification), unit->mParent->mNode.mID, (int)id,
                          (int)unit->sameCount, CheckBadValues_fpclassString(unit->prevclass));
                };
                unit->sameCount = 0;
            };
            switch (classification) {
                case FP_INFINITE:
                    ZXP(out) = 2;
                    break;
                case FP_NAN:
                    ZXP(out) = 1;
                    break;
                case FP_SUBNORMAL:
                    ZXP(out) = 3;
                    break;
                default:
                    ZXP(out) = 0;
            };
            unit->sameCount++; unit->prevclass = classification;);
        break;
    default: // no post
        LOOP1(
            inNumSamples, samp = ZXP(in); classification = std::fpclassify(samp); switch (classification) {
                case FP_INFINITE:
                    ZXP(out) = 2;
                    break;
                case FP_NAN:
                    ZXP(out) = 1;
                    break;
                case FP_SUBNORMAL:
                    ZXP(out) = 3;
                    break;
                default:
                    ZXP(out) = 0;
            };);
        break;
    }
}

const char* CheckBadValues_fpclassString(int fpclass) {
    switch (fpclass) {
    case FP_NORMAL:
        return "normal";
    case FP_NAN:
        return "NaN";
    case FP_INFINITE:
        return "infinity";
    case FP_ZERO:
        return "zero";
    case FP_SUBNORMAL:
        return "denormal";
    default:
        return "unknown";
    }
}

inline int CheckBadValues_fold_fpclasses(int fpclass) {
    switch (fpclass) {
    case FP_ZERO:
        return FP_NORMAL; // a bit hacky. we mean "zero is fine too".
    default:
        return fpclass;
    }
}

///////////////////////////////////////////////////////////////////////////////////////

void Sanitize_Ctor(Sanitize* unit) {
    if (INRATE(0) == calc_FullRate) {
        if (INRATE(1) == calc_FullRate) {
            SETCALC(Sanitize_next_aa);
        } else {
            SETCALC(Sanitize_next_ak);
        }
    } else {
        SETCALC(Sanitize_next_kk);
    }
    Sanitize_next_kk(unit, 1);
}


void Sanitize_next_aa(Sanitize* unit, int inNumSamples) {
    float* in = IN(0);
    float* replace = IN(1);
    float* out = OUT(0);

    float samp;
    for (int i = 0; i < inNumSamples; i++) {
        samp = in[i];
        switch (std::fpclassify(samp)) {
        case FP_INFINITE:
        case FP_NAN:
        case FP_SUBNORMAL:
            out[i] = replace[i];
            break;
        default:
            out[i] = samp;
        };
    };
}

void Sanitize_next_ak(Sanitize* unit, int inNumSamples) {
    float* in = IN(0);
    float replace = IN0(1);
    float* out = OUT(0);

    float samp;
    for (int i = 0; i < inNumSamples; i++) {
        samp = in[i];
        switch (std::fpclassify(samp)) {
        case FP_INFINITE:
        case FP_NAN:
        case FP_SUBNORMAL:
            out[i] = replace;
            break;
        default:
            out[i] = samp;
        };
    };
}

void Sanitize_next_kk(Sanitize* unit, int inNumSamples) {
    float samp = IN0(0);
    float replace = IN0(1);

    switch (std::fpclassify(samp)) {
    case FP_INFINITE:
    case FP_NAN:
    case FP_SUBNORMAL:
        OUT0(0) = replace;
        break;
    default:
        OUT0(0) = samp;
    };
}

////////////////////////////////////////////////////////////////////

// the load function is called by the host when the plug-in is loaded
PluginLoad(Test) {
    ft = inTable;
    DefineSimpleUnit(CheckBadValues);
    DefineSimpleUnit(Sanitize);
}