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.

63 lines
1.7KB

  1. /*
  2. * Copyright 2012 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. class Sin {
  17. public:
  18. Sin();
  19. static void init();
  20. static int32_t lookup(int32_t phase);
  21. static int32_t compute(int32_t phase);
  22. // A more accurate sine, both input and output Q30
  23. static int32_t compute10(int32_t phase);
  24. };
  25. #define SIN_LG_N_SAMPLES 10
  26. #define SIN_N_SAMPLES (1 << SIN_LG_N_SAMPLES)
  27. #define SIN_INLINE
  28. // Use twice as much RAM for the LUT but avoid a little computation
  29. #define SIN_DELTA
  30. #ifdef SIN_DELTA
  31. extern int32_t sintab[SIN_N_SAMPLES << 1];
  32. #else
  33. extern int32_t sintab[SIN_N_SAMPLES + 1];
  34. #endif
  35. #ifdef SIN_INLINE
  36. inline
  37. int32_t Sin::lookup(int32_t phase) {
  38. const int SHIFT = 24 - SIN_LG_N_SAMPLES;
  39. int lowbits = phase & ((1 << SHIFT) - 1);
  40. #ifdef SIN_DELTA
  41. int phase_int = (phase >> (SHIFT - 1)) & ((SIN_N_SAMPLES - 1) << 1);
  42. int dy = sintab[phase_int];
  43. int y0 = sintab[phase_int + 1];
  44. return y0 + (((int64_t)dy * (int64_t)lowbits) >> SHIFT);
  45. #else
  46. int phase_int = (phase >> SHIFT) & (SIN_N_SAMPLES - 1);
  47. int y0 = sintab[phase_int];
  48. int y1 = sintab[phase_int + 1];
  49. return y0 + (((int64_t)(y1 - y0) * (int64_t)lowbits) >> SHIFT);
  50. #endif
  51. }
  52. #endif