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
|
#include "SC_PlugIn.h"
// InterfaceTable contains pointers to functions in the host (server).
static InterfaceTable *ft;
struct StatsTriggeredUnit : public Unit
{
};
struct MeanTriggered : public StatsTriggeredUnit
{
float* m_circbuf;
int m_circbufpos;
float m_mean;
int m_length;
};
struct MedianTriggered : public StatsTriggeredUnit
{
float* m_circbuf;
float* m_sortbuf;
int m_circbufpos;
int m_length;
bool m_length_is_odd;
int m_medianpos;
float m_outval;
};
// declare unit generator functions
extern "C"
{
void load(InterfaceTable *inTable);
void MeanTriggered_Ctor(MeanTriggered* unit);
void MeanTriggered_next_xa(MeanTriggered *unit, int inNumSamples);
void MeanTriggered_next_xk(MeanTriggered *unit, int inNumSamples);
void MeanTriggered_Dtor(MeanTriggered* unit);
void MedianTriggered_Ctor(MedianTriggered* unit);
void MedianTriggered_next_xa(MedianTriggered *unit, int inNumSamples);
void MedianTriggered_next_xk(MedianTriggered *unit, int inNumSamples);
void MedianTriggered_SelectionSort(float *array, int length);
void MedianTriggered_Dtor(MedianTriggered* unit);
};
//////////////////////////////////////////////////////////////////
void MeanTriggered_Ctor(MeanTriggered* unit)
{
if (INRATE(1) == calc_FullRate) {
SETCALC(MeanTriggered_next_xa);
}else{
SETCALC(MeanTriggered_next_xk);
}
int length = (int)ZIN0(2); // Fixed number of items to average over
unit->m_circbuf = (float*)RTAlloc(unit->mWorld, length * sizeof(float));
for(int i=0; i<length; i++){
unit->m_circbuf[i] = 0.f;
}
unit->m_circbufpos = 0;
unit->m_length = length;
unit->m_mean = 0.f;
// prime the pumps
MeanTriggered_next_xk(unit, 1);
}
void MeanTriggered_next_xa(MeanTriggered* unit, int inNumSamples)
{
float *out = OUT(0);
float *in = IN(0);
float *trig = IN(1);
// Get state and instance variables from the struct
float* circbuf = unit->m_circbuf;
int circbufpos = unit->m_circbufpos;
int length = unit->m_length;
// This may or may not be recalculated as we go through the loop, depending on triggering
float mean = unit->m_mean;
float curr;
int i, j;
for(i=0; i<inNumSamples; ++i){
if(*(trig++) > 0.f){
curr = in[i];
//printf("%g, ", curr);
circbuf[circbufpos] = curr;
circbufpos++;
if(circbufpos==length){
circbufpos = 0;
}
double total = 0.;
for(j=0; j<length; ++j)
total += circbuf[j];
mean = (float)(total / length);
}
*(out++) = mean;
}
// Store state variables back
unit->m_circbufpos = circbufpos;
unit->m_mean = mean;
}
void MeanTriggered_next_xk(MeanTriggered* unit, int inNumSamples)
{
float *out = OUT(0);
float *in = IN(0);
float trig = IN0(1);
// Get state and instance variables from the struct
float* circbuf = unit->m_circbuf;
int circbufpos = unit->m_circbufpos;
int length = unit->m_length;
// This may or may not be recalculated as we go through the loop, depending on triggering
float mean = unit->m_mean;
float curr;
int i, j;
for(i=0; i<inNumSamples; ++i){
if(trig > 0.f){
curr = in[i];
//printf("%g, ", curr);
circbuf[circbufpos] = curr;
circbufpos++;
if(circbufpos==length){
circbufpos = 0;
}
double total = 0.;
for(j=0; j<length; ++j)
total += circbuf[j];
mean = (float)(total / length);
}
*(out++) = mean;
}
// Store state variables back
unit->m_circbufpos = circbufpos;
unit->m_mean = mean;
}
void MeanTriggered_Dtor(MeanTriggered* unit)
{
//printf("\n");
RTFree(unit->mWorld, unit->m_circbuf);
}
//////////////////////////////////////////////////////////////////
void MedianTriggered_Ctor(MedianTriggered* unit)
{
if (INRATE(1) == calc_FullRate) {
SETCALC(MedianTriggered_next_xa);
}else{
SETCALC(MedianTriggered_next_xk);
}
int length = (int)ZIN0(2); // Fixed number of items to average over
unit->m_circbuf = (float*)RTAlloc(unit->mWorld, length * sizeof(float));
unit->m_sortbuf = (float*)RTAlloc(unit->mWorld, length * sizeof(float));
for(int i=0; i<length; i++){
unit->m_circbuf[i] = 0.f;
}
unit->m_circbufpos = 0;
unit->m_length = length;
unit->m_outval = 0.f;
unit->m_length_is_odd = (length % 2) == 1;
// If odd, this number is the index to take. If even, we take this number and the one above.
unit->m_medianpos = unit->m_length_is_odd ? ((length-1)/2) : (length/2 - 1);
// prime the pumps
MedianTriggered_next_xk(unit, 1);
}
void MedianTriggered_next_xa(MedianTriggered* unit, int inNumSamples)
{
float *out = OUT(0);
float *in = IN(0);
float *trig = IN(1);
// Get state and instance variables from the struct
float* circbuf = unit->m_circbuf;
float* sortbuf = unit->m_sortbuf;
int circbufpos = unit->m_circbufpos;
int length = unit->m_length;
bool length_is_odd = unit->m_length_is_odd;
int medianpos = unit->m_medianpos;
// This may or may not be recalculated as we go through the loop, depending on triggering
float median = unit->m_outval;
float curr;
int i;
for(i=0; i<inNumSamples; ++i){
if(*(trig++) > 0.f){
curr = in[i];
circbuf[circbufpos] = curr;
circbufpos++;
if(circbufpos==length){
circbufpos = 0;
}
// NOW CALCULATE THE MEDIAN
// Copy the data into the buffer for sorting
// TODO: Implement the copy more efficiently using memcpy
for(int i=0; i<length; ++i){
sortbuf[i] = circbuf[i];
}
// Then sort
MedianTriggered_SelectionSort(sortbuf, length);
// Then update the median
median = length_is_odd ? sortbuf[medianpos] : ((sortbuf[medianpos] + sortbuf[medianpos+1]) * 0.5f);
}
*(out++) = median;
}
// Store state variables back
unit->m_circbufpos = circbufpos;
unit->m_outval = median;
}
void MedianTriggered_next_xk(MedianTriggered* unit, int inNumSamples)
{
float *out = OUT(0);
float *in = IN(0);
float trig = IN0(1);
// Get state and instance variables from the struct
float* circbuf = unit->m_circbuf;
float* sortbuf = unit->m_sortbuf;
int circbufpos = unit->m_circbufpos;
int length = unit->m_length;
bool length_is_odd = unit->m_length_is_odd;
int medianpos = unit->m_medianpos;
// This may or may not be recalculated as we go through the loop, depending on triggering
float median = unit->m_outval;
float curr;
int i;
for(i=0; i<inNumSamples; ++i){
if(trig > 0.f){
curr = in[i];
circbuf[circbufpos] = curr;
circbufpos++;
if(circbufpos==length){
circbufpos = 0;
}
// NOW CALCULATE THE MEDIAN
// Copy the data into the buffer for sorting
// TODO: Implement the copy more efficiently using memcpy
for(int i=0; i<length; ++i){
sortbuf[i] = circbuf[i];
}
// Then sort
MedianTriggered_SelectionSort(sortbuf, length);
// Then update the median
median = length_is_odd ? sortbuf[medianpos] : ((sortbuf[medianpos] + sortbuf[medianpos+1]) * 0.5f);
}
*(out++) = median;
}
// Store state variables back
unit->m_circbufpos = circbufpos;
unit->m_outval = median;
}
void MedianTriggered_Dtor(MedianTriggered* unit)
{
RTFree(unit->mWorld, unit->m_circbuf);
RTFree(unit->mWorld, unit->m_sortbuf);
}
void MedianTriggered_SelectionSort(float *array, int length)
{
// Algo is from http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Selection_sort
// Debug:
/*
int lllll = length;
Print("\nBefore selectionsort: ");
for(int j=0; j<lllll; j++){
Print("%g, ", array[j]);
}
*/
int max, i;
float temp;
while(length > 0)
{
max = 0;
for(i = 1; i < length; i++)
if(array[i] > array[max])
max = i;
temp = array[length-1];
array[length-1] = array[max];
array[max] = temp;
length--;
}
// Debug:
/*
Print("\nAfter selectionsort: ");
for(int j=0; j<lllll; j++){
Print("%g, ", array[j]);
}
*/
}
//////////////////////////////////////////////////////////////////
// the load function is called by the host when the plug-in is loaded
PluginLoad(MCLDTriggeredStats)
{
ft = inTable;
DefineDtorUnit(MeanTriggered);
DefineDtorUnit(MedianTriggered);
}
|