common: prevent buffer overflow
[supercollider.git] / include / plugin_interface / FFT_UGens.h
blobc0adcc06d8ba2077fd83068a37f34ccb9f6255ae
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef FFT_UGENS_H
22 #define FFT_UGENS_H
24 #include "SC_PlugIn.h"
25 #include "SCComplex.h"
27 #include <string.h>
29 struct SCComplexBuf
31 float dc, nyq;
32 SCComplex bin[1];
35 struct SCPolarBuf
37 float dc, nyq;
38 SCPolar bin[1];
41 static inline SCPolarBuf* ToPolarApx(SndBuf *buf)
43 if (buf->coord == coord_Complex) {
44 SCComplexBuf* p = (SCComplexBuf*)buf->data;
45 int numbins = buf->samples - 2 >> 1;
46 for (int i=0; i<numbins; ++i) {
47 p->bin[i].ToPolarApxInPlace();
49 buf->coord = coord_Polar;
52 return (SCPolarBuf*)buf->data;
55 static inline SCComplexBuf* ToComplexApx(SndBuf *buf)
57 if (buf->coord == coord_Polar) {
58 SCPolarBuf* p = (SCPolarBuf*)buf->data;
59 int numbins = buf->samples - 2 >> 1;
60 for (int i=0; i<numbins; ++i) {
61 p->bin[i].ToComplexApxInPlace();
63 buf->coord = coord_Complex;
65 return (SCComplexBuf*)buf->data;
68 struct PV_Unit : Unit
73 #define sc_clipbuf(x, hi) ((x) >= (hi) ? 0 : ((x) < 0 ? 0 : (x)))
75 // for operation on one buffer
76 #define PV_GET_BUF \
77 float fbufnum = ZIN0(0); \
78 if (fbufnum < 0.f) { ZOUT0(0) = -1.f; return; } \
79 ZOUT0(0) = fbufnum; \
80 uint32 ibufnum = (uint32)fbufnum; \
81 World *world = unit->mWorld; \
82 SndBuf *buf; \
83 if (ibufnum >= world->mNumSndBufs) { \
84 int localBufNum = ibufnum - world->mNumSndBufs; \
85 Graph *parent = unit->mParent; \
86 if(localBufNum <= parent->localBufNum) { \
87 buf = parent->mLocalSndBufs + localBufNum; \
88 } else { \
89 buf = world->mSndBufs; \
90 } \
91 } else { \
92 buf = world->mSndBufs + ibufnum; \
93 } \
94 LOCK_SNDBUF(buf); \
95 int numbins = buf->samples - 2 >> 1; \
98 // for operation on two input buffers, result goes in first one.
99 #define PV_GET_BUF2 \
100 float fbufnum1 = ZIN0(0); \
101 float fbufnum2 = ZIN0(1); \
102 if (fbufnum1 < 0.f || fbufnum2 < 0.f) { ZOUT0(0) = -1.f; return; } \
103 ZOUT0(0) = fbufnum1; \
104 uint32 ibufnum1 = (int)fbufnum1; \
105 uint32 ibufnum2 = (int)fbufnum2; \
106 World *world = unit->mWorld; \
107 SndBuf *buf1; \
108 SndBuf *buf2; \
109 if (ibufnum1 >= world->mNumSndBufs) { \
110 int localBufNum = ibufnum1 - world->mNumSndBufs; \
111 Graph *parent = unit->mParent; \
112 if(localBufNum <= parent->localBufNum) { \
113 buf1 = parent->mLocalSndBufs + localBufNum; \
114 } else { \
115 buf1 = world->mSndBufs; \
117 } else { \
118 buf1 = world->mSndBufs + ibufnum1; \
120 if (ibufnum2 >= world->mNumSndBufs) { \
121 int localBufNum = ibufnum2 - world->mNumSndBufs; \
122 Graph *parent = unit->mParent; \
123 if(localBufNum <= parent->localBufNum) { \
124 buf2 = parent->mLocalSndBufs + localBufNum; \
125 } else { \
126 buf2 = world->mSndBufs; \
128 } else { \
129 buf2 = world->mSndBufs + ibufnum2; \
131 LOCK_SNDBUF2(buf1, buf2); \
132 if (buf1->samples != buf2->samples) return; \
133 int numbins = buf1->samples - 2 >> 1;
135 #define MAKE_TEMP_BUF \
136 if (!unit->m_tempbuf) { \
137 unit->m_tempbuf = (float*)RTAlloc(unit->mWorld, buf->samples * sizeof(float)); \
138 unit->m_numbins = numbins; \
139 } else if (numbins != unit->m_numbins) return;
141 extern InterfaceTable *ft;
143 #endif