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
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
20 ==============================================================================
27 namespace universal_midi_packets
31 A base class for classes which convert bytestream midi to other formats.
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;
44 Parses a continuous bytestream and emits complete MidiMessages whenever a full
49 struct BytestreamToBytestreamHandler
: public BytestreamInputHandler
51 BytestreamToBytestreamHandler (MidiInput
& i
, MidiInputCallback
& c
)
52 : input (i
), callback (c
), concatenator (2048) {}
55 Provides an `operator()` which can create an input handler for a given
58 All handler classes should have a similar Factory to facilitate
59 creation of handlers in generic contexts.
64 explicit Factory (MidiInputCallback
* c
)
67 std::unique_ptr
<BytestreamToBytestreamHandler
> operator() (MidiInput
& i
) const
69 if (callback
!= nullptr)
70 return std::make_unique
<BytestreamToBytestreamHandler
> (i
, *callback
);
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
);
88 MidiInputCallback
& callback
;
89 MidiDataConcatenator concatenator
;
93 Parses a continuous MIDI 1.0 bytestream, and emits full messages in the requested
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
107 All handler classes should have a similar Factory to facilitate
108 creation of handlers in generic contexts.
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
);
122 PacketProtocol protocol
;
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
);
138 BytestreamToUMPDispatcher dispatcher
;