fldsp.h 4.55 KB
Newer Older
thomas's avatar
thomas committed
1 2 3 4
/* 

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

5
Copyright (c) 2001-2009 Thomas Grill (gr@grrrr.org)
thomas's avatar
thomas committed
6 7 8
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

thomas's avatar
thomas committed
9 10 11
$LastChangedRevision$
$LastChangedDate$
$LastChangedBy$
thomas's avatar
thomas committed
12 13 14 15 16 17 18 19 20 21
*/

/*! \file fldsp.h
    \brief Declares the flext dsp class
    
*/

#ifndef __FLDSP_H
#define __FLDSP_H

thomas's avatar
thomas committed
22
// include the header file declaring the base classes
thomas's avatar
thomas committed
23
#include "flext.h"
thomas's avatar
thomas committed
24

25
#include "flpushns.h"
thomas's avatar
thomas committed
26 27 28

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

29 30
FLEXT_TEMPLATE class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp);
typedef FLEXT_SHARE FLEXT_TEMPINST(FLEXT_CLASSDEF(flext_dsp)) flext_dsp;
31 32


thomas's avatar
thomas committed
33
/*! \brief Flext dsp enabled base object
thomas's avatar
thomas committed
34
*/
35
FLEXT_TEMPLATE
36
class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp):
thomas's avatar
thomas committed
37
	public flext_base
