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.

100 lines
2.6KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Polyphonic voice allocator.
  19. #include "edges/voice_allocator.h"
  20. #include <string.h>
  21. namespace edges {
  22. void VoiceAllocator::Clear() {
  23. ClearNotes();
  24. for (uint8_t i = 0; i < kMaxPolyphony; ++i) {
  25. lru_[i] = kMaxPolyphony - i - 1;
  26. }
  27. }
  28. void VoiceAllocator::ClearNotes() {
  29. memset(&pool_, 0, sizeof(pool_));
  30. }
  31. uint8_t VoiceAllocator::NoteOn(uint8_t note) {
  32. if (size_ == 0) {
  33. return 0xff;
  34. }
  35. // First, check if there is a voice currently playing this note. In this case
  36. // This voice will be responsible for retriggering this note.
  37. // Hint: if you're more into string instruments than keyboard instruments,
  38. // you can safely comment those lines.
  39. uint8_t voice = Find(note);
  40. // Then, try to find the least recently touched, currently inactive voice.
  41. if (voice == 0xff) {
  42. for (uint8_t i = 0; i < kMaxPolyphony; ++i) {
  43. if (lru_[i] < size_ && !(pool_[lru_[i]] & 0x80)) {
  44. voice = lru_[i];
  45. }
  46. }
  47. }
  48. // If all voices are active, use the least recently played note.
  49. if (voice == 0xff) {
  50. for (uint8_t i = 0; i < kMaxPolyphony; ++i) {
  51. if (lru_[i] < size_) {
  52. voice = lru_[i];
  53. }
  54. }
  55. }
  56. pool_[voice] = 0x80 | note;
  57. Touch(voice);
  58. return voice;
  59. }
  60. uint8_t VoiceAllocator::Find(uint8_t note) const {
  61. for (uint8_t i = 0; i < size_; ++i) {
  62. if ((pool_[i] & 0x7f) == note) {
  63. return i;
  64. }
  65. }
  66. return 0xff;
  67. }
  68. uint8_t VoiceAllocator::NoteOff(uint8_t note) {
  69. uint8_t voice = Find(note);
  70. if (voice != 0xff) {
  71. pool_[voice] &= 0x7f;
  72. Touch(voice);
  73. }
  74. return voice;
  75. }
  76. void VoiceAllocator::Touch(uint8_t voice) {
  77. int8_t source = kMaxPolyphony - 1;
  78. int8_t destination = kMaxPolyphony - 1;
  79. while (source >= 0) {
  80. if (lru_[source] != voice) {
  81. lru_[destination--] = lru_[source];
  82. }
  83. --source;
  84. }
  85. lru_[0] = voice;
  86. }
  87. } // namespace edges