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.

142 lines
4.0KB

  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 bytestream midi to other formats.
  22. @tags{Audio}
  23. */
  24. struct BytestreamInputHandler
  25. {
  26. virtual ~BytestreamInputHandler() noexcept = default;
  27. virtual void reset() = 0;
  28. virtual void pushMidiData (const void* data, int bytes, double time) = 0;
  29. };
  30. /**
  31. Parses a continuous bytestream and emits complete MidiMessages whenever a full
  32. message is received.
  33. @tags{Audio}
  34. */
  35. struct BytestreamToBytestreamHandler : public BytestreamInputHandler
  36. {
  37. BytestreamToBytestreamHandler (MidiInput& i, MidiInputCallback& c)
  38. : input (i), callback (c), concatenator (2048) {}
  39. /**
  40. Provides an `operator()` which can create an input handler for a given
  41. MidiInput.
  42. All handler classes should have a similar Factory to facilitate
  43. creation of handlers in generic contexts.
  44. */
  45. class Factory
  46. {
  47. public:
  48. explicit Factory (MidiInputCallback* c)
  49. : callback (c) {}
  50. std::unique_ptr<BytestreamToBytestreamHandler> operator() (MidiInput& i) const
  51. {
  52. if (callback != nullptr)
  53. return std::make_unique<BytestreamToBytestreamHandler> (i, *callback);
  54. jassertfalse;
  55. return {};
  56. }
  57. private:
  58. MidiInputCallback* callback = nullptr;
  59. };
  60. void reset() override { concatenator.reset(); }
  61. void pushMidiData (const void* data, int bytes, double time) override
  62. {
  63. concatenator.pushMidiData (data, bytes, time, &input, callback);
  64. }
  65. MidiInput& input;
  66. MidiInputCallback& callback;
  67. MidiDataConcatenator concatenator;
  68. };
  69. /**
  70. Parses a continuous MIDI 1.0 bytestream, and emits full messages in the requested
  71. UMP format.
  72. @tags{Audio}
  73. */
  74. struct BytestreamToUMPHandler : public BytestreamInputHandler
  75. {
  76. BytestreamToUMPHandler (PacketProtocol protocol, Receiver& c)
  77. : recipient (c), dispatcher (protocol, 2048) {}
  78. /**
  79. Provides an `operator()` which can create an input handler for a given
  80. MidiInput.
  81. All handler classes should have a similar Factory to facilitate
  82. creation of handlers in generic contexts.
  83. */
  84. class Factory
  85. {
  86. public:
  87. Factory (PacketProtocol p, Receiver& c)
  88. : protocol (p), callback (c) {}
  89. std::unique_ptr<BytestreamToUMPHandler> operator() (MidiInput&) const
  90. {
  91. return std::make_unique<BytestreamToUMPHandler> (protocol, callback);
  92. }
  93. private:
  94. PacketProtocol protocol;
  95. Receiver& callback;
  96. };
  97. void reset() override { dispatcher.reset(); }
  98. void pushMidiData (const void* data, int bytes, double time) override
  99. {
  100. const auto* ptr = static_cast<const uint8_t*> (data);
  101. dispatcher.dispatch (ptr, ptr + bytes, time, [&] (const View& v)
  102. {
  103. recipient.packetReceived (v, time);
  104. });
  105. }
  106. Receiver& recipient;
  107. BytestreamToUMPDispatcher dispatcher;
  108. };
  109. } // juce::universal_midi_packets
  110. #endif