thomas's avatar
thomas committed
38
{
39
	FLEXT_HEADER_S(FLEXT_CLASSDEF(flext_dsp),flext_base,Setup)
thomas's avatar
thomas committed
40
	
41
	friend class FLEXT_SHARE FLEXT_TEMPINST(FLEXT_CLASSDEF(flext_base));
42

thomas's avatar
thomas committed
43 44
public:

thomas's avatar
thomas committed
45 46 47 48 49 50
/*!	\defgroup FLEXT_DSP Flext dsp class

	@{ 
*/

/*!	\defgroup FLEXT_C_DSP Basic dsp functionality
thomas's avatar
thomas committed
51 52 53 54

	@{ 
*/

thomas's avatar
thomas committed
55 56 57
	//! returns current sample rate
	float Samplerate() const { return srate; }
	
thomas's avatar
thomas committed
58
	//! returns current block (aka vector) size
thomas's avatar
thomas committed
59
	int Blocksize() const { return blksz; }
thomas's avatar
thomas committed
60
    
61
	//! returns array of input vectors (CntInSig() vectors)
62
    t_sample *const *InSig() const { return vecs; }
63

64
	//! returns input vector
thomas's avatar
thomas committed
65
    t_sample *InSig(int i) const { return InSig()[i]; }
66

67
	//! returns array of output vectors (CntOutSig() vectors)
thomas's avatar
thomas committed
68 69 70 71
    // \todo cache that returned pointer
    t_sample *const *OutSig() const 
    { 
        int i = CntInSig(); 
72 73
        // in PD we have at least one actual dsp in vector
#if FLEXT_SYS == FLEXT_SYS_PD
thomas's avatar
thomas committed
74
        return vecs+(i?i:1); 
75 76 77 78 79
#elif FLEXT_SYS == FLEXT_SYS_MAX
        return vecs+i; 
#else
#error
#endif
thomas's avatar
thomas committed
80
    }
81

82
	//! returns output vector
thomas's avatar
thomas committed
83
    t_sample *OutSig(int i) const { return OutSig()[i]; }
84

thomas's avatar
thomas committed
85 86
	//! typedef describing a signal vector
	typedef t_sample *t_signalvec;
thomas's avatar
thomas committed
87

thomas's avatar
thomas committed
88
//!	@} 
thomas's avatar
thomas committed
89 90 91

// --- inheritable virtual methods --------------------------------

thomas's avatar
thomas committed
92
/*!	\defgroup FLEXT_C_DSP_VIRTUAL Flext virtual dsp functions
thomas's avatar
thomas committed
93 94 95

	@{ 
*/
thomas's avatar
thomas committed
96
	/*! \brief Called on every dsp init.
thomas's avatar
thomas committed
97
		\note Don't expect any valid data in the signal vectors!
98 99 100 101 102 103 104 105 106 107 108
        flext_dsp::CbDsp should not be called by the derived class

        \return true (default)... use DSP, false, don't use DSP
    */
	virtual bool CbDsp();

	/*! \brief Called with every signal vector - here you do the dsp calculation
        flext_dsp::CbSignal fills all output vectors with silence
    */
	virtual void CbSignal();

thomas's avatar
thomas committed
109

110 111
    /*! \brief Deprecated method for CbSignal
        \deprecated
thomas's avatar
thomas committed
112 113 114
		\param n: frames (aka samples) in one signal vector
		\param insigs: array of input vectors  (get number with function CntInSig())
		\param outsigs: array of output vectors  (get number with function CntOutSig())
thomas's avatar
thomas committed
115
	*/
thomas's avatar
thomas committed
116
	virtual void m_dsp(int n,t_signalvec const *insigs,t_signalvec const *outsigs);
thomas's avatar
thomas committed
117

118 119
    /*! \brief Deprecated method for CbSignal
        \deprecated
thomas's avatar
thomas committed
120 121 122
		\param n: frames (aka samples) in one signal vector
		\param insigs: array of input vectors  (get number with function CntInSig())
		\param outsigs: array of output vectors  (get number with function CntOutSig())
thomas's avatar
thomas committed
123
	*/
thomas's avatar
thomas committed
124
	virtual void m_signal(int n,t_sample *const *insigs,t_sample *const *outsigs);
thomas's avatar
thomas committed
125

thomas's avatar
thomas committed
126 127 128
//!	@} 


thomas's avatar
thomas committed
129
/*!	\defgroup FLEXT_C_DSP_INOUT Flext dsp in-/outlet functions
thomas's avatar
thomas committed
130 131 132 133
	\note These must be called in the class' constructor

	@{ 
*/
thomas's avatar
thomas committed
134 135
// --- inlet/outlet stuff -----------------------------------------	

thomas's avatar
thomas committed
136 137 138
	/*! \brief Add signal inlet(s)
		\param m Number of inlets to add
	*/
thomas's avatar
thomas committed
139
	void AddInSignal(int m = 1) { AddInlet(xlet_sig,m); }
thomas's avatar
thomas committed
140

thomas's avatar
thomas committed
141 142 143
	/*! \brief Add signal inlet (with description)
		\param desc Description of inlet
	*/
thomas's avatar
thomas committed
144
	void AddInSignal(const char *desc) { AddInlet(xlet_sig,1,desc); }
thomas's avatar
thomas committed
145

thomas's avatar
thomas committed
146 147 148
	/*! \brief Add signal outlet(s)
		\param m Number of inlets to add
	*/
thomas's avatar
thomas committed
149
	void AddOutSignal(int m = 1) { AddOutlet(xlet_sig,m); }
thomas's avatar
thomas committed
150

thomas's avatar
thomas committed
151 152 153
	/*! \brief Add signal outlet (with description)
		\param desc Description of outlet
	*/
thomas's avatar
thomas committed
154
	void AddOutSignal(const char *desc) { AddOutlet(xlet_sig,1,desc); }
thomas's avatar
thomas committed
155

thomas's avatar
thomas committed
156 157
//!	@} 

thomas's avatar
thomas committed
158
//!	@} 
thomas's avatar
thomas committed
159 160 161

protected:
	
162
	FLEXT_CLASSDEF(flext_dsp)();
163 164

    virtual void Exit();
thomas's avatar
thomas committed
165

thomas's avatar
thomas committed
166 167 168 169 170
private:

	// not static, could be different in different patchers..
	float srate; 
	int blksz;
171
	t_signalvec *vecs;
thomas's avatar
thomas committed
172 173

	// setup function
thomas's avatar
thomas committed
174
	static void Setup(t_classid c);
thomas's avatar
thomas committed
175

176
#if FLEXT_SYS == FLEXT_SYS_PD
177
	static bool cb_enable(flext_base *c,float &on);
thomas's avatar
thomas committed
178 179 180
	bool dspon;
#endif

181 182 183 184
	static inline flext_dsp *thisObject(flext_hdr *c) { return FLEXT_CAST<flext_dsp *>(c->data); } 

	void SetupDsp(t_signal **sp);

thomas's avatar
thomas committed
185 186 187 188
	// dsp stuff
	static t_int *dspmeth(t_int *w); 
};

189 190
#include "flpopns.h"

thomas's avatar
thomas committed
191
#endif