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.

203 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. 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. } // namespace juce