JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_VSTMidiEventList.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26// NB: this must come first, *before* the header-guard.
27#ifdef JUCE_VSTINTERFACE_H_INCLUDED
28
29namespace juce
30{
31
32//==============================================================================
41{
42 // "events" is expected to be a const- or non-const-ref to Vst2::VstEvents.
43 template <typename Events>
44 static auto& getEvent (Events& events, int index)
45 {
46 using EventType = decltype (&*events.events);
47
48 // We static cast rather than using a direct array index here to circumvent
49 // UB sanitizer's bounds-checks. The original struct is supposed to contain
50 // a variable-length array, but the declaration uses a size of "2" for this
51 // member.
52 return static_cast<EventType> (events.events)[index];
53 }
54
55 Vst2::VstEvent* const& getEvent (int index) const { return getEvent (*events, index); }
56 Vst2::VstEvent* & getEvent (int index) { return getEvent (*events, index); }
57
58public:
59 //==============================================================================
62 {
63 }
64
66 {
67 freeEvents();
68 }
69
70 //==============================================================================
71 void clear()
72 {
73 numEventsUsed = 0;
74
75 if (events != nullptr)
76 events->numEvents = 0;
77 }
78
79 void addEvent (const void* const midiData, int numBytes, int frameOffset)
80 {
81 ensureSize (numEventsUsed + 1);
82
83 void* const ptr = getEvent (numEventsUsed);
84 events->numEvents = ++numEventsUsed;
85
86 if (numBytes <= 4)
87 {
88 auto* const e = static_cast<Vst2::VstMidiEvent*> (ptr);
89
90 if (e->type == Vst2::kVstSysExType)
91 {
92 delete[] reinterpret_cast<Vst2::VstMidiSysexEvent*> (e)->sysexDump;
93 e->type = Vst2::kVstMidiType;
94 e->byteSize = sizeof (Vst2::VstMidiEvent);
95 e->noteLength = 0;
96 e->noteOffset = 0;
97 e->detune = 0;
98 e->noteOffVelocity = 0;
99 }
100
101 e->deltaFrames = frameOffset;
102 memcpy (e->midiData, midiData, (size_t) numBytes);
103 }
104 else
105 {
106 auto* const se = static_cast<Vst2::VstMidiSysexEvent*> (ptr);
107
108 if (se->type == Vst2::kVstSysExType)
109 delete[] se->sysexDump;
110
111 se->sysexDump = new char [(size_t) numBytes];
112 memcpy (se->sysexDump, midiData, (size_t) numBytes);
113
114 se->type = Vst2::kVstSysExType;
115 se->byteSize = sizeof (Vst2::VstMidiSysexEvent);
116 se->deltaFrames = frameOffset;
117 se->flags = 0;
118 se->dumpBytes = numBytes;
119 se->resvd1 = 0;
120 se->resvd2 = 0;
121 }
122 }
123
124 //==============================================================================
125 // Handy method to pull the events out of an event buffer supplied by the host
126 // or plugin.
127 static void addEventsToMidiBuffer (const Vst2::VstEvents* events, MidiBuffer& dest)
128 {
129 for (int i = 0; i < events->numEvents; ++i)
130 {
131 const auto* const e = getEvent (*events, i);
132
133 if (e != nullptr)
134 {
135 const void* const ptr = e;
136
137 if (e->type == Vst2::kVstMidiType)
138 {
139 dest.addEvent ((const juce::uint8*) static_cast<const Vst2::VstMidiEvent*> (ptr)->midiData,
140 4, e->deltaFrames);
141 }
142 else if (e->type == Vst2::kVstSysExType)
143 {
144 const auto* se = static_cast<const Vst2::VstMidiSysexEvent*> (ptr);
145 dest.addEvent ((const juce::uint8*) se->sysexDump,
146 (int) se->dumpBytes,
147 e->deltaFrames);
148 }
149 }
150 }
151 }
152
153 //==============================================================================
154 void ensureSize (int numEventsNeeded)
155 {
157 {
158 numEventsNeeded = (numEventsNeeded + 32) & ~31;
159
160 const size_t size = 20 + (size_t) numEventsNeeded * sizeof (Vst2::VstEvent*);
161
162 if (events == nullptr)
163 events.calloc (size, 1);
164 else
165 events.realloc (size, 1);
166
167 for (int i = numEventsAllocated; i < numEventsNeeded; ++i)
168 getEvent (i) = allocateVSTEvent();
169
171 }
172 }
173
174 void freeEvents()
175 {
176 if (events != nullptr)
177 {
178 for (int i = numEventsAllocated; --i >= 0;)
179 freeVSTEvent (getEvent (i));
180
181 events.free();
182 numEventsUsed = 0;
184 }
185 }
186
187 //==============================================================================
189
190private:
192
193 static Vst2::VstEvent* allocateVSTEvent()
194 {
195 constexpr auto size = jmax (sizeof (Vst2::VstMidiEvent), sizeof (Vst2::VstMidiSysexEvent));
196
197 if (auto* e = static_cast<Vst2::VstEvent*> (std::calloc (1, size)))
198 {
199 e->type = Vst2::kVstMidiType;
200 e->byteSize = sizeof (Vst2::VstMidiEvent);
201 return e;
202 }
203
204 return nullptr;
205 }
206
207 static void freeVSTEvent (Vst2::VstEvent* e)
208 {
209 if (e->type == Vst2::kVstSysExType)
210 {
211 delete[] (reinterpret_cast<Vst2::VstMidiSysexEvent*> (e)->sysexDump);
212 }
213
214 std::free (e);
215 }
216};
217
218} // namespace juce
219
220#endif // JUCE_VSTINTERFACE_H_INCLUDED
T calloc(T... args)
T free(T... args)
typedef int
memcpy
JUCE Namespace.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
T size(T... args)
typedef size_t