/[svn]/linuxsampler/trunk/src/engines/common/SignalUnit.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/common/SignalUnit.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2219 by iliev, Thu Jul 28 12:35:49 2011 UTC revision 2232 by iliev, Mon Aug 8 13:40:04 2011 UTC
# Line 28  Line 28 
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      /**      /**
# Line 277  namespace LinuxSampler { Line 323  namespace LinuxSampler {
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 controller changes       * The level of this unit corresponds to the controllers changes
379       * and is normalized to be in the range from -1 to +1.       * and their influences.
380       */       */
381      class CCSignalUnit: public SignalUnit {      class CCSignalUnit: public SignalUnit {
382          private:          public:
383              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. */
384                class Listener {
385                    public:
386                        virtual void ValueChanged(CCSignalUnit* pUnit) = 0;
387                };
388                
389            protected:
390                class CC {
391                    public:
392                        uint8_t   Controller;  ///< MIDI controller number.
393                        uint8_t   Value;       ///< Controller Value.
394                        short int Curve;       ///< specifies the curve type
395                        float     Influence;
396                        
397                        Smoother* pSmoother;
398                        
399                        CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1, Smoother* pSmoother = NULL) {
400                            this->Controller = Controller;
401                            this->Value = 0;
402                            this->Curve = Curve;
403                            this->Influence = Influence;
404                            this->pSmoother = pSmoother;
405                        }
406                        
407                        CC(const CC& cc) { Copy(cc); }
408                        void operator=(const CC& cc) { Copy(cc); }
409                        
410                        void Copy(const CC& cc) {
411                            Controller = cc.Controller;
412                            Value = cc.Value;
413                            Influence = cc.Influence;
414                            Curve = cc.Curve;
415                            pSmoother = cc.pSmoother;
416                        }
417                };
418                
419                FixedArray<CC> Ctrls; // The MIDI controllers which modulates this signal unit.
420                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              CCSignalUnit(SignalUnitRack* rack, uint8_t Controller): SignalUnit(rack) {              
426                  Ctrl = Controller;              CCSignalUnit(SignalUnitRack* rack, Listener* l = NULL): SignalUnit(rack), Ctrls(128) {
427                    pListener = l;
428                    hasSmoothCtrls = isSmoothingOut = false;
429              }              }
430                            
431              CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack) { Copy(Unit); }              CCSignalUnit(const CCSignalUnit& Unit): SignalUnit(Unit.pRack), Ctrls(128) { Copy(Unit); }
432              void operator=(const CCSignalUnit& Unit) { Copy(Unit); }              void operator=(const CCSignalUnit& Unit) { Copy(Unit); }
433                            
434              void Copy(const CCSignalUnit& Unit) {              void Copy(const CCSignalUnit& Unit) {
435                  Ctrl = Unit.Ctrl;                  Ctrls.copy(Unit.Ctrls);
436                    pListener = Unit.pListener;
437                    hasSmoothCtrls = Unit.hasSmoothCtrls;
438                    isSmoothingOut = Unit.isSmoothingOut;
439                  SignalUnit::Copy(Unit);                  SignalUnit::Copy(Unit);
440              }              }
441                            
442              virtual void Increment() { }              void AddCC(uint8_t Controller, float Influence, short int Curve = -1, Smoother* pSmoother = NULL) {
443                    Ctrls.add(CC(Controller, Influence, Curve, pSmoother));
444                    if (pSmoother != NULL) hasSmoothCtrls = true;
445                }
446                
447                void RemoveAllCCs() {
448                    Ctrls.clear();
449                }
450                
451                virtual void Increment() {
452                    if (hasSmoothCtrls && isSmoothingOut) Calculate();
453                }
454                
455                virtual void Trigger() {
456                    Calculate();
457                    bActive = Level != 0;
458                }
459                            
460              virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) {              virtual void ProcessCCEvent(uint8_t Controller, uint8_t Value) {
461                  if (Controller != Ctrl) return;                  bool recalculate = false;
462                                    
463                  // Normalize the value so it belongs to the interval [-1, +1]                  for (int i = 0; i < Ctrls.size(); i++) {
464                  Level = 2 * Value;                      if (Controller != Ctrls[i].Controller) continue;
465                  Level = Level/127.0f - 1.0f;                      if (Ctrls[i].Value == Value) continue;
466                        Ctrls[i].Value = Value;
467                        if (Ctrls[i].pSmoother != NULL) Ctrls[i].pSmoother->update(Value);
468                        if (!bActive) bActive = true;
469                        recalculate = true;
470                    }
471                                    
472                  if (!bActive) bActive = true;                  if (!(hasSmoothCtrls && isSmoothingOut) && recalculate) Calculate();
473                }
474                
475                virtual void Calculate() {
476                    float l = 0;
477                    isSmoothingOut = false;
478                    for (int i = 0; i < Ctrls.size(); i++) {
479                        if (Ctrls[i].pSmoother == NULL) {
480                            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            

Legend:
Removed from v.2219  
changed lines
  Added in v.2232

  ViewVC Help
Powered by ViewVC