VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_devices / midi_io / ump / juce_UMPBytestreamInputHandler.h
blob1cdad80c68f4c2199dd2e6d1ceab520a3bb07e11
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 #ifndef DOXYGEN
25 namespace juce
27 namespace universal_midi_packets
30 /**
31 A base class for classes which convert bytestream midi to other formats.
33 @tags{Audio}
35 struct BytestreamInputHandler
37 virtual ~BytestreamInputHandler() noexcept = default;
39 virtual void reset() = 0;
40 virtual void pushMidiData (const void* data, int bytes, double time) = 0;
43 /**
44 Parses a continuous bytestream and emits complete MidiMessages whenever a full
45 message is received.
47 @tags{Audio}
49 struct BytestreamToBytestreamHandler : public BytestreamInputHandler
51 BytestreamToBytestreamHandler (MidiInput& i, MidiInputCallback& c)
52 : input (i), callback (c), concatenator (2048) {}
54 /**
55 Provides an `operator()` which can create an input handler for a given
56 MidiInput.
58 All handler classes should have a similar Factory to facilitate
59 creation of handlers in generic contexts.
61 class Factory
63 public:
64 explicit Factory (MidiInputCallback* c)
65 : callback (c) {}
67 std::unique_ptr<BytestreamToBytestreamHandler> operator() (MidiInput& i) const
69 if (callback != nullptr)
70 return std::make_unique<BytestreamToBytestreamHandler> (i, *callback);
72 jassertfalse;
73 return {};
76 private:
77 MidiInputCallback* callback = nullptr;
80 void reset() override { concatenator.reset(); }
82 void pushMidiData (const void* data, int bytes, double time) override
84 concatenator.pushMidiData (data, bytes, time, &input, callback);
87 MidiInput& input;
88 MidiInputCallback& callback;
89 MidiDataConcatenator concatenator;
92 /**
93 Parses a continuous MIDI 1.0 bytestream, and emits full messages in the requested
94 UMP format.
96 @tags{Audio}
98 struct BytestreamToUMPHandler : public BytestreamInputHandler
100 BytestreamToUMPHandler (PacketProtocol protocol, Receiver& c)
101 : recipient (c), dispatcher (protocol, 2048) {}
104 Provides an `operator()` which can create an input handler for a given
105 MidiInput.
107 All handler classes should have a similar Factory to facilitate
108 creation of handlers in generic contexts.
110 class Factory
112 public:
113 Factory (PacketProtocol p, Receiver& c)
114 : protocol (p), callback (c) {}
116 std::unique_ptr<BytestreamToUMPHandler> operator() (MidiInput&) const
118 return std::make_unique<BytestreamToUMPHandler> (protocol, callback);
121 private:
122 PacketProtocol protocol;
123 Receiver& callback;
126 void reset() override { dispatcher.reset(); }
128 void pushMidiData (const void* data, int bytes, double time) override
130 const auto* ptr = static_cast<const uint8_t*> (data);
131 dispatcher.dispatch (ptr, ptr + bytes, time, [&] (const View& v)
133 recipient.packetReceived (v, time);
137 Receiver& recipient;
138 BytestreamToUMPDispatcher dispatcher;
144 #endif