fldsp.h 5.05 KB
Newer Older
Thomas Grill's avatar
Thomas Grill committed
1 2
/*
flext - C++ layer for Max and Pure Data externals
thomas's avatar
thomas committed
3

Thomas Grill's avatar
Thomas Grill committed
4
Copyright (c) 2001-2015 Thomas Grill (gr@grrrr.org)
thomas's avatar
thomas committed
5
For information on usage and redistribution, and for a DISCLAIMER OF ALL
Thomas Grill's avatar
Thomas Grill committed
6
WARRANTIES, see the file, "license.txt," in this distribution.
thomas's avatar
thomas committed
7 8 9 10 11 12 13 14 15 16
*/

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

#ifndef __FLDSP_H
#define __FLDSP_H

thomas's avatar
thomas committed
17
// include the header file declaring the base classes
thomas's avatar
thomas committed
18
#include "flext.h"
thomas's avatar
thomas committed
19

20
#include "flpushns.h"
thomas's avatar
thomas committed
21 22 23

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

24 25
FLEXT_TEMPLATE class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp);
typedef FLEXT_SHARE FLEXT_TEMPINST(FLEXT_CLASSDEF(flext_dsp)) flext_dsp;
26 27


thomas's avatar
thomas committed
28
/*! \brief Flext dsp enabled base object
thomas's avatar
thomas committed
29
*/
30
FLEXT_TEMPLATE
31
class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp):
thomas's avatar
thomas committed
32
	public flext_base
thomas's avatar
thomas committed
33
{
34
	FLEXT_HEADER_S(FLEXT_CLASSDEF(flext_dsp),flext_base,Setup)
thomas's avatar
thomas committed
35
	
36
	friend class FLEXT_SHARE FLEXT_TEMPINST(FLEXT_CLASSDEF(flext_base));
37

thomas's avatar
thomas committed
38 39
public:

thomas's avatar
thomas committed
40 41 42 43 44 45
/*!	\defgroup FLEXT_DSP Flext dsp class

	@{ 
*/

/*!	\defgroup FLEXT_C_DSP Basic dsp functionality
thomas's avatar
thomas committed
46 47 48 49

	@{ 
*/

thomas's avatar
thomas committed
50 51 52
	//! returns current sample rate
	float Samplerate() const { return srate; }
	
thomas's avatar
thomas committed
53
	//! returns current block (aka vector) size
thomas's avatar
thomas committed
54
	int Blocksize() const { return blksz; }
thomas's avatar
thomas committed
55
    
56
	//! returns array of input vectors (CntInSig() vectors)
martin hermant's avatar
martin hermant committed
57 58 59 60 61 62 63 64
    t_sample *const *InSig() const {
#if MSP64
        return inVec;
#else
        
        return vecs;
#endif
    }
65

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

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

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

thomas's avatar
thomas committed
91 92
	//! typedef describing a signal vector
	typedef t_sample *t_signalvec;
thomas's avatar
thomas committed
93

thomas's avatar
thomas committed
94
//!	@} 
thomas's avatar
thomas committed
95 96 97

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

thomas's avatar
thomas committed
98
/*!	\defgroup FLEXT_C_DSP_VIRTUAL Flext virtual dsp functions
thomas's avatar
thomas committed
99 100 101

	@{ 
*/
thomas's avatar
thomas committed
102
	/*! \brief Called on every dsp init.
thomas's avatar
thomas committed
103
		\note Don't expect any valid data in the signal vectors!
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();
martin hermant's avatar
martin hermant committed
109
    virtual bool CbDsp64();
110 111 112 113

	/*! \brief Called with every signal vector - here you do the dsp calculation
        flext_dsp::CbSignal fills all output vectors with silence
    */
martin hermant's avatar
martin hermant committed
114 115
	virtual void CbSignal();    
    virtual void CbSignal64();
116

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_dsp(int n,t_signalvec const *insigs,t_signalvec const *outsigs);
thomas's avatar
thomas committed
125

126 127
    /*! \brief Deprecated method for CbSignal
        \deprecated
thomas's avatar
thomas committed
128 129 130
		\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
131
	*/
thomas's avatar
thomas committed
132
	virtual void m_signal(int n,t_sample *const *insigs,t_sample *const *outsigs);
thomas's avatar
thomas committed
133

thomas's avatar
thomas committed
134 135 136
//!	@} 


thomas's avatar
thomas committed
137
/*!	\defgroup FLEXT_C_DSP_INOUT Flext dsp in-/outlet functions
thomas's avatar
thomas committed
138 139 140 141
	\note These must be called in the class' constructor

	@{ 
*/
thomas's avatar
thomas committed
142 143
// --- inlet/outlet stuff -----------------------------------------	

thomas's avatar
thomas committed
144 145 146
	/*! \brief Add signal inlet(s)
		\param m Number of inlets to add
	*/
thomas's avatar
thomas committed
147
	void AddInSignal(int m = 1) { AddInlet(xlet_sig,m); }
thomas's avatar
thomas committed
148

thomas's avatar
thomas committed
149 150 151
	/*! \brief Add signal inlet (with description)
		\param desc Description of inlet
	*/
thomas's avatar
thomas committed
152
	void AddInSignal(const char *desc) { AddInlet(xlet_sig,1,desc); }
thomas's avatar
thomas committed
153

thomas's avatar
thomas committed
154 155 156
	/*! \brief Add signal outlet(s)
		\param m Number of inlets to add
	*/
thomas's avatar
thomas committed
157
	void AddOutSignal(int m = 1) { AddOutlet(xlet_sig,m); }
thomas's avatar
thomas committed
158

thomas's avatar
thomas committed
159 160 161
	/*! \brief Add signal outlet (with description)
		\param desc Description of outlet
	*/
thomas's avatar
thomas committed
162
	void AddOutSignal(const char *desc) { AddOutlet(xlet_sig,1,desc); }
thomas's avatar
thomas committed
163

thomas's avatar
thomas committed
164 165
//!	@} 

thomas's avatar
thomas committed
166
//!	@} 
thomas's avatar
thomas committed
167 168 169

protected:
	
170
	FLEXT_CLASSDEF(flext_dsp)();
171 172

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

thomas's avatar
thomas committed
174 175 176 177 178
private:

	// not static, could be different in different patchers..
	float srate; 
	int blksz;
martin hermant's avatar
martin hermant committed
179 180 181 182 183 184 185 186
	
    
#if MSP64
    t_signalvec * inVec;
    t_signalvec * outVec;
#else
    t_signalvec *vecs;
#endif
thomas's avatar
thomas committed
187 188

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

191
#if FLEXT_SYS == FLEXT_SYS_PD
192
	static bool cb_enable(flext_base *c,float &on);
thomas's avatar
thomas committed
193 194 195
	bool dspon;
#endif

196 197
	static inline flext_dsp *thisObject(flext_hdr *c) { return FLEXT_CAST<flext_dsp *>(c->data); } 

martin hermant's avatar
martin hermant committed
198 199
	
    
200

thomas's avatar
thomas committed
201
	// dsp stuff
martin hermant's avatar
martin hermant committed
202 203 204 205 206 207 208 209
	
#if MSP64
    static void dspmeth64(flext_hdr *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam);
    void SetupDsp64(flext_hdr *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags);
#else
    void SetupDsp(t_signal **sp);
    static t_int *dspmeth(t_int *w);
#endif
thomas's avatar
thomas committed
210 211
};

212 213
#include "flpopns.h"

thomas's avatar
thomas committed
214
#endif