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.

182 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. MidiKeyboardState::MidiKeyboardState()
  20. {
  21. zerostruct (noteStates);
  22. }
  23. MidiKeyboardState::~MidiKeyboardState()
  24. {
  25. }
  26. //==============================================================================
  27. void MidiKeyboardState::reset()
  28. {
  29. const ScopedLock sl (lock);
  30. zerostruct (noteStates);
  31. eventsToAdd.clear();
  32. }
  33. bool MidiKeyboardState::isNoteOn (const int midiChannel, const int n) const noexcept
  34. {
  35. jassert (midiChannel >= 0 && midiChannel <= 16);
  36. return isPositiveAndBelow (n, 128)
  37. && (noteStates[n] & (1 << (midiChannel - 1))) != 0;
  38. }
  39. bool MidiKeyboardState::isNoteOnForChannels (const int midiChannelMask, const int n) const noexcept
  40. {
  41. return isPositiveAndBelow (n, 128)
  42. && (noteStates[n] & midiChannelMask) != 0;
  43. }
  44. void MidiKeyboardState::noteOn (const int midiChannel, const int midiNoteNumber, const float velocity)
  45. {
  46. jassert (midiChannel >= 0 && midiChannel <= 16);
  47. jassert (isPositiveAndBelow (midiNoteNumber, 128));
  48. const ScopedLock sl (lock);
  49. if (isPositiveAndBelow (midiNoteNumber, 128))
  50. {
  51. const int timeNow = (int) Time::getMillisecondCounter();
  52. eventsToAdd.addEvent (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity), timeNow);
  53. eventsToAdd.clear (0, timeNow - 500);
  54. noteOnInternal (midiChannel, midiNoteNumber, velocity);
  55. }
  56. }
  57. void MidiKeyboardState::noteOnInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
  58. {
  59. if (isPositiveAndBelow (midiNoteNumber, 128))
  60. {
  61. noteStates[midiNoteNumber] = static_cast<uint16> (noteStates[midiNoteNumber] | (1 << (midiChannel - 1)));
  62. for (int i = listeners.size(); --i >= 0;)
  63. listeners.getUnchecked(i)->handleNoteOn (this, midiChannel, midiNoteNumber, velocity);
  64. }
  65. }
  66. void MidiKeyboardState::noteOff (const int midiChannel, const int midiNoteNumber, const float velocity)
  67. {
  68. const ScopedLock sl (lock);
  69. if (isNoteOn (midiChannel, midiNoteNumber))
  70. {
  71. const int timeNow = (int) Time::getMillisecondCounter();
  72. eventsToAdd.addEvent (MidiMessage::noteOff (midiChannel, midiNoteNumber), timeNow);
  73. eventsToAdd.clear (0, timeNow - 500);
  74. noteOffInternal (midiChannel, midiNoteNumber, velocity);
  75. }
  76. }
  77. void MidiKeyboardState::noteOffInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
  78. {
  79. if (isNoteOn (midiChannel, midiNoteNumber))
  80. {
  81. noteStates[midiNoteNumber] = static_cast<uint16> (noteStates[midiNoteNumber] & ~(1 << (midiChannel - 1)));
  82. for (int i = listeners.size(); --i >= 0;)
  83. listeners.getUnchecked(i)->handleNoteOff (this, midiChannel, midiNoteNumber, velocity);
  84. }
  85. }
  86. void MidiKeyboardState::allNotesOff (const int midiChannel)
  87. {
  88. const ScopedLock sl (lock);
  89. if (midiChannel <= 0)
  90. {
  91. for (int i = 1; i <= 16; ++i)
  92. allNotesOff (i);
  93. }
  94. else
  95. {
  96. for (int i = 0; i < 128; ++i)
  97. noteOff (midiChannel, i, 0.0f);
  98. }
  99. }
  100. void MidiKeyboardState::processNextMidiEvent (const MidiMessage& message)
  101. {
  102. if (message.isNoteOn())
  103. {
  104. noteOnInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
  105. }
  106. else if (message.isNoteOff())
  107. {
  108. noteOffInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
  109. }
  110. else if (message.isAllNotesOff())
  111. {
  112. for (int i = 0; i < 128; ++i)
  113. noteOffInternal (message.getChannel(), i, 0.0f);
  114. }
  115. }
  116. void MidiKeyboardState::processNextMidiBuffer (MidiBuffer& buffer,
  117. const int startSample,
  118. const int numSamples,
  119. const bool injectIndirectEvents)
  120. {
  121. const ScopedLock sl (lock);
  122. for (const auto metadata : buffer)
  123. processNextMidiEvent (metadata.getMessage());
  124. if (injectIndirectEvents)
  125. {
  126. const int firstEventToAdd = eventsToAdd.getFirstEventTime();
  127. const double scaleFactor = numSamples / (double) (eventsToAdd.getLastEventTime() + 1 - firstEventToAdd);
  128. for (const auto metadata : eventsToAdd)
  129. {
  130. const auto pos = jlimit (0, numSamples - 1, roundToInt ((metadata.samplePosition - firstEventToAdd) * scaleFactor));
  131. buffer.addEvent (metadata.getMessage(), startSample + pos);
  132. }
  133. }
  134. eventsToAdd.clear();
  135. }
  136. //==============================================================================
  137. void MidiKeyboardState::addListener (MidiKeyboardStateListener* const listener)
  138. {
  139. const ScopedLock sl (lock);
  140. listeners.addIfNotAlreadyThere (listener);
  141. }
  142. void MidiKeyboardState::removeListener (MidiKeyboardStateListener* const listener)
  143. {
  144. const ScopedLock sl (lock);
  145. listeners.removeFirstMatchingValue (listener);
  146. }
  147. } // namespace juce