323 |
}; |
}; |
324 |
|
|
325 |
/** |
/** |
326 |
|
* Used to smooth out the parameter changes. |
327 |
|
*/ |
328 |
|
class Smoother { |
329 |
|
protected: |
330 |
|
uint timeSteps; // The number of time steps to reach the goal |
331 |
|
uint currentTimeStep; |
332 |
|
uint8_t goal; // 0 - 127 |
333 |
|
uint8_t prev; // 0 - 127 |
334 |
|
|
335 |
|
public: |
336 |
|
/** |
337 |
|
* |
338 |
|
* @param time The time (in seconds) to reach the goal |
339 |
|
* @param sampleRate |
340 |
|
* @param val The initial value |
341 |
|
*/ |
342 |
|
void trigger(float time, float sampleRate, uint8_t val = 0) { |
343 |
|
currentTimeStep = timeSteps = time * sampleRate; |
344 |
|
prev = goal = val; |
345 |
|
} |
346 |
|
|
347 |
|
/** |
348 |
|
* Set the current value, which the smoother will not smooth out. |
349 |
|
* If you want the value to be smoothen out, use update() instead. |
350 |
|
*/ |
351 |
|
void setValue( uint8_t val) { |
352 |
|
currentTimeStep = timeSteps; |
353 |
|
prev = goal = val; |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* Sets a new value. The render function will return |
358 |
|
* values gradually approaching this value. |
359 |
|
*/ |
360 |
|
void update(uint8_t val) { |
361 |
|
if (val == goal) return; |
362 |
|
|
363 |
|
prev = prev + (goal - prev) * (currentTimeStep / (float)timeSteps); |
364 |
|
goal = val; |
365 |
|
currentTimeStep = 0; |
366 |
|
} |
367 |
|
|
368 |
|
uint8_t render() { |
369 |
|
if (currentTimeStep >= timeSteps) return goal; |
370 |
|
return prev + (goal - prev) * (currentTimeStep++ / (float)timeSteps); |
371 |
|
} |
372 |
|
|
373 |
|
bool isSmoothingOut() { return currentTimeStep < timeSteps; } |
374 |
|
}; |
375 |
|
|
376 |
|
/** |
377 |
* Continuous controller signal unit. |
* Continuous controller signal unit. |
378 |
* The level of this unit corresponds to the controllers changes |
* The level of this unit corresponds to the controllers changes |
379 |
* and their influences. |
* and their influences. |
394 |
short int Curve; ///< specifies the curve type |
short int Curve; ///< specifies the curve type |
395 |
float Influence; |
float Influence; |
396 |
|
|
397 |
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1) { |
Smoother* pSmoother; |
398 |
|
|
399 |
|
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1, Smoother* pSmoother = NULL) { |
400 |
this->Controller = Controller; |
this->Controller = Controller; |
401 |
this->Value = 0; |
this->Value = 0; |
402 |
this->Curve = Curve; |
this->Curve = Curve; |
403 |
this->Influence = Influence; |
this->Influence = Influence; |
404 |
|
this->pSmoother = pSmoother; |
405 |
} |
} |
406 |
|
|
407 |
CC(const CC& cc) { Copy(cc); } |
CC(const CC& cc) { Copy(cc); } |
412 |
Value = cc.Value; |
Value = cc.Value; |
413 |
Influence = cc.Influence; |
Influence = cc.Influence; |
414 |
Curve = cc.Curve; |
Curve = cc.Curve; |
415 |
|
pSmoother = cc.pSmoother; |
416 |
} |
} |
417 |
}; |
}; |
418 |
|
|
419 |
FixedArray<CC> Ctrls; // The MIDI controllers which modulates this signal unit. |
FixedArray<CC> Ctrls; // The MIDI controllers which modulates this signal unit. |
420 |
Listener* pListener; |
Listener* pListener; |
421 |
|
bool hasSmoothCtrls; // determines whether there are smooth controllers (used for optimization) |
422 |
|
bool isSmoothingOut; // determines whether there is a CC which is in process of smoothing out (used for optimization) |
423 |
|
|
424 |
public: |
public: |
425 |
|
|
426 |
CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), Ctrls(128) { |
CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), Ctrls(128) { |
427 |
pListener = l; |
pListener = l; |
428 |
|
hasSmoothCtrls = isSmoothingOut = false; |
429 |
} |
} |
430 |
|
|
431 |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), Ctrls(128) { Copy(Unit); } |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), Ctrls(128) { Copy(Unit); } |
434 |
void Copy(const CCSignalUnit& Unit) { |
void Copy(const CCSignalUnit& Unit) { |
435 |
Ctrls.copy(Unit.Ctrls); |
Ctrls.copy(Unit.Ctrls); |
436 |
pListener = Unit.pListener; |
pListener = Unit.pListener; |
437 |
|
hasSmoothCtrls = Unit.hasSmoothCtrls; |
438 |
|
isSmoothingOut = Unit.isSmoothingOut; |
439 |
SignalUnit::Copy(Unit); |
SignalUnit::Copy(Unit); |
440 |
} |
} |
441 |
|
|
442 |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1) { |
void AddCC(uint8_t Controller, float Influence, short int Curve = -1, Smoother* pSmoother = NULL) { |
443 |
Ctrls.add(CC(Controller, Influence, Curve)); |
Ctrls.add(CC(Controller, Influence, Curve, pSmoother)); |
444 |
|
if (pSmoother != NULL) hasSmoothCtrls = true; |
445 |
} |
} |
446 |
|
|
447 |
void RemoveAllCCs() { |
void RemoveAllCCs() { |
448 |
Ctrls.clear(); |
Ctrls.clear(); |
449 |
} |
} |
450 |
|
|
451 |
virtual void Increment() { } |
virtual void Increment() { |
452 |
|
if (hasSmoothCtrls && isSmoothingOut) Calculate(); |
453 |
|
} |
454 |
|
|
455 |
virtual void Trigger() { |
virtual void Trigger() { |
456 |
Calculate(); |
Calculate(); |
464 |
if (Controller != Ctrls[i].Controller) continue; |
if (Controller != Ctrls[i].Controller) continue; |
465 |
if (Ctrls[i].Value == Value) continue; |
if (Ctrls[i].Value == Value) continue; |
466 |
Ctrls[i].Value = Value; |
Ctrls[i].Value = Value; |
467 |
|
if (Ctrls[i].pSmoother != NULL) Ctrls[i].pSmoother->update(Value); |
468 |
if (!bActive) bActive = true; |
if (!bActive) bActive = true; |
469 |
recalculate = true; |
recalculate = true; |
470 |
} |
} |
471 |
|
|
472 |
if (recalculate) { |
if (!(hasSmoothCtrls && isSmoothingOut) && recalculate) Calculate(); |
|
Calculate(); |
|
|
if (pListener!= NULL) pListener->ValueChanged(this); |
|
|
} |
|
473 |
} |
} |
474 |
|
|
475 |
virtual void Calculate() { |
virtual void Calculate() { |
476 |
Level = 0; |
float l = 0; |
477 |
|
isSmoothingOut = false; |
478 |
for (int i = 0; i < Ctrls.size(); i++) { |
for (int i = 0; i < Ctrls.size(); i++) { |
479 |
if (Ctrls[i].Value == 0) continue; |
if (Ctrls[i].pSmoother == NULL) { |
480 |
Level += (Ctrls[i].Value / 127.0f) * Ctrls[i].Influence; |
l += Normalize(Ctrls[i].Value, Ctrls[i].Curve) * Ctrls[i].Influence; |
481 |
|
} else { |
482 |
|
if (Ctrls[i].pSmoother->isSmoothingOut()) isSmoothingOut = true; |
483 |
|
l += Normalize(Ctrls[i].pSmoother->render(), Ctrls[i].Curve) * Ctrls[i].Influence; |
484 |
|
} |
485 |
|
} |
486 |
|
if (Level != l) { |
487 |
|
Level = l; |
488 |
|
if (pListener != NULL) pListener->ValueChanged(this); |
489 |
} |
} |
490 |
} |
} |
491 |
|
|
492 |
|
virtual float Normalize(uint8_t val, short int curve = -1) { |
493 |
|
return val / 127.0f; |
494 |
|
} |
495 |
}; |
}; |
496 |
|
|
497 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |