LMMS
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#pragma once
27
28namespace Vst2
30#include "juce_VSTInterface.h"
31}
32
33namespace juce
34{
35
36//==============================================================================
45{
46 // "events" is expected to be a const- or non-const-ref to Vst2::VstEventBlock.
47 template <typename Events>
48 static auto& getEvent (Events& events, int index)
49 {
50 using EventType = decltype (&*events.events);
51
52 // We static cast rather than using a direct array index here to circumvent
53 // UB sanitizer's bounds-checks. The original struct is supposed to contain
54 // a variable-length array, but the declaration uses a size of "2" for this
55 // member.
56 return static_cast<EventType> (events.events)[index];
57 }
58
59 Vst2::VstEvent* const& getEvent (int index) const { return getEvent (*events, index); }
60 Vst2::VstEvent* & getEvent (int index) { return getEvent (*events, index); }
62public:
63 //==============================================================================
65 : numEventsUsed (0), numEventsAllocated (0)
66 {
67 }
70 {
71 freeEvents();
72 }
74 //==============================================================================
75 void clear()
76 {
77 numEventsUsed = 0;
79 if (events != nullptr)
80 events->numberOfEvents = 0;
81 }
82
83 void addEvent (const void* const midiData, int numBytes, int frameOffset)
84 {
85 ensureSize (numEventsUsed + 1);
87 void* const ptr = getEvent (numEventsUsed);
88 events->numberOfEvents = ++numEventsUsed;
90 if (numBytes <= 4)
91 {
92 auto* const e = static_cast<Vst2::VstMidiEvent*> (ptr);
93
94 if (e->type == Vst2::vstSysExEventType)
95 {
96 delete[] reinterpret_cast<Vst2::VstSysExEvent*> (e)->sysExDump;
98 e->size = sizeof (Vst2::VstMidiEvent);
99 e->noteSampleLength = 0;
100 e->noteSampleOffset = 0;
101 e->tuning = 0;
102 e->noteVelocityOff = 0;
105 e->sampleOffset = frameOffset;
106 memcpy (e->midiData, midiData, (size_t) numBytes);
108 else
110 auto* const se = static_cast<Vst2::VstSysExEvent*> (ptr);
112 if (se->type == Vst2::vstSysExEventType)
113 delete[] se->sysExDump;
115 se->sysExDump = new char [(size_t) numBytes];
116 memcpy (se->sysExDump, midiData, (size_t) numBytes);
119 se->size = sizeof (Vst2::VstSysExEvent);
120 se->offsetSamples = frameOffset;
121 se->flags = 0;
122 se->sysExDumpSize = numBytes;
123 se->future1 = 0;
124 se->future2 = 0;
128 //==============================================================================
129 // Handy method to pull the events out of an event buffer supplied by the host
130 // or plugin.
133 for (int i = 0; i < events->numberOfEvents; ++i)
135 const auto* const e = getEvent (*events, i);
137 if (e != nullptr)
139 const void* const ptr = e;
143 dest.addEvent ((const juce::uint8*) static_cast<const Vst2::VstMidiEvent*> (ptr)->midiData,
144 4, e->sampleOffset);
146 else if (e->type == Vst2::vstSysExEventType)
148 const auto* se = static_cast<const Vst2::VstSysExEvent*> (ptr);
149 dest.addEvent ((const juce::uint8*) se->sysExDump,
150 (int) se->sysExDumpSize,
151 e->sampleOffset);
152 }
153 }
154 }
156
157 //==============================================================================
158 void ensureSize (int numEventsNeeded)
160 if (numEventsNeeded > numEventsAllocated)
162 numEventsNeeded = (numEventsNeeded + 32) & ~31;
164 const size_t size = 20 + (size_t) numEventsNeeded * sizeof (Vst2::VstEvent*);
166 if (events == nullptr)
167 events.calloc (size, 1);
168 else
169 events.realloc (size, 1);
171 for (int i = numEventsAllocated; i < numEventsNeeded; ++i)
172 getEvent (i) = allocateVSTEvent();
174 numEventsAllocated = numEventsNeeded;
180 if (events != nullptr)
182 for (int i = numEventsAllocated; --i >= 0;)
183 freeVSTEvent (getEvent (i));
185 events.free();
186 numEventsUsed = 0;
187 numEventsAllocated = 0;
191 //==============================================================================
194private:
199 constexpr auto size = jmax (sizeof (Vst2::VstMidiEvent), sizeof (Vst2::VstSysExEvent));
201 if (auto* e = static_cast<Vst2::VstEvent*> (std::calloc (1, size)))
202 {
204 e->size = sizeof (Vst2::VstMidiEvent);
205 return e;
208 return nullptr;
209 }
210
212 {
214 {
215 delete[] (reinterpret_cast<Vst2::VstSysExEvent*> (e)->sysExDump);
218 std::free (e);
222} // namespace juce
Definition juce_HeapBlock.h:87
Definition juce_MidiBuffer.h:145
bool addEvent(const MidiMessage &midiMessage, int sampleNumber)
Definition juce_MidiBuffer.cpp:122
Definition juce_VSTMidiEventList.h:45
static Vst2::VstEvent * allocateVSTEvent()
Definition juce_VSTMidiEventList.h:197
Vst2::VstEvent *& getEvent(int index)
Definition juce_VSTMidiEventList.h:60
static auto & getEvent(Events &events, int index)
Definition juce_VSTMidiEventList.h:48
static void addEventsToMidiBuffer(const Vst2::VstEventBlock *events, MidiBuffer &dest)
Definition juce_VSTMidiEventList.h:131
int numEventsUsed
Definition juce_VSTMidiEventList.h:195
static void freeVSTEvent(Vst2::VstEvent *e)
Definition juce_VSTMidiEventList.h:211
HeapBlock< Vst2::VstEventBlock > events
Definition juce_VSTMidiEventList.h:192
Vst2::VstEvent *const & getEvent(int index) const
Definition juce_VSTMidiEventList.h:59
void addEvent(const void *const midiData, int numBytes, int frameOffset)
Definition juce_VSTMidiEventList.h:83
int numEventsAllocated
Definition juce_VSTMidiEventList.h:195
void clear()
Definition juce_VSTMidiEventList.h:75
VSTMidiEventList()
Definition juce_VSTMidiEventList.h:64
void freeEvents()
Definition juce_VSTMidiEventList.h:178
void ensureSize(int numEventsNeeded)
Definition juce_VSTMidiEventList.h:158
* e
Definition inflate.c:1404
register unsigned i
Definition inflate.c:1575
Definition juce_VSTMidiEventList.h:29
@ vstSysExEventType
Definition juce_VSTMidiEventList.h:291
@ vstMidiEventType
Definition juce_VSTMidiEventList.h:290
Definition carla_juce.cpp:31
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
jack_client_t client jack_client_t client jack_client_t client jack_client_t JackInfoShutdownCallback void arg jack_client_t jack_port_t port void func jack_client_t const char const char unsigned long flags const jack_port_t port jack_client_t jack_port_id_t port_id const jack_port_t const char port_name const jack_port_t port void * ptr
Definition juce_linux_JackAudio.cpp:79
unsigned char uint8
Definition juce_MathsFunctions.h:37
Definition juce_VSTMidiEventList.h:299
int32 numberOfEvents
Definition juce_VSTMidiEventList.h:300
Definition juce_VSTMidiEventList.h:280
Definition juce_VSTMidiEventList.h:310
Definition juce_VSTMidiEventList.h:334
char * sysExDump
Definition juce_VSTMidiEventList.h:341
memcpy(hh, h, RAND_HEAD_LEN)
ulg size
Definition extract.c:2350