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.

106 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MIDIMESSAGECOLLECTOR_JUCEHEADER__
  19. #define __JUCE_MIDIMESSAGECOLLECTOR_JUCEHEADER__
  20. #include "juce_MidiInput.h"
  21. //==============================================================================
  22. /**
  23. Collects incoming realtime MIDI messages and turns them into blocks suitable for
  24. processing by a block-based audio callback.
  25. The class can also be used as either a MidiKeyboardStateListener or a MidiInputCallback
  26. so it can easily use a midi input or keyboard component as its source.
  27. @see MidiMessage, MidiInput
  28. */
  29. class JUCE_API MidiMessageCollector : public MidiKeyboardStateListener,
  30. public MidiInputCallback
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a MidiMessageCollector. */
  35. MidiMessageCollector();
  36. /** Destructor. */
  37. ~MidiMessageCollector();
  38. //==============================================================================
  39. /** Clears any messages from the queue.
  40. You need to call this method before starting to use the collector, so that
  41. it knows the correct sample rate to use.
  42. */
  43. void reset (double sampleRate);
  44. /** Takes an incoming real-time message and adds it to the queue.
  45. The message's timestamp is taken, and it will be ready for retrieval as part
  46. of the block returned by the next call to removeNextBlockOfMessages().
  47. This method is fully thread-safe when overlapping calls are made with
  48. removeNextBlockOfMessages().
  49. */
  50. void addMessageToQueue (const MidiMessage& message);
  51. /** Removes all the pending messages from the queue as a buffer.
  52. This will also correct the messages' timestamps to make sure they're in
  53. the range 0 to numSamples - 1.
  54. This call should be made regularly by something like an audio processing
  55. callback, because the time that it happens is used in calculating the
  56. midi event positions.
  57. This method is fully thread-safe when overlapping calls are made with
  58. addMessageToQueue().
  59. */
  60. void removeNextBlockOfMessages (MidiBuffer& destBuffer, int numSamples);
  61. //==============================================================================
  62. /** @internal */
  63. void handleNoteOn (MidiKeyboardState* source, int midiChannel, int midiNoteNumber, float velocity);
  64. /** @internal */
  65. void handleNoteOff (MidiKeyboardState* source, int midiChannel, int midiNoteNumber);
  66. /** @internal */
  67. void handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message);
  68. private:
  69. //==============================================================================
  70. double lastCallbackTime;
  71. CriticalSection midiCallbackLock;
  72. MidiBuffer incomingMessages;
  73. double sampleRate;
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiMessageCollector)
  75. };
  76. #endif // __JUCE_MIDIMESSAGECOLLECTOR_JUCEHEADER__