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.

108 lines
3.3KB

  1. // Copyright 2015 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. // Note quantizer
  28. #include "braids/quantizer.h"
  29. #include <algorithm>
  30. #include <cstdlib>
  31. namespace braids {
  32. void Quantizer::Init() {
  33. enabled_ = true;
  34. codeword_ = 0;
  35. previous_boundary_ = 0;
  36. next_boundary_ = 0;
  37. for (int16_t i = 0; i < 128; ++i) {
  38. codebook_[i] = (i - 64) << 7;
  39. }
  40. }
  41. void Quantizer::Configure(
  42. const int16_t* notes,
  43. int16_t span,
  44. size_t num_notes) {
  45. enabled_ = notes != NULL && num_notes != 0 && span != 0;
  46. if (enabled_) {
  47. int32_t octave = 0;
  48. size_t note = 0;
  49. int16_t root = 0;
  50. for (int32_t i = 0; i < 64; ++i) {
  51. int32_t up = root + notes[note] + span * octave;
  52. int32_t down = root + notes[num_notes - 1 - note] + (-octave - 1) * span;
  53. CLIP(up)
  54. CLIP(down)
  55. codebook_[64 + i] = up;
  56. codebook_[64 - i - 1] = down;
  57. ++note;
  58. if (note >= num_notes) {
  59. note = 0;
  60. ++octave;
  61. }
  62. }
  63. }
  64. }
  65. int32_t Quantizer::Process(int32_t pitch, int32_t root) {
  66. if (!enabled_) {
  67. return pitch;
  68. }
  69. pitch -= root;
  70. if (pitch >= previous_boundary_ && pitch <= next_boundary_) {
  71. // We're still in the voronoi cell for the active codeword.
  72. pitch = codeword_;
  73. } else {
  74. // Search for the nearest neighbour in the codebook.
  75. int16_t upper_bound_index = std::upper_bound(
  76. &codebook_[3],
  77. &codebook_[126],
  78. static_cast<int16_t>(pitch)) - &codebook_[0];
  79. int16_t lower_bound_index = upper_bound_index - 2;
  80. int16_t best_distance = 16384;
  81. int16_t q = -1;
  82. for (int16_t i = lower_bound_index; i <= upper_bound_index; ++i) {
  83. int16_t distance = abs(pitch - codebook_[i]);
  84. if (distance < best_distance) {
  85. best_distance = distance;
  86. q = i;
  87. }
  88. }
  89. codeword_ = codebook_[q];
  90. // Enlarge the current voronoi cell a bit for hysteresis.
  91. previous_boundary_ = (9 * codebook_[q - 1] + 7 * codeword_) >> 4;
  92. next_boundary_ = (9 * codebook_[q + 1] + 7 * codeword_) >> 4;
  93. pitch = codeword_;
  94. }
  95. pitch += root;
  96. return pitch;
  97. }
  98. } // namespace braids