329 |
*/ |
*/ |
330 |
class Smoother { |
class Smoother { |
331 |
protected: |
protected: |
332 |
uint timeSteps; // The number of time steps to reach the goal |
uint timeSteps; // The number of time steps to reach the goal |
333 |
uint currentTimeStep; |
uint currentTimeStep; |
334 |
uint8_t goal; // 0 - 127 |
float goal; |
335 |
uint8_t prev; // 0 - 127 |
float prev; |
336 |
|
|
337 |
public: |
public: |
338 |
/** |
/** |
341 |
* @param sampleRate |
* @param sampleRate |
342 |
* @param val The initial value |
* @param val The initial value |
343 |
*/ |
*/ |
344 |
void trigger(float time, float sampleRate, uint8_t val = 0) { |
void trigger(float time, float sampleRate, float val = 0) { |
345 |
currentTimeStep = timeSteps = time * sampleRate; |
currentTimeStep = timeSteps = time * sampleRate; |
346 |
prev = goal = val; |
prev = goal = val; |
347 |
} |
} |
350 |
* Set the current value, which the smoother will not smooth out. |
* Set the current value, which the smoother will not smooth out. |
351 |
* If you want the value to be smoothen out, use update() instead. |
* If you want the value to be smoothen out, use update() instead. |
352 |
*/ |
*/ |
353 |
void setValue( uint8_t val) { |
void setValue( float val) { |
354 |
currentTimeStep = timeSteps; |
currentTimeStep = timeSteps; |
355 |
prev = goal = val; |
prev = goal = val; |
356 |
} |
} |
359 |
* Sets a new value. The render function will return |
* Sets a new value. The render function will return |
360 |
* values gradually approaching this value. |
* values gradually approaching this value. |
361 |
*/ |
*/ |
362 |
void update(uint8_t val) { |
void update(float val) { |
363 |
if (val == goal) return; |
if (val == goal) return; |
364 |
|
|
365 |
prev = prev + (goal - prev) * (currentTimeStep / (float)timeSteps); |
prev = prev + (goal - prev) * (currentTimeStep / (float)timeSteps); |
367 |
currentTimeStep = 0; |
currentTimeStep = 0; |
368 |
} |
} |
369 |
|
|
370 |
uint8_t render() { |
float render() { |
371 |
if (currentTimeStep >= timeSteps) return goal; |
if (currentTimeStep >= timeSteps) return goal; |
372 |
return prev + (goal - prev) * (currentTimeStep++ / (float)timeSteps); |
return prev + (goal - prev) * (currentTimeStep++ / (float)timeSteps); |
373 |
} |
} |
374 |
|
|
375 |
bool isSmoothingOut() { return currentTimeStep < timeSteps; } |
bool isSmoothingOut() { return currentTimeStep < timeSteps; } |
376 |
|
|
377 |
|
float getGoal() { return goal; } |
378 |
}; |
}; |
379 |
|
|
380 |
/** |
/** |
396 |
uint8_t Value; ///< Controller Value. |
uint8_t Value; ///< Controller Value. |
397 |
short int Curve; ///< specifies the curve type |
short int Curve; ///< specifies the curve type |
398 |
float Influence; |
float Influence; |
399 |
|
float Step; |
400 |
|
|
401 |
Smoother* pSmoother; |
Smoother* pSmoother; |
402 |
|
|
403 |
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1, Smoother* pSmoother = NULL) { |
CC ( |
404 |
|
uint8_t Controller = 0, |
405 |
|
float Influence = 0.0f, |
406 |
|
short int Curve = -1, |
407 |
|
Smoother* pSmoother = NULL, |
408 |
|
float Step = 0 |
409 |
|
) { |
410 |
this->Controller = Controller; |
this->Controller = Controller; |
411 |
this->Value = 0; |
this->Value = 0; |
412 |
this->Curve = Curve; |
this->Curve = Curve; |
413 |
this->Influence = Influence; |
this->Influence = Influence; |
414 |
this->pSmoother = pSmoother; |
this->pSmoother = pSmoother; |
415 |
|
this->Step = Step; |
416 |
} |
} |
417 |
|
|
418 |
CC(const CC& cc) { Copy(cc); } |
CC(const CC& cc) { Copy(cc); } |
420 |
|
|
421 |
void Copy(const CC& cc) { |
void Copy(const CC& cc) { |
422 |
Controller = cc.Controller; |
Controller = cc.Controller; |
423 |
Value = cc.Value; |
Value = cc.Value; |
424 |
Influence = cc.Influence; |
Influence = cc.Influence; |
425 |
Curve = cc.Curve; |
Curve = cc.Curve; |
426 |
pSmoother = cc.pSmoother; |
pSmoother = cc.pSmoother; |
427 |
|
Step = cc.Step; |
428 |
} |
} |
429 |
}; |
}; |
430 |
|
|
466 |
pCtrls = new RTList<CC>(pCCPool); |
pCtrls = new RTList<CC>(pCCPool); |
467 |
} |
} |
468 |
|
|
469 |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1, Smoother* pSmoother = NULL) { |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1, Smoother* pSmoother = NULL, float Step = 0) { |
470 |
if(pCtrls->poolIsEmpty()) { |
if(pCtrls->poolIsEmpty()) { |
471 |
std::cerr << "Maximum number of CC reached!" << std::endl; |
std::cerr << "Maximum number of CC reached!" << std::endl; |
472 |
return; |
return; |
473 |
} |
} |
474 |
*(pCtrls->allocAppend()) = CC(Controller, Influence, Curve, pSmoother); |
*(pCtrls->allocAppend()) = CC(Controller, Influence, Curve, pSmoother, Step); |
475 |
if (pSmoother != NULL) hasSmoothCtrls = true; |
if (pSmoother != NULL) hasSmoothCtrls = true; |
476 |
} |
} |
477 |
|
|
496 |
for(; ctrl != end; ++ctrl) { |
for(; ctrl != end; ++ctrl) { |
497 |
if (Controller != (*ctrl).Controller) continue; |
if (Controller != (*ctrl).Controller) continue; |
498 |
if ((*ctrl).Value == Value) continue; |
if ((*ctrl).Value == Value) continue; |
499 |
|
|
500 |
(*ctrl).Value = Value; |
(*ctrl).Value = Value; |
501 |
if ((*ctrl).pSmoother != NULL) (*ctrl).pSmoother->update(Value); |
|
502 |
|
if ((*ctrl).Step > 0 && (*ctrl).pSmoother != NULL) { |
503 |
|
float oldGoal = (*ctrl).pSmoother->getGoal(); |
504 |
|
float newGoal = Normalize(Value, (*ctrl).Curve) * (*ctrl).Influence; |
505 |
|
newGoal = ((int) (newGoal / (*ctrl).Step)) * (*ctrl).Step; |
506 |
|
if (oldGoal != newGoal) (*ctrl).pSmoother->update(newGoal); |
507 |
|
} |
508 |
|
|
509 |
|
if ((*ctrl).pSmoother != NULL && (*ctrl).Step <= 0) (*ctrl).pSmoother->update(Value); |
510 |
if (!bActive) bActive = true; |
if (!bActive) bActive = true; |
511 |
recalculate = true; |
recalculate = true; |
512 |
} |
} |
521 |
RTList<CC>::Iterator end = pCtrls->end(); |
RTList<CC>::Iterator end = pCtrls->end(); |
522 |
for(; ctrl != end; ++ctrl) { |
for(; ctrl != end; ++ctrl) { |
523 |
if ((*ctrl).pSmoother == NULL) { |
if ((*ctrl).pSmoother == NULL) { |
524 |
l += Normalize((*ctrl).Value, (*ctrl).Curve) * (*ctrl).Influence; |
float val = Normalize((*ctrl).Value, (*ctrl).Curve) * (*ctrl).Influence; |
525 |
|
if ((*ctrl).Step > 0) val = ( (int)(val / (*ctrl).Step) ) * (*ctrl).Step; |
526 |
|
l += val; |
527 |
} else { |
} else { |
528 |
if ((*ctrl).pSmoother->isSmoothingOut()) isSmoothingOut = true; |
if ((*ctrl).pSmoother->isSmoothingOut()) isSmoothingOut = true; |
529 |
l += Normalize((*ctrl).pSmoother->render(), (*ctrl).Curve) * (*ctrl).Influence; |
|
530 |
|
if ((*ctrl).Step > 0) { |
531 |
|
l += (*ctrl).pSmoother->render(); |
532 |
|
} else { |
533 |
|
l += Normalize((*ctrl).pSmoother->render(), (*ctrl).Curve) * (*ctrl).Influence; |
534 |
|
} |
535 |
} |
} |
536 |
} |
} |
537 |
if (Level != l) { |
if (Level != l) { |