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.

102 lines
3.8KB

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