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.

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