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.

206 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIKEYBOARDSTATE_H_INCLUDED
  18. #define JUCE_MIDIKEYBOARDSTATE_H_INCLUDED
  19. class MidiKeyboardState;
  20. //==============================================================================
  21. /**
  22. Receives events from a MidiKeyboardState object.
  23. @see MidiKeyboardState
  24. */
  25. class JUCE_API MidiKeyboardStateListener
  26. {
  27. public:
  28. //==============================================================================
  29. MidiKeyboardStateListener() noexcept {}
  30. virtual ~MidiKeyboardStateListener() {}
  31. //==============================================================================
  32. /** Called when one of the MidiKeyboardState's keys is pressed.
  33. This will be called synchronously when the state is either processing a
  34. buffer in its MidiKeyboardState::processNextMidiBuffer() method, or
  35. when a note is being played with its MidiKeyboardState::noteOn() method.
  36. Note that this callback could happen from an audio callback thread, so be
  37. careful not to block, and avoid any UI activity in the callback.
  38. */
  39. virtual void handleNoteOn (MidiKeyboardState* source,
  40. int midiChannel, int midiNoteNumber, float velocity) = 0;
  41. /** Called when one of the MidiKeyboardState's keys is released.
  42. This will be called synchronously when the state is either processing a
  43. buffer in its MidiKeyboardState::processNextMidiBuffer() method, or
  44. when a note is being played with its MidiKeyboardState::noteOff() method.
  45. Note that this callback could happen from an audio callback thread, so be
  46. careful not to block, and avoid any UI activity in the callback.
  47. */
  48. virtual void handleNoteOff (MidiKeyboardState* source,
  49. int midiChannel, int midiNoteNumber, float velocity) = 0;
  50. };
  51. //==============================================================================
  52. /**
  53. Represents a piano keyboard, keeping track of which keys are currently pressed.
  54. This object can parse a stream of midi events, using them to update its idea
  55. of which keys are pressed for each individiual midi channel.
  56. When keys go up or down, it can broadcast these events to listener objects.
  57. It also allows key up/down events to be triggered with its noteOn() and noteOff()
  58. methods, and midi messages for these events will be merged into the
  59. midi stream that gets processed by processNextMidiBuffer().
  60. */
  61. class JUCE_API MidiKeyboardState
  62. {
  63. public:
  64. //==============================================================================
  65. MidiKeyboardState();
  66. ~MidiKeyboardState();
  67. //==============================================================================
  68. /** Resets the state of the object.
  69. All internal data for all the channels is reset, but no events are sent as a
  70. result.
  71. If you want to release any keys that are currently down, and to send out note-up
  72. midi messages for this, use the allNotesOff() method instead.
  73. */
  74. void reset();
  75. /** Returns true if the given midi key is currently held down for the given midi channel.
  76. The channel number must be between 1 and 16. If you want to see if any notes are
  77. on for a range of channels, use the isNoteOnForChannels() method.
  78. */
  79. bool isNoteOn (int midiChannel, int midiNoteNumber) const noexcept;
  80. /** Returns true if the given midi key is currently held down on any of a set of midi channels.
  81. The channel mask has a bit set for each midi channel you want to test for - bit
  82. 0 = midi channel 1, bit 1 = midi channel 2, etc.
  83. If a note is on for at least one of the specified channels, this returns true.
  84. */
  85. bool isNoteOnForChannels (int midiChannelMask, int midiNoteNumber) const noexcept;
  86. /** Turns a specified note on.
  87. This will cause a suitable midi note-on event to be injected into the midi buffer during the
  88. next call to processNextMidiBuffer().
  89. It will also trigger a synchronous callback to the listeners to tell them that the key has
  90. gone down.
  91. */
  92. void noteOn (int midiChannel, int midiNoteNumber, float velocity);
  93. /** Turns a specified note off.
  94. This will cause a suitable midi note-off event to be injected into the midi buffer during the
  95. next call to processNextMidiBuffer().
  96. It will also trigger a synchronous callback to the listeners to tell them that the key has
  97. gone up.
  98. But if the note isn't acutally down for the given channel, this method will in fact do nothing.
  99. */
  100. void noteOff (int midiChannel, int midiNoteNumber, float velocity);
  101. /** This will turn off any currently-down notes for the given midi channel.
  102. If you pass 0 for the midi channel, it will in fact turn off all notes on all channels.
  103. Calling this method will make calls to noteOff(), so can trigger synchronous callbacks
  104. and events being added to the midi stream.
  105. */
  106. void allNotesOff (int midiChannel);
  107. //==============================================================================
  108. /** Looks at a key-up/down event and uses it to update the state of this object.
  109. To process a buffer full of midi messages, use the processNextMidiBuffer() method
  110. instead.
  111. */
  112. void processNextMidiEvent (const MidiMessage& message);
  113. /** Scans a midi stream for up/down events and adds its own events to it.
  114. This will look for any up/down events and use them to update the internal state,
  115. synchronously making suitable callbacks to the listeners.
  116. If injectIndirectEvents is true, then midi events to produce the recent noteOn()
  117. and noteOff() calls will be added into the buffer.
  118. Only the section of the buffer whose timestamps are between startSample and
  119. (startSample + numSamples) will be affected, and any events added will be placed
  120. between these times.
  121. If you're going to use this method, you'll need to keep calling it regularly for
  122. it to work satisfactorily.
  123. To process a single midi event at a time, use the processNextMidiEvent() method
  124. instead.
  125. */
  126. void processNextMidiBuffer (MidiBuffer& buffer,
  127. int startSample,
  128. int numSamples,
  129. bool injectIndirectEvents);
  130. //==============================================================================
  131. /** Registers a listener for callbacks when keys go up or down.
  132. @see removeListener
  133. */
  134. void addListener (MidiKeyboardStateListener* listener);
  135. /** Deregisters a listener.
  136. @see addListener
  137. */
  138. void removeListener (MidiKeyboardStateListener* listener);
  139. private:
  140. //==============================================================================
  141. CriticalSection lock;
  142. uint16 noteStates [128];
  143. MidiBuffer eventsToAdd;
  144. Array <MidiKeyboardStateListener*> listeners;
  145. void noteOnInternal (int midiChannel, int midiNoteNumber, float velocity);
  146. void noteOffInternal (int midiChannel, int midiNoteNumber, float velocity);
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiKeyboardState)
  148. };
  149. #endif // JUCE_MIDIKEYBOARDSTATE_H_INCLUDED