1 // Copyright 2014 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Cosine oscillator. Generates a cosine between 0.0 and 1.0 with minimal
28 // CPU use.
29 
30 #ifndef STMLIB_DSP_COSINE_OSCILLATOR_H_
31 #define STMLIB_DSP_COSINE_OSCILLATOR_H_
32 
33 #include "stmlib/stmlib.h"
34 
35 #include <cmath>
36 
37 namespace stmlib {
38 
39 enum CosineOscillatorMode {
40   COSINE_OSCILLATOR_APPROXIMATE,
41   COSINE_OSCILLATOR_EXACT
42 };
43 
44 class CosineOscillator {
45  public:
CosineOscillator()46   CosineOscillator() { }
~CosineOscillator()47   ~CosineOscillator() { }
48 
49   template<CosineOscillatorMode mode>
Init(float frequency)50   inline void Init(float frequency) {
51     if (mode == COSINE_OSCILLATOR_APPROXIMATE) {
52       InitApproximate(frequency);
53     } else {
54       iir_coefficient_ = 2.0f * cosf(2.0f * M_PI * frequency);
55       initial_amplitude_ = iir_coefficient_ * 0.25f;
56     }
57     Start();
58   }
59 
InitApproximate(float frequency)60   inline void InitApproximate(float frequency) {
61     float sign = 16.0f;
62     frequency -= 0.25f;
63     if (frequency < 0.0f) {
64       frequency = -frequency;
65     } else {
66       if (frequency > 0.5f) {
67         frequency -= 0.5f;
68       } else {
69         sign = -16.0f;
70       }
71     }
72     iir_coefficient_ = sign * frequency * (1.0f - 2.0f * frequency);
73     initial_amplitude_ = iir_coefficient_ * 0.25f;
74   }
75 
Start()76   inline void Start() {
77     y1_ = initial_amplitude_;
78     y0_ = 0.5f;
79   }
80 
value()81   inline float value() const {
82     return y1_ + 0.5f;
83   }
84 
Next()85   inline float Next() {
86     float temp = y0_;
87     y0_ = iir_coefficient_ * y0_ - y1_;
88     y1_ = temp;
89     return temp + 0.5f;
90   }
91 
92  private:
93   float y1_;
94   float y0_;
95   float iir_coefficient_;
96   float initial_amplitude_;
97 
98   DISALLOW_COPY_AND_ASSIGN(CosineOscillator);
99 };
100 
101 }  // namespace stmlib
102 
103 #endif  // STMLIB_DSP_COSINE_OSCILLATOR_H_
104