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
|
/*
"Unpack FFT" UGens for SuperCollider 3.
Copyright (c) 2007 Dan Stowell. All rights reserved.
(Written during the SC Symposium 2007! Thanks to all whose conversation fed into this.)
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
*/
#include "SC_PlugIn.h"
#include "SCComplex.h"
#include "FFT_UGens.h"
struct Unpack1FFT : Unit {
int bufsize;
int latestMomentProcessed; // To avoid processing a given FFT frame more than once
int binindex;
bool wantmag; // yes for mag, no for phase
float outval;
// int numOldSkipped; // for debug
};
struct PackFFT : Unit {
int bufsize, numinvals, frombin, tobin;
bool zeroothers;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" {
void Unpack1FFT_Ctor(Unpack1FFT* unit);
void Unpack1FFT_next_dc(Unpack1FFT* unit, int inNumSamples);
void Unpack1FFT_next_nyq(Unpack1FFT* unit, int inNumSamples);
void Unpack1FFT_next_mag(Unpack1FFT* unit, int inNumSamples);
void Unpack1FFT_next_phase(Unpack1FFT* unit, int inNumSamples);
void PackFFT_Ctor(PackFFT* unit);
void PackFFT_Dtor(PackFFT* unit);
void PackFFT_next(PackFFT* unit, int inNumSamples);
}
InterfaceTable* ft;
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Unpack1FFT_Ctor(Unpack1FFT* unit) {
unit->bufsize = (int)ZIN0(1);
unit->latestMomentProcessed = -1;
// unit->numOldSkipped = 0;
unit->outval = 0.f;
unit->binindex = (int)ZIN0(2);
if (ZIN0(3) == 0.f) {
// Mags
if (unit->binindex == 0) {
SETCALC(Unpack1FFT_next_dc);
} else if (unit->binindex == unit->bufsize >> 1) {
SETCALC(Unpack1FFT_next_nyq);
} else {
SETCALC(Unpack1FFT_next_mag);
}
} else {
// Phases
if (unit->binindex == 0) {
SETCALC(*ClearUnitOutputs);
} else if (unit->binindex == unit->bufsize >> 1) {
SETCALC(*ClearUnitOutputs);
} else {
SETCALC(Unpack1FFT_next_phase);
}
}
ZOUT0(0) = unit->outval;
}
#define UNPACK1FFT_NEXT_COMMON \
float fbufnum = ZIN0(0); \
if (fbufnum < 0.f) { \
if (unit->mWorld->mVerbosity > -1) { \
Print("Unpack1FFT_next: warning, fbufnum < 0\n"); \
} \
ZOUT0(0) = unit->outval; \
return; \
} \
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; \
if (unit->mWorld->mVerbosity > -1) { \
Print("Unpack1FFT_next: warning, bufnum too large: i%\n", ibufnum); \
} \
} \
} else { \
buf = world->mSndBufs + ibufnum; \
} \
int binindex __attribute__((__unused__)) = unit->binindex; \
LOCK_SNDBUF(buf); \
SCComplexBuf* p = ToComplexApx(buf);
void Unpack1FFT_next_mag(Unpack1FFT* unit, int inNumSamples) {
if (unit->latestMomentProcessed != unit->mWorld->mBufCounter) {
UNPACK1FFT_NEXT_COMMON
unit->outval = hypotf(p->bin[binindex - 1].real, p->bin[binindex - 1].imag);
unit->latestMomentProcessed = unit->mWorld->mBufCounter; // So we won't copy it again, not this frame anyway
// unit->numOldSkipped = 0;
//}else{
// Print("skipold{%i,%i}", unit->mWorld->mBufCounter, ++unit->numOldSkipped);
// Print("Calculation previously done - skipping. unit->mWorld->mBufCounter = %i\n", unit->mWorld->mBufCounter);
}
ZOUT0(0) = unit->outval;
}
void Unpack1FFT_next_phase(Unpack1FFT* unit, int inNumSamples) {
if (unit->latestMomentProcessed != unit->mWorld->mBufCounter) {
UNPACK1FFT_NEXT_COMMON
unit->outval = atan2(p->bin[binindex - 1].imag, p->bin[binindex - 1].real);
unit->latestMomentProcessed = unit->mWorld->mBufCounter; // So we won't copy it again, not this frame anyway
// unit->numOldSkipped = 0;
//}else{
// Print("skipold{%i,%i}", unit->mWorld->mBufCounter, ++unit->numOldSkipped);
// Print("Calculation previously done - skipping. unit->mWorld->mBufCounter = %i\n", unit->mWorld->mBufCounter);
}
ZOUT0(0) = unit->outval;
}
void Unpack1FFT_next_dc(Unpack1FFT* unit, int inNumSamples) {
if (unit->latestMomentProcessed != unit->mWorld->mBufCounter) {
UNPACK1FFT_NEXT_COMMON
unit->outval = p->dc;
unit->latestMomentProcessed = unit->mWorld->mBufCounter; // So we won't copy it again, not this frame anyway
// unit->numOldSkipped = 0;
//}else{
// Print("skipold{%i,%i}", unit->mWorld->mBufCounter, ++unit->numOldSkipped);
// Print("Calculation previously done - skipping. unit->mWorld->mBufCounter = %i\n", unit->mWorld->mBufCounter);
}
ZOUT0(0) = unit->outval;
}
void Unpack1FFT_next_nyq(Unpack1FFT* unit, int inNumSamples) {
if (unit->latestMomentProcessed != unit->mWorld->mBufCounter) {
UNPACK1FFT_NEXT_COMMON
unit->outval = p->nyq;
unit->latestMomentProcessed = unit->mWorld->mBufCounter; // So we won't copy it again, not this frame anyway
// unit->numOldSkipped = 0;
//}else{
// Print("skipold{%i,%i}", unit->mWorld->mBufCounter, ++unit->numOldSkipped);
// Print("Calculation previously done - skipping. unit->mWorld->mBufCounter = %i\n", unit->mWorld->mBufCounter);
}
ZOUT0(0) = unit->outval;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void PackFFT_Ctor(PackFFT* unit) {
SETCALC(PackFFT_next);
unit->bufsize = (int)ZIN0(1);
unit->frombin = (int)ZIN0(2);
unit->tobin = (int)ZIN0(3);
unit->zeroothers = ZIN0(4) > 0;
unit->numinvals = (int)ZIN0(5);
// Print("PackFFT_Ctor: Passing chain through, val %g\n", ZIN0(0));
ZOUT0(0) = ZIN0(0); // Required: allows the buffer index to fall through nicely to the IFFT
}
#define PACKFFT_INPUTSOFFSET 6
void PackFFT_next(PackFFT* unit, int inNumSamples) {
/////////////////// cf PV_GET_BUF
float fbufnum = ZIN0(0);
if (fbufnum < 0.f) {
ZOUT0(0) = -1.f;
return;
}
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;
/////////////////// cf PV_GET_BUF
// RM Print("PackFFT_next: fbufnum = %g\n", fbufnum);
int numinvals = unit->numinvals;
SCComplexBuf* p = ToComplexApx(buf);
int frombin = unit->frombin;
int tobin = unit->tobin;
int zeroothers = unit->zeroothers;
// Load data from inputs into "p"
if (frombin == 0) {
p->dc = DEMANDINPUT(PACKFFT_INPUTSOFFSET);
} else if (zeroothers) {
p->dc = 0.f;
}
// Print("New DC is %g\n", p->dc);
if (tobin == numbins + 1) {
// Print("PackFFT: Fetching nyquist from input #%i\n", (PACKFFT_INPUTSOFFSET + numinvals - 2 - frombin -
// frombin));
p->nyq = DEMANDINPUT(PACKFFT_INPUTSOFFSET + numinvals - 2 - frombin - frombin);
} else if (zeroothers) {
p->nyq = 0.f;
}
// Print("New nyq (input #%i) is %g\n", numinvals, p->nyq);
// real, imag = (mag * cos(phase), mag * sin(phase))
float mag, phase;
int startat = frombin == 0 ? 0 : frombin - 1;
int endbefore = sc_min(numbins, tobin);
for (int i = startat; i < endbefore; i++) {
// Print("PackFFT: Fetching mag from input #%i\n", (i + i + PACKFFT_INPUTSOFFSET + 2 - frombin - frombin));
mag = DEMANDINPUT(i + i + PACKFFT_INPUTSOFFSET + 2 - frombin - frombin);
phase = DEMANDINPUT(i + i + PACKFFT_INPUTSOFFSET + 3 - frombin - frombin);
p->bin[i].real = mag * cos(phase);
p->bin[i].imag = mag * sin(phase);
}
// Print("New bin 7 is %g,%g\n", p->bin[7].real, p->bin[7].imag);
if (zeroothers) {
// Iterate through the ones we didn't fill in, wiping the magnitude
for (int i = 0; i < startat; i++)
p->bin[i].real = p->bin[i].imag = 0.f;
for (int i = endbefore; i < numbins; i++)
p->bin[i].real = p->bin[i].imag = 0.f;
}
ZOUT0(0) = fbufnum;
// Print("PackFFT: fbufnum=%g, ibufnum=%i, numinvals=%i, frombin=%i, tobin=%i, zeroothers=%i\n",
// fbufnum, ibufnum, numinvals, frombin, tobin, zeroothers);
// Print("PackFFT: p->bin[4].real=%g, p->bin[4].imag=%g, p->bin[5].real=%g, p->bin[5].imag=%g\n",
// p->bin[4].real, p->bin[4].imag, p->bin[5].real, p->bin[5].imag);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
PluginLoad(UnpackFFTUGens) {
ft = inTable;
DefineSimpleUnit(Unpack1FFT);
DefineSimpleUnit(PackFFT);
}
|