minilogue xd SDK  v1.1-0
simplelfo.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3  BSD 3-Clause License
4 
5  Copyright (c) 2018, KORG INC.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  * Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 
14  * Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 //*/
34 
35 #include "fixed_math.h"
36 #include "int_math.h"
37 #include "float_math.h"
38 
50 namespace dsp {
51 
55  struct SimpleLFO {
56 
57  /*===========================================================================*/
58  /* Types and Data Structures. */
59  /*===========================================================================*/
60 
61  /*===========================================================================*/
62  /* Constructor / Destructor. */
63  /*===========================================================================*/
64 
68  SimpleLFO(void) :
69  phi0(0x80000000), w0(0)
70  { }
71 
72  /*===========================================================================*/
73  /* Public Methods. */
74  /*===========================================================================*/
75 
79  inline __attribute__((optimize("Ofast"),always_inline))
80  void cycle(void)
81  {
82  phi0 += w0;
83  }
84 
88  inline __attribute__((optimize("Ofast"),always_inline))
89  void reset(void)
90  {
91  phi0 = 0x80000000;
92  }
93 
100  inline __attribute__((optimize("Ofast"),always_inline))
101  void setF0(const float f0, const float fsrecip)
102  {
103  w0 = f32_to_q31(2.f * f0 * fsrecip);
104  }
105 
111  inline __attribute__((optimize("Ofast"),always_inline))
112  void setW0(const float w)
113  {
114  w0 = f32_to_q31(2.f * w);
115  }
116 
117  // --- Sinusoids --------------
118 
122  inline __attribute__((optimize("Ofast"),always_inline))
123  float sine_bi(void)
124  {
125  const float phif = q31_to_f32(phi0);
126  return 4 * phif * (si_fabsf(phif) - 1.f);
127  }
128 
132  inline __attribute__((optimize("Ofast"),always_inline))
133  float sine_uni(void)
134  {
135  const float phif = q31_to_f32(phi0);
136  return 0.5f + 2 * phif * (si_fabsf(phif) - 1.f);
137  }
138 
144  inline __attribute__((optimize("Ofast"),always_inline))
145  float sine_bi_off(const float offset)
146  {
147  const float phi = q31_to_f32(phi0 + f32_to_q31(2*offset));
148  return 4 * phi * (si_fabsf(phi) - 1.f);
149  }
150 
156  inline __attribute__((optimize("Ofast"),always_inline))
157  float sine_uni_off(const float offset)
158  {
159  const float phi = q31_to_f32(phi0 + f32_to_q31(2*offset));
160  return 0.5f + 2 * phi * (si_fabsf(phi) - 1.f);
161  }
162 
163  // --- Triangles --------------
167  inline __attribute__((optimize("Ofast"),always_inline))
168  float triangle_bi(void)
169  {
170  return q31_to_f32(qsub(q31abs(phi0),0x40000000)<<1);
171  }
172 
176  inline __attribute__((optimize("Ofast"),always_inline))
177  float triangle_uni(void)
178  {
179  return si_fabsf(q31_to_f32(phi0));
180  }
181 
187  inline __attribute__((optimize("Ofast"),always_inline))
188  float triangle_bi_off(const float offset)
189  {
190  const q31_t phi = phi0 + f32_to_q31(2*offset);
191  return q31_to_f32(qsub(q31abs(phi),0x40000000)<<1);
192  }
193 
199  inline __attribute__((optimize("Ofast"),always_inline))
200  float triangle_uni_off(const float offset)
201  {
202  const float phi = q31_to_f32(phi0 + f32_to_q31(2*offset));
203  return si_fabsf(phi);
204  }
205 
206  // --- Saws --------------
210  inline __attribute__((optimize("Ofast"),always_inline))
211  float saw_bi(void)
212  {
213  return q31_to_f32(phi0);
214  }
215 
219  inline __attribute__((optimize("Ofast"),always_inline))
220  float saw_uni(void)
221  {
222  return q31_to_f32(qadd((phi0>>1),0x40000000));
223  }
224 
230  inline __attribute__((optimize("Ofast"),always_inline))
231  float saw_bi_off(const float offset)
232  {
233  const q31_t phi = phi0 + (f32_to_q31(offset)<<1);
234  return q31_to_f32(phi);
235  }
236 
242  inline __attribute__((optimize("Ofast"),always_inline))
243  float saw_uni_off(const float offset)
244  {
245  q31_t phi = phi0 + (f32_to_q31(offset)<<1);
246  return 0.5f * phi + 0.5f;
247  phi >>= 1;
248  return q31_to_f32(qadd(phi,0x40000000));
249  }
250 
251  // --- Squares --------------
255  inline __attribute__((optimize("Ofast"),always_inline))
256  float square_bi(void)
257  {
258  return (phi0 < 0) ? -1.f : 1.f;
259  }
260 
264  inline __attribute__((optimize("Ofast"),always_inline))
265  float square_uni(void)
266  {
267  return (phi0 < 0) ? 0.f : 1.f;
268  }
269 
275  inline __attribute__((optimize("Ofast"),always_inline))
276  float square_bi_off(const float offset)
277  {
278  const q31_t phi = phi0 + (f32_to_q31(offset)<<1);
279  return (phi < 0) ? -1.f : 1.f;
280  }
281 
287  inline __attribute__((optimize("Ofast"),always_inline))
288  float square_uni_off(const float offset)
289  {
290  const q31_t phi = phi0 + (f32_to_q31(offset)<<1);
291  return (phi < 0) ? 0.f : 1.f;
292  }
293 
294  /*===========================================================================*/
295  /* Members Vars */
296  /*===========================================================================*/
297 
298  q31_t phi0;
299  q31_t w0;
300 
301  };
302 }
303 
dsp::SimpleLFO::square_uni_off
float square_uni_off(const float offset)
Get current value of positive unipolar square wave for phase with offset.
Definition: simplelfo.hpp:288
fixed_math.h
Fixed Point Math Utilities.
dsp::SimpleLFO::triangle_bi
float triangle_bi(void)
Get current value of bipolar triangle wave for current phase.
Definition: simplelfo.hpp:168
dsp::SimpleLFO::reset
void reset(void)
Reset phase.
Definition: simplelfo.hpp:89
dsp::SimpleLFO::triangle_bi_off
float triangle_bi_off(const float offset)
Get current value of bipolar triangle wave for phase with offset.
Definition: simplelfo.hpp:188
int_math.h
Integer Math Utilities.
dsp::SimpleLFO::setW0
void setW0(const float w)
Set LFO frequency in radians.
Definition: simplelfo.hpp:112
dsp::SimpleLFO::sine_bi_off
float sine_bi_off(const float offset)
Get current value of bipolar sine wave for phase with offset.
Definition: simplelfo.hpp:145
dsp::SimpleLFO::triangle_uni_off
float triangle_uni_off(const float offset)
Get current value of positive unipolar triangle wave for phase with offset.
Definition: simplelfo.hpp:200
float_math.h
Floating Point Math Utilities.
dsp::SimpleLFO::triangle_uni
float triangle_uni(void)
Get value of positive unipolar triangle wave for current phase.
Definition: simplelfo.hpp:177
dsp::SimpleLFO::square_bi_off
float square_bi_off(const float offset)
Get current value of bipolar square wave for phase with offset.
Definition: simplelfo.hpp:276
dsp::SimpleLFO::setF0
void setF0(const float f0, const float fsrecip)
Set LFO frequency.
Definition: simplelfo.hpp:101
dsp::SimpleLFO::SimpleLFO
SimpleLFO(void)
Default constructor.
Definition: simplelfo.hpp:68
dsp::SimpleLFO::saw_uni_off
float saw_uni_off(const float offset)
Get current value of positive unipolar saw wave for phase with offset.
Definition: simplelfo.hpp:243
dsp::SimpleLFO::saw_bi
float saw_bi(void)
Get current value of bipolar saw wave for current phase.
Definition: simplelfo.hpp:211
dsp::SimpleLFO::square_uni
float square_uni(void)
Get value of positive unipolar square wave for current phase.
Definition: simplelfo.hpp:265
dsp::SimpleLFO::square_bi
float square_bi(void)
Get current value of bipolar square wave for current phase.
Definition: simplelfo.hpp:256
dsp::SimpleLFO
Simple LFO.
Definition: simplelfo.hpp:55
dsp
Common DSP Utilities.
Definition: biquad.hpp:49
dsp::SimpleLFO::sine_uni
float sine_uni(void)
Get value of positive unipolar sine wave for current phase.
Definition: simplelfo.hpp:133
dsp::SimpleLFO::sine_bi
float sine_bi(void)
Get value of bipolar sine wave for current phase.
Definition: simplelfo.hpp:123
dsp::SimpleLFO::saw_uni
float saw_uni(void)
Get value of positive unipolar saw wave for current phase.
Definition: simplelfo.hpp:220
si_fabsf
static float si_fabsf(float x)
Absolute value.
Definition: float_math.h:284
dsp::SimpleLFO::sine_uni_off
float sine_uni_off(const float offset)
Get current value of positive unipolar sine wave for phase with offset.
Definition: simplelfo.hpp:157
dsp::SimpleLFO::cycle
void cycle(void)
Step phase one cycle forward.
Definition: simplelfo.hpp:80
dsp::SimpleLFO::saw_bi_off
float saw_bi_off(const float offset)
Get current value of bipolar saw wave for phase with offset.
Definition: simplelfo.hpp:231