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.

114 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. //==============================================================================
  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 MidiKeyboardState::Listener or a MidiInputCallback
  24. so it can easily use a midi input or keyboard component as its source.
  25. @see MidiMessage, MidiInput
  26. @tags{Audio}
  27. */
  28. class JUCE_API MidiMessageCollector : public MidiKeyboardState::Listener,
  29. public MidiInputCallback
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a MidiMessageCollector. */
  34. MidiMessageCollector();
  35. /** Destructor. */
  36. ~MidiMessageCollector() override;
  37. //==============================================================================
  38. /** Clears any messages from the queue.
  39. You need to call this method before starting to use the collector, so that
  40. it knows the correct sample rate to use.
  41. */
  42. void reset (double sampleRate);
  43. /** Takes an incoming real-time message and adds it to the queue.
  44. The message's timestamp is taken, and it will be ready for retrieval as part
  45. of the block returned by the next call to removeNextBlockOfMessages().
  46. This method is fully thread-safe when overlapping calls are made with
  47. removeNextBlockOfMessages().
  48. */
  49. void addMessageToQueue (const MidiMessage& message);
  50. /** Removes all the pending messages from the queue as a buffer.
  51. This will also correct the messages' timestamps to make sure they're in
  52. the range 0 to numSamples - 1.
  53. This call should be made regularly by something like an audio processing
  54. callback, because the time that it happens is used in calculating the
  55. midi event positions.
  56. This method is fully thread-safe when overlapping calls are made with
  57. addMessageToQueue().
  58. Precondition: numSamples must be greater than 0.
  59. */
  60. void removeNextBlockOfMessages (MidiBuffer& destBuffer, int numSamples);
  61. /** Preallocates storage for collected messages.
  62. This can be called before audio processing begins to ensure that there
  63. is sufficient space for the expected MIDI messages, in order to avoid
  64. allocations within the audio callback.
  65. */
  66. void ensureStorageAllocated (size_t bytes);
  67. //==============================================================================
  68. /** @internal */
  69. void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override;
  70. /** @internal */
  71. void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override;
  72. /** @internal */
  73. void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override;
  74. private:
  75. //==============================================================================
  76. double lastCallbackTime = 0;
  77. CriticalSection midiCallbackLock;
  78. MidiBuffer incomingMessages;
  79. double sampleRate = 44100.0;
  80. #if JUCE_DEBUG
  81. bool hasCalledReset = false;
  82. #endif
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiMessageCollector)
  84. };
  85. } // namespace juce