minilogue xd SDK  v1.1-0
fx_api.h
Go to the documentation of this file.
1 /*
2  BSD 3-Clause License
3 
4  Copyright (c) 2018, KORG INC.
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12 
13  * Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17  * Neither the name of the copyright holder nor the names of its
18  contributors may be used to endorse or promote products derived from
19  this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 //*/
33 
45 #ifndef __fx_api_h
46 #define __fx_api_h
47 
48 #include "float_math.h"
49 #include "int_math.h"
50 #include "fixed_math.h"
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 #define __fast_inline static inline __attribute__((always_inline, optimize("Ofast")))
57 
58  /*===========================================================================*/
59  /* Runtime Environment */
60  /*===========================================================================*/
61 
70  extern const uint32_t k_fx_api_platform;
71 
75  extern const uint32_t k_fx_api_version;
76 
82  uint32_t _fx_mcu_hash(void);
83 
84  __fast_inline uint32_t fx_mcu_hash(void) {
85  return _fx_mcu_hash();
86  }
87 
93  uint16_t _fx_get_bpm(void);
94 
95  __fast_inline uint16_t fx_get_bpm(void) {
96  return _fx_get_bpm();
97  }
98 
104  float _fx_get_bpmf(void);
105 
106  __fast_inline float fx_get_bpmf(void) {
107  return _fx_get_bpmf();
108  }
109 
112  /*===========================================================================*/
113  /* Lookup tables */
114  /*===========================================================================*/
115 
123 #define k_wt_sine_size_exp (7)
124 #define k_wt_sine_size (1U<<k_wt_sine_size_exp)
125 #define k_wt_sine_u32shift (24)
126 #define k_wt_sine_frrecip (5.96046447753906e-008f) // 1/(1<<24)
127 #define k_wt_sine_mask (k_wt_sine_size-1)
128 #define k_wt_sine_lut_size (k_wt_sine_size+1)
129 
130  extern const float wt_sine_lut_f[k_wt_sine_lut_size];
131 
138  __fast_inline float fx_sinf(float x) {
139  const float p = x - (uint32_t)x;
140 
141  // half period stored -- wrap around and invert
142  const float x0f = 2.f * p * k_wt_sine_size;
143  const uint32_t x0p = (uint32_t)x0f;
144 
145  const uint32_t x0 = x0p & k_wt_sine_mask;
146  const uint32_t x1 = (x0 + 1) & k_wt_sine_mask;
147 
148  const float y0 = linintf(x0f - x0p, wt_sine_lut_f[x0], wt_sine_lut_f[x1]);
149  return (x0p < k_wt_sine_size)?y0:-y0;
150  }
151 
158  __fast_inline float fx_sinuf(uint32_t x) {
159  //TODO
160  (void)x;
161  return 0.f;
162  }
163 
170  __fast_inline float fx_cosf(float x) {
171  return fx_sinf(x+0.25f);
172  }
173 
180  __fast_inline float fx_cosuf(uint32_t x) {
181  return fx_sinuf(x+((k_wt_sine_size>>2)<<k_wt_sine_u32shift));
182  }
183 
186  /*===========================================================================*/
187  /* Various function lookups */
188  /*===========================================================================*/
189 
196 #define k_log_size_exp (8)
197 #define k_log_size (1U<<k_log_size_exp)
198 #define k_log_mask (k_log_size-1)
199 #define k_log_lut_size (k_log_size+1)
200 
201  extern const float log_lut_f[k_log_lut_size];
202 
210  __fast_inline float fx_logf(float x) {
211  const float idxf = x * k_log_size;
212  const uint32_t idx = (uint32_t)idxf;
213  const float y0 = log_lut_f[idx];
214  const float y1 = log_lut_f[idx+1];
215  return linintf(idxf - idx, y0, y1);
216  }
217 
218 #define k_tanpi_size_exp (8)
219 #define k_tanpi_size (1U<<k_tanpi_size_exp)
220 #define k_tanpi_mask (k_tanpi_size-1)
221 #define k_tanpi_range_recip (2.04081632653061f) // 1/0.49
222 #define k_tanpi_lut_size (k_tanpi_size+1)
223 
224  extern const float tanpi_lut_f[k_log_lut_size];
225 
233  __fast_inline float fx_tanpif(float x) {
234  const float idxf = x * k_tanpi_range_recip * k_tanpi_size;
235  const uint32_t idx = (uint32_t)idxf;
236  const float y0 = tanpi_lut_f[idx];
237  const float y1 = tanpi_lut_f[idx+1];
238  return linintf(idxf - idx, y0, y1);
239  }
240 
241 #define k_sqrtm2log_size_exp (8)
242 #define k_sqrtm2log_size (1U<<k_sqrtm2log_size_exp)
243 #define k_sqrtm2log_mask (k_sqrtm2log_size-1)
244 #define k_sqrtm2log_base (0.005f)
245 #define k_sqrtm2log_range_recip (1.00502512562814f) // 1/0.995
246 #define k_sqrtm2log_lut_size (k_sqrtm2log_size+1)
247 
248  extern const float sqrtm2log_lut_f[k_sqrtm2log_lut_size];
249 
257  __fast_inline float fx_sqrtm2logf(float x) {
258  const float idxf = (x-k_sqrtm2log_base) * k_sqrtm2log_range_recip * k_sqrtm2log_size;
259  const uint32_t idx = (uint32_t)idxf;
260  const float y0 = sqrtm2log_lut_f[idx];
261  const float y1 = sqrtm2log_lut_f[idx+1];
262  return linintf(idxf - idx, y0, y1);
263  }
264 
265 #define k_pow2_size_exp (8)
266 #define k_pow2_size (1U<<k_pow2_size_exp)
267 #define k_pow2_scale (85.3333333333333f) // 256 / 3
268 #define k_pow2_mask (k_pow2_size-1)
269 #define k_pow2_lut_size (k_pow2_size+1)
270 
271  extern const float pow2_lut_f[k_pow2_lut_size];
272 
280  __fast_inline float fx_pow2f(float x) {
281  const float idxf = x * k_pow2_scale;
282  const uint32_t idx = (uint32_t)idxf;
283  const float y0 = pow2_lut_f[idx];
284  const float y1 = pow2_lut_f[idx+1];
285  return linintf(idxf - idx, y0, y1);
286  }
287 
290  /*===========================================================================*/
291  /* Clipping and Saturation */
292  /*===========================================================================*/
307  __fast_inline float fx_softclipf(const float c, float x)
308  {
309  x = clip1m1f(x);
310  return x - c * (x*x*x);
311  }
312 
313 #define k_cubicsat_size_exp (7)
314 #define k_cubicsat_size (1U<<k_cubicsat_size_exp)
315 #define k_cubicsat_mask (k_cubicsat_size-1)
316 #define k_cubicsat_lut_size (k_cubicsat_size+1)
317 
318  extern const float cubicsat_lut_f[k_cubicsat_lut_size];
319 
326  __fast_inline float fx_sat_cubicf(float x) {
327  const float xf = si_fabsf(clip1f(x)) * k_cubicsat_size;
328  const uint32_t xi = (uint32_t)x;
329  const float y0 = cubicsat_lut_f[xi];
330  const float y1 = cubicsat_lut_f[xi+1];
331  return si_copysignf(linintf(xf - xi, y0, y1), x);
332  }
333 
334 #define k_schetzen_size_exp (7)
335 #define k_schetzen_size (1U<<k_schetzen_size_exp)
336 #define k_schetzen_mask (k_schetzen_size-1)
337 #define k_schetzen_lut_size (k_schetzen_size+1)
338 
339  extern const float schetzen_lut_f[k_schetzen_lut_size];
340 
347  __fast_inline float fx_sat_schetzenf(float x) {
348  const float xf = si_fabsf(clip1f(x)) * k_schetzen_size;
349  const uint32_t xi = (uint32_t)x;
350  const float y0 = schetzen_lut_f[xi];
351  const float y1 = schetzen_lut_f[xi+1];
352  return si_copysignf(linintf(xf - xi, y0, y1), x);
353  }
354 
363 #define k_bitres_size_exp (7)
364 #define k_bitres_size (1U<<k_bitres_size_exp)
365 #define k_bitres_mask (k_bitres_size-1)
366 #define k_bitres_lut_size (k_bitres_size+1)
367 
368  extern const float bitres_lut_f[k_bitres_lut_size];
369 
377  __fast_inline float fx_bitresf(float x) {
378  const float xf = x * k_bitres_size;
379  const uint32_t xi = (uint32_t)xf;
380  const float y0 = bitres_lut_f[xi];
381  const float y1 = bitres_lut_f[xi+1];
382  return linintf(xf - xi, y0, y1);
383  }
384 
387  /*===========================================================================*/
388  /* Noise source */
389  /*===========================================================================*/
390 
402  uint32_t _fx_rand(void);
403 
404  __fast_inline uint32_t fx_rand(void) {
405  return _fx_rand();
406  }
407 
413  float _fx_white(void);
414 
415  __fast_inline float fx_white(void) {
416  return _fx_white();
417  }
418 
421 #ifdef __cplusplus
422 }
423 #endif
424 
425 
426 #endif // __fx_api_h
427 
k_fx_api_platform
const uint32_t k_fx_api_platform
Current platform.
fx_cosf
__fast_inline float fx_cosf(float x)
Lookup value of cos(2*pi*x) in [0, 1.0] range.
Definition: fx_api.h:170
_fx_rand
uint32_t _fx_rand(void)
Random integer.
fx_sinf
__fast_inline float fx_sinf(float x)
Lookup value of sin(2*pi*x).
Definition: fx_api.h:138
fx_sqrtm2logf
__fast_inline float fx_sqrtm2logf(float x)
Lookup value of sqrt(-2*log(x)) in [0.005, 1.0] range.
Definition: fx_api.h:257
fixed_math.h
Fixed Point Math Utilities.
fx_cosuf
__fast_inline float fx_cosuf(uint32_t x)
Lookup value of cos(2*pi*x) in [0, 1.0] range.
Definition: fx_api.h:180
fx_sat_schetzenf
__fast_inline float fx_sat_schetzenf(float x)
Schetzen saturation.
Definition: fx_api.h:347
_fx_get_bpm
uint16_t _fx_get_bpm(void)
Get current tempo as beats per minute as integer.
int_math.h
Integer Math Utilities.
float_math.h
Floating Point Math Utilities.
k_fx_api_version
const uint32_t k_fx_api_version
Current API version.
fx_sinuf
__fast_inline float fx_sinuf(uint32_t x)
Lookup value of sin(2*pi*x) in [0, 1.0] range.
Definition: fx_api.h:158
_fx_mcu_hash
uint32_t _fx_mcu_hash(void)
Get MCU hash.
si_copysignf
static float si_copysignf(const float x, const float y)
Return x with sign of y applied.
Definition: float_math.h:270
_fx_get_bpmf
float _fx_get_bpmf(void)
Get current tempo as beats per minute as floating point.
fx_logf
__fast_inline float fx_logf(float x)
Lookup value of log(x) in [0.00001, 1.0] range.
Definition: fx_api.h:210
fx_sat_cubicf
__fast_inline float fx_sat_cubicf(float x)
Cubic saturation.
Definition: fx_api.h:326
_fx_white
float _fx_white(void)
Gaussian white noise.
fx_tanpif
__fast_inline float fx_tanpif(float x)
Lookup value of tan(pi*x) in [0.0001, 0.49] range.
Definition: fx_api.h:233
fx_bitresf
__fast_inline float fx_bitresf(float x)
Bit depth scaling table.
Definition: fx_api.h:377
clip1f
static float clip1f(const float x)
Clip upper bound of x to 1.f (inclusive)
Definition: float_math.h:413
fx_pow2f
__fast_inline float fx_pow2f(float x)
Lookup value of 2^k for k in [0, 3.0] range.
Definition: fx_api.h:280
clip1m1f
static float clip1m1f(const float x)
Clip x to [-1.f, 1.f] (inclusive)
Definition: float_math.h:431
linintf
static float linintf(const float fr, const float x0, const float x1)
Linear interpolation.
Definition: float_math.h:824
fx_softclipf
__fast_inline float fx_softclipf(const float c, float x)
Soft clip.
Definition: fx_api.h:307
si_fabsf
static float si_fabsf(float x)
Absolute value.
Definition: float_math.h:284