The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

105 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_MIDIMESSAGECOLLECTOR_H_INCLUDED
  18. #define JUCE_MIDIMESSAGECOLLECTOR_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Collects incoming realtime MIDI messages and turns them into blocks suitable for
  22. processing by a block-based audio callback.
  23. The class can also be used as either a MidiKeyboardStateListener or a MidiInputCallback
  24. so it can easily use a midi input or keyboard component as its source.
  25. @see MidiMessage, MidiInput
  26. */
  27. class JUCE_API MidiMessageCollector : public MidiKeyboardStateListener,
  28. public MidiInputCallback
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a MidiMessageCollector. */
  33. MidiMessageCollector();
  34. /** Destructor. */
  35. ~MidiMessageCollector();
  36. //==============================================================================
  37. /** Clears any messages from the queue.
  38. You need to call this method before starting to use the collector, so that
  39. it knows the correct sample rate to use.
  40. */
  41. void reset (double sampleRate);
  42. /** Takes an incoming real-time message and adds it to the queue.
  43. The message's timestamp is taken, and it will be ready for retrieval as part
  44. of the block returned by the next call to removeNextBlockOfMessages().
  45. This method is fully thread-safe when overlapping calls are made with
  46. removeNextBlockOfMessages().
  47. */
  48. void addMessageToQueue (const MidiMessage& message);
  49. /** Removes all the pending messages from the queue as a buffer.
  50. This will also correct the messages' timestamps to make sure they're in
  51. the range 0 to numSamples - 1.
  52. This call should be made regularly by something like an audio processing
  53. callback, because the time that it happens is used in calculating the
  54. midi event positions.
  55. This method is fully thread-safe when overlapping calls are made with
  56. addMessageToQueue().
  57. Precondition: numSamples must be greater than 0.
  58. */
  59. void removeNextBlockOfMessages (MidiBuffer& destBuffer, int numSamples);
  60. //==============================================================================
  61. /** @internal */
  62. void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override;
  63. /** @internal */
  64. void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber) override;
  65. /** @internal */
  66. void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override;
  67. private:
  68. //==============================================================================
  69. double lastCallbackTime;
  70. CriticalSection midiCallbackLock;
  71. MidiBuffer incomingMessages;
  72. double sampleRate;
  73. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiMessageCollector)
  74. };
  75. #endif // JUCE_MIDIMESSAGECOLLECTOR_H_INCLUDED