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

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

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

*/

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

#ifndef __FLDSP_H
#define __FLDSP_H

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


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

25 26 27 28
class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp);
typedef class FLEXT_SHARE FLEXT_CLASSDEF(flext_dsp) flext_dsp;


thomas's avatar
thomas committed
29
/*! \brief Flext dsp enabled base object
thomas's avatar
thomas committed
30
*/
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 37
	friend class FLEXT_SHARE FLEXT_CLASSDEF(flext_base);

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)
57
    t_sample *const *InSig() const { return vecs; }
58

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

62
	//! returns array of output vectors (CntOutSig() vectors)
thomas's avatar
thomas committed
63 64 65 66 67 68 69
    // \todo cache that returned pointer
    t_sample *const *OutSig() const 
    { 
        int i = CntInSig(); 
        // we have at least one actual dsp in vector
        return vecs+(i?i:1); 
    }
70

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

thomas's avatar
thomas committed
74 75
	//! typedef describing a signal vector
	typedef t_sample *t_signalvec;
thomas's avatar
thomas committed
76

thomas's avatar
thomas committed
77
//!	@} 
thomas's avatar
thomas committed
78 79 80

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

thomas's avatar
thomas committed
81
/*!	\defgroup FLEXT_C_DSP_VIRTUAL Flext virtual dsp functions
thomas's avatar
thomas committed
82 83 84

	@{ 
*/
thomas's avatar
thomas committed
85
	/*! \brief Called on every dsp init.
thomas's avatar
thomas committed
86
		\note Don't expect any valid data in the signal vectors!
87 88 89 90 91 92 93 94 95 96 97
        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
98

99 100
    /*! \brief Deprecated method for CbSignal
        \deprecated
thomas's avatar
thomas committed
101 102 103
		\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
104
	*/
thomas's avatar
thomas committed
105
	virtual void m_dsp(int n,t_signalvec const *insigs,t_signalvec const *outsigs);
thomas's avatar
thomas committed
106

107 108
    /*! \brief Deprecated method for CbSignal
        \deprecated
thomas's avatar
thomas committed
109 110 111
		\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
112
	*/
thomas's avatar
thomas committed
113
	virtual void m_signal(int n,t_sample *const *insigs,t_sample *const *outsigs);
thomas's avatar
thomas committed
114

thomas's avatar
thomas committed
115 116 117
//!	@} 


thomas's avatar
thomas committed
118
/*!	\defgroup FLEXT_C_DSP_INOUT Flext dsp in-/outlet functions
thomas's avatar
thomas committed
119 120 121 122
	\note These must be called in the class' constructor

	@{ 
*/
thomas's avatar
thomas committed
123 124
// --- inlet/outlet stuff -----------------------------------------	

thomas's avatar
thomas committed
125 126 127
	/*! \brief Add signal inlet(s)
		\param m Number of inlets to add
	*/
thomas's avatar
thomas committed
128
	void AddInSignal(int m = 1) { AddInlet(xlet_sig,m); }
thomas's avatar
thomas committed
129

thomas's avatar
thomas committed
130 131 132
	/*! \brief Add signal inlet (with description)
		\param desc Description of inlet
	*/
thomas's avatar
thomas committed
133
	void AddInSignal(const char *desc) { AddInlet(xlet_sig,1,desc); }
thomas's avatar
thomas committed
134

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

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

thomas's avatar
thomas committed
145 146
//!	@} 

thomas's avatar
thomas committed
147
//!	@} 
thomas's avatar
thomas committed
148 149 150

protected:
	
151
	FLEXT_CLASSDEF(flext_dsp)();
152 153

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

thomas's avatar
thomas committed
155 156 157 158 159
private:

	// not static, could be different in different patchers..
	float srate; 
	int blksz;
160
	t_signalvec *vecs;
thomas's avatar
thomas committed
161 162

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

165 166
#if FLEXT_SYS == FLEXT_SYS_PD
	static void cb_enable(flext_hdr *c,t_float on);
thomas's avatar
thomas committed
167 168 169
	bool dspon;
#endif

170 171 172 173
	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
174 175 176 177 178
	// dsp stuff
	static t_int *dspmeth(t_int *w); 
};

#endif