28 |
|
|
29 |
namespace LinuxSampler { |
namespace LinuxSampler { |
30 |
|
|
31 |
|
template<typename T> |
32 |
|
class FixedArray { |
33 |
|
public: |
34 |
|
FixedArray(int capacity) { |
35 |
|
iSize = 0; |
36 |
|
iCapacity = capacity; |
37 |
|
pData = new T[iCapacity]; |
38 |
|
} |
39 |
|
|
40 |
|
~FixedArray() { |
41 |
|
delete pData; |
42 |
|
pData = NULL; |
43 |
|
} |
44 |
|
|
45 |
|
inline int size() const { return iSize; } |
46 |
|
inline int capacity() { return iCapacity; } |
47 |
|
|
48 |
|
void add(T element) { |
49 |
|
if (iSize >= iCapacity) throw Exception("Array out of bounds"); |
50 |
|
pData[iSize++] = element; |
51 |
|
} |
52 |
|
|
53 |
|
|
54 |
|
T increment() { |
55 |
|
if (iSize >= iCapacity) throw Exception("Array out of bounds"); |
56 |
|
return pData[iSize++]; |
57 |
|
} |
58 |
|
|
59 |
|
void clear() { iSize = 0; } |
60 |
|
|
61 |
|
void copy(const FixedArray<T>& array) { |
62 |
|
if(array.size() >= capacity()) throw Exception("Not enough space to copy array"); |
63 |
|
for (int i = 0; i < array.size(); i++) pData[i] = array[i]; |
64 |
|
iSize = array.size(); |
65 |
|
} |
66 |
|
|
67 |
|
inline T& operator[](int idx) const { |
68 |
|
return pData[idx]; |
69 |
|
} |
70 |
|
|
71 |
|
private: |
72 |
|
T* pData; |
73 |
|
int iSize; |
74 |
|
int iCapacity; |
75 |
|
}; |
76 |
|
|
77 |
class SignalUnitRack; |
class SignalUnitRack; |
78 |
|
|
79 |
/** |
/** |
324 |
|
|
325 |
/** |
/** |
326 |
* Continuous controller signal unit. |
* Continuous controller signal unit. |
327 |
* The level of this unit corresponds to the controller changes |
* The level of this unit corresponds to the controllers changes |
328 |
* and is normalized to be in the range from -1 to +1. |
* and their influences. |
329 |
*/ |
*/ |
330 |
class CCSignalUnit: public SignalUnit { |
class CCSignalUnit: public SignalUnit { |
331 |
private: |
public: |
332 |
uint8_t Ctrl; // The number of the MIDI controller which modulates this signal unit. |
/** Listener which will be notified when the level of the unit is changed. */ |
333 |
|
class Listener { |
334 |
|
public: |
335 |
|
virtual void ValueChanged(CCSignalUnit* pUnit) = 0; |
336 |
|
}; |
337 |
|
|
338 |
|
protected: |
339 |
|
class CC { |
340 |
|
public: |
341 |
|
uint8_t Controller; ///< MIDI controller number. |
342 |
|
uint8_t Value; ///< Controller Value. |
343 |
|
short int Curve; ///< specifies the curve type |
344 |
|
float Influence; |
345 |
|
|
346 |
|
CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1) { |
347 |
|
this->Controller = Controller; |
348 |
|
this->Value = 0; |
349 |
|
this->Curve = Curve; |
350 |
|
this->Influence = Influence; |
351 |
|
} |
352 |
|
|
353 |
|
CC(const CC& cc) { Copy(cc); } |
354 |
|
void operator=(const CC& cc) { Copy(cc); } |
355 |
|
|
356 |
|
void Copy(const CC& cc) { |
357 |
|
Controller = cc.Controller; |
358 |
|
Value = cc.Value; |
359 |
|
Influence = cc.Influence; |
360 |
|
Curve = cc.Curve; |
361 |
|
} |
362 |
|
}; |
363 |
|
|
364 |
|
FixedArray<CC> Ctrls; // The MIDI controllers which modulates this signal unit. |
365 |
|
Listener* pListener; |
366 |
|
|
367 |
public: |
public: |
368 |
CCSignalUnit(SignalUnitRack* rack, uint8_t Controller): SignalUnit(rack) { |
|
369 |
Ctrl = Controller; |
CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), Ctrls(128) { |
370 |
|
pListener = l; |
371 |
} |
} |
372 |
|
|
373 |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack) { Copy(Unit); } |
CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), Ctrls(128) { Copy(Unit); } |
374 |
void operator=(const CCSignalUnit& Unit) { Copy(Unit); } |
void operator=(const CCSignalUnit& Unit) { Copy(Unit); } |
375 |
|
|
376 |
void Copy(const CCSignalUnit& Unit) { |
void Copy(const CCSignalUnit& Unit) { |
377 |
Ctrl = Unit.Ctrl; |
Ctrls.copy(Unit.Ctrls); |
378 |
|
pListener = Unit.pListener; |
379 |
SignalUnit::Copy(Unit); |
SignalUnit::Copy(Unit); |
380 |
} |
} |
381 |
|
|
382 |
|
void AddCC(uint8_t Controller, float Influence, short int Curve = -1) { |
383 |
|
Ctrls.add(CC(Controller, Influence, Curve)); |
384 |
|
} |
385 |
|
|
386 |
|
void RemoveAllCCs() { |
387 |
|
Ctrls.clear(); |
388 |
|
} |
389 |
|
|
390 |
virtual void Increment() { } |
virtual void Increment() { } |
391 |
|
|
392 |
|
virtual void Trigger() { |
393 |
|
Calculate(); |
394 |
|
bActive = Level != 0; |
395 |
|
} |
396 |
|
|
397 |
virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) { |
virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) { |
398 |
if (Controller != Ctrl) return; |
bool recalculate = false; |
399 |
|
|
400 |
// Normalize the value so it belongs to the interval [-1, +1] |
for (int i = 0; i < Ctrls.size(); i++) { |
401 |
Level = 2 * Value; |
if (Controller != Ctrls[i].Controller) continue; |
402 |
Level = Level/127.0f - 1.0f; |
if (Ctrls[i].Value == Value) continue; |
403 |
|
Ctrls[i].Value = Value; |
404 |
|
if (!bActive) bActive = true; |
405 |
|
recalculate = true; |
406 |
|
} |
407 |
|
|
408 |
if (!bActive) bActive = true; |
if (recalculate) { |
409 |
|
Calculate(); |
410 |
|
if (pListener!= NULL) pListener->ValueChanged(this); |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
|
virtual void Calculate() { |
415 |
|
Level = 0; |
416 |
|
for (int i = 0; i < Ctrls.size(); i++) { |
417 |
|
if (Ctrls[i].Value == 0) continue; |
418 |
|
Level += (Ctrls[i].Value / 127.0f) * Ctrls[i].Influence; |
419 |
|
} |
420 |
} |
} |
421 |
}; |
}; |
422 |
|
|