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.

154 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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. #ifndef DOXYGEN
  18. namespace juce::universal_midi_packets
  19. {
  20. /**
  21. A base class for classes which convert Universal MIDI Packets to other
  22. formats.
  23. @tags{Audio}
  24. */
  25. struct U32InputHandler
  26. {
  27. virtual ~U32InputHandler() noexcept = default;
  28. virtual void reset() = 0;
  29. virtual void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) = 0;
  30. };
  31. /**
  32. Parses a continuous stream of U32 words and emits complete MidiMessages whenever a full
  33. message is received.
  34. @tags{Audio}
  35. */
  36. struct U32ToBytestreamHandler : public U32InputHandler
  37. {
  38. U32ToBytestreamHandler (MidiInput& i, MidiInputCallback& c)
  39. : input (i), callback (c), dispatcher (2048) {}
  40. /**
  41. Provides an `operator()` which can create an input handler for a given
  42. MidiInput.
  43. All handler classes should have a similar Factory to facilitate
  44. creation of handlers in generic contexts.
  45. */
  46. class Factory
  47. {
  48. public:
  49. explicit Factory (MidiInputCallback* c)
  50. : callback (c) {}
  51. std::unique_ptr<U32ToBytestreamHandler> operator() (MidiInput& i) const
  52. {
  53. if (callback != nullptr)
  54. return std::make_unique<U32ToBytestreamHandler> (i, *callback);
  55. jassertfalse;
  56. return {};
  57. }
  58. private:
  59. MidiInputCallback* callback = nullptr;
  60. };
  61. void reset() override { dispatcher.reset(); }
  62. void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) override
  63. {
  64. dispatcher.dispatch (begin, end, time, [this] (const BytestreamMidiView& m)
  65. {
  66. callback.handleIncomingMidiMessage (&input, m.getMessage());
  67. });
  68. }
  69. MidiInput& input;
  70. MidiInputCallback& callback;
  71. ToBytestreamDispatcher dispatcher;
  72. };
  73. /**
  74. Parses a continuous stream of U32 words and emits full messages in the requested
  75. UMP format.
  76. @tags{Audio}
  77. */
  78. struct U32ToUMPHandler : public U32InputHandler
  79. {
  80. U32ToUMPHandler (PacketProtocol protocol, Receiver& c)
  81. : recipient (c), converter (protocol) {}
  82. /**
  83. Provides an `operator()` which can create an input handler for a given
  84. MidiInput.
  85. All handler classes should have a similar Factory to facilitate
  86. creation of handlers in generic contexts.
  87. */
  88. class Factory
  89. {
  90. public:
  91. Factory (PacketProtocol p, Receiver& c)
  92. : protocol (p), callback (c) {}
  93. std::unique_ptr<U32ToUMPHandler> operator() (MidiInput&) const
  94. {
  95. return std::make_unique<U32ToUMPHandler> (protocol, callback);
  96. }
  97. private:
  98. PacketProtocol protocol;
  99. Receiver& callback;
  100. };
  101. void reset() override
  102. {
  103. dispatcher.reset();
  104. converter.reset();
  105. }
  106. void pushMidiData (const uint32_t* begin, const uint32_t* end, double time) override
  107. {
  108. dispatcher.dispatch (begin, end, time, [this] (const View& view, double thisTime)
  109. {
  110. converter.convert (view, [&] (const View& converted)
  111. {
  112. recipient.packetReceived (converted, thisTime);
  113. });
  114. });
  115. }
  116. Receiver& recipient;
  117. Dispatcher dispatcher;
  118. GenericUMPConverter converter;
  119. };
  120. } // namespace juce::universal_midi_packets
  121. #endif