24 |
#define __LS_SIGNALUNIT_H__ |
#define __LS_SIGNALUNIT_H__ |
25 |
|
|
26 |
#include "../../common/ArrayList.h" |
#include "../../common/ArrayList.h" |
27 |
|
#include "../../common/Pool.h" |
28 |
|
|
29 |
|
|
30 |
namespace LinuxSampler { |
namespace LinuxSampler { |
39 |
} |
} |
40 |
|
|
41 |
~FixedArray() { |
~FixedArray() { |
42 |
delete pData; |
delete[] pData; |
43 |
pData = NULL; |
pData = NULL; |
44 |
} |
} |
45 |
|
|
52 |
} |
} |
53 |
|
|
54 |
|
|
55 |
T increment() { |
T& increment() { |
56 |
if (iSize >= iCapacity) throw Exception("Array out of bounds"); |
if (iSize >= iCapacity) throw Exception("Array out of bounds"); |
57 |
return pData[iSize++]; |
return pData[iSize++]; |
58 |
} |
} |
167 |
SignalUnit(SignalUnitRack* rack): pRack(rack), bActive(false), Level(0.0f), bCalculating(false), uiDelayTrigger(0) { } |
SignalUnit(SignalUnitRack* rack): pRack(rack), bActive(false), Level(0.0f), bCalculating(false), uiDelayTrigger(0) { } |
168 |
SignalUnit(const SignalUnit& Unit): pRack(Unit.pRack) { Copy(Unit); } |
SignalUnit(const SignalUnit& Unit): pRack(Unit.pRack) { Copy(Unit); } |
169 |
void operator=(const SignalUnit& Unit) { Copy(Unit); } |
void operator=(const SignalUnit& Unit) { Copy(Unit); } |
170 |
|
virtual ~SignalUnit() { } |
171 |
|
|
172 |
void Copy(const SignalUnit& Unit) { |
void Copy(const SignalUnit& Unit) { |
173 |
if (this == &Unit) return; |
if (this == &Unit) return; |
325 |
}; |
}; |
326 |
|
|
327 |
/** |
/** |
328 |
|
* Used to smooth out the parameter changes. |
329 |
|
*/ |
330 |
|
class Smoother { |
331 |
|
protected: |
332 |
|
uint timeSteps; // The number of time steps to reach the goal |
333 |
|
uint currentTimeStep; |
334 |
|
uint8_t goal; // 0 - 127 |
335 |
|
uint8_t prev; // 0 - 127 |
336 |
|
|
337 |
|
public: |
338 |
|
/** |
339 |
|
* |
340 |
|
* @param time The time (in seconds) to reach the goal |
341 |
|
* @param sampleRate |
342 |
|
* @param val The initial value |
343 |
|
*/ |
344 |
|
void trigger(float time, float sampleRate, uint8_t val = 0) { |
345 |
|
currentTimeStep = timeSteps = time * sampleRate; |
346 |
|
prev = goal = val; |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
|
* Set the current value, which the smoother will not smooth out. |
351 |
|
* If you want the value to be smoothen out, use update() instead. |
352 |
|
*/ |
353 |
|
void setValue( uint8_t val) { |
354 |
|
currentTimeStep = timeSteps; |
355 |
|
prev = goal = val; |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
|
* Sets a new value. The render function will return |
360 |
|
* values gradually approaching this value. |
361 |
|
*/ |
362 |
|
void update(uint8_t val) { |
363 |
|
if (val == goal) return; |
364 |
|
|
365 |
|
prev = prev + (goal - prev) * (currentTimeStep / (float)timeSteps); |
366 |
|
goal = val; |
367 |
|
currentTimeStep = 0; |
368 |
|
} |
369 |
|
|
370 |
|
uint8_t render() { |
371 |
|
if (currentTimeStep >= timeSteps) return goal; |
372 |
|
return prev + (goal - prev) * (currentTimeStep++ / (float)timeSteps); |
373 |
|
} |
374 |
|
|
375 |
|
bool isSmoothingOut() { return currentTimeStep < timeSteps; } |
376 |
|
}; |
377 |
|
|
378 |
|
/** |
379 |
* Continuous controller signal unit. |
* Continuous controller signal unit. |
380 |
* The level of this unit corresponds to the controllers changes |
* The level of this unit corresponds to the controllers changes |
381 |
* and their influences. |
* and their influences. |
388 |
virtual void ValueChanged(CCSignalUnit* pUnit) = 0; |
virtual void ValueChanged(CCSignalUnit* pUnit) = 0; |
389 |
}; |
}; |
390 |
|
|
|
protected: |
|
391 |
class CC { |
class CC { |
392 |
public: |
public: |
393 |
uint8_t Controller; ///< MIDI controller number. |
uint8_t Controller; ///< MIDI controller number. |
395 |
short int Curve; ///< specifies the curve type |
short int Curve; ///< specifies the curve type |
396 |
float Influence; |
float Influence; |
397 |
|
|
398 |
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1) { |
Smoother* pSmoother; |
399 |
|
|
400 |
|
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1, Smoother* pSmoother = NULL) { |
401 |
this->Controller = Controller; |
this->Controller = Controller; |
402 |
this->Value = 0; |
this->Value = 0; |
403 |
this->Curve = Curve; |
this->Curve = Curve; |
404 |
this->Influence = Influence; |
this->Influence = Influence; |
405 |
|
this->pSmoother = pSmoother; |
406 |
} |
} |
407 |
|
|
408 |
CC(const CC& cc) { Copy(cc); } |
CC(const CC& cc) { Copy(cc); } |
413 |
Value = cc.Value; |
Value = cc.Value; |
414 |
Influence = cc.Influence; |
Influence = cc.Influence; |
415 |
Curve = cc.Curve; |
Curve = cc.Curve; |
416 |
|
pSmoother = cc.pSmoother; |
417 |
} |
} |
418 |
}; |
}; |
419 |
|
|
420 |
FixedArray<CC> Ctrls; // The MIDI controllers which modulates this signal unit. |
protected: |
421 |
|
RTList<CC>* pCtrls; // The MIDI controllers which modulates this signal unit. |
422 |
Listener* pListener; |
Listener* pListener; |
423 |
|
bool hasSmoothCtrls; // determines whether there are smooth controllers (used for optimization) |
424 |
|
bool isSmoothingOut; // determines whether there is a CC which is in process of smoothing out (used for optimization) |
425 |
|
|
426 |
public: |
public: |
427 |
|
|
428 |
CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), Ctrls(128) { |
CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), pCtrls(NULL) { |
429 |
pListener = l; |
pListener = l; |
430 |
|
hasSmoothCtrls = isSmoothingOut = false; |
431 |
} |
} |
432 |
|
|
433 |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), Ctrls(128) { Copy(Unit); } |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), pCtrls(NULL) { Copy(Unit); } |
434 |
void operator=(const CCSignalUnit& Unit) { Copy(Unit); } |
void operator=(const CCSignalUnit& Unit) { Copy(Unit); } |
435 |
|
|
436 |
|
virtual ~CCSignalUnit() { |
437 |
|
if (pCtrls != NULL) delete pCtrls; |
438 |
|
} |
439 |
|
|
440 |
void Copy(const CCSignalUnit& Unit) { |
void Copy(const CCSignalUnit& Unit) { |
441 |
Ctrls.copy(Unit.Ctrls); |
if (pCtrls != NULL) delete pCtrls; |
442 |
|
pCtrls = new RTList<CC>(*(Unit.pCtrls)); |
443 |
|
if (pCtrls->poolIsEmpty() && pCtrls->count() < Unit.pCtrls->count()) { |
444 |
|
std::cerr << "Maximum number of CC reached!" << std::endl; |
445 |
|
} |
446 |
|
|
447 |
pListener = Unit.pListener; |
pListener = Unit.pListener; |
448 |
|
hasSmoothCtrls = Unit.hasSmoothCtrls; |
449 |
|
isSmoothingOut = Unit.isSmoothingOut; |
450 |
SignalUnit::Copy(Unit); |
SignalUnit::Copy(Unit); |
451 |
} |
} |
452 |
|
|
453 |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1) { |
virtual void InitCCList(Pool<CC>* pCCPool, Pool<Smoother>* pSmootherPool) { |
454 |
Ctrls.add(CC(Controller, Influence, Curve)); |
if (pCtrls != NULL) delete pCtrls; |
455 |
|
pCtrls = new RTList<CC>(pCCPool); |
456 |
} |
} |
457 |
|
|
458 |
void RemoveAllCCs() { |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1, Smoother* pSmoother = NULL) { |
459 |
Ctrls.clear(); |
if(pCtrls->poolIsEmpty()) { |
460 |
|
std::cerr << "Maximum number of CC reached!" << std::endl; |
461 |
|
return; |
462 |
|
} |
463 |
|
*(pCtrls->allocAppend()) = CC(Controller, Influence, Curve, pSmoother); |
464 |
|
if (pSmoother != NULL) hasSmoothCtrls = true; |
465 |
} |
} |
466 |
|
|
467 |
virtual void Increment() { } |
virtual void RemoveAllCCs() { pCtrls->clear(); } |
468 |
|
|
469 |
|
int GetCCCount() { return pCtrls->count(); } |
470 |
|
|
471 |
|
virtual void Increment() { |
472 |
|
if (hasSmoothCtrls && isSmoothingOut) Calculate(); |
473 |
|
} |
474 |
|
|
475 |
virtual void Trigger() { |
virtual void Trigger() { |
476 |
Calculate(); |
Calculate(); |
480 |
virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) { |
virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) { |
481 |
bool recalculate = false; |
bool recalculate = false; |
482 |
|
|
483 |
for (int i = 0; i < Ctrls.size(); i++) { |
RTList<CC>::Iterator ctrl = pCtrls->first(); |
484 |
if (Controller != Ctrls[i].Controller) continue; |
RTList<CC>::Iterator end = pCtrls->end(); |
485 |
if (Ctrls[i].Value == Value) continue; |
for(; ctrl != end; ++ctrl) { |
486 |
Ctrls[i].Value = Value; |
if (Controller != (*ctrl).Controller) continue; |
487 |
|
if ((*ctrl).Value == Value) continue; |
488 |
|
(*ctrl).Value = Value; |
489 |
|
if ((*ctrl).pSmoother != NULL) (*ctrl).pSmoother->update(Value); |
490 |
if (!bActive) bActive = true; |
if (!bActive) bActive = true; |
491 |
recalculate = true; |
recalculate = true; |
492 |
} |
} |
493 |
|
|
494 |
if (recalculate) { |
if (!(hasSmoothCtrls && isSmoothingOut) && recalculate) Calculate(); |
|
Calculate(); |
|
|
if (pListener!= NULL) pListener->ValueChanged(this); |
|
|
} |
|
495 |
} |
} |
496 |
|
|
497 |
virtual void Calculate() { |
virtual void Calculate() { |
498 |
Level = 0; |
float l = 0; |
499 |
for (int i = 0; i < Ctrls.size(); i++) { |
isSmoothingOut = false; |
500 |
if (Ctrls[i].Value == 0) continue; |
RTList<CC>::Iterator ctrl = pCtrls->first(); |
501 |
Level += (Ctrls[i].Value / 127.0f) * Ctrls[i].Influence; |
RTList<CC>::Iterator end = pCtrls->end(); |
502 |
|
for(; ctrl != end; ++ctrl) { |
503 |
|
if ((*ctrl).pSmoother == NULL) { |
504 |
|
l += Normalize((*ctrl).Value, (*ctrl).Curve) * (*ctrl).Influence; |
505 |
|
} else { |
506 |
|
if ((*ctrl).pSmoother->isSmoothingOut()) isSmoothingOut = true; |
507 |
|
l += Normalize((*ctrl).pSmoother->render(), (*ctrl).Curve) * (*ctrl).Influence; |
508 |
|
} |
509 |
|
} |
510 |
|
if (Level != l) { |
511 |
|
Level = l; |
512 |
|
if (pListener != NULL) pListener->ValueChanged(this); |
513 |
} |
} |
514 |
} |
} |
515 |
|
|
516 |
|
virtual float Normalize(uint8_t val, short int curve = -1) { |
517 |
|
return val / 127.0f; |
518 |
|
} |
519 |
}; |
}; |
520 |
|
|
521 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |