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
|
/*
Spectral statistics UGens for SuperCollider, by Dan Stowell.
Copyright (c) Dan Stowell 2006-2007.
Now part of SuperCollider 3, (c) James McCartney.
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"
#include "ML.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
struct FFTAnalyser_Unit : Unit
{
float outval;
// Not always used: multipliers which convert from bin indices to freq vals, and vice versa.
// See also the macros for deriving these.
float m_bintofreq; // , m_freqtobin;
};
struct FFTAnalyser_OutOfPlace : FFTAnalyser_Unit
{
int m_numbins;
float *m_tempbuf;
};
struct SpecFlatness : FFTAnalyser_Unit
{
};
struct SpecPcile : FFTAnalyser_OutOfPlace
{
bool m_interpolate;
};
struct SpecCentroid : FFTAnalyser_Unit
{
};
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
// for operation on one buffer
// just like PV_GET_BUF except it outputs unit->outval rather than -1 when FFT not triggered
#define FFTAnalyser_GET_BUF \
float fbufnum = ZIN0(0); \
if (fbufnum < 0.f) { ZOUT0(0) = unit->outval; 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;
// Copied from FFT_UGens.cpp
#define GET_BINTOFREQ \
if(unit->m_bintofreq==0.f){ \
unit->m_bintofreq = world->mFullRate.mSampleRate / buf->samples; \
} \
float bintofreq = unit->m_bintofreq;
/*
#define GET_FREQTOBIN \
if(unit->m_freqtobin==0.f){ \
unit->m_freqtobin = buf->samples / world->mFullRate.mSampleRate; \
} \
float freqtobin = unit->m_freqtobin;
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
extern "C"
{
void SpecFlatness_Ctor(SpecFlatness *unit);
void SpecFlatness_next(SpecFlatness *unit, int inNumSamples);
void SpecPcile_Ctor(SpecPcile *unit);
void SpecPcile_next(SpecPcile *unit, int inNumSamples);
void SpecPcile_Dtor(SpecPcile *unit);
void SpecCentroid_Ctor(SpecCentroid *unit);
void SpecCentroid_next(SpecCentroid *unit, int inNumSamples);
}
*/
/*
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;
}
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;
}
InterfaceTable *ft;
void init_SCComplex(InterfaceTable *inTable);
*/
//////////////////////////////////////////////////////////////////////////////////////////////////
void SpecFlatness_Ctor(SpecFlatness *unit)
{
SETCALC(SpecFlatness_next);
ZOUT0(0) = unit->outval = 0.;
unit->m_oneovern = 0.;
}
void SpecFlatness_next(SpecFlatness *unit, int inNumSamples)
{
FFTAnalyser_GET_BUF
if(unit->m_oneovern == 0.)
unit->m_oneovern = 1./(numbins + 2);
SCComplexBuf *p = ToComplexApx(buf);
// Spectral Flatness Measure is geometric mean divided by arithmetic mean.
//
// In order to calculate geom mean without hitting the precision limit,
// we use the trick of converting to log, taking the average, then converting back from log.
double geommean = std::log(sc_abs(p->dc)) + std::log(sc_abs(p->nyq));
double mean = sc_abs(p->dc) + sc_abs(p->nyq);
for (int i=0; i<numbins; ++i) {
float rabs = (p->bin[i].real);
float iabs = (p->bin[i].imag);
float amp = std::sqrt((rabs*rabs) + (iabs*iabs));
if(amp != 0.f) { // zeroes lead to NaNs
geommean += std::log(amp);
mean += amp;
}
}
double oneovern = unit->m_oneovern;
geommean = exp(geommean * oneovern); // Average and then convert back to linear
mean *= oneovern;
// Store the val for output in future calls
unit->outval = (mean==0.f ? 0.8f : (geommean / mean));
// Note: for silence the value is undefined.
// Here, for silence we instead output an empirical value based on very quiet white noise.
ZOUT0(0) = unit->outval;
}
////////////////////////////////////////////////////////////////////////////////////
void SpecPcile_Ctor(SpecPcile *unit)
{
SETCALC(SpecPcile_next);
unit->m_interpolate = ZIN0(2) > 0.f;
ZOUT0(0) = unit->outval = 0.;
unit->m_tempbuf = 0;
}
void SpecPcile_next(SpecPcile *unit, int inNumSamples)
{
FFTAnalyser_GET_BUF
// Used to be MAKE_TEMP_BUF but we can handle it more cleanly in this specific case:
if (!unit->m_tempbuf) {
unit->m_tempbuf = (float*)RTAlloc(unit->mWorld, numbins * sizeof(float));
unit->m_numbins = numbins;
unit->m_halfnyq_over_numbinsp2 = ((float)unit->mWorld->mSampleRate) * 0.5f / (float)(numbins+2);
} else if (numbins != unit->m_numbins) return;
// Percentile value as a fraction. eg: 0.5 == 50-percentile (median).
float fraction = ZIN0(1);
bool interpolate = unit->m_interpolate;
// The magnitudes in *p will be converted to cumulative sum values and stored in *q temporarily
SCComplexBuf *p = ToComplexApx(buf);
float *q = (float*)unit->m_tempbuf;
float cumul = sc_abs(p->dc);
for (int i=0; i<numbins; ++i) {
float real = p->bin[i].real;
float imag = p->bin[i].imag;
cumul += std::sqrt(real*real + imag*imag);
// A convenient place to store the mag values...
q[i] = cumul;
}
cumul += sc_abs(p->nyq);
float target = cumul * fraction; // The target cumul value, stored somewhere in q
float bestposition = 0; // May be linear-interpolated between bins, but not implemented yet
// NB If nothing beats the target (e.g. if fraction is -1) zero Hz is returned
float binpos;
for(int i=0; i<numbins; ++i) {
//Print("Testing %g, at position %i", q->bin[i].real, i);
if(!(q[i] < target)){ // this is a ">=" comparison, done more efficiently as "!(<)"
if(interpolate && i!=0) {
binpos = ((float)i) + 1.f - (q[i] - target) / (q[i] - q[i-1]);
} else {
binpos = ((float)i) + 1.f;
}
bestposition = binpos * unit->m_halfnyq_over_numbinsp2;
//Print("Target %g beaten by %g (at position %i), equating to freq %g\n",
// target, p->bin[i].real, i, bestposition);
break;
}
}
// Store the val for output in future calls
unit->outval = bestposition;
ZOUT0(0) = unit->outval;
}
void SpecPcile_Dtor(SpecPcile *unit)
{
if(unit->m_tempbuf) RTFree(unit->mWorld, unit->m_tempbuf);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void SpecCentroid_Ctor(SpecCentroid *unit)
{
SETCALC(SpecCentroid_next);
ZOUT0(0) = unit->outval = 0.;
unit->m_bintofreq = 0.f;
}
void SpecCentroid_next(SpecCentroid *unit, int inNumSamples)
{
FFTAnalyser_GET_BUF
SCPolarBuf *p = ToPolarApx(buf);
GET_BINTOFREQ
double num = sc_abs(p->nyq) * (numbins+1);
double denom = sc_abs(p->nyq);
for (int i=0; i<numbins; ++i) {
num += sc_abs(p->bin[i].mag) * (i+1);
denom += sc_abs(p->bin[i].mag);
}
ZOUT0(0) = unit->outval = denom == 0.0 ? 0.f : (float) (bintofreq * num/denom);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
void load(InterfaceTable *inTable)
{
ft= inTable;
//(*ft->fDefineUnit)("SpecFlatness", sizeof(FFTAnalyser_Unit), (UnitCtorFunc)&SpecFlatness_Ctor, 0, 0);
//(*ft->fDefineUnit)("SpecPcile", sizeof(SpecPcile_Unit), (UnitCtorFunc)&SpecPcile_Ctor, (UnitDtorFunc)&SpecPcile_Dtor, 0);
DefineSimpleUnit(SpecFlatness);
DefineDtorUnit(SpecPcile);
DefineSimpleUnit(SpecCentroid);
}
*/
|