File: SC_Unit.h

package info (click to toggle)
supercollider 1%3A3.11.2%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 71,152 kB
  • sloc: cpp: 387,846; lisp: 80,328; ansic: 76,515; sh: 22,779; python: 7,932; makefile: 2,333; perl: 1,123; javascript: 915; java: 677; xml: 582; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (354 lines) | stat: -rw-r--r-- 19,985 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
    SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
    http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/


#pragma once

#include "SC_Types.h"
#include "SC_SndBuf.h"

typedef void (*UnitCtorFunc)(struct Unit* inUnit);
typedef void (*UnitDtorFunc)(struct Unit* inUnit);

typedef void (*UnitCalcFunc)(struct Unit* inThing, int inNumSamples);

struct SC_Unit_Extensions {
    float* todo;
};

struct Unit {
    struct World* mWorld;
    struct UnitDef* mUnitDef;
    struct Graph* mParent;
    uint32 mNumInputs, mNumOutputs; // changed from uint16 for synthdef ver 2
    int16 mCalcRate;
    int16 mSpecialIndex; // used by unary and binary ops
    int16 mParentIndex;
    int16 mDone;
    struct Wire **mInput, **mOutput;
    struct Rate* mRate;
    SC_Unit_Extensions*
        mExtensions; // future proofing and backwards compatibility; used to be SC_Dimension struct pointer
    float **mInBuf, **mOutBuf;

    UnitCalcFunc mCalcFunc;
    int mBufLength;
};

typedef struct Unit Unit;

enum { kUnitDef_CantAliasInputsToOutputs = 1 };

#ifdef _WIN32
// Win32 headers (included by C std library headers) define IN and OUT macros
// for their own purposes.
#    undef IN
#    undef OUT
#endif

// These return float* pointers to input and output buffers.
#define IN(index) (unit->mInBuf[index])
#define OUT(index) (unit->mOutBuf[index])

// These return a float value. Used for control rate inputs and outputs.
#define IN0(index) (IN(index)[0])
#define OUT0(index) (OUT(index)[0])

// get the rate of the input.
#define INRATE(index) (unit->mInput[index]->mCalcRate)

// get the blocksize of the input
#define INBUFLENGTH(index) (unit->mInput[index]->mFromUnit->mBufLength)

// set the calculation function
#define SETCALC(func) (unit->mCalcFunc = (UnitCalcFunc)&func)

// calculate a slope for control rate interpolation to audio rate.
#define CALCSLOPE(next, prev) ((next - prev) * sc_typeof_cast(next) unit->mRate->mSlopeFactor)

// get useful values
#define SAMPLERATE (unit->mRate->mSampleRate)
#define SAMPLEDUR (unit->mRate->mSampleDur)
#define BUFLENGTH (unit->mBufLength)
#define BUFRATE (unit->mRate->mBufRate)
#define BUFDUR (unit->mRate->mBufDuration)
#define FULLRATE (unit->mWorld->mFullRate.mSampleRate)
#define FULLBUFLENGTH (unit->mWorld->mFullRate.mBufLength)

#ifdef SUPERNOVA

template <bool shared1, bool shared2> struct buffer_lock2 {
    buffer_lock2(const SndBuf* buf1, const SndBuf* buf2): buf1_(buf1), buf2_(buf2) {
        if (buf1 == buf2) {
            lock1();
            return;
        }

        for (;;) {
            lock1();

            if (lock2())
                return;
            unlock1();
        }
    }

    ~buffer_lock2(void) {
        unlock1();
        if (buf1_ != buf2_)
            unlock2();
    }

private:
    void lock1(void) {
        if (buf1_->isLocal)
            return;

        if (!shared1)
            buf1_->lock.lock();
        else
            buf1_->lock.lock_shared();
    }

    bool lock2(void) {
        if (buf2_->isLocal)
            return true;

        if (!shared2)
            return buf2_->lock.try_lock();
        else
            return buf2_->lock.try_lock_shared();
    }

    void unlock1(void) {
        if (buf1_->isLocal)
            return;

        if (!shared1)
            buf1_->lock.unlock();
        else
            buf1_->lock.unlock_shared();
    }

    void unlock2(void) {
        if (buf2_->isLocal)
            return;

        if (!shared2)
            buf2_->lock.unlock();
        else
            buf2_->lock.unlock_shared();
    }

    const SndBuf* buf1_;
    const SndBuf* buf2_;
};

template <bool shared> struct buffer_lock {
    buffer_lock(const SndBuf* buf): buf_(buf) {
        if (!buf->isLocal) {
            if (shared)
                buf->lock.lock_shared();
            else
                buf->lock.lock();
        }
    }

    ~buffer_lock(void) {
        if (!buf_->isLocal) {
            if (shared)
                buf_->lock.unlock_shared();
            else
                buf_->lock.unlock();
        }
    }

    const SndBuf* buf_;
};

