File: fldsp.cpp

package info (click to toggle)
pd-flext 0.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,960 kB
  • sloc: cpp: 12,978; makefile: 223; sh: 149
file content (172 lines) | stat: -rw-r--r-- 4,457 bytes parent folder | download | duplicates (4)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
flext - C++ layer for Max and Pure Data externals

Copyright (c) 2001-2017 Thomas Grill (gr@grrrr.org)
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.
*/
 
#ifndef __FLEXT_DSP_CPP
#define __FLEXT_DSP_CPP

#include "flext.h"
#include "flinternal.h"
#include <cstring>

#include "flpushns.h"

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

FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::Setup(t_classid id)
{
#if FLEXT_SYS == FLEXT_SYS_PD
//    add_method1(c,cb_enable,"enable",A_FLOAT);
    AddMethod(id,0,MakeSymbol("enable"),&cb_enable);
#endif
}

FLEXT_TEMPIMPL(FLEXT_CLASSDEF(flext_dsp))::FLEXT_CLASSDEF(flext_dsp)()
    : srate(sys_getsr()),blksz(sys_getblksize())
#if MSP64
    , inVec(NULL), outVec(NULL)
#else
    , vecs(NULL)
#endif
#if FLEXT_SYS != FLEXT_SYS_MAX
    , dspon(true)
#endif
{}

FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::Exit()
{
    flext_base::Exit();
#if !MSP64
    if(vecs) delete[] vecs;
#endif
}


#if MSP64
FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::dspmeth64(flext_hdr *x, t_object *dsp64, double **ins, long numins, double **outs, long numouts, long sampleframes, long flags, void *userparam)
{
    flext_dsp *obj = (flext_dsp *)userparam;
    
    obj->blksz = sampleframes;
    obj->inVec = ins;
    obj->outVec = outs;

    if(!obj->thisHdr()->z_disabled) {
        flext_base::indsp = true;
        obj->CbSignal();
        flext_base::indsp = false;
    }
}

FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::SetupDsp64(flext_hdr *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags)
{
    // store current dsp parameters
    srate = samplerate;
    // overlap = sp[0]->s_sr/srate;  // currently not used/exposed
    blksz = maxvectorsize; // will be overwritten in dspmeth64 anyway...

    // with the following call derived classes can do their eventual DSP setup
    if(CbDsp()) {
        // set the DSP function
        dsp_add64(dsp64, (t_object *)&x->obj, (t_dspmethod)dspmeth64, 0, this);
    }
}

#else

FLEXT_TEMPIMPL(t_int *FLEXT_CLASSDEF(flext_dsp))::dspmeth(t_int *w)
{ 
    flext_dsp *obj = (flext_dsp *)(size_t)w[1];

#if FLEXT_SYS == FLEXT_SYS_MAX
    if(!obj->thisHdr()->z_disabled)
#else
    if(LIKELY(obj->dspon))
#endif
    {
        flext_base::indsp = true;
        obj->CbSignal(); 
        flext_base::indsp = false;
    }
    return w+2;
}

FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::SetupDsp(t_signal **sp)
{ 
    int i;
    int in = CntInSig();
    int out = CntOutSig();
#if FLEXT_SYS == FLEXT_SYS_PD
    // min. 1 input channel! (CLASS_MAININLET in pd...)
    if(!in) in = 1;
#endif

    // store current dsp parameters
    srate = sys_getsr();
    // overlap = sp[0]->s_sr/srate;  // currently not used/exposed
    
    blksz = sp[0]->s_n;  // is this guaranteed to be the same as sys_getblksize() ?

    // store in and out signal vectors

    if((in+out) && !vecs)
        vecs = new t_signalvec[in+out];

    for(i = 0; i < in; ++i) 
        vecs[i] = sp[i]->s_vec;
    for(i = 0; i < out; ++i) 
        vecs[in+i] = sp[in+i]->s_vec;

    // with the following call derived classes can do their eventual DSP setup
    if(CbDsp()) {
        // set the DSP function
        dsp_add((t_dspmethod)dspmeth, 1, this);
    }
}
#endif


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

FLEXT_TEMPIMPL(bool FLEXT_CLASSDEF(flext_dsp))::CbDsp()
{ 
	// invoke legacy method
    m_dsp(Blocksize(),InSig(),OutSig()); 
    return true;
}


// this function will be overridden anyway - the probably useless default is clearing all outputs
FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::m_signal(int n,t_sample *const * /*insigs*/,t_sample *const *outs)
{
    for(int i = 0; i < CntOutSig(); ++i) ZeroSamples(outs[i],n);
}

FLEXT_TEMPIMPL(void FLEXT_CLASSDEF(flext_dsp))::CbSignal()
{ 
	// invoke legacy method
	m_signal(Blocksize(),InSig(),OutSig()); 
}

#if FLEXT_SYS == FLEXT_SYS_PD
//void flext_dsp::cb_enable(flext_hdr *c,t_float on) { thisObject(c)->dspon = on != 0; }
FLEXT_TEMPIMPL(bool FLEXT_CLASSDEF(flext_dsp))::cb_enable(flext_base *b,float &on)
{
    static_cast<flext_dsp *>(b)->dspon = on != 0;
    return true;
}
#endif

#include "flpopns.h"

#endif // __FLEXT_DSP_CPP