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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
* Created by Nicholas Collins on 13/08/2010.
* Copyright 2010 Nicholas M Collins. All rights reserved.
*/
//#define SC_DARWIN
#include "SC_PlugIn.h"
#include "FFT_UGens.h"
//Dec 6th 2013 added options for per frame normalisation, choice of octave ratio (allowing aggregation, say half-octave bands over 5 octave span, Bohlen-Pierce scale etc)
InterfaceTable *ft;
struct Chromagram : public Unit
{
int fftsize_;
float chromanorm_; //for normalisation
int numdivisions_;
int tuningbase_;
int octaves_;
float octaveratio_;
//int integrateflag_;
//float integrationconstant_;
int numindices_;
int * indexdata_;
float * indexweights_;
float * fftpower_;
float * chroma_;
};
extern "C" {
void Chromagram_next_k(Chromagram *unit, int inNumSamples);
void Chromagram_Ctor(Chromagram* unit);
void Chromagram_Dtor(Chromagram* unit);
}
void Chromagram_Ctor( Chromagram* unit ) {
int i, j;
double sr = unit->mWorld->mFullRate.mSampleRate; //never trust SAMPLERATE, gives UGens output rate, not audio rate
float nyquist = sr*0.5;
unit->fftsize_ = ZIN0(1);
float freqperbin = sr / unit->fftsize_;
int divisions = ZIN0(2);
float tuningbase = ZIN0(3);
int octaves = ZIN0(4);
float octaveratio = ZIN0(7);
if (octaves<1) octaves = 1;
float nyminusonebin = nyquist-freqperbin;
//must have room for at least one octave of divisions
if ((tuningbase<0.0) || (tuningbase>(nyminusonebin/octaveratio))) {
tuningbase = 32.703195662575 ; //C at MIDI note 24
}
unit->tuningbase_ = tuningbase;
//if octaves can't fit within available fft bins, have to cut short
float topfreq = tuningbase * pow(octaveratio,octaves);
//if can't fit all octaves into Nyquist
if (topfreq >= (nyminusonebin)) {
octaves = (int) (log2(nyminusonebin/tuningbase)/(log2(octaveratio))); //if octaveratio==2 denominator will be 1
}
if (octaves<1) octaves = 1;
unit->octaves_ = octaves;
//printf("checks %d %f %d %f %f %f %f \n", divisions, tuningbase, octaves, sr, nyminusonebin, topfreq, octaveratio);
//should include number of octaves as divisor too
unit->chromanorm_ = 1.0/((float)unit->fftsize_ * octaves);
//unit->integrateflag_ = ZIN0(5);
//unit->integrationconstant_ = ZIN0(6);
int totaldivisions = octaves * divisions;
unit->numindices_ = 2*totaldivisions;
int * indexdata = (int *)RTAlloc(unit->mWorld, sizeof(int)*unit->numindices_);
float * weightdata = (float *) RTAlloc(unit->mWorld, sizeof(float)*unit->numindices_);
unit->indexdata_ = indexdata;
unit->indexweights_ = weightdata;
//work out indices of fft bins corresponding to chroma positions, from basefreq up
//avoid lots of pow calls by working out cumulative ratios as go
float ratio = pow(octaveratio,1.0/divisions); //pow(2.0,1.0/divisions);
//float rationow= 1.0;
float octavenow = 1.0;
float temp = 1.0f/freqperbin;
int indexnow;
for ( j=0; j<octaves; ++j) {
float freqnow= tuningbase * octavenow;
indexnow = 2*j*divisions;
for ( i=0; i<divisions; ++i) {
//express as fft bins
float binpos = freqnow * temp;
int lower = (int) binpos;
float interp = binpos- lower;
int upper = lower+1;
//linear interpolation
indexdata[indexnow] = lower;
weightdata[indexnow] = 1.0 - interp;
//printf("lower oct %d div %d value: %d %f \n",j, i, indexdata[indexnow], weightdata[indexnow]);
++indexnow;
indexdata[indexnow] = upper;
weightdata[indexnow] = interp;
//printf("upper oct %d div %d value: %d %f \n",j, i, indexdata[indexnow], weightdata[indexnow]);
++indexnow;
freqnow *= ratio;
}
octavenow *= octaveratio; //2.0;
}
//would depend if taking harmonics as well as pure bin evidence
//int highestbin =
//unit->fftpower_ = RTAlloc(unit->mWorld, sizeof(float)*highestbin);
//for now only need to calculate as go, no need for intermediate storage
unit->chroma_ = (float*)RTAlloc(unit->mWorld, sizeof(float)*divisions);
SETCALC(Chromagram_next_k);
//in case of later interpolation, first output must be set to zero
for ( i=0; i<divisions; ++i) {
unit->chroma_[i] = 0.0;
ZOUT0(i) = 0.f;
}
unit->numdivisions_ = divisions;
}
void Chromagram_Dtor(Chromagram *unit)
{
RTFree(unit->mWorld, unit->indexdata_);
RTFree(unit->mWorld, unit->indexweights_);
RTFree(unit->mWorld, unit->chroma_);
}
//NEXT: destructor then next function using octaves and divisions slots, with appropriate power calc from fft data as go
void Chromagram_next_k( Chromagram *unit, int inNumSamples ) {
int i, j;
int divisions = unit->numdivisions_;
float * chroma = unit->chroma_;
int octaves = unit->octaves_;
int perframenormalize = ZIN0(8);
//float *input = IN(0);
//int numSamples = unit->mWorld->mFullRate.mBufLength;
//if input is legitimate buffer number:
float fbufnum = ZIN0(0);
//next FFT bufffer ready, update
//assuming at this point that buffer precalculated for any resampling
if (fbufnum > -0.01f) {
int 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;
}
//make sure in real and imag form
//SCComplexBuf * complexbuf = ToComplexApx(buf);
float * data= (float *)ToComplexApx(buf);
//float * data= buf->data;
//int numindices= unit->numindices_;
int * indexdata= unit->indexdata_;
float * indexweights= unit->indexweights_;
float real, imag;
int index, indexnow; //index2,
float weight1, intensity1, weight2, intensity2;
float norm = unit->chromanorm_;
//reset chroma unless keeping previous via leaky integration
if((int)(ZIN0(5)) > 0) {
float integration = ZIN0(6);
if (integration>0.999999999f) {
integration = 0.999999999f;
}
for ( i=0; i<divisions; ++i) {
chroma[i] *= integration;
}
} else {
for ( i=0; i<divisions; ++i) {
chroma[i] = 0.0;
}
}
//for (int j=0; j< numindices; j+=2) {
//
//
//
// }
//
for ( j=0; j<octaves; ++j) {
indexnow = 2*j*divisions;
for ( i=0; i<divisions; ++i) {
index = 2*indexdata[indexnow];
real= data[index];
imag= data[index+1];
intensity1 = (real*real) + (imag*imag);
weight1= indexweights[indexnow];
index+=2;
real= data[index];
imag= data[index+1];
intensity2 = (real*real) + (imag*imag);
weight2= indexweights[indexnow+1];
chroma[i] += (norm*(weight1*intensity1 + weight2*intensity2));
indexnow+=2;
}
}
if(perframenormalize) {
// //norm by max: actually want norm by sum
// float maxval = 0.0f;
//
// for (i=0; i<divisions; ++i) {
//
// if(chroma[i]>maxval) maxval = chroma[i];
// }
//
// //don't normalize if values negligible
// if (maxval>0.0001f) {
//
// maxval = 1.0f/maxval;
//
// for (i=0; i<divisions; ++i)
// chroma[i] *= maxval;
//
// }
//
//norm by sum; makes probability distribution over chroma
float sumval = 0.0f;
for (i=0; i<divisions; ++i) {
sumval += chroma[i];
}
//don't normalize if values negligible
if (sumval>0.0001f) {
sumval = 1.0f/sumval;
for (i=0; i<divisions; ++i)
chroma[i] *= sumval;
}
}
}
for (i=0; i<divisions; ++i)
ZOUT0(i) = chroma[i];
}
PluginLoad(Chromagram) {
init_SCComplex(inTable);
ft = inTable;
DefineDtorCantAliasUnit(Chromagram);
}
|