aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/py/source/pydsp.cpp
blob: 07fa75af9701ddee26be3e784d77fd827f052c1f (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* 

py/pyext - python script object for PD and Max/MSP

Copyright (c)2002-2005 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.  

*/

#ifndef PY_NODSP

#include "pyext.h"

class pydsp
    : public pyext
{
	FLEXT_HEADER(pydsp,pyext)
public:
    pydsp(int argc,const t_atom *argv);

protected:
    virtual bool CbDsp();
    virtual void CbSignal();

    virtual bool DoInit();
    virtual void DoExit();

    virtual PyObject *GetSig(int ix,bool in);

    void NewBuffers();
    void FreeBuffers();

    PyObject *dspfun,*sigfun;
    PyObject **buffers;
};

FLEXT_LIB_DSP_V("pyext~ pyext.~ pyx~ pyx.~",pydsp)

pydsp::pydsp(int argc,const t_atom *argv)
    : pyext(argc,argv,true) 
    , dspfun(NULL),sigfun(NULL)
{}

bool pydsp::DoInit()
{
    if(!pyext::DoInit()) return false;
    
    if(pyobj) 
	{ 
        dspfun = PyObject_GetAttrString(pyobj,"_dsp"); // get ref
	    if(!dspfun)
			PyErr_Clear();
		else if(!PyMethod_Check(dspfun)) {
            Py_DECREF(dspfun);
		    dspfun = NULL;
		}
	}
    return true;
}

void pydsp::DoExit()
{
    if(dspfun) { Py_DECREF(dspfun); dspfun = NULL; }
    if(sigfun) { Py_DECREF(sigfun); sigfun = NULL; }

    FreeBuffers();
}

PyObject *arrayfrombuffer(PyObject *buf,int c,int n);

void pydsp::NewBuffers()
{
    int i,n = Blocksize();
    const int ins = CntInSig(),outs = CntOutSig();
    t_sample *const *insigs = InSig();
    t_sample *const *outsigs = OutSig();

    // inlet/outlet count can't change so we don't have to deallocate
    if(!buffers) {
        int cnt = ins+outs;
        buffers = new PyObject *[cnt];
        memset(buffers,0,cnt*sizeof(*buffers));
    }

    for(i = 0; i < ins; ++i) {
        Py_XDECREF(buffers[i]);
        PyObject *b = PyBuffer_FromReadWriteMemory(insigs[i],n*sizeof(t_sample));
        buffers[i] = arrayfrombuffer(b,1,n);
        Py_DECREF(b);
    }
    for(i = 0; i < outs; ++i) {
        Py_XDECREF(buffers[ins+i]);
        if(i < ins && outsigs[i] == insigs[i]) {
            // same vectors - share the objects!
            buffers[ins+i] = buffers[i];
            Py_XINCREF(buffers[i]);
        }
        else {
            PyObject *b = PyBuffer_FromReadWriteMemory(outsigs[i],n*sizeof(t_sample));
            buffers[ins+i] = arrayfrombuffer(b,1,n);
            Py_DECREF(b);
        }
    }
}

void pydsp::FreeBuffers()
{
    if(buffers) {
        int cnt = CntInSig()+CntOutSig();
        for(int i = 0; i < cnt; ++i) Py_XDECREF(buffers[i]);
        delete[] buffers;
        buffers = NULL;
    }
}

bool pydsp::CbDsp()
{
    if(pyobj && (CntInSig() || CntOutSig()))
    {
       	ThrState state = PyLockSys();

        NewBuffers();

		bool dodsp = true;
        if(dspfun) {
            PyObject *ret = PyObject_CallObject(dspfun,NULL);
            if(ret) {
				dodsp = PyObject_IsTrue(ret) != 0;
                Py_DECREF(ret);
			}
            else {
#ifdef FLEXT_DEBUG
                PyErr_Print();
#else
                PyErr_Clear();
#endif   
            }
        }

        // do that here instead of where dspfun is initialized, so that
        // _signal can be assigned in _dsp
        // optimizations may be done there to assign the right _signal version
        Py_XDECREF(sigfun);
		
		if(dodsp) {
			sigfun = PyObject_GetAttrString(pyobj,"_signal"); // get ref
			if(!sigfun)
				PyErr_Clear();
			else if(!PyMethod_Check(sigfun)) {
				Py_DECREF(sigfun);
				sigfun = NULL;
			}
		}
		else
			sigfun = NULL;

        PyUnlock(state);
        return sigfun != NULL;
    }
    else
        // switch on dsp only if there are signal inlets or outlets
        return false;
}

void pydsp::CbSignal()
{
    ThrState state = PyLockSys();
    PyObject *ret = PyObject_CallObject(sigfun,NULL);

    if(ret) 
        Py_DECREF(ret);
    else {
#ifdef FLEXT_DEBUG
        PyErr_Print();
#else
        PyErr_Clear();
#endif   
    }
    PyUnlock(state);
}

PyObject *pydsp::GetSig(int ix,bool in) 
{
    PyObject *r = buffers[in?ix:CntInSig()+ix];
    Py_XINCREF(r);
    return r;
}

#endif