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.

135 lines
3.9KB

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