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.

145 lines
4.0KB

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