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.

96 lines
2.6KB

  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. #include "synth.h"
  17. #include "pitchenv.h"
  18. int PitchEnv::unit_;
  19. void PitchEnv::init(FRAC_NUM sample_rate) {
  20. unit_ = _N_ * (1 << 24) / (21.3 * sample_rate) + 0.5;
  21. }
  22. const uint8_t pitchenv_rate[] = {
  23. 1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12,
  24. 12, 13, 13, 14, 14, 15, 16, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24,
  25. 25, 26, 27, 28, 30, 31, 33, 34, 36, 37, 38, 39, 41, 42, 44, 46, 47,
  26. 49, 51, 53, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 79, 82,
  27. 85, 88, 91, 94, 98, 102, 106, 110, 115, 120, 125, 130, 135, 141, 147,
  28. 153, 159, 165, 171, 178, 185, 193, 202, 211, 232, 243, 254, 255
  29. };
  30. const int8_t pitchenv_tab[] = {
  31. -128, -116, -104, -95, -85, -76, -68, -61, -56, -52, -49, -46, -43,
  32. -41, -39, -37, -35, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24,
  33. -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10,
  34. -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  35. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
  36. 28, 29, 30, 31, 32, 33, 34, 35, 38, 40, 43, 46, 49, 53, 58, 65, 73,
  37. 82, 92, 103, 115, 127
  38. };
  39. void PitchEnv::set(const int r[4], const int l[4]) {
  40. for (int i = 0; i < 4; i++) {
  41. rates_[i] = r[i];
  42. levels_[i] = l[i];
  43. }
  44. level_ = pitchenv_tab[l[3]] << 19;
  45. down_ = true;
  46. advance(0);
  47. }
  48. int32_t PitchEnv::getsample() {
  49. if (ix_ < 3 || ((ix_ < 4) && !down_)) {
  50. if (rising_) {
  51. level_ += inc_;
  52. if (level_ >= targetlevel_) {
  53. level_ = targetlevel_;
  54. advance(ix_ + 1);
  55. }
  56. } else { // !rising
  57. level_ -= inc_;
  58. if (level_ <= targetlevel_) {
  59. level_ = targetlevel_;
  60. advance(ix_ + 1);
  61. }
  62. }
  63. }
  64. return level_;
  65. }
  66. void PitchEnv::keydown(bool d) {
  67. if (down_ != d) {
  68. down_ = d;
  69. advance(d ? 0 : 3);
  70. }
  71. }
  72. void PitchEnv::advance(int newix) {
  73. ix_ = newix;
  74. if (ix_ < 4) {
  75. int newlevel = levels_[ix_];
  76. targetlevel_ = pitchenv_tab[newlevel] << 19;
  77. rising_ = (targetlevel_ > level_);
  78. inc_ = pitchenv_rate[rates_[ix_]] * unit_;
  79. }
  80. }
  81. void PitchEnv::getPosition(char *step) {
  82. *step = ix_;
  83. }