File: TestUGens.cpp

package info (click to toggle)
supercollider 1%3A3.4.5-1wheezy1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 26,972 kB
  • sloc: cpp: 116,645; lisp: 64,914; ansic: 10,725; python: 3,548; perl: 766; ruby: 487; sh: 152; makefile: 117; xml: 13
file content (235 lines) | stat: -rw-r--r-- 5,560 bytes parent folder | download
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
/*
 *  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>

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

// Visual C++ doesn't have fpclassify (C99), so define it here if needed
#ifdef SC_WIN32
#include <float.h>
enum { FP_NORMAL, FP_NAN, FP_INFINITE, FP_SUBNORMAL };

static int sc_fpclassify(float x)
{
	int result;
	int kind = _fpclass((double)x);
	switch (kind)
	{
	case _FPCLASS_NINF:
		result = FP_INFINITE;
		break;
	case _FPCLASS_PINF:
		result = FP_INFINITE;
		break;
	case _FPCLASS_SNAN:
		result = FP_NAN;
		break;
	case _FPCLASS_QNAN:
		result = FP_NAN;
		break;
	case _FPCLASS_ND:
		result = FP_SUBNORMAL;
		break;
	case _FPCLASS_PD:
		result = FP_SUBNORMAL;
		break;
	default:
		result = FP_NORMAL;
	};
	return result;
}

#else
inline int sc_fpclassify(float x)
{
	return std::fpclassify(x);
}
#endif // SC_WIN32

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

static InterfaceTable *ft;

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

// declare unit generator functions
extern "C"
{
	void load(InterfaceTable *inTable);

	void CheckBadValues_Ctor(CheckBadValues* unit);
	void CheckBadValues_next(CheckBadValues* unit, int inNumSamples);
};

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

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

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


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 = sc_fpclassify(samp);
			switch (classification)
			{
				case FP_INFINITE:
					printf("Infinite number found in Synth %d, ID: %d\n", unit->mParent->mNode.mID, (int)id);
					ZXP(out) = 2;
					break;
				case FP_NAN:
					printf("NaN found in Synth %d, ID: %d\n", unit->mParent->mNode.mID, (int)id);
					ZXP(out) = 1;
					break;
				case FP_SUBNORMAL:
					printf("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(sc_fpclassify(samp));
			if(classification != unit->prevclass) {
				if(unit->sameCount == 0) {
					printf("CheckBadValues: %s found in Synth %d, ID %d\n",
						CheckBadValues_fpclassString(classification), unit->mParent->mNode.mID, (int)id);
				} else {
					printf("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 = sc_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";
#ifndef SC_WIN32
		case FP_ZERO:         return "zero";
#endif
		case FP_SUBNORMAL:    return "denormal";
		default:              return "unknown";
	}
}

#ifndef SC_WIN32
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;
	}
}
#else
inline int CheckBadValues_fold_fpclasses(int fpclass)
{
	return fpclass;
}
#endif

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

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

	DefineSimpleUnit(CheckBadValues);
}