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.

154 lines
5.2KB

  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. // Class monitoring a sequence of notes on the MIDI input and automatically
  28. // chosing the best layout
  29. #include "yarns/layout_configurator.h"
  30. #include "yarns/multi.h"
  31. #include <algorithm>
  32. namespace yarns {
  33. using namespace std;
  34. void LayoutConfigurator::StopLearning(Multi* multi) {
  35. learning_ = false;
  36. bool used_channels[16];
  37. uint8_t num_used_channels = 0;
  38. uint8_t num_extra_drum_channels = 0;
  39. fill(&used_channels[0], &used_channels[16], 0);
  40. for (uint8_t i = 0; i < num_notes_; ++i) {
  41. uint8_t channel = recorded_notes_[i].channel;
  42. if (!used_channels[channel]) {
  43. ++num_used_channels;
  44. } else {
  45. if (channel == 9) {
  46. ++num_extra_drum_channels;
  47. }
  48. }
  49. used_channels[channel] = true;
  50. }
  51. if (!num_used_channels) {
  52. return;
  53. }
  54. // Count the number of splits
  55. uint8_t num_splits = 0;
  56. for (uint8_t i = 0; i < num_notes_; ++i) {
  57. for (uint8_t j = i + 1; j < num_notes_; ++j) {
  58. if (recorded_notes_[i].channel != 9 &&
  59. recorded_notes_[i].channel == recorded_notes_[j].channel && (
  60. (recorded_notes_[i].note == recorded_notes_[j].note + 1) ||
  61. (recorded_notes_[i].note + 1 == recorded_notes_[j].note))) {
  62. Split s;
  63. s.channel = recorded_notes_[i].channel;
  64. s.split_point = min(recorded_notes_[i].note, recorded_notes_[j].note);
  65. splits_[num_splits++] = s;
  66. }
  67. }
  68. }
  69. sort(&splits_[0], &splits_[num_splits], SplitLess());
  70. uint8_t num_parts = num_used_channels + num_splits;
  71. if (num_parts == 1) {
  72. uint8_t channel = recorded_notes_[0].channel;
  73. if (channel == 9) {
  74. multi->Set(MULTI_LAYOUT, LAYOUT_QUAD_TRIGGERS);
  75. for (uint8_t i = 0; i < num_notes_ && i < kNumParts; ++i) {
  76. uint8_t drum_note = recorded_notes_[i].note;
  77. Part* instrument = multi->mutable_part(i);
  78. instrument->Set(PART_MIDI_MIN_NOTE, drum_note);
  79. instrument->Set(PART_MIDI_MAX_NOTE, drum_note);
  80. instrument->Set(PART_MIDI_CHANNEL, channel);
  81. }
  82. } else {
  83. if (num_notes_ > 2) {
  84. multi->Set(MULTI_LAYOUT, LAYOUT_QUAD_POLY);
  85. } else if (num_notes_ > 1) {
  86. multi->Set(MULTI_LAYOUT, LAYOUT_DUAL_POLY);
  87. } else {
  88. multi->Set(MULTI_LAYOUT, LAYOUT_MONO);
  89. }
  90. Part* p = multi->mutable_part(0);
  91. p->Set(PART_MIDI_CHANNEL, channel);
  92. p->Set(PART_MIDI_MIN_NOTE, 0);
  93. p->Set(PART_MIDI_MAX_NOTE, 127);
  94. }
  95. } else {
  96. num_parts += num_extra_drum_channels;
  97. if (num_parts == 2) {
  98. multi->Set(MULTI_LAYOUT, LAYOUT_DUAL_MONO);
  99. } else {
  100. multi->Set(MULTI_LAYOUT, LAYOUT_QUAD_MONO);
  101. }
  102. uint8_t part_index = 0;
  103. for (uint8_t i = 0; i < num_notes_; ++i) {
  104. uint8_t channel = recorded_notes_[i].channel;
  105. if (used_channels[channel]) {
  106. Part* part = multi->mutable_part(part_index);
  107. part->Set(PART_MIDI_CHANNEL, channel);
  108. if (channel == 9) {
  109. part->Set(PART_MIDI_MIN_NOTE, recorded_notes_[i].note);
  110. part->Set(PART_MIDI_MAX_NOTE, recorded_notes_[i].note);
  111. used_channels[channel] = true;
  112. } else {
  113. part->Set(PART_MIDI_MIN_NOTE, 0);
  114. part->Set(PART_MIDI_MAX_NOTE, 127);
  115. used_channels[channel] = false; // Mark the channel as processed.
  116. }
  117. // Register splits.
  118. for (uint8_t j = 0; j < num_splits; ++j) {
  119. const Split& split = splits_[j];
  120. if (split.channel == channel) {
  121. part->Set(PART_MIDI_MAX_NOTE, split.split_point);
  122. part = multi->mutable_part(++part_index);
  123. part->Set(PART_MIDI_CHANNEL, channel);
  124. part->Set(PART_MIDI_MIN_NOTE, split.split_point + 1);
  125. part->Set(PART_MIDI_MAX_NOTE, 127);
  126. }
  127. }
  128. ++part_index;
  129. }
  130. }
  131. if (part_index == 3) {
  132. multi->mutable_part(3)->Set(PART_MIDI_MIN_NOTE, 0);
  133. multi->mutable_part(3)->Set(PART_MIDI_MAX_NOTE, 0);
  134. }
  135. }
  136. }
  137. } // namespace yarns