aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/obj_split.cpp
blob: c39a15ca1468875c84029f456086feb6143d555c (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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* 

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


/*! \class vasp_split
	\remark \b vasp.split
	\brief Splits a vasp into a number of vectors and the remainder.
	\since 0.0.1
	\param cmdln.1 int - number of vectors to split vasp into (excl. one for the remainder vectors)
	\param inlet.1 vasp - is stored and triggered
	\param inlet.1 bang - triggers output
	\param inlet.1 set - vasp to be stored 
	\retval outlet.n vasp - vector of stored vasp
	\retval outlet.+ vasp - remainder of stored vasp

	\note if there is no remainder outputs a bang
*/
class vasp_split:
	public vasp_op
{
	FLEXT_HEADER(vasp_split,vasp_op)

public:
	vasp_split(I argc,const t_atom *argv)
	{
		I cnt = -1;
		if(argc) {
			if(CanbeInt(argv[0])) cnt = GetAInt(argv[0]);
			if(cnt <= 1) {
				post("%s - integer argument invalid: set to 2",thisName());
				cnt = 2;
			}
		}
		else cnt = 2;

		AddInAnything();
		AddOutAnything(cnt+1);
	}

	virtual V m_bang() 
	{ 
		if(!ref.Ok()) {
			post("%s - Invalid vasp!",thisName());
			return;
		}

		I outs = CntOut()-1,rem = ref.Vectors()-outs;
		for(I i = min(outs,ref.Vectors())-1; i >= 0; --i) {
			Vasp v(ref.Frames(),ref.Vector(i));
			ToOutVasp(i,v);
		}
		if(rem > 0) {
			Vasp v(ref.Frames(),ref.Vector(outs));
			for(I i = 1; i < rem; ++i) v.AddVector(ref.Vector(outs+i));
			ToOutVasp(outs,v);
		}
		else
			ToOutBang(outs);
	}

	virtual V m_help() { post("%s - Split a vasp into its vectors",thisName()); }
};

FLEXT_LIB_V("vasp.split, vasp",vasp_split)


/*! \class vasp_join
	\remark \b vasp.join
	\brief Joins several vasps into one.
	\since 0.0.1
	\param cmdln.1 int - number of vasp slots
	\param inlet.1 vasp - is stored and output triggered
	\param inlet.1 bang - triggers output
	\param inlet.1 set - vasp to be stored 
	\param inlet.1 reset - clears slots
	\param inlet.+n vasp - is stored in this slot 
	\retval outlet vasp - joined vasp

	The several vectors of the several vasps are all joined into one vasp.

	\note On different vasp frame count the minmum frame count is taken.
	\note The latest vector input to a slot is taken for the resulting vasp
*/
class vasp_join:
	public vasp_tx
{
	FLEXT_HEADER_S(vasp_join,vasp_tx,Setup)

public:
	vasp_join(I argc,const t_atom *argv):
		cnt(-1),vi(NULL)
	{
		if(argc) {
			if(CanbeInt(argv[0])) cnt = GetAInt(argv[0]);
			if(cnt <= 1) {
				post("%s - integer argument invalid: set to 2",thisName());
				cnt = 2;
			}
		}
		else cnt = 2;

		vi = new Vasp *[cnt-1];
		for(I i = 0; i < cnt-1; ++i) vi[i] = NULL;

		AddInAnything(cnt);
		AddOutAnything();
	}

	~vasp_join()	{ if(vi) delete[] vi; }

	static V Setup(t_classid c)
	{
		FLEXT_CADDMETHOD_(c,0,"reset",m_reset);
	}

	virtual Vasp *x_work() { 
		CVasp *ret = new CVasp(ref);
		for(I i = 0; i < cnt-1; ++i) if(vi[i]) *ret += *vi[i];
		return ret;
	}

	V m_reset() 
	{ 
		ref.Clear();
		for(I i = 0; i < cnt-1; ++i) if(vi[i]) { delete vi[i]; vi[i] = NULL; }
	}

	virtual bool m_method_(I inlet,const t_symbol *s,I argc,const t_atom *argv)
	{
		if(inlet > 0 && s == sym_vasp) {
			if(vi[inlet-1]) delete vi[inlet-1];
			vi[inlet-1] = new Vasp(argc,argv);
			return true;
		}
		else
			return vasp_tx::m_method_(inlet,s,argc,argv);
	}

	virtual V m_help() { post("%s - Join several vasps into one",thisName()); }
private:
	I cnt;
	Vasp **vi;

	FLEXT_CALLBACK(m_reset)
};

FLEXT_LIB_V("vasp.join, vasp",vasp_join)



/*! \class vasp_spit
	\remark \b vasp.spit
	\brief Spit out vectors of a vasp consecutively.
	\since 0.0.1
	\param inlet.1 vasp - is stored and triggered
	\param inlet.1 bang - triggers output
	\param inlet.1 set - vasp to be stored 
	\retval outlet.n vasp - vectors of stored vasp
	\retval outlet.+ bang - triggered after last spit 
*/
class vasp_spit:
	public vasp_op
{
	FLEXT_HEADER(vasp_spit,vasp_op)

public:
	vasp_spit(I argc,const t_atom *argv)
	{
		I n = 1;
		if(argc >= 1) n = GetAInt(argv[0]);
		if(n < 1) {
			post("%s - illegal outlet count (%i) -> set to 1",thisName(),n);
			n = 1;
		}

		AddInAnything();
		AddOutAnything(n);
		AddOutBang();
	}

	virtual V m_bang() 
	{ 
		if(!ref.Ok()) {
			post("%s - Invalid vasp!",thisName());
			return;
		}

		I outs = CntOut()-1,rem = ref.Vectors();
		for(I vi = 0; rem;) {
			I r = min(rem,outs);
			for(I i = 0; i < r; ++i) {
				Vasp v(ref.Frames(),ref.Vector(vi+i));
				ToOutVasp(outs-1-i,v);
			}
			vi += r;
			rem -= r;
		}
		ToOutBang(outs);
	}

	virtual V m_help() { post("%s - Spit out vectors of a vasp",thisName()); }
};

FLEXT_LIB_V("vasp.spit, vasp",vasp_spit)


/*! \class vasp_gather
	\remark \b vasp.gather
	\brief Gathers several consecutive vasps into one.
	\since 0.0.1
	\param cmdln.1 int - number of vasp slots
	\param inlet.1 vasp - is stored and output triggered
	\param inlet.1 bang - triggers output
	\param inlet.1 set - sets result vasp 
	\param inlet.1 reset - clears result
	\param inlet.2 vasp - add to result vasp
	\retval outlet vasp - gathered vasp

	The several incoming vectors are all gathered into one vasp.

	\note On different vasp frame count the minimum frame count is taken.
*/
class vasp_gather:
	public vasp_tx
{
	FLEXT_HEADER_S(vasp_gather,vasp_tx,Setup)

public:
	vasp_gather(I argc,const t_atom *argv)
	{
		cnt = 0;
		if(argc >= 1) cnt = GetAInt(argv[0]);
		if(cnt < 0) {
			post("%s - illegal count (%i) -> set to 0 (triggered mode)",thisName(),cnt);
			cnt = 0;
		}
		rem = cnt;

		AddInAnything(2);
		AddOutAnything();
	}

	static V Setup(t_classid c)
	{
		FLEXT_CADDMETHOD_(c,0,"reset",m_reset);
		FLEXT_CADDMETHOD_(c,1,"vasp",m_add);
	}

	virtual Vasp *x_work() 
	{ 
		CVasp *ret = new CVasp(ref); 
		*ret += cdst;
		m_reset(); 
		return ret; 
	}

	V m_reset() { ref.Clear(); cdst.Clear(); rem = cnt; }

	virtual I m_set(I argc,const t_atom *argv) { rem = cnt; return vasp_tx::m_set(argc,argv); }

	V m_add(I argc,const t_atom *argv) 
	{ 
		cdst += Vasp(argc,argv);
		if(cnt && !--rem) m_bang();
	}

	virtual V m_help() { post("%s - Gather several vasps into one",thisName()); }
private:
	I cnt,rem;
	CVasp cdst;

	FLEXT_CALLBACK(m_reset)
	FLEXT_CALLBACK_V(m_add)
};

FLEXT_LIB_V("vasp.gather, vasp",vasp_gather)