MicroDexed is a compatible 6-operator-FM-synth based on the Teensy(-3.6/-4.0) Microcontroller. https://www.parasitstudio.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1KB

  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define _USE_MATH_DEFINES
  17. #include <math.h>
  18. #include "synth.h"
  19. #include "exp2.h"
  20. #include <stdio.h>
  21. #ifdef _MSC_VER
  22. #define exp2(arg) pow(2.0, arg)
  23. #endif
  24. int32_t exp2tab[EXP2_N_SAMPLES << 1];
  25. void Exp2::init() {
  26. FRAC_NUM inc = exp2(1.0 / EXP2_N_SAMPLES);
  27. FRAC_NUM y = 1 << 30;
  28. for (int i = 0; i < EXP2_N_SAMPLES; i++) {
  29. exp2tab[(i << 1) + 1] = (int32_t)floor(y + 0.5);
  30. y *= inc;
  31. }
  32. for (int i = 0; i < EXP2_N_SAMPLES - 1; i++) {
  33. exp2tab[i << 1] = exp2tab[(i << 1) + 3] - exp2tab[(i << 1) + 1];
  34. }
  35. exp2tab[(EXP2_N_SAMPLES << 1) - 2] = (1U << 31) - exp2tab[(EXP2_N_SAMPLES << 1) - 1];
  36. }
  37. int32_t tanhtab[TANH_N_SAMPLES << 1];
  38. static FRAC_NUM dtanh(FRAC_NUM y) {
  39. return 1 - y * y;
  40. }
  41. void Tanh::init() {
  42. FRAC_NUM step = 4.0 / TANH_N_SAMPLES;
  43. FRAC_NUM y = 0;
  44. for (int i = 0; i < TANH_N_SAMPLES; i++) {
  45. tanhtab[(i << 1) + 1] = (1 << 24) * y + 0.5;
  46. //printf("%d\n", tanhtab[(i << 1) + 1]);
  47. // Use a basic 4th order Runge-Kutte to compute tanh from its
  48. // differential equation.
  49. FRAC_NUM k1 = dtanh(y);
  50. FRAC_NUM k2 = dtanh(y + 0.5 * step * k1);
  51. FRAC_NUM k3 = dtanh(y + 0.5 * step * k2);
  52. FRAC_NUM k4 = dtanh(y + step * k3);
  53. FRAC_NUM dy = (step / 6) * (k1 + k4 + 2 * (k2 + k3));
  54. y += dy;
  55. }
  56. for (int i = 0; i < TANH_N_SAMPLES - 1; i++) {
  57. tanhtab[i << 1] = tanhtab[(i << 1) + 3] - tanhtab[(i << 1) + 1];
  58. }
  59. int32_t lasty = (1 << 24) * y + 0.5;
  60. tanhtab[(TANH_N_SAMPLES << 1) - 2] = lasty - tanhtab[(TANH_N_SAMPLES << 1) - 1];
  61. }