/
IPlugStructs.h
411 lines (349 loc) · 10.1 KB
/
IPlugStructs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
==============================================================================
This file is part of the iPlug 2 library
Oli Larkin et al. 2018 - https://www.olilarkin.co.uk
iPlug 2 is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the WDL license
- https://www.cockos.com/wdl/
==============================================================================
*/
#pragma once
#include <algorithm>
#include "wdlstring.h"
#include "ptrlist.h"
#include "IPlugConstants.h"
#include "IPlugPlatform.h"
#include "IPlugMidi.h" // <- Midi related structs in here
#include "IPlugUtilities.h"
/** In certain cases we need to queue parameter changes for transferral between threads */
struct IParamChange
{
int paramIdx;
double value;
bool normalized; // TODO: Remove this
};
/** A helper class for IBtyeChunk and IBtyeStream that avoids code duplication **/
struct IByteGetter
{
static inline int GetBytes(const uint8_t* pData, int dataSize, void* pBuf, int size, int startPos)
{
int endPos = startPos + size;
if (startPos >= 0 && endPos <= dataSize)
{
memcpy(pBuf, pData + startPos, size);
return endPos;
}
return -1;
}
static inline int GetStr(const uint8_t* pData, int dataSize, WDL_String& str, int startPos)
{
int len;
int strStartPos = GetBytes(pData, dataSize, &len, sizeof(len), startPos);
if (strStartPos >= 0)
{
int strEndPos = strStartPos + len;
if (strEndPos <= dataSize)
{
if (len > 0)
str.Set((char*) (pData + strStartPos), len);
else
str.Set("");
}
return strEndPos;
}
return -1;
}
};
/** Manages a block of memory, for plug-in settings store/recall */
class IByteChunk : private IByteGetter
{
public:
IByteChunk() {}
~IByteChunk() {}
/** This method is used in order to place the IPlug version number in the chunk when serialising data. In theory this is for backwards compatibility.
* @param chunk reference to the chunk where the version number will be placed */
static void InitChunkWithIPlugVer(IByteChunk& chunk)
{
chunk.Clear();
int magic = IPLUG_VERSION_MAGIC;
chunk.Put(&magic);
int ver = IPLUG_VERSION;
chunk.Put(&ver);
}
/** Helper method to retrieve the IPlug version number from the beginning of the byte chunk
* @param chunk The incoming byte chunk that contains the version number
* @param pos The position (in bytes) to start looking
* @return The IPlug version number, retrieved from the chunk, or 0 if it failed */
static int GetIPlugVerFromChunk(const IByteChunk& chunk, int& position)
{
int magic = 0, ver = 0;
int magicpos = chunk.Get(&magic, position);
if (magicpos > position && magic == IPLUG_VERSION_MAGIC)
position = chunk.Get(&ver, magicpos);
return ver;
}
/**
* Copies data into the chunk
* @param pBuf Pointer to the object to copy data from
* @param size Number of bytes to copy
*/
inline int PutBytes(const void* pBuf, int size)
{
int n = mBytes.GetSize();
mBytes.Resize(n + size);
memcpy(mBytes.Get() + n, pBuf, size);
return mBytes.GetSize();
}
inline int GetBytes(void* pBuf, int size, int startPos) const
{
return IByteGetter::GetBytes(mBytes.Get(), Size(), pBuf, size, startPos);
}
template <class T> inline int Put(const T* pVal)
{
return PutBytes(pVal, sizeof(T));
}
template <class T> inline int Get(T* pVal, int startPos) const
{
return GetBytes(pVal, sizeof(T), startPos);
}
inline int PutStr(const char* str)
{
int slen = (int) strlen(str);
Put(&slen);
return PutBytes(str, slen);
}
inline int GetStr(WDL_String& str, int startPos) const
{
return IByteGetter::GetStr(mBytes.Get(), Size(), str, startPos);
}
inline int PutChunk(IByteChunk* pRHS)
{
return PutBytes(pRHS->GetBytes(), pRHS->Size());
}
/** Clears the chunk */
inline void Clear()
{
mBytes.Resize(0);
}
/**
* Returns the current size of the chunk
* @return Current size (in bytes)
*/
inline int Size() const
{
return mBytes.GetSize();
}
/** Resizes the chunk /todo check
* @param newSize Desired size (in bytes)
* @return Old size (in bytes) */
inline int Resize(int newSize)
{
int n = mBytes.GetSize();
mBytes.Resize(newSize);
if (newSize > n)
{
memset(mBytes.Get() + n, 0, (newSize - n));
}
return n;
}
inline uint8_t* GetBytes() // TODO: BAD NAME!
{
return mBytes.Get();
}
inline bool IsEqual(IByteChunk& otherChunk) const
{
return (otherChunk.Size() == Size() && !memcmp(otherChunk.mBytes.Get(), mBytes.Get(), Size()));
}
private:
WDL_TypedBuf<uint8_t> mBytes;
};
/** Manages a non-owned block of memory, for receiving arbitrary message byte streams */
class IByteStream : private IByteGetter
{
public:
IByteStream(const void *pData, int dataSize) : mBytes(reinterpret_cast<const uint8_t *>(pData)), mSize(dataSize) {}
~IByteStream() {}
inline int GetBytes(void* pBuf, int size, int startPos) const
{
return IByteGetter::GetBytes(mBytes, Size(), pBuf, size, startPos);
}
template <class T> inline int Get(T* pVal, int startPos) const
{
return GetBytes(pVal, sizeof(T), startPos);
}
inline int GetStr(WDL_String& str, int startPos) const
{
return IByteGetter::GetStr(mBytes, Size(), str, startPos);
}
/** Returns the size of the chunk
* @return size (in bytes) */
inline int Size() const
{
return mSize;
}
inline bool IsEqual(IByteStream& otherStream) const
{
return (otherStream.Size() == Size() && !memcmp(otherStream.mBytes, mBytes, Size()));
}
inline const uint8_t* GetData()
{
return mBytes;
}
private:
const uint8_t* mBytes;
int mSize;
};
/** Helper struct to set compile time options to an API class constructor */
struct IPlugConfig
{
int nParams;
int nPresets;
const char* channelIOStr;
const char* pluginName;
const char* productName;
const char* mfrName;
int vendorVersion;
int uniqueID;
int mfrID;
int latency;
bool plugDoesMidi;
bool plugDoesChunks;
bool plugIsInstrument;
bool plugHasUI;
int plugWidth;
int plugHeight;
const char* bundleID;
IPlugConfig(int nParams,
int nPresets,
const char* channelIOStr,
const char* pluginName,
const char* productName,
const char* mfrName,
int vendorVersion,
int uniqueID,
int mfrID,
int latency,
bool plugDoesMidi,
bool plugDoesChunks,
bool plugIsInstrument,
bool plugHasUI,
int plugWidth,
int plugHeight,
const char* bundleID)
: nParams(nParams)
, nPresets(nPresets)
, channelIOStr(channelIOStr)
, pluginName(pluginName)
, productName(productName)
, mfrName(mfrName)
, vendorVersion(vendorVersion)
, uniqueID(uniqueID)
, mfrID(mfrID)
, latency(latency)
, plugDoesMidi(plugDoesMidi)
, plugDoesChunks(plugDoesChunks)
, plugIsInstrument(plugIsInstrument)
, plugHasUI(plugHasUI)
, plugWidth(plugWidth)
, plugHeight(plugHeight)
, bundleID(bundleID)
{};
};
/** Used to manage scratch buffers for each channel of I/O, which may involve converting from single to double precision */
template<class TIN = PLUG_SAMPLE_SRC, class TOUT = PLUG_SAMPLE_DST>
struct IChannelData
{
bool mConnected = false;
TOUT** mData = nullptr; // If this is for an input channel, points into IPlugProcessor::mInData, if it's for an output channel points into IPlugProcessor::mOutData
TIN* mIncomingData = nullptr;
WDL_TypedBuf<TOUT> mScratchBuf;
WDL_String mLabel = WDL_String("");
};
struct IBusInfo
{
ERoute mDirection;
int mNChans;
WDL_String mLabel;
IBusInfo(ERoute direction, int nchans = 0, const char* label = "")
: mDirection(direction)
, mNChans(nchans)
{
if(CStringHasContents(label))
mLabel.Set(label);
else
mLabel.Set(RoutingDirStrs[direction]);
}
};
/** An IOConfig is used to store bus info for each input/output configuration defined in the channel io string */
struct IOConfig
{
WDL_PtrList<IBusInfo> mBusInfo[2]; // A particular valid io config may have multiple input buses or output busses
~IOConfig()
{
mBusInfo[0].Empty(true);
mBusInfo[1].Empty(true);
}
void AddBusInfo(ERoute direction, int NChans, const char* label = "")
{
mBusInfo[direction].Add(new IBusInfo(direction, NChans, label));
}
IBusInfo* GetBusInfo(ERoute direction, int index)
{
assert(index >= 0 && index < mBusInfo[direction].GetSize());
return mBusInfo[direction].Get(index);
}
int NChansOnBusSAFE(ERoute direction, int index)
{
int NChans = 0;
if(index >= 0 && index < mBusInfo[direction].GetSize())
NChans = mBusInfo[direction].Get(index)->mNChans;
return NChans;
}
int NBuses(ERoute direction)
{
return mBusInfo[direction].GetSize();
}
/** Get the total number of channels across all direction buses for this IOConfig */
int GetTotalNChannels(ERoute direction) const
{
int total = 0;
for(int i = 0; i < mBusInfo[direction].GetSize(); i++)
total += mBusInfo[direction].Get(i)->mNChans;
return total;
}
bool ContainsWildcard(ERoute direction)
{
for(auto i = 0; i < mBusInfo[direction].GetSize(); i++)
{
if(mBusInfo[direction].Get(i)->mNChans < 0)
return true;
}
return false;
}
};
/** Encapsulates information about the host transport state */
struct ITimeInfo
{
double mTempo = DEFAULT_TEMPO;
double mSamplePos = -1.0;
double mPPQPos = -1.0;
double mLastBar = -1.0;
double mCycleStart = -1.0;
double mCycleEnd = -1.0;
int mNumerator = 4;
int mDenominator = 4;
bool mTransportIsRunning = false;
bool mTransportLoopEnabled = false;
};
/** A struct used for specifying baked-in factory presets */
struct IPreset
{
bool mInitialized = false;
char mName[MAX_PRESET_NAME_LEN];
IByteChunk mChunk;
IPreset()
{
sprintf(mName, "%s", UNUSED_PRESET_NAME);
}
};