openshot-audio  0.1.7
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) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifdef __aeffect__ // NB: this must come first, *before* the header-guard.
26 
27 #ifndef JUCE_VSTMIDIEVENTLIST_H_INCLUDED
28 #define JUCE_VSTMIDIEVENTLIST_H_INCLUDED
29 
30 //==============================================================================
36 class VSTMidiEventList
37 {
38 public:
39  //==============================================================================
40  VSTMidiEventList()
41  : numEventsUsed (0), numEventsAllocated (0)
42  {
43  }
44 
45  ~VSTMidiEventList()
46  {
47  freeEvents();
48  }
49 
50  //==============================================================================
51  void clear()
52  {
53  numEventsUsed = 0;
54 
55  if (events != nullptr)
56  events->numEvents = 0;
57  }
58 
59  void addEvent (const void* const midiData, const int numBytes, const int frameOffset)
60  {
61  ensureSize (numEventsUsed + 1);
62 
63  VstMidiEvent* const e = (VstMidiEvent*) (events->events [numEventsUsed]);
64  events->numEvents = ++numEventsUsed;
65 
66  if (numBytes <= 4)
67  {
68  if (e->type == kVstSysExType)
69  {
70  delete[] (((VstMidiSysexEvent*) e)->sysexDump);
71  e->type = kVstMidiType;
72  e->byteSize = sizeof (VstMidiEvent);
73  e->noteLength = 0;
74  e->noteOffset = 0;
75  e->detune = 0;
76  e->noteOffVelocity = 0;
77  }
78 
79  e->deltaFrames = frameOffset;
80  memcpy (e->midiData, midiData, (size_t) numBytes);
81  }
82  else
83  {
84  VstMidiSysexEvent* const se = (VstMidiSysexEvent*) e;
85 
86  if (se->type == kVstSysExType)
87  delete[] se->sysexDump;
88 
89  se->sysexDump = new char [numBytes];
90  memcpy (se->sysexDump, midiData, (size_t) numBytes);
91 
92  se->type = kVstSysExType;
93  se->byteSize = sizeof (VstMidiSysexEvent);
94  se->deltaFrames = frameOffset;
95  se->flags = 0;
96  se->dumpBytes = numBytes;
97  se->resvd1 = 0;
98  se->resvd2 = 0;
99  }
100  }
101 
102  //==============================================================================
103  // Handy method to pull the events out of an event buffer supplied by the host
104  // or plugin.
105  static void addEventsToMidiBuffer (const VstEvents* events, MidiBuffer& dest)
106  {
107  for (int i = 0; i < events->numEvents; ++i)
108  {
109  const VstEvent* const e = events->events[i];
110 
111  if (e != nullptr)
112  {
113  if (e->type == kVstMidiType)
114  {
115  dest.addEvent ((const juce::uint8*) ((const VstMidiEvent*) e)->midiData,
116  4, e->deltaFrames);
117  }
118  else if (e->type == kVstSysExType)
119  {
120  dest.addEvent ((const juce::uint8*) ((const VstMidiSysexEvent*) e)->sysexDump,
121  (int) ((const VstMidiSysexEvent*) e)->dumpBytes,
122  e->deltaFrames);
123  }
124  }
125  }
126  }
127 
128  //==============================================================================
129  void ensureSize (int numEventsNeeded)
130  {
131  if (numEventsNeeded > numEventsAllocated)
132  {
133  numEventsNeeded = (numEventsNeeded + 32) & ~31;
134 
135  const size_t size = 20 + sizeof (VstEvent*) * (size_t) numEventsNeeded;
136 
137  if (events == nullptr)
138  events.calloc (size, 1);
139  else
140  events.realloc (size, 1);
141 
142  for (int i = numEventsAllocated; i < numEventsNeeded; ++i)
143  events->events[i] = allocateVSTEvent();
144 
145  numEventsAllocated = numEventsNeeded;
146  }
147  }
148 
149  void freeEvents()
150  {
151  if (events != nullptr)
152  {
153  for (int i = numEventsAllocated; --i >= 0;)
154  freeVSTEvent (events->events[i]);
155 
156  events.free();
157  numEventsUsed = 0;
158  numEventsAllocated = 0;
159  }
160  }
161 
162  //==============================================================================
163  HeapBlock <VstEvents> events;
164 
165 private:
166  int numEventsUsed, numEventsAllocated;
167 
168  static VstEvent* allocateVSTEvent()
169  {
170  VstEvent* const e = (VstEvent*) std::calloc (1, sizeof (VstMidiEvent) > sizeof (VstMidiSysexEvent) ? sizeof (VstMidiEvent)
171  : sizeof (VstMidiSysexEvent));
172  e->type = kVstMidiType;
173  e->byteSize = sizeof (VstMidiEvent);
174  return e;
175  }
176 
177  static void freeVSTEvent (VstEvent* e)
178  {
179  if (e->type == kVstSysExType)
180  delete[] (((VstMidiSysexEvent*) e)->sysexDump);
181 
182  std::free (e);
183  }
184 };
185 
186 #endif // JUCE_VSTMIDIEVENTLIST_H_INCLUDED
187 #endif // JUCE_VSTMIDIEVENTLIST_H_INCLUDED
void addEvent(const MidiMessage &midiMessage, int sampleNumber)
Definition: juce_MidiBuffer.cpp:110
Definition: juce_HeapBlock.h:90
unsigned char uint8
Definition: juce_core.h:44
Definition: juce_MidiBuffer.h:43