VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_basics / midi / juce_MidiDataConcatenator.h
blobf58637186410cd2911ab58d0d5d669915d1b8ca8
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 namespace juce
26 //==============================================================================
27 /**
28 Helper class that takes chunks of incoming midi bytes, packages them into
29 messages, and dispatches them to a midi callback.
31 @tags{Audio}
33 class MidiDataConcatenator
35 public:
36 MidiDataConcatenator (int initialBufferSize)
37 : pendingSysexData ((size_t) initialBufferSize)
41 void reset()
43 currentMessageLen = 0;
44 pendingSysexSize = 0;
45 pendingSysexTime = 0;
48 template <typename UserDataType, typename CallbackType>
49 void pushMidiData (const void* inputData, int numBytes, double time,
50 UserDataType* input, CallbackType& callback)
52 auto d = static_cast<const uint8*> (inputData);
54 while (numBytes > 0)
56 auto nextByte = *d;
58 if (pendingSysexSize != 0 || nextByte == 0xf0)
60 processSysex (d, numBytes, time, input, callback);
61 currentMessageLen = 0;
62 continue;
65 ++d;
66 --numBytes;
68 if (isRealtimeMessage (nextByte))
70 callback.handleIncomingMidiMessage (input, MidiMessage (nextByte, time));
71 // These can be embedded in the middle of a normal message, so we won't
72 // reset the currentMessageLen here.
73 continue;
76 if (isInitialByte (nextByte))
78 currentMessage[0] = nextByte;
79 currentMessageLen = 1;
81 else if (currentMessageLen > 0 && currentMessageLen < 3)
83 currentMessage[currentMessageLen++] = nextByte;
85 else
87 // message is too long or invalid MIDI - abandon it and start again with the next byte
88 currentMessageLen = 0;
89 continue;
92 auto expectedLength = MidiMessage::getMessageLengthFromFirstByte (currentMessage[0]);
94 if (expectedLength == currentMessageLen)
96 callback.handleIncomingMidiMessage (input, MidiMessage (currentMessage, expectedLength, time));
97 currentMessageLen = 1; // reset, but leave the first byte to use as the running status byte
102 private:
103 template <typename UserDataType, typename CallbackType>
104 void processSysex (const uint8*& d, int& numBytes, double time,
105 UserDataType* input, CallbackType& callback)
107 if (*d == 0xf0)
109 pendingSysexSize = 0;
110 pendingSysexTime = time;
113 pendingSysexData.ensureSize ((size_t) (pendingSysexSize + numBytes), false);
114 auto totalMessage = static_cast<uint8*> (pendingSysexData.getData());
115 auto dest = totalMessage + pendingSysexSize;
119 if (pendingSysexSize > 0 && isStatusByte (*d))
121 if (*d == 0xf7)
123 *dest++ = *d++;
124 ++pendingSysexSize;
125 --numBytes;
126 break;
129 if (*d >= 0xfa || *d == 0xf8)
131 callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
132 ++d;
133 --numBytes;
135 else
137 pendingSysexSize = 0;
138 int used = 0;
139 const MidiMessage m (d, numBytes, used, 0, time);
141 if (used > 0)
143 callback.handleIncomingMidiMessage (input, m);
144 numBytes -= used;
145 d += used;
148 break;
151 else
153 *dest++ = *d++;
154 ++pendingSysexSize;
155 --numBytes;
158 while (numBytes > 0);
160 if (pendingSysexSize > 0)
162 if (totalMessage [pendingSysexSize - 1] == 0xf7)
164 callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingSysexSize, pendingSysexTime));
165 pendingSysexSize = 0;
167 else
169 callback.handlePartialSysexMessage (input, totalMessage, pendingSysexSize, pendingSysexTime);
174 static bool isRealtimeMessage (uint8 byte) { return byte >= 0xf8 && byte <= 0xfe; }
175 static bool isStatusByte (uint8 byte) { return byte >= 0x80; }
176 static bool isInitialByte (uint8 byte) { return isStatusByte (byte) && byte != 0xf7; }
178 uint8 currentMessage[3];
179 int currentMessageLen = 0;
181 MemoryBlock pendingSysexData;
182 double pendingSysexTime = 0;
183 int pendingSysexSize = 0;
185 JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
188 } // namespace juce