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.

83 lines
2.3KB

  1. /*
  2. * Copyright 2017 Pascal Gauthier.
  3. * Copyright 2012 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #ifndef __ENV_H
  18. #define __ENV_H
  19. #include "synth.h"
  20. // DX7 envelope generation
  21. #define ACCURATE_ENVELOPE
  22. class Env {
  23. public:
  24. // The rates and levels arrays are calibrated to match the Dx7 parameters
  25. // (ie, value 0..99). The outlevel parameter is calibrated in microsteps
  26. // (ie units of approx .023 dB), with 99 * 32 = nominal full scale. The
  27. // rate_scaling parameter is in qRate units (ie 0..63).
  28. void init(const int rates[4], const int levels[4], int32_t outlevel,
  29. int rate_scaling);
  30. void update(const int rates[4], const int levels[4], int32_t outlevel,
  31. int rate_scaling);
  32. // Result is in Q24/doubling log format. Also, result is subsampled
  33. // for every N samples.
  34. // A couple more things need to happen for this to be used as a gain
  35. // value. First, the # of outputs scaling needs to be applied. Also,
  36. // modulation.
  37. // Then, of course, log to linear.
  38. int32_t getsample();
  39. void keydown(bool down);
  40. static int scaleoutlevel(int outlevel);
  41. void getPosition(char *step);
  42. static void init_sr(FRAC_NUM sample_rate);
  43. void transfer(Env &src);
  44. private:
  45. // PG: This code is normalized to 44100, need to put a multiplier
  46. // if we are not using 44100.
  47. static uint32_t sr_multiplier;
  48. int rates_[4];
  49. int levels_[4];
  50. int outlevel_;
  51. int rate_scaling_;
  52. // Level is stored so that 2^24 is one doubling, ie 16 more bits than
  53. // the DX7 itself (fraction is stored in level rather than separate
  54. // counter)
  55. int32_t level_;
  56. int targetlevel_;
  57. bool rising_;
  58. int ix_;
  59. int inc_;
  60. #ifdef ACCURATE_ENVELOPE
  61. int staticcount_;
  62. #endif
  63. bool down_;
  64. void advance(int newix);
  65. };
  66. #endif // __ENV_H