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.

56 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. // Resolve frequency signal (1.0 in Q24 format = 1 octave) to phase delta.
  17. // The LUT is just a global, and we'll need the init function to be called before
  18. // use.
  19. #include <stdint.h>
  20. #include <math.h>
  21. #include "freqlut.h"
  22. #define LG_N_SAMPLES 10
  23. #define N_SAMPLES (1 << LG_N_SAMPLES)
  24. #define SAMPLE_SHIFT (24 - LG_N_SAMPLES)
  25. #define MAX_LOGFREQ_INT 20
  26. int32_t lut[N_SAMPLES + 1];
  27. void Freqlut::init(FRAC_NUM sample_rate) {
  28. FRAC_NUM y = (1LL << (24 + MAX_LOGFREQ_INT)) / sample_rate;
  29. FRAC_NUM inc = pow(2, 1.0 / N_SAMPLES);
  30. for (int i = 0; i < N_SAMPLES + 1; i++) {
  31. lut[i] = (int32_t)floor(y + 0.5);
  32. y *= inc;
  33. }
  34. }
  35. // Note: if logfreq is more than 20.0, the results will be inaccurate. However,
  36. // that will be many times the Nyquist rate.
  37. int32_t Freqlut::lookup(int32_t logfreq) {
  38. int ix = (logfreq & 0xffffff) >> SAMPLE_SHIFT;
  39. int32_t y0 = lut[ix];
  40. int32_t y1 = lut[ix + 1];
  41. int lowbits = logfreq & ((1 << SAMPLE_SHIFT) - 1);
  42. int32_t y = y0 + ((((int64_t)(y1 - y0) * (int64_t)lowbits)) >> SAMPLE_SHIFT);
  43. int hibits = logfreq >> 24;
  44. return y >> (MAX_LOGFREQ_INT - hibits);
  45. }