#    define ACQUIRE_BUS_AUDIO(index) unit->mWorld->mAudioBusLocks[index].lock()
#    define ACQUIRE_BUS_AUDIO_SHARED(index) unit->mWorld->mAudioBusLocks[index].lock_shared()
#    define RELEASE_BUS_AUDIO(index) unit->mWorld->mAudioBusLocks[index].unlock()
#    define RELEASE_BUS_AUDIO_SHARED(index) unit->mWorld->mAudioBusLocks[index].unlock_shared()

#    define LOCK_SNDBUF(buf) buffer_lock<false> lock_##buf(buf)
#    define LOCK_SNDBUF_SHARED(buf) buffer_lock<true> lock_##buf(buf);

#    define LOCK_SNDBUF2(buf1, buf2) buffer_lock2<false, false> lock_##buf1##_##buf2(buf1, buf2);
#    define LOCK_SNDBUF2_SHARED(buf1, buf2) buffer_lock2<true, true> lock_##buf1##_##buf2(buf1, buf2);
#    define LOCK_SNDBUF2_EXCLUSIVE_SHARED(buf1, buf2) buffer_lock2<false, true> lock_##buf1##_##buf2(buf1, buf2);
#    define LOCK_SNDBUF2_SHARED_EXCLUSIVE(buf1, buf2) buffer_lock2<true, false> lock_##buf1##_##buf2(buf1, buf2);

#    define ACQUIRE_SNDBUF(buf)                                                                                        \
        do {                                                                                                           \
            if (!buf->isLocal)                                                                                         \
                buf->lock.lock();                                                                                      \
        } while (false)
#    define ACQUIRE_SNDBUF_SHARED(buf)                                                                                 \
        do {                                                                                                           \
            if (!buf->isLocal)                                                                                         \
                buf->lock.lock_shared();                                                                               \
        } while (false)
#    define RELEASE_SNDBUF(buf)                                                                                        \
        do {                                                                                                           \
            if (!buf->isLocal)                                                                                         \
                buf->lock.unlock();                                                                                    \
        } while (false)
#    define RELEASE_SNDBUF_SHARED(buf)                                                                                 \
        do {                                                                                                           \
            if (!buf->isLocal)                                                                                         \
                buf->lock.unlock_shared();                                                                             \
        } while (false)


#    define ACQUIRE_BUS_CONTROL(index) unit->mWorld->mControlBusLock->lock()
#    define RELEASE_BUS_CONTROL(index) unit->mWorld->mControlBusLock->unlock()

#else

#    define ACQUIRE_BUS_AUDIO(index)
#    define ACQUIRE_BUS_AUDIO_SHARED(index)
#    define RELEASE_BUS_AUDIO(index)
#    define RELEASE_BUS_AUDIO_SHARED(index)

#    define LOCK_SNDBUF(buf)
#    define LOCK_SNDBUF_SHARED(buf)

#    define LOCK_SNDBUF2(buf1, buf2)
#    define LOCK_SNDBUF2_SHARED(buf1, buf2)
#    define LOCK_SNDBUF2_EXCLUSIVE_SHARED(buf1, buf2)
#    define LOCK_SNDBUF2_SHARED_EXCLUSIVE(buf1, buf2)

#    define ACQUIRE_SNDBUF(buf)
#    define ACQUIRE_SNDBUF_SHARED(buf)
#    define RELEASE_SNDBUF(buf)
#    define RELEASE_SNDBUF_SHARED(buf)

#    define ACQUIRE_BUS_CONTROL(index)
#    define RELEASE_BUS_CONTROL(index)

#endif

// macros to grab a Buffer reference from the buffer indicated by the UGen's FIRST input
#define GET_BUF                                                                                                        \
    float fbufnum = ZIN0(0);                                                                                           \
    if (fbufnum < 0.f) {                                                                                               \
        fbufnum = 0.f;                                                                                                 \
    }                                                                                                                  \
    if (fbufnum != unit->m_fbufnum) {                                                                                  \
        uint32 bufnum = (int)fbufnum;                                                                                  \
        World* world = unit->mWorld;                                                                                   \
        if (bufnum >= world->mNumSndBufs) {                                                                            \
            int localBufNum = bufnum - world->mNumSndBufs;                                                             \
            Graph* parent = unit->mParent;                                                                             \
            if (localBufNum <= parent->localBufNum) {                                                                  \
                unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
            } else {                                                                                                   \
                bufnum = 0;                                                                                            \
                unit->m_buf = world->mSndBufs + bufnum;                                                                \
            }                                                                                                          \
        } else {                                                                                                       \
            unit->m_buf = world->mSndBufs + bufnum;                                                                    \
        }                                                                                                              \
        unit->m_fbufnum = fbufnum;                                                                                     \
    }                                                                                                                  \
    SndBuf* buf = unit->m_buf;                                                                                         \
    LOCK_SNDBUF(buf);                                                                                                  \
    float* bufData __attribute__((__unused__)) = buf->data;                                                            \
    uint32 bufChannels __attribute__((__unused__)) = buf->channels;                                                    \
    uint32 bufSamples __attribute__((__unused__)) = buf->samples;                                                      \
    uint32 bufFrames = buf->frames;                                                                                    \
    int mask __attribute__((__unused__)) = buf->mask;                                                                  \
    int guardFrame __attribute__((__unused__)) = bufFrames - 2;

