fldsp.cpp 2.83 KB
Newer Older
thomas's avatar
thomas committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001,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.  

*/

/*! \file fldsp.cpp
    \brief Implementation of the flext dsp base class.
*/
 
#include "flext.h"
#include "flinternal.h"
#include <string.h>



// === flext_dsp ==============================================

void flext_dsp::Setup(t_class *c)
{
thomas's avatar
thomas committed
25
#if FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
26
27
28
29
30
//	dsp_initclass();
	dsp_initboxclass();
#endif
	
	add_dsp(c,cb_dsp);
thomas's avatar
thomas committed
31
32
#if FLEXT_SYS != FLEXT_SYS_MAX
	add_method1(c,cb_enable,"enable",A_FLOAT);
thomas's avatar
thomas committed
33
34
35
36
#endif
}

flext_dsp::flext_dsp(): 
thomas's avatar
thomas committed
37
#if FLEXT_SYS != FLEXT_SYS_MAX
thomas's avatar
thomas committed
38
39
40
41
	dspon(true),
#endif
	srate(sys_getsr()),  // should we set it?
	blksz(sys_getblksize()),
thomas's avatar
thomas committed
42
#if FLEXT_SYS == FLEXT_SYS_PD
thomas's avatar
thomas committed
43
44
	chnsin(sys_get_inchannels()),
	chnsout(sys_get_outchannels()),
thomas's avatar
thomas committed
45
#elif FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
46
47
	chnsin(sys_getch()),
	chnsout(sys_getch()),
thomas's avatar
thomas committed
48
49
#else
#error
thomas's avatar
thomas committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#endif
	invecs(NULL),outvecs(NULL)
{}


flext_dsp::~flext_dsp()
{
	if(invecs) delete[] invecs;
	if(outvecs) delete[] outvecs;
}


t_int *flext_dsp::dspmeth(t_int *w) 
{ 
	flext_dsp *obj = (flext_dsp *)w[1];
thomas's avatar
thomas committed
65
#if FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	if(!obj->thisHdr()->z_disabled) 
#else
	if(obj->dspon) 
#endif
		obj->m_signal((int)w[2],obj->invecs,obj->outvecs); 
	return w+3;
}

void flext_dsp::m_dsp(int /*n*/,t_sample *const * /*insigs*/,t_sample *const * /*outsigs*/) {}

void flext_dsp::m_signal(int n,t_sample *const * /*insigs*/,t_sample *const *outsigs) 
{
	for(int i = 0; i < CntOutSig(); ++i)
		memset(outsigs[i],0,n*sizeof(*outsigs[i]));
}

void flext_dsp::cb_dsp(t_class *c,t_signal **sp) 
{ 
	flext_dsp *obj = thisObject(c); 

thomas's avatar
thomas committed
86
87
	if(obj->CntInSig()+obj->CntOutSig() == 0) return;

thomas's avatar
thomas committed
88
89
90
91
	// store current dsp parameters
	obj->srate = sp[0]->s_sr;
	obj->blksz = sp[0]->s_n;  // is this guaranteed to be the same as sys_getblksize() ?
	
thomas's avatar
thomas committed
92
#if FLEXT_SYS == FLEXT_SYS_PD
thomas's avatar
thomas committed
93
94
	obj->chnsin = sys_get_inchannels();
	obj->chnsout = sys_get_outchannels();
thomas's avatar
thomas committed
95
#elif FLEXT_SYS == FLEXT_SYS_MAX
thomas's avatar
thomas committed
96
	obj->chnsin = obj->chnsout = sys_getch();
thomas's avatar
thomas committed
97
98
#else
#error
thomas's avatar
thomas committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#endif

	// store in and out signal vectors
	int i,in = obj->CntInSig(),out = obj->CntOutSig();
	if(obj->invecs) delete[] obj->invecs;
	obj->invecs = new t_sample *[in];
	for(i = 0; i < in; ++i) obj->invecs[i] = sp[i]->s_vec;

	if(obj->outvecs) delete[] obj->outvecs;
	obj->outvecs = new t_sample *[out];
	for(i = 0; i < out; ++i) obj->outvecs[i] = sp[in+i]->s_vec;

	// with the following call derived classes can do their eventual DSP setup
	obj->m_dsp(sp[0]->s_n,obj->invecs,obj->outvecs);

	// set the DSP function
	dsp_add((t_dspmethod)dspmeth,2,obj,sp[0]->s_n);  
}

thomas's avatar
thomas committed
118
119
#if FLEXT_SYS != FLEXT_SYS_MAX
void flext_dsp::cb_enable(t_class *c,t_float on) { thisObject(c)->m_enable(on != 0); }
thomas's avatar
thomas committed
120
121
122
123
void flext_dsp::m_enable(bool en) { dspon = en; }
#endif