Nu:Tekt NTS-1 digital 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 
122 #define k_wt_sine_size_exp (7)
123 #define k_wt_sine_size (1U<<k_wt_sine_size_exp)
124 #define k_wt_sine_u32shift (24)
125 #define k_wt_sine_frrecip (5.96046447753906e-008f) // 1/(1<<24)
126 #define k_wt_sine_mask (k_wt_sine_size-1)
127 #define k_wt_sine_lut_size (k_wt_sine_size+1)
128 
129  extern const float wt_sine_lut_f[k_wt_sine_lut_size];
130 
137  __fast_inline float fx_sinf(float x) {
138  const float p = x - (uint32_t)x;
139 
140  // half period stored -- wrap around and invert
141  const float x0f = 2.f * p * k_wt_sine_size;
142  const uint32_t x0p = (uint32_t)x0f;
143 
144  const uint32_t x0 = x0p & k_wt_sine_mask;
145  const uint32_t x1 = (x0 + 1) & k_wt_sine_mask;
146 
147  const float y0 = linintf(x0f - x0p, wt_sine_lut_f[x0], wt_sine_lut_f[x1]);
148  return (x0p < k_wt_sine_size)?y0:-y0;
149  }
150 
157  __fast_inline float fx_sinuf(uint32_t x) {
158  (void)x;
159  return 0.f;
160  }
161 
168  __fast_inline float fx_cosf(float x) {
169  return fx_sinf(x+0.25f);
170  }
171 
178  __fast_inline float fx_cosuf(uint32_t x) {
179  return fx_sinuf(x+((k_wt_sine_size>>2)<<k_wt_sine_u32shift));
180  }
181 
184  /*===========================================================================*/
185  /* Various function lookups */
186  /*===========================================================================*/
187 
194 #define k_log_size_exp (8)
195 #define k_log_size (1U<<k_log_size_exp)
196 #define k_log_mask (k_log_size-1)
197 #define k_log_lut_size (k_log_size+1)
198 
199  extern const float log_lut_f[k_log_lut_size];
200 
208  __fast_inline float fx_logf(float x) {
209  const float idxf = x * k_log_size;
210  const uint32_t idx = (uint32_t)idxf;
211  const float y0 = log_lut_f[idx];
212  const float y1 = log_lut_f[idx+1];
213  return linintf(idxf - idx, y0, y1);
214  }
215 
216 #define k_tanpi_size_exp (8)
217 #define k_tanpi_size (1U<<k_tanpi_size_exp)
218 #define k_tanpi_mask (k_tanpi_size-1)
219 #define k_tanpi_range_recip (2.04081632653061f) // 1/0.49
220 #define k_tanpi_lut_size (k_tanpi_size+1)
221 
222  extern const float tanpi_lut_f[k_log_lut_size];
223 
231  __fast_inline float fx_tanpif(float x) {
232  const float idxf = x * k_tanpi_range_recip * k_tanpi_size;
233  const uint32_t idx = (uint32_t)idxf;
234  const float y0 = tanpi_lut_f[idx];
235  const float y1 = tanpi_lut_f[idx+1];
236  return linintf(idxf - idx, y0, y1);
237  }
238 
239 #define k_sqrtm2log_size_exp (8)
240 #define k_sqrtm2log_size (1U<<k_sqrtm2log_size_exp)
241 #define k_sqrtm2log_mask (k_sqrtm2log_size-1)
242 #define k_sqrtm2log_base (0.005f)
243 #define k_sqrtm2log_range_recip (1.00502512562814f) // 1/0.995
244 #define k_sqrtm2log_lut_size (k_sqrtm2log_size+1)
245 
246  extern const float sqrtm2log_lut_f[k_sqrtm2log_lut_size];
247 
255  __fast_inline float fx_sqrtm2logf(float x) {
256  const float idxf = (x-k_sqrtm2log_base) * k_sqrtm2log_range_recip * k_sqrtm2log_size;
257  const uint32_t idx = (uint32_t)idxf;
258  const float y0 = sqrtm2log_lut_f[idx];
259  const float y1 = sqrtm2log_lut_f[idx+1];
260  return linintf(idxf - idx, y0, y1);
261  }
262 
263 #define k_pow2_size_exp (8)
264 #define k_pow2_size (1U<<k_pow2_size_exp)
265 #define k_pow2_scale (85.3333333333333f) // 256 / 3
266 #define k_pow2_mask (k_pow2_size-1)
267 #define k_pow2_lut_size (k_pow2_size+1)
268 
269  extern const float pow2_lut_f[k_pow2_lut_size];
270 
278  __fast_inline float fx_pow2f(float x) {
279  const float idxf = x * k_pow2_scale;
280  const uint32_t idx = (uint32_t)idxf;
281  const float y0 = pow2_lut_f[idx];
282  const float y1 = pow2_lut_f[idx+1];
283  return linintf(idxf - idx, y0, y1);
284  }
285 
288  /*===========================================================================*/
289  /* Clipping and Saturation */
290  /*===========================================================================*/
305  __fast_inline float fx_softclipf(const float c, float x)
306  {
307  x = clip1m1f(x);
308  return x - c * (x*x*x);
309  }
310 
311 #define k_cubicsat_size_exp (7)
312 #define k_cubicsat_size (1U<<k_cubicsat_size_exp)
313 #define k_cubicsat_mask (k_cubicsat_size-1)
314 #define k_cubicsat_lut_size (k_cubicsat_size+1)
315 
316  extern const float cubicsat_lut_f[k_cubicsat_lut_size];
317 
324  __fast_inline float fx_sat_cubicf(float x) {
325  const float xf = si_fabsf(clip1f(x)) * k_cubicsat_size;
326  const uint32_t xi = (uint32_t)x;
327  const float y0 = cubicsat_lut_f[xi];
328  const float y1 = cubicsat_lut_f[xi+1];
329  return si_copysignf(linintf(xf - xi, y0, y1), x);
330  }
331 
332 #define k_schetzen_size_exp (7)
333 #define k_schetzen_size (1U<<k_schetzen_size_exp)
334 #define k_schetzen_mask (k_schetzen_size-1)
335 #define k_schetzen_lut_size (k_schetzen_size+1)
336 
337  extern const float schetzen_lut_f[k_schetzen_lut_size];
338 
345  __fast_inline float fx_sat_schetzenf(float x) {
346  const float xf = si_fabsf(clip1f(x)) * k_schetzen_size;
347  const uint32_t xi = (uint32_t)x;
348  const float y0 = schetzen_lut_f[xi];
349  const float y1 = schetzen_lut_f[xi+1];
350  return si_copysignf(linintf(xf - xi, y0, y1), x);
351  }
352 
361 #define k_bitres_size_exp (7)
362 #define k_bitres_size (1U<<k_bitres_size_exp)
363 #define k_bitres_mask (k_bitres_size-1)
364 #define k_bitres_lut_size (k_bitres_size+1)
365 
366  extern const float bitres_lut_f[k_bitres_lut_size];
367 
375  __fast_inline float fx_bitresf(float x) {
376  const float xf = x * k_bitres_size;
377  const uint32_t xi = (uint32_t)xf;
378  const float y0 = bitres_lut_f[xi];
379  const float y1 = bitres_lut_f[xi+1];
380  return linintf(xf - xi, y0, y1);
381  }
382 
385  /*===========================================================================*/
386  /* Noise source */
387  /*===========================================================================*/
388 
400  uint32_t _fx_rand(void);
401 
402  __fast_inline uint32_t fx_rand(void) {
403  return _fx_rand();
404  }
405 
411  float _fx_white(void);
412 
413  __fast_inline float fx_white(void) {
414  return _fx_white();
415  }
416 
419 #ifdef __cplusplus
420 }
421 #endif
422 
423 
424 #endif // __fx_api_h
425 
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:168
_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:137
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:255
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:178
fx_sat_schetzenf
__fast_inline float fx_sat_schetzenf(float x)
Schetzen saturation.
Definition: fx_api.h:345
_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:157
_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:208
fx_sat_cubicf
__fast_inline float fx_sat_cubicf(float x)
Cubic saturation.
Definition: fx_api.h:324
_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:231
fx_bitresf
__fast_inline float fx_bitresf(float x)
Bit depth scaling table.
Definition: fx_api.h:375
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:278
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:305
si_fabsf
static float si_fabsf(float x)
Absolute value.
Definition: float_math.h:284