#define GET_BUF_SHARED                                                                                                 \
    float fbufnum = ZIN0(0);                                                                                           \
    if (fbufnum < 0.f) {                                                                                               \
        fbufnum = 0.f;                                                                                                 \
    }                                                                                                                  \
    if (fbufnum != unit->m_fbufnum) {                                                                                  \
        uint32 bufnum = (int)fbufnum;                                                                                  \
        World* world = unit->mWorld;                                                                                   \
        if (bufnum >= world->mNumSndBufs) {                                                                            \
            int localBufNum = bufnum - world->mNumSndBufs;                                                             \
            Graph* parent = unit->mParent;                                                                             \
            if (localBufNum <= parent->localBufNum) {                                                                  \
                unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
            } else {                                                                                                   \
                bufnum = 0;                                                                                            \
                unit->m_buf = world->mSndBufs + bufnum;                                                                \
            }                                                                                                          \
        } else {                                                                                                       \
            unit->m_buf = world->mSndBufs + bufnum;                                                                    \
        }                                                                                                              \
        unit->m_fbufnum = fbufnum;                                                                                     \
    }                                                                                                                  \
    const SndBuf* buf = unit->m_buf;                                                                                   \
    LOCK_SNDBUF_SHARED(buf);                                                                                           \
    const float* bufData __attribute__((__unused__)) = buf->data;                                                      \
    uint32 bufChannels __attribute__((__unused__)) = buf->channels;                                                    \
    uint32 bufSamples __attribute__((__unused__)) = buf->samples;                                                      \
    uint32 bufFrames = buf->frames;                                                                                    \
    int mask __attribute__((__unused__)) = buf->mask;                                                                  \
    int guardFrame __attribute__((__unused__)) = bufFrames - 2;

#define SIMPLE_GET_BUF                                                                                                 \
    float fbufnum = ZIN0(0);                                                                                           \
    fbufnum = sc_max(0.f, fbufnum);                                                                                    \
    if (fbufnum != unit->m_fbufnum) {                                                                                  \
        uint32 bufnum = (int)fbufnum;                                                                                  \
        World* world = unit->mWorld;                                                                                   \
        if (bufnum >= world->mNumSndBufs) {                                                                            \
            int localBufNum = bufnum - world->mNumSndBufs;                                                             \
            Graph* parent = unit->mParent;                                                                             \
            if (localBufNum <= parent->localBufNum) {                                                                  \
                unit->m_buf = parent->mLocalSndBufs + localBufNum;                                                     \
            } else {                                                                                                   \
                bufnum = 0;                                                                                            \
                unit->m_buf = world->mSndBufs + bufnum;                                                                \
            }                                                                                                          \
        } else {                                                                                                       \
            unit->m_buf = world->mSndBufs + bufnum;                                                                    \
        }                                                                                                              \
        unit->m_fbufnum = fbufnum;                                                                                     \
    }                                                                                                                  \
    SndBuf* buf = unit->m_buf;

#define SIMPLE_GET_BUF_EXCLUSIVE                                                                                       \
    SIMPLE_GET_BUF;                                                                                                    \
    LOCK_SNDBUF(buf);

#define SIMPLE_GET_BUF_SHARED                                                                                          \
    SIMPLE_GET_BUF;                                                                                                    \
    LOCK_SNDBUF_SHARED(buf);

// macros to get pseudo-random number generator, and put its state in registers
#define RGET                                                                                                           \
    RGen& rgen = *unit->mParent->mRGen;                                                                                \
    uint32 s1 = rgen.s1;                                                                                               \
    uint32 s2 = rgen.s2;                                                                                               \
    uint32 s3 = rgen.s3;
#define RPUT                                                                                                           \
    rgen.s1 = s1;                                                                                                      \
    rgen.s2 = s2;                                                                                                      \
    rgen.s3 = s3;

typedef void (*UnitCmdFunc)(struct Unit* unit, struct sc_msg_iter* args);
typedef void (*PlugInCmdFunc)(World* inWorld, void* inUserData, struct sc_msg_iter* args, void* replyAddr);