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.

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