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.

179 lines
4.7KB

  1. // Copyright 2012 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. #ifndef EDGES_MIDI_HANDLER_H_
  18. #define EDGES_MIDI_HANDLER_H_
  19. #include "avrlibx/avrlibx.h"
  20. #include "edges/hardware_config.h"
  21. #include "edges/midi.h"
  22. #include "edges/note_stack.h"
  23. #include "edges/settings.h"
  24. #include "edges/voice_allocator.h"
  25. namespace edges {
  26. enum MidiMode {
  27. MIDI_MODE_MULTITIMBRAL,
  28. MIDI_MODE_POLYPHONIC
  29. };
  30. class MidiHandler : public midi::MidiDevice {
  31. public:
  32. MidiHandler() { }
  33. static inline void Init() {
  34. for (uint8_t channel = 0; channel < kNumChannels; ++channel) {
  35. stack_[channel].Init();
  36. }
  37. allocator_.Init();
  38. allocator_.set_size(4);
  39. learning_ = false;
  40. memset(gate_, false, sizeof(gate_));
  41. memset(pitch_, -1, sizeof(pitch_));
  42. }
  43. static void ToggleMidiMode() {
  44. settings.ToggleMidiMode();
  45. Init();
  46. }
  47. static void DisableMidiCoupling() {
  48. Init();
  49. }
  50. static void ResetAllControllers(uint8_t channel) {
  51. channel = (channel - base_channel()) & 0xf;
  52. pitch_bend_[channel] = 0;
  53. }
  54. static inline void NoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
  55. channel = (channel - base_channel()) & 0xf;
  56. if (velocity == 0) {
  57. NoteOff(channel, note, 0);
  58. }
  59. if (midi_mode() == MIDI_MODE_MULTITIMBRAL) {
  60. stack_[channel].NoteOn(note, velocity);
  61. pitch_[channel] = stack_[channel].most_recent_note().note << 7;
  62. } else {
  63. channel = allocator_.NoteOn(note);
  64. pitch_[channel] = note << 7;
  65. }
  66. gate_[channel] = true;
  67. }
  68. static inline void NoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
  69. channel = (channel - base_channel()) & 0xf;
  70. if (midi_mode() == MIDI_MODE_MULTITIMBRAL) {
  71. stack_[channel].NoteOff(note);
  72. if (stack_[channel].size()) {
  73. pitch_[channel] = stack_[channel].most_recent_note().note << 7;
  74. }
  75. gate_[channel] = stack_[channel].size() != 0;
  76. } else {
  77. channel = allocator_.NoteOff(note);
  78. if (channel != 0xff) {
  79. gate_[channel] = false;
  80. }
  81. }
  82. }
  83. static inline void PitchBend(uint8_t channel, uint16_t value) {
  84. channel = (channel - base_channel()) & 0xf;
  85. int16_t v = value;
  86. v -= 8192;
  87. v >>= 5;
  88. if (midi_mode() == MIDI_MODE_MULTITIMBRAL) {
  89. pitch_bend_[channel] = v;
  90. } else {
  91. for (uint8_t i = 0; i < kNumChannels; ++i) {
  92. pitch_bend_[i] = v;
  93. }
  94. }
  95. }
  96. static inline void RawMidiData(
  97. uint8_t status,
  98. uint8_t* data,
  99. uint8_t data_size,
  100. uint8_t accepted_channel) {
  101. if (learning_ && (status & 0xf0) == 0x90) {
  102. settings.set_midi_channel(status & 0xf);
  103. learning_ = false;
  104. }
  105. }
  106. static uint8_t CheckChannel(uint8_t channel) {
  107. if (midi_mode() == MIDI_MODE_MULTITIMBRAL) {
  108. return ((channel - base_channel()) & 0xf) < kNumChannels;
  109. } else {
  110. return channel == base_channel();
  111. }
  112. }
  113. static void Learn() {
  114. learning_ = true;
  115. }
  116. static inline bool learning() { return learning_; }
  117. static inline bool gate(uint8_t channel) { return gate_[channel]; }
  118. static int16_t shift_pitch(uint8_t channel, int16_t pitch) {
  119. if (pitch_[channel] == -1) {
  120. return pitch;
  121. } else {
  122. pitch -= (60 << 7);
  123. pitch += pitch_[channel];
  124. pitch += pitch_bend_[channel];
  125. if (pitch < 0) {
  126. pitch = 0;
  127. } else if (pitch > 16383) {
  128. pitch = 16383;
  129. }
  130. return pitch;
  131. }
  132. return pitch;
  133. }
  134. static inline uint8_t base_channel() {
  135. return settings.midi_channel();
  136. }
  137. static inline MidiMode midi_mode() {
  138. return static_cast<MidiMode>(settings.midi_mode());
  139. }
  140. private:
  141. static bool learning_;
  142. static bool gate_[kNumChannels];
  143. static int16_t pitch_[kNumChannels];
  144. static int16_t pitch_bend_[kNumChannels];
  145. static NoteStack<10> stack_[kNumChannels];
  146. static VoiceAllocator allocator_;
  147. DISALLOW_COPY_AND_ASSIGN(MidiHandler);
  148. };
  149. extern MidiHandler midi_handler;
  150. } // namespace edges
  151. #endif // EDGES_MIDI_HANDLER_H_