Audio plugin host https://kx.studio/carla
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.

190 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MidiKeyboardState::MidiKeyboardState()
  24. {
  25. zerostruct (noteStates);
  26. }
  27. MidiKeyboardState::~MidiKeyboardState()
  28. {
  29. }
  30. //==============================================================================
  31. void MidiKeyboardState::reset()
  32. {
  33. const ScopedLock sl (lock);
  34. zerostruct (noteStates);
  35. eventsToAdd.clear();
  36. }
  37. bool MidiKeyboardState::isNoteOn (const int midiChannel, const int n) const noexcept
  38. {
  39. jassert (midiChannel >= 0 && midiChannel <= 16);
  40. return isPositiveAndBelow (n, (int) 128)
  41. && (noteStates[n] & (1 << (midiChannel - 1))) != 0;
  42. }
  43. bool MidiKeyboardState::isNoteOnForChannels (const int midiChannelMask, const int n) const noexcept
  44. {
  45. return isPositiveAndBelow (n, (int) 128)
  46. && (noteStates[n] & midiChannelMask) != 0;
  47. }
  48. void MidiKeyboardState::noteOn (const int midiChannel, const int midiNoteNumber, const float velocity)
  49. {
  50. jassert (midiChannel >= 0 && midiChannel <= 16);
  51. jassert (isPositiveAndBelow (midiNoteNumber, (int) 128));
  52. const ScopedLock sl (lock);
  53. if (isPositiveAndBelow (midiNoteNumber, (int) 128))
  54. {
  55. const int timeNow = (int) Time::getMillisecondCounter();
  56. eventsToAdd.addEvent (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity), timeNow);
  57. eventsToAdd.clear (0, timeNow - 500);
  58. noteOnInternal (midiChannel, midiNoteNumber, velocity);
  59. }
  60. }
  61. void MidiKeyboardState::noteOnInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
  62. {
  63. if (isPositiveAndBelow (midiNoteNumber, (int) 128))
  64. {
  65. noteStates [midiNoteNumber] |= (1 << (midiChannel - 1));
  66. for (int i = listeners.size(); --i >= 0;)
  67. listeners.getUnchecked(i)->handleNoteOn (this, midiChannel, midiNoteNumber, velocity);
  68. }
  69. }
  70. void MidiKeyboardState::noteOff (const int midiChannel, const int midiNoteNumber, const float velocity)
  71. {
  72. const ScopedLock sl (lock);
  73. if (isNoteOn (midiChannel, midiNoteNumber))
  74. {
  75. const int timeNow = (int) Time::getMillisecondCounter();
  76. eventsToAdd.addEvent (MidiMessage::noteOff (midiChannel, midiNoteNumber), timeNow);
  77. eventsToAdd.clear (0, timeNow - 500);
  78. noteOffInternal (midiChannel, midiNoteNumber, velocity);
  79. }
  80. }
  81. void MidiKeyboardState::noteOffInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
  82. {
  83. if (isNoteOn (midiChannel, midiNoteNumber))
  84. {
  85. noteStates [midiNoteNumber] &= ~(1 << (midiChannel - 1));
  86. for (int i = listeners.size(); --i >= 0;)
  87. listeners.getUnchecked(i)->handleNoteOff (this, midiChannel, midiNoteNumber, velocity);
  88. }
  89. }
  90. void MidiKeyboardState::allNotesOff (const int midiChannel)
  91. {
  92. const ScopedLock sl (lock);
  93. if (midiChannel <= 0)
  94. {
  95. for (int i = 1; i <= 16; ++i)
  96. allNotesOff (i);
  97. }
  98. else
  99. {
  100. for (int i = 0; i < 128; ++i)
  101. noteOff (midiChannel, i, 0.0f);
  102. }
  103. }
  104. void MidiKeyboardState::processNextMidiEvent (const MidiMessage& message)
  105. {
  106. if (message.isNoteOn())
  107. {
  108. noteOnInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
  109. }
  110. else if (message.isNoteOff())
  111. {
  112. noteOffInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
  113. }
  114. else if (message.isAllNotesOff())
  115. {
  116. for (int i = 0; i < 128; ++i)
  117. noteOffInternal (message.getChannel(), i, 0.0f);
  118. }
  119. }
  120. void MidiKeyboardState::processNextMidiBuffer (MidiBuffer& buffer,
  121. const int startSample,
  122. const int numSamples,
  123. const bool injectIndirectEvents)
  124. {
  125. MidiBuffer::Iterator i (buffer);
  126. MidiMessage message;
  127. int time;
  128. const ScopedLock sl (lock);
  129. while (i.getNextEvent (message, time))
  130. processNextMidiEvent (message);
  131. if (injectIndirectEvents)
  132. {
  133. MidiBuffer::Iterator i2 (eventsToAdd);
  134. const int firstEventToAdd = eventsToAdd.getFirstEventTime();
  135. const double scaleFactor = numSamples / (double) (eventsToAdd.getLastEventTime() + 1 - firstEventToAdd);
  136. while (i2.getNextEvent (message, time))
  137. {
  138. const int pos = jlimit (0, numSamples - 1, roundToInt ((time - firstEventToAdd) * scaleFactor));
  139. buffer.addEvent (message, startSample + pos);
  140. }
  141. }
  142. eventsToAdd.clear();
  143. }
  144. //==============================================================================
  145. void MidiKeyboardState::addListener (MidiKeyboardStateListener* const listener)
  146. {
  147. const ScopedLock sl (lock);
  148. listeners.addIfNotAlreadyThere (listener);
  149. }
  150. void MidiKeyboardState::removeListener (MidiKeyboardStateListener* const listener)
  151. {
  152. const ScopedLock sl (lock);
  153. listeners.removeFirstMatchingValue (listener);
  154. }