55 #define __fast_inline static inline __attribute__((always_inline, optimize("Ofast")))
83 __fast_inline uint32_t osc_mcu_hash(
void) {
87 #define k_samplerate (48000)
88 #define k_samplerate_recipf (2.08333333333333e-005f)
101 #define k_midi_to_hz_size (152)
103 extern const float midi_to_hz_lut_f[k_midi_to_hz_size];
105 #define k_note_mod_fscale (0.00392156862745098f)
106 #define k_note_max_hz (23679.643054f)
115 return midi_to_hz_lut_f[
clipmaxu32(note,k_midi_to_hz_size-1)];
128 const float f =
clipmaxf(
linintf(mod * k_note_mod_fscale, f0, f1), k_note_max_hz);
130 return f * k_samplerate_recipf;
141 #define k_wt_sine_size_exp (7)
142 #define k_wt_sine_size (1U<<k_wt_sine_size_exp)
143 #define k_wt_sine_u32shift (24)
144 #define k_wt_sine_frrecip (5.96046447753906e-008f) // 1/(1<<24)
145 #define k_wt_sine_mask (k_wt_sine_size-1)
146 #define k_wt_sine_lut_size (k_wt_sine_size+1)
148 extern const float wt_sine_lut_f[k_wt_sine_lut_size];
157 const float p = x - (uint32_t)x;
160 const float x0f = 2.f * p * k_wt_sine_size;
161 const uint32_t x0p = (uint32_t)x0f;
163 const uint32_t x0 = x0p & k_wt_sine_mask;
164 const uint32_t x1 = (x0 + 1) & k_wt_sine_mask;
166 const float y0 =
linintf(x0f - x0p, wt_sine_lut_f[x0], wt_sine_lut_f[x1]);
167 return (x0p < k_wt_sine_size)?y0:-y0;
188 #define k_wt_saw_size_exp (7)
189 #define k_wt_saw_size (1U<<k_wt_saw_size_exp)
190 #define k_wt_saw_u32shift (24)
191 #define k_wt_saw_frrecip (5.96046447753906e-008f) // 1/(1<<24)
192 #define k_wt_saw_mask (k_wt_saw_size-1)
193 #define k_wt_saw_lut_size (k_wt_saw_size+1)
194 #define k_wt_saw_notes_cnt (7)
195 #define k_wt_saw_lut_tsize (k_wt_saw_notes_cnt * k_wt_saw_lut_size)
197 extern const uint8_t wt_saw_notes[k_wt_saw_notes_cnt];
198 extern const float wt_saw_lut_f[k_wt_saw_lut_tsize];
209 const float p = x - (uint32_t)x;
211 const float x0f = 2.f * p * k_wt_saw_size;
212 const uint32_t x0p = (uint32_t)x0f;
214 uint32_t x0 = x0p, x1 = x0p+1;
216 if (x0p >= k_wt_saw_size) {
217 x0 = k_wt_saw_size - (x0p & k_wt_saw_mask);
222 const float y0 =
linintf(x0f - x0p, wt_saw_lut_f[x0], wt_saw_lut_f[x1]);
234 const float p = x - (uint32_t)x;
236 const float x0f = 2.f * p * k_wt_saw_size;
237 const uint32_t x0p = (uint32_t)x0f;
239 uint32_t x0 = x0p, x1 = x0p+1;
241 if (x0p >= k_wt_saw_size) {
242 x0 = k_wt_saw_size - (x0p & k_wt_saw_mask);
246 const float *wt = &wt_saw_lut_f[idx*k_wt_saw_lut_size];
247 const float y0 =
linintf(x0f - x0p, wt[x0], wt[x1]);
259 const float p = x - (uint32_t)x;
261 const float x0f = 2.f * p * k_wt_saw_size;
262 const uint32_t x0p = (uint32_t)x0f;
264 uint32_t x0 = x0p, x1 = x0p+1;
266 if (x0p >= k_wt_saw_size) {
267 x0 = k_wt_saw_size - (x0p & k_wt_saw_mask);
271 const float *wt = &wt_saw_lut_f[(uint16_t)idx*k_wt_saw_lut_size];
272 const float fr = x0f - x0p;
273 const float y0 = sign *
linintf(fr, wt[x0], wt[x1]);
275 wt += k_wt_saw_lut_size;
276 const float y1 = sign *
linintf(fr, wt[x0], wt[x1]);
278 return linintf((idx - (uint8_t)idx), y0, y1);
289 __fast_inline
float osc_bl_saw_idx(
float note) {
302 #define k_wt_sqr_size_exp 7
303 #define k_wt_sqr_size (1U<<k_wt_sqr_size_exp)
304 #define k_wt_sqr_u32shift (24)
305 #define k_wt_sqr_frrecip (5.96046447753906e-008f) // 1/(1<<24)
306 #define k_wt_sqr_mask (k_wt_sqr_size-1)
307 #define k_wt_sqr_lut_size (k_wt_sqr_size+1)
308 #define k_wt_sqr_notes_cnt 7
309 #define k_wt_sqr_lut_tsize (k_wt_sqr_notes_cnt * k_wt_sqr_lut_size)
311 extern const uint8_t wt_sqr_notes[k_wt_sqr_notes_cnt];
312 extern const float wt_sqr_lut_f[k_wt_sqr_lut_tsize];
322 const float p = x - (uint32_t)x;
324 const float x0f = 2.f * p * k_wt_sqr_size;
325 const uint32_t x0p = (uint32_t)x0f;
327 uint32_t x0 = x0p, x1 = x0p+1;
329 if (x0p >= k_wt_sqr_size) {
330 x0 = k_wt_sqr_size - (x0p & k_wt_sqr_mask);
335 const float y0 =
linintf(x0f - x0p, wt_sqr_lut_f[x0], wt_sqr_lut_f[x1]);
348 const float p = x - (uint32_t)x;
350 const float x0f = 2.f * p * k_wt_sqr_size;
351 const uint32_t x0p = (uint32_t)x0f;
353 uint32_t x0 = x0p, x1 = x0p+1;
355 if (x0p >= k_wt_sqr_size) {
356 x0 = k_wt_sqr_size - (x0p & k_wt_sqr_mask);
360 const float *wt = &wt_sqr_lut_f[idx*k_wt_sqr_lut_size];
361 const float y0 =
linintf(x0f - x0p, wt[x0], wt[x1]);
374 const float p = x - (uint32_t)x;
376 const float x0f = 2.f * p * k_wt_sqr_size;
377 const uint32_t x0p = (uint32_t)x0f;
379 uint32_t x0 = x0p, x1 = x0p+1;
381 if (x0p >= k_wt_sqr_size) {
382 x0 = k_wt_sqr_size - (x0p & k_wt_sqr_mask);
386 const float *wt = &wt_sqr_lut_f[(uint16_t)idx*k_wt_sqr_lut_size];
387 const float fr = x0f - x0p;
388 const float y0 = sign *
linintf(fr, wt[x0], wt[x1]);
390 wt += k_wt_sqr_lut_size;
391 const float y1 = sign *
linintf(fr, wt[x0], wt[x1]);
393 return linintf((idx - (uint8_t)idx), y0, y1);
404 __fast_inline
float osc_bl_sqr_idx(
float note) {
417 #define k_wt_par_size_exp 7
418 #define k_wt_par_size (1U<<k_wt_par_size_exp)
419 #define k_wt_par_u32shift (24)
420 #define k_wt_par_frrecip (5.96046447753906e-008f) // 1/(1<<24)
421 #define k_wt_par_mask (k_wt_par_size-1)
422 #define k_wt_par_lut_size (k_wt_par_size+1)
423 #define k_wt_par_notes_cnt 7
424 #define k_wt_par_lut_tsize (k_wt_par_notes_cnt * k_wt_par_lut_size)
426 extern const uint8_t wt_par_notes[k_wt_par_notes_cnt];
427 extern const float wt_par_lut_f[k_wt_par_lut_tsize];
437 const float p = x - (uint32_t)x;
439 const float x0f = 2.f * p * k_wt_par_size;
440 const uint32_t x0p = (uint32_t)x0f;
442 const uint32_t x0 = (x0p<=k_wt_par_size) ? x0p : (k_wt_par_size - (x0p & k_wt_par_mask));
443 const uint32_t x1 = (x0p<(k_wt_par_size-1)) ? (x0 + 1) & k_wt_par_mask : (x0p >= k_wt_par_size) ? (x0 - 1) & k_wt_par_mask : (x0 + 1);
444 const float y0 =
linintf(x0f - x0p, wt_par_lut_f[x0], wt_par_lut_f[x1]);
457 const float p = x - (uint32_t)x;
459 const float x0f = 2.f * p * k_wt_par_size;
460 const uint32_t x0p = (uint32_t)x0f;
462 const uint32_t x0 = (x0p<=k_wt_par_size) ? x0p : (k_wt_par_size - (x0p & k_wt_par_mask));
463 const uint32_t x1 = (x0p<(k_wt_par_size-1)) ? (x0 + 1) & k_wt_par_mask : (x0p >= k_wt_par_size) ? (x0 - 1) & k_wt_par_mask : (x0 + 1);
465 const float *wt = &wt_par_lut_f[idx*k_wt_par_lut_size];
466 const float y0 =
linintf(x0f - x0p, wt[x0], wt[x1]);
479 const float p = x - (uint32_t)x;
481 const float x0f = 2.f * p * k_wt_par_size;
482 const uint32_t x0p = (uint32_t)x0f;
484 uint32_t x0 = (x0p<=k_wt_par_size) ? x0p : (k_wt_par_size - (x0p & k_wt_par_mask));
485 uint32_t x1 = (x0p<(k_wt_par_size-1)) ? (x0 + 1) & k_wt_par_mask : (x0p >= k_wt_par_size) ? (x0 - 1) & k_wt_par_mask : (x0 + 1);
487 const float *wt = &wt_par_lut_f[(uint16_t)idx*k_wt_par_lut_size];
488 const float fr = x0f - x0p;
489 const float y0 =
linintf(fr, wt[x0], wt[x1]);
491 wt += k_wt_par_lut_size;
493 const float y1 =
linintf(fr, wt[x0], wt[x1]);
495 return linintf((idx - (uint8_t)idx), y0, y1);
506 __fast_inline
float osc_bl_par_idx(
float note) {
523 #define k_waves_size_exp (7)
524 #define k_waves_size (1U<<k_waves_size_exp)
525 #define k_waves_u32shift (24)
526 #define k_waves_frrecip (5.96046447753906e-008f) // 1/(1<<24)
527 #define k_waves_mask (k_waves_size-1)
528 #define k_waves_lut_size (k_waves_size+1)
530 #define k_waves_a_cnt 16
532 extern const float *
const wavesA[k_waves_a_cnt];
534 #define k_waves_b_cnt 16
536 extern const float *
const wavesB[k_waves_b_cnt];
538 #define k_waves_c_cnt 14
540 extern const float *
const wavesC[k_waves_c_cnt];
542 #define k_waves_d_cnt 13
544 extern const float *
const wavesD[k_waves_d_cnt];
546 #define k_waves_e_cnt 15
548 extern const float *
const wavesE[k_waves_e_cnt];
550 #define k_waves_f_cnt 16
552 extern const float *
const wavesF[k_waves_f_cnt];
554 static inline __attribute__((always_inline, optimize(
"Ofast")))
555 float osc_wave_scanf(const
float *w,
float x) {
556 const float p = x - (uint32_t)x;
557 const float x0f = p * k_waves_size;
558 const uint32_t x0 = ((uint32_t)x0f) & k_waves_mask;
559 const uint32_t x1 = (x0 + 1) & k_waves_mask;
560 return linintf(x0f - (uint32_t)x0f, w[x0], w[x1]);
563 static inline __attribute__((always_inline, optimize(
"Ofast")))
564 float osc_wave_scanuf(const
float *w, uint32_t x) {
565 const uint32_t x0 = (x>>k_waves_u32shift);
566 const uint32_t x1 = (x0 + 1) & k_waves_mask;
567 const float fr = k_waves_frrecip * (float)(x & ((1U<<k_waves_u32shift)-1));
568 return linintf(fr, w[x0], w[x1]);
583 #define k_log_size_exp (8)
584 #define k_log_size (1U<<k_log_size_exp)
585 #define k_log_mask (k_log_size-1)
586 #define k_log_lut_size (k_log_size+1)
588 extern const float log_lut_f[k_log_lut_size];
598 const float idxf = x * k_log_size;
599 const uint32_t idx = (uint32_t)idxf;
600 const float y0 = log_lut_f[idx];
601 const float y1 = log_lut_f[idx+1];
602 return linintf(idxf - idx, y0, y1);
605 #define k_tanpi_size_exp (8)
606 #define k_tanpi_size (1U<<k_tanpi_size_exp)
607 #define k_tanpi_mask (k_tanpi_size-1)
608 #define k_tanpi_range_recip (2.04081632653061f) // 1/0.49
609 #define k_tanpi_lut_size (k_tanpi_size+1)
611 extern const float tanpi_lut_f[k_log_lut_size];
621 const float idxf = x * k_tanpi_range_recip * k_tanpi_size;
622 const uint32_t idx = (uint32_t)idxf;
623 const float y0 = tanpi_lut_f[idx];
624 const float y1 = tanpi_lut_f[idx+1];
625 return linintf(idxf - idx, y0, y1);
628 #define k_sqrtm2log_size_exp (8)
629 #define k_sqrtm2log_size (1U<<k_sqrtm2log_size_exp)
630 #define k_sqrtm2log_mask (k_sqrtm2log_size-1)
631 #define k_sqrtm2log_base (0.005f)
632 #define k_sqrtm2log_range_recip (1.00502512562814f) // 1/0.995
633 #define k_sqrtm2log_lut_size (k_sqrtm2log_size+1)
635 extern const float sqrtm2log_lut_f[k_sqrtm2log_lut_size];
645 const float idxf = (x-k_sqrtm2log_base) * k_sqrtm2log_range_recip * k_sqrtm2log_size;
646 const uint32_t idx = (uint32_t)idxf;
647 const float y0 = sqrtm2log_lut_f[idx];
648 const float y1 = sqrtm2log_lut_f[idx+1];
649 return linintf(idxf - idx, y0, y1);
674 return x - c * (x*x*x);
677 #define k_cubicsat_size_exp (7)
678 #define k_cubicsat_size (1U<<k_cubicsat_size_exp)
679 #define k_cubicsat_mask (k_cubicsat_size-1)
680 #define k_cubicsat_lut_size (k_cubicsat_size+1)
682 extern const float cubicsat_lut_f[k_cubicsat_lut_size];
692 const uint32_t xi = (uint32_t)x;
693 const float y0 = cubicsat_lut_f[xi];
694 const float y1 = cubicsat_lut_f[xi+1];
698 #define k_schetzen_size_exp (7)
699 #define k_schetzen_size (1U<<k_schetzen_size_exp)
700 #define k_schetzen_mask (k_schetzen_size-1)
701 #define k_schetzen_lut_size (k_schetzen_size+1)
703 extern const float schetzen_lut_f[k_schetzen_lut_size];
713 const uint32_t xi = (uint32_t)x;
714 const float y0 = schetzen_lut_f[xi];
715 const float y1 = schetzen_lut_f[xi+1];
727 #define k_bitres_size_exp (7)
728 #define k_bitres_size (1U<<k_bitres_size_exp)
729 #define k_bitres_mask (k_bitres_size-1)
730 #define k_bitres_lut_size (k_bitres_size+1)
732 extern const float bitres_lut_f[k_bitres_lut_size];
742 const float xf = x * k_bitres_size;
743 const uint32_t xi = (uint32_t)xf;
744 const float y0 = bitres_lut_f[xi];
745 const float y1 = bitres_lut_f[xi+1];
746 return linintf(xf - xi, y0, y1);
768 __fast_inline uint32_t osc_rand(
void) {
779 __fast_inline
float osc_white(
void) {
792 #endif // __osc_api_h