aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/ops_search.cpp
blob: cf7e7df3da31953282a780fdc0e10b7380673c80 (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
206
207
208
209
210
211
212
213
/* 

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_search.h"
#include "util.h"
#include "oploop.h"


BL VecOp::d_search(OpParam &p) 
{ 
	const I off = p.srch.offs;
	const S val = p.srch.val;
	const R cur = p.rsdt[off];

	I i,ofl = -1,ofr = -1;
	
    if(p.srch.incl && cur == val) {
        // if @incl attribute is set and current sample matches 
        ofl = ofr = off;
    }
    else {
	    if(p.srch.dir <= 0) {
		    BL y = cur >= val;
		    i = off-1;
		    _D_WHILE(i >= 0)
			    BL y2 = p.rsdt[i] >= val;
			    if(y != y2) {
				    if(p.srch.slope <= 0 && y2) break; 
				    if(p.srch.slope >= 0 && !y2) break;
			    }
			    y = y2;
			    --i;
		    _E_WHILE

		    if(i >= 0) ofl = i;
	    }

	    if(p.srch.dir >= 0) {
		    BL y = cur >= val;
		    i = off+1; 
		    _D_WHILE(i < p.frames)
			    BL y2 = p.rsdt[i] >= val;
			    if(y != y2) {
				    if(p.srch.slope <= 0 && !y2) break;
				    if(p.srch.slope >= 0 && y2) break;
			    }
			    y = y2;
			    ++i;
		    _E_WHILE

		    if(i < p.frames) ofr = i;
	    }
    }

	if(!p.srch.dir) {
		if(ofl >= 0) {
			p.srch.dif = ofl-off;
			if(ofr >= 0 && abs(p.srch.dif) < abs(ofr-off)) p.srch.dif = ofr-off;
		}
		else 
			p.srch.dif = ofr >= 0?ofr-off:0;
	}
	else if(p.srch.dir > 0) 
		p.srch.dif = ofr >= 0?ofr-off:0;
	else
		p.srch.dif = ofl >= 0?ofl-off:0;

	return true;
}

Vasp *VaspOp::m_search(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL st) 
{ 
	Vasp *ret = NULL;
	if(src.Vectors() != 1) 
		post("%s - Need exactly one vector in vasp!",p.opName());
	else if(arg.CanbeFloat() || (arg.IsList() && arg.GetList().Count() >= 1)) {
		I fr = src.Frames();
		I o = src.Vector(0).Offset();

        VBuffer *buf = src.Buffer(0);
        I sz = buf->Frames();
        delete buf;

		CVasp all(src);
		if(st) {
			// search start point
			p.srch.offs = o;

			// set bounds of search buffer
			all.Offset(0);
			all.Frames(fr+o);  // all frames of buffer
		}
		else {
			// search end point
			p.srch.offs = o+fr;
			// check if current offset is past buffer
			if(p.srch.offs >= sz) p.srch.offs = sz-1;

			// set bounds of search buffer
			all.Offset(o);
			all.Frames(sz-o);  // all frames of buffer			
		}

		RVecBlock *vecs = GetRVecs(p.opname,all,dst);
		if(vecs) {
			p.srch.val = arg.IsList()?flext::GetAFloat(arg.GetList()[0]):arg.GetAFloat();

			ret = DoOp(vecs,VecOp::d_search,p);

			if(st) o += p.srch.dif,fr -= p.srch.dif;
			else fr += p.srch.dif;

			if(ret) {
				ret->Offset(o);
				ret->Frames(fr);
				ret->Frames(ret->ChkFrames()); // What's that????
			}

			delete vecs;
		}
	}
	else
		post("%s - no arguments: no operation",p.opName());

	return ret;
}


class vasp_search:
	public vasp_anyop
{																				
	FLEXT_HEADER_S(vasp_search,vasp_anyop,Setup)
public:			
	
	vasp_search(I argc,t_atom *argv): 
		vasp_anyop(argc,argv,VASP_ARG_R(0),false,XletCode(xlet_float,0)),
		slope(0),dir(0),incl(false)
	{}

	static V Setup(t_classid c)
	{
		FLEXT_CADDATTR_VAR1(c,"dir",dir);
		FLEXT_CADDATTR_VAR1(c,"slope",slope);
		FLEXT_CADDATTR_VAR1(c,"incl",incl);
	}

	virtual Vasp *do_work(OpParam &p) = 0;
		
	virtual Vasp *tx_work(const Argument &arg) 
	{ 
		OpParam p(thisName(),1);													
		p.srch.dir = dir;
		p.srch.slope  = slope;
        p.srch.incl = incl;

		Vasp *ret = do_work(p);
		if(ret) ToOutFloat(1,p.srch.dif);
		return ret;
	}

protected:
	I dir,slope;
    BL incl;

private:
	FLEXT_ATTRVAR_I(dir)
	FLEXT_ATTRVAR_I(slope)
	FLEXT_ATTRVAR_B(incl)
};																				


class vasp_soffset:
	public vasp_search
{																				
	FLEXT_HEADER(vasp_soffset,vasp_search)
public:			
	vasp_soffset(I argc,t_atom *argv): vasp_search(argc,argv) {}
	virtual Vasp *do_work(OpParam &p) 
	{ 
		CVasp cdst(dst),cref(ref);
		return VaspOp::m_soffset(p,cref,arg,&cdst); 
	}

	virtual V m_help() { post("%s - Define starting point by searching for given value",thisName()); }
};																				
VASP_LIB_V("vasp.offset= vasp.o=",vasp_soffset)


class vasp_sframes:
	public vasp_search
{																				
	FLEXT_HEADER(vasp_sframes,vasp_search)
public:			
	vasp_sframes(I argc,t_atom *argv): vasp_search(argc,argv) {}
	virtual Vasp *do_work(OpParam &p) 
	{ 
		CVasp cdst(dst),cref(ref);
		return VaspOp::m_sframes(p,cref,arg,&cdst); 
	}

	virtual V m_help() { post("%s - Define vasp frame length by searching for given value",thisName()); }
};																				
VASP_LIB_V("vasp.frames= vasp.f=",vasp_sframes)