File: FFT_UGens.h

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 80,296 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (153 lines) | stat: -rw-r--r-- 10,967 bytes parent folder | download | duplicates (3)
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
/*
    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_PlugIn.h"
#include "SCComplex.h"

#include <string.h>

struct SCComplexBuf {
    float dc, nyq;
    SCComplex bin[1];
};

struct SCPolarBuf {
    float dc, nyq;
    SCPolar bin[1];
};

static inline SCPolarBuf* ToPolarApx(SndBuf* buf) {
    if (buf->coord == coord_Complex) {
        SCComplexBuf* p = (SCComplexBuf*)buf->data;
        int numbins = (buf->samples - 2) >> 1;
        for (int i = 0; i < numbins; ++i) {
            p->bin[i].ToPolarApxInPlace();
        }
        buf->coord = coord_Polar;
    }

    return (SCPolarBuf*)buf->data;
}

static inline SCComplexBuf* ToComplexApx(SndBuf* buf) {
    if (buf->coord == coord_Polar) {
        SCPolarBuf* p = (SCPolarBuf*)buf->data;
        int numbins = (buf->samples - 2) >> 1;
        for (int i = 0; i < numbins; ++i) {
            p->bin[i].ToComplexApxInPlace();
        }
        buf->coord = coord_Complex;
    }
    return (SCComplexBuf*)buf->data;
}

struct PV_Unit : Unit {};

// Ordinary ClearUnitOutputs outputs zero, potentially telling the IFFT (+ PV UGens) to act on buffer zero, so let's
// skip that:
static inline void FFT_ClearUnitOutputs(Unit* unit, int wrongNumSamples) { ZOUT0(0) = -1; }

#define ClearFFTUnitIfMemFailed(condition)                                                                             \
    if (!(condition)) {                                                                                                \
        Print("%s: alloc failed, increase server's RT memory (e.g. via ServerOptions)\n", __func__);                   \
        SETCALC(FFT_ClearUnitOutputs);                                                                                 \
        unit->mDone = true;                                                                                            \
        return;                                                                                                        \
    }

#define sc_clipbuf(x, hi) ((x) >= (hi) ? 0 : ((x) < 0 ? 0 : (x)))

// for operation on one buffer
#define PV_GET_BUF                                                                                                     \
    float fbufnum = ZIN0(0);                                                                                           \
    if (fbufnum < 0.f) {                                                                                               \
        ZOUT0(0) = -1.f;                                                                                               \
        return;                                                                                                        \
    }                                                                                                                  \
    ZOUT0(0) = fbufnum;                                                                                                \
    uint32 ibufnum = (uint32)fbufnum;                                                                                  \
    World* world = unit->mWorld;                                                                                       \
    SndBuf* buf;                                                                                                       \
    if (ibufnum >= world->mNumSndBufs) {                                                                               \
        int localBufNum = ibufnum - world->mNumSndBufs;                                                                \
        Graph* parent = unit->mParent;                                                                                 \
        if (localBufNum <= parent->localBufNum) {                                                                      \
            buf = parent->mLocalSndBufs + localBufNum;                                                                 \
        } else {                                                                                                       \
            buf = world->mSndBufs;                                                                                     \
        }                                                                                                              \
    } else {                                                                                                           \
        buf = world->mSndBufs + ibufnum;                                                                               \
    }                                                                                                                  \
    LOCK_SNDBUF(buf);                                                                                                  \
    int numbins = (buf->samples - 2) >> 1;


// for operation on two input buffers, result goes in first one.
#define PV_GET_BUF2                                                                                                    \
    float fbufnum1 = ZIN0(0);                                                                                          \
    float fbufnum2 = ZIN0(1);                                                                                          \
    if (fbufnum1 < 0.f || fbufnum2 < 0.f) {                                                                            \
        ZOUT0(0) = -1.f;                                                                                               \
        return;                                                                                                        \
    }                                                                                                                  \
    ZOUT0(0) = fbufnum1;                                                                                               \
    uint32 ibufnum1 = (int)fbufnum1;                                                                                   \
    uint32 ibufnum2 = (int)fbufnum2;                                                                                   \
    World* world = unit->mWorld;                                                                                       \
    SndBuf* buf1;                                                                                                      \
    SndBuf* buf2;                                                                                                      \
    if (ibufnum1 >= world->mNumSndBufs) {                                                                              \
        int localBufNum = ibufnum1 - world->mNumSndBufs;                                                               \
        Graph* parent = unit->mParent;                                                                                 \
        if (localBufNum <= parent->localBufNum) {                                                                      \
            buf1 = parent->mLocalSndBufs + localBufNum;                                                                \
        } else {                                                                                                       \
            buf1 = world->mSndBufs;                                                                                    \
        }                                                                                                              \
    } else {                                                                                                           \
        buf1 = world->mSndBufs + ibufnum1;                                                                             \
    }                                                                                                                  \
    if (ibufnum2 >= world->mNumSndBufs) {                                                                              \
        int localBufNum = ibufnum2 - world->mNumSndBufs;                                                               \
        Graph* parent = unit->mParent;                                                                                 \
        if (localBufNum <= parent->localBufNum) {                                                                      \
            buf2 = parent->mLocalSndBufs + localBufNum;                                                                \
        } else {                                                                                                       \
            buf2 = world->mSndBufs;                                                                                    \
        }                                                                                                              \
    } else {                                                                                                           \
        buf2 = world->mSndBufs + ibufnum2;                                                                             \
    }                                                                                                                  \
    LOCK_SNDBUF2(buf1, buf2);                                                                                          \
    if (buf1->samples != buf2->samples)                                                                                \
        return;                                                                                                        \
    int numbins = (buf1->samples - 2) >> 1;

#define MAKE_TEMP_BUF                                                                                                  \
    if (!unit->m_tempbuf) {                                                                                            \
        unit->m_tempbuf = (float*)RTAlloc(unit->mWorld, buf->samples * sizeof(float));                                 \
        unit->m_numbins = numbins;                                                                                     \
    } else if (numbins != unit->m_numbins)                                                                             \
        return;

extern InterfaceTable* ft;