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.

119 lines
4.1KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Just intonation processor. This use a very simple algorithm, which gives
  28. // surprising nice results in simple cases.
  29. //
  30. // A history of the previous notes is kept in a circular buffer. For memory
  31. // and efficienty reason, we keep here the 16 previous note. Each note is
  32. // given a weight. The weight of a note stays 1.0 while the note is still heard,
  33. // but it decays exponentially once the note is no longer played.
  34. //
  35. // To tune a note, each possible tuning in the +/- 1 quartertone range is
  36. // considered. For each tuning, a "badness" score is computed. The badness score
  37. // is equal to the weighted sum of the "dissonance" score of the interval
  38. // between the candidate tuning and the tuning of each previous note in the
  39. // history. The dissonance score is read from a lookup table. It has a low
  40. // value for just intervals (say 4/3), which goes slightly higher as the
  41. // interval involves more convoluted ratios (say 32/27), and goes up according
  42. // to a square law as we move away from the just intervals.
  43. // The tuning giving the least badness score is selected.
  44. #ifndef YARNS_JUST_INTONATION_PROCESSOR_H_
  45. #define YARNS_JUST_INTONATION_PROCESSOR_H_
  46. #include "stmlib/stmlib.h"
  47. namespace yarns {
  48. struct HistoryEntry {
  49. uint8_t note;
  50. uint8_t weight;
  51. int16_t pitch;
  52. };
  53. const size_t kHistorySize = 16;
  54. class JustIntonationProcessor {
  55. public:
  56. JustIntonationProcessor() { }
  57. ~JustIntonationProcessor() { }
  58. void Init();
  59. inline void NoteOff(uint8_t note) {
  60. for (uint8_t i = 0; i < kHistorySize; ++i) {
  61. if (history_[i].note == note && history_[i].weight == 255) {
  62. history_[i].weight = 192;
  63. }
  64. }
  65. }
  66. inline int16_t NoteOn(uint8_t note) {
  67. if (note != cached_note_) {
  68. // Skip the computationally expensive routine on expensive notes.
  69. cached_note_ = note;
  70. cached_pitch_ = Tune(static_cast<int>(note) << 7);
  71. }
  72. // Decay the weight of the previous notes - except those that are still
  73. // playing.
  74. for (size_t i = 0; i < kHistorySize; ++i) {
  75. if (history_[i].weight != 255) {
  76. history_[i].weight = (history_[i].weight * 3) >> 2;
  77. }
  78. }
  79. history_[write_ptr_].note = note;
  80. history_[write_ptr_].weight = 255;
  81. history_[write_ptr_].pitch = cached_pitch_;
  82. ++write_ptr_;
  83. if (write_ptr_ >= kHistorySize) {
  84. write_ptr_ = 0;
  85. }
  86. return cached_pitch_;
  87. }
  88. private:
  89. int Tune(int note, int min, int max, int steps);
  90. int16_t Tune(int note) {
  91. int coarse = Tune(note, -32, 32, 4);
  92. return int16_t(note + Tune(note, coarse - 6, coarse + 6, 1));
  93. }
  94. size_t write_ptr_;
  95. int16_t cached_pitch_;
  96. uint8_t cached_note_;
  97. HistoryEntry history_[kHistorySize];
  98. DISALLOW_COPY_AND_ASSIGN(JustIntonationProcessor);
  99. };
  100. extern JustIntonationProcessor just_intonation_processor;
  101. } // namespace yarns
  102. #endif // YARNS_JUST_INTONATION_PROCESSOR_H_