aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/flstk.cpp
blob: 85d52751e870d8bcbd60307bb07b6dc95dae7c21 (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
/* 

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 "flext.h"
#include "flstk.h"

flext_stk::flext_stk():
	inobjs(0),outobjs(0),
	inobj(NULL),outobj(NULL),
	smprt(0),blsz(0)
{}

bool flext_stk::Init()
{
	bool ret = flext_dsp::Init();
	inobjs = CntInSig();
	outobjs = CntOutSig();
	return ret;
}

void flext_stk::Exit()
{
	ClearObjs();
	flext_dsp::Exit();
}

void flext_stk::ClearObjs()
{
	FreeObjs();

	if(inobj) {
		for(int i = 0; i < inobjs; ++i) delete inobj[i]; 
		delete[] inobj; inobj = NULL; inobjs = 0;
	}
	if(outobj) {
		for(int i = 0; i < outobjs; ++i) delete outobj[i];
		delete[] outobj; outobj = NULL; outobjs = 0;
	}
}

void flext_stk::m_dsp(int n,t_sample *const *in,t_sample *const *out)
{
	// called on every rebuild of the dsp chain

	int i;
	
	if(Blocksize() != blsz || Samplerate() != smprt) {
		// block size or sample rate has changed... rebuild all objects

		ClearObjs();

		smprt = Samplerate();
		blsz = Blocksize();
		Stk::setSampleRate(smprt); 

		// set up sndobjs for inlets and outlets
		inobj = new Inlet *[inobjs];
		for(i = 0; i < inobjs; ++i) {
			inobj[i] = new Inlet(in[i],blsz);
		}
		outobj = new Outlet *[outobjs];
		for(i = 0; i < outobjs; ++i) outobj[i] = new Outlet(out[i],blsz);

		NewObjs();
	}
	else {
		// assign changed input/output vectors

		for(i = 0; i < inobjs; ++i) inobj[i]->SetBuf(in[i]);
		for(i = 0; i < outobjs; ++i) outobj[i]->SetBuf(out[i]);
	}
}

void flext_stk::m_signal(int n,t_sample *const *in,t_sample *const *out)
{
	ProcessObjs();
}


// inlet class

flext_stk::Inlet::Inlet(const t_sample *b,int v): 
	buf(b),vecsz(v),
	index(0)
{}

t_sample flext_stk::Inlet::tick() 
{ 
	if(++index >= vecsz) index = 0; 
	return lastOut(); 
}

t_sample *flext_stk::Inlet::tick(t_sample *vector,unsigned int vectorSize)
{
	for(unsigned int i = 0; i < vectorSize; i++) vector[i] = tick();
	return vector;
}


// outlet class

flext_stk::Outlet::Outlet(t_sample *b,int v): 
	buf(b),vecsz(v),
	index(0)
{}

void flext_stk::Outlet::tick(t_sample s) 
{ 
	buf[index] = s;
	if(++index >= vecsz) index = 0; 
}

void flext_stk::Outlet::tick(const t_sample *vector,unsigned int vectorSize)
{
	for(unsigned int i = 0; i < vectorSize; i++) tick(vector[i]);
}