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.

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