Audio Effects Framework
Lfo.h
Go to the documentation of this file.
1 #if !defined(__Lfo_hdr__)
2 #define __Lfo_hdr__
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h>
6 
7 #include "ErrorDef.h"
8 #include "RingBuffer.h"
9 #include "Synthesis.h"
10 
11 class CLfo
12 {
13 public:
14  CLfo(float fSampleRate) :
15  m_fSampleRate(fSampleRate),
16  m_fReadIndex(0),
17  m_eType(kSine),
18  m_pCRingBuff(0)
19  {
20  for (int i = 0; i < kNumLfoParams; i++)
21  m_afParam[i] = 0;
22 
23  m_pCRingBuff = new CRingBuffer<float>(m_kiBufferLength);
24 
26  }
27  virtual ~CLfo()
28  {
29  delete m_pCRingBuff;
30  }
31 
32  enum LfoType_t
33  {
37 
39  };
41  {
44 
46  };
47  Error_t setLfoType (LfoType_t eType)
48  {
49  m_eType = eType;
50  computeWaveTable();
51  return kNoError;
52  }
53  Error_t setParam(LfoParam_t eParam, float fValue)
54  {
55  m_afParam[eParam] = fValue;
56 
57  return kNoError;
58  }
59  float getParam (LfoParam_t eParam) const
60  {
61  return m_afParam[eParam];
62  }
63 
64  float getNext()
65  {
66  float fValue = m_afParam[kLfoParamAmplitude] * m_pCRingBuff->get(m_fReadIndex);
67  m_fReadIndex = m_fReadIndex + m_afParam[kLfoParamFrequency]/m_fSampleRate * m_kiBufferLength;
68 
69  if (m_fReadIndex >= m_kiBufferLength)
70  m_fReadIndex -= m_kiBufferLength;
71  return fValue;
72  }
73 private:
74  void computeWaveTable ()
75  {
76  float *pfBuff = new float [m_kiBufferLength];
77  switch (m_eType)
78  {
79  case kSine:
80  CSynthesis::generateSine (pfBuff, 1.F, 1.F*m_kiBufferLength, m_kiBufferLength);
81  break;
82  case kSaw:
83  CSynthesis::generateSaw (pfBuff, 1.F, 1.F*m_kiBufferLength, m_kiBufferLength);
84  break;
85  case kRect:
86  CSynthesis::generateRect (pfBuff, 1.F, 1.F*m_kiBufferLength, m_kiBufferLength);
87  break;
88  }
89 
90  m_pCRingBuff->put(pfBuff, m_kiBufferLength);
91 
92  delete [] pfBuff;
93  }
94  static const int m_kiBufferLength = 4096;
95 
96  float m_fSampleRate;
97  float m_fReadIndex;
98  float m_afParam[kNumLfoParams];
99 
100  LfoType_t m_eType;
101 
102  CRingBuffer<float> *m_pCRingBuff;
103 };
104 #endif // __Lfo_hdr__
CLfo
Definition: Lfo.h:12
CLfo::CLfo
CLfo(float fSampleRate)
Definition: Lfo.h:14
CLfo::kLfoParamFrequency
@ kLfoParamFrequency
Definition: Lfo.h:43
CRingBuffer::put
void put(T tNewValue)
Definition: RingBuffer.h:57
CRingBuffer< float >
CLfo::kSaw
@ kSaw
Definition: Lfo.h:35
CLfo::getParam
float getParam(LfoParam_t eParam) const
Definition: Lfo.h:59
CLfo::setParam
Error_t setParam(LfoParam_t eParam, float fValue)
Definition: Lfo.h:53
CRingBuffer::get
T get(float fOffset=0) const
Definition: RingBuffer.h:104
CLfo::LfoParam_t
LfoParam_t
Definition: Lfo.h:41
CLfo::~CLfo
virtual ~CLfo()
Definition: Lfo.h:27
CSynthesis::generateSine
static Error_t generateSine(float *pfOutBuf, float fFreqInHz, float fSampleFreqInHz, int iLength, float fAmplitude=1.F, float fStartPhaseInRad=0.F)
Definition: Synthesis.h:16
CLfo::kNumLfoTypes
@ kNumLfoTypes
Definition: Lfo.h:38
CLfo::kLfoParamAmplitude
@ kLfoParamAmplitude
Definition: Lfo.h:42
CLfo::kRect
@ kRect
Definition: Lfo.h:36
Synthesis.h
CLfo::LfoType_t
LfoType_t
Definition: Lfo.h:33
CLfo::getNext
float getNext()
Definition: Lfo.h:64
CLfo::kNumLfoParams
@ kNumLfoParams
Definition: Lfo.h:45
CSynthesis::generateSaw
static Error_t generateSaw(float *pfOutBuf, float fFreqInHz, float fSampleFreqInHz, int iLength, float fAmplitude=1.F)
Definition: Synthesis.h:48
RingBuffer.h
CLfo::kSine
@ kSine
Definition: Lfo.h:34
CLfo::setLfoType
Error_t setLfoType(LfoType_t eType)
Definition: Lfo.h:47
CSynthesis::generateRect
static Error_t generateRect(float *pfOutBuf, float fFreqInHz, float fSampleFreqInHz, int iLength, float fAmplitude=1.F)
Definition: Synthesis.h:28