aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/ops_flt.cpp
blob: deecc5fff5e2f4431d545e2cc8575318ae59133f (plain)
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
/* 

VASP modular - vector assembling signal processor / objects for Max/MSP and PD

Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "main.h"
#include "ops_flt.h"
#include "opdefs.h"
#include "util.h"

// --- highpass ---------------------------------------

//! \todo handle carry
//! \todo handle yield

BL VecOp::d_fhp(OpParam &p) 
{ 
	if(p.revdir)
		post("%s - reversing operation direction due to overlap: opposite sample delay",p.opname);

/*
	R coef = (2*PI)/perln;
    if(coef > 1) coef = 1;
*/  
    const R coef = 1-p.flt.coef;
	const I arep = abs(p.flt.rep);
	S *src = p.rsdt,*dst = p.rddt;

	for(I ti = 0; ti < arep; ++ti) {
		register S v1;
		I i;

		// t+ direction
		for(i = 0,v1 = 0; i < p.frames; ++i) {
			register const S v0 = *src + coef*v1;
			*dst = v0-v1;
			v1 = v0;
			src += p.rss,dst += p.rds;
		}
		
		if(p.flt.rep < 0) {
			if(++ti == arep) break;

			// t- direction
			for(i = p.frames-1,v1 = 0; i >= 0; --i) {
				src -= p.rss,dst -= p.rds;
				register const S v0 = *src + coef*v1;
				*dst = v0-v1;
				v1 = v0;
			}
		}
	}

	return true;
}


// --- lowpass ---------------------------------------

//! \todo handle carry
//! \todo handle yield

BL VecOp::d_flp(OpParam &p) 
{ 
	if(p.revdir)
		post("%s - reversing operation direction due to overlap: opposite sample delay",p.opname);

/*
	R coef = (2*PI)/perln;
    if(coef > 1) coef = 1;
*/    
    
	const R coef = p.flt.coef,feed = 1-coef;
	const I arep = abs(p.flt.rep);

	for(I ti = 0; ti < arep; ++ti) {
		register S v1;
		I i;
		S *src = p.rsdt,*dst = p.rddt;

		// t+ direction
		for(i = 0,v1 = 0; i < p.frames; ++i) {
			v1 = *dst = coef* *src + feed*v1;
			src += p.rss,dst += p.rds;
		}
		
		if(p.flt.rep < 0) {
			if(++ti == arep) break;

			// t- direction
			for(i = p.frames-1,v1 = 0; i >= 0; --i) {
				src -= p.rss,dst -= p.rds;
				v1 = *dst = coef* *src + feed*v1;
			}
		}
	}

	return true;
}



Vasp *VaspOp::m_fhp(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL hp) 
{ 
	Vasp *ret = NULL;
	if(arg.IsList() && arg.GetList().Count() >= 1) {
		RVecBlock *vecs = GetRVecs(p.opname,src,dst);
		if(vecs) {
			p.flt.coef = 2*PI/flext::GetAFloat(arg.GetList()[0]);
		    if(p.flt.coef > 1) p.flt.coef = 1;
			p.flt.rep = arg.GetList().Count() >= 2?flext::GetAInt(arg.GetList()[1]):1;
			p.flt.rep = -p.flt.rep;  // fwd/bwd operation
/*
			if(p.SROvr()) {
				p.SDRRev();
				post("%s - reversing operation direction due to overlap: opposite sample delay",opnm);
			}	
*/
			ret = DoOp(vecs,hp?VecOp::d_fhp:VecOp::d_flp,p);

			delete vecs;
		}
	}

	return ret;
}

VASP_ANYOP("vasp.flp",flp,1,true,VASP_ARG(),"Passive low pass filter")
VASP_ANYOP("vasp.fhp",fhp,1,true,VASP_ARG(),"Passive high pass filter")


// --- integrate/differentiate

/*! \brief Integration
	\remark The delay of the result is +/- one sample, depending on the direction of the calculation 
	
	\todo different modes how to initialize first carry?
	\todo repetition count
*/
BL VecOp::d_int(OpParam &p) 
{ 
	if(p.revdir)
		post("%s - reversed operation direction due to overlap: opposite sample delay",p.opname);

	register S d = p.intdif.carry;
	register I i;
	_DE_LOOP(i,p.frames, ( *p.rddt = (d += *p.rsdt), p.rsdt += p.rss,p.rddt += p.rds ) )
	p.intdif.carry = d;
	return true; 
}

/*! \brief Differentiation
	\remark The delay of the result is +/- one sample, depending on the direction of the calculation 

	\todo different modes how to initialize first carry?
	\todo repetition count
*/
BL VecOp::d_dif(OpParam &p) 
{ 
	if(p.revdir)
		post("%s - reversed operation direction due to overlap: opposite sample delay",p.opname);

	register S d = p.intdif.carry,d1;
	register I i;
	_DE_LOOP(i,p.frames, ( d1 = *p.rsdt, *p.rddt = d1-d,d = d1, p.rsdt += p.rss,p.rddt += p.rds ) )
	p.intdif.carry = d;
	return true; 
}

/*! \brief Does vasp integration/differentiation.

	\param arg argument list 
	\param dst destination vasp (NULL for in-place operation)
	\param inv true for differentiation
	\return normalized destination vasp
*/
Vasp *VaspOp::m_int(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL inv) 
{ 
	Vasp *ret = NULL;
	RVecBlock *vecs = GetRVecs(p.opname,src,dst);
	if(vecs) {
		p.intdif.carry = 0,p.intdif.rep = 1;
		if(arg.IsList() && arg.GetList().Count() >= 1) p.intdif.rep = flext::GetAInt(arg.GetList()[0]);
		
		if(p.intdif.rep < 0) {
			post("%s - invalid repetition count (%i) -> set to 1",p.opname,p.intdif.rep);
			p.intdif.rep = 1;
		}
		
		ret = DoOp(vecs,inv?VecOp::d_dif:VecOp::d_int,p);
		delete vecs;
	}
	return ret;
}

VASP_ANYOP("vasp.int",int,0,true,VASP_ARG_I(1),"Integration") 
VASP_ANYOP("vasp.dif",dif,0,true,VASP_ARG_I(1),"Differentiation") 


VASP_UNARY("vasp.fix",fix,true,"Bashes denormals/NANs to zero")