iPlug2 - C++ Audio Plug-in Framework
IPlugStructs.h
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 
20 #include <algorithm>
21 #include "wdlstring.h"
22 #include "ptrlist.h"
23 
24 #include "IPlugConstants.h"
25 #include "IPlugPlatform.h"
26 #include "IPlugMidi.h" // <- Midi related structs in here
27 #include "IPlugUtilities.h"
28 
29 BEGIN_IPLUG_NAMESPACE
30 
32 struct ParamTuple
33 {
34  int idx;
35  double value;
36 
37  ParamTuple(int idx = kNoParameter, double value = 0.)
38  : idx(idx)
39  , value(value)
40  {}
41 };
42 
44 struct SysExData
45 {
46  SysExData(int offset = 0, int size = 0, const void* pData = 0)
47  : mOffset(offset)
48  , mSize(size)
49  {
50  assert(size < MAX_SYSEX_SIZE);
51 
52  if (pData)
53  memcpy(mData, pData, size);
54  else
55  memset(mData, 0, MAX_SYSEX_SIZE);
56  }
57 
58  int mOffset;
59  int mSize;
60  uint8_t mData[MAX_SYSEX_SIZE];
61 };
62 
65 {
73  static inline int GetBytes(const uint8_t* pSrc, int srcSize, void* pDst, int nBytesToCopy, int startPos)
74  {
75  int endPos = startPos + nBytesToCopy;
76  if (startPos >= 0 && endPos <= srcSize)
77  {
78  memcpy(pDst, pSrc + startPos, nBytesToCopy);
79  return endPos;
80  }
81  return -1;
82  }
83 
90  static inline int GetStr(const uint8_t* pSrc, int srcSize, WDL_String& str, int startPos)
91  {
92  int len;
93  int strStartPos = GetBytes(pSrc, srcSize, &len, sizeof(len), startPos);
94  if (strStartPos >= 0)
95  {
96  int strEndPos = strStartPos + len;
97  if (strEndPos <= srcSize)
98  {
99  if (len > 0)
100  str.Set((char*) (pSrc + strStartPos), len);
101  else
102  str.Set("");
103  }
104  return strEndPos;
105  }
106  return -1;
107  }
108 };
109 
111 class IByteChunk : private IByteGetter
112 {
113 public:
114  IByteChunk() {}
115  ~IByteChunk() {}
116 
119  static void InitChunkWithIPlugVer(IByteChunk& chunk)
120  {
121  chunk.Clear();
122  int magic = IPLUG_VERSION_MAGIC;
123  chunk.Put(&magic);
124  int ver = IPLUG_VERSION;
125  chunk.Put(&ver);
126  }
127 
132  static int GetIPlugVerFromChunk(const IByteChunk& chunk, int& position)
133  {
134  int magic = 0, ver = 0;
135  int magicpos = chunk.Get(&magic, position);
136 
137  if (magicpos > position && magic == IPLUG_VERSION_MAGIC)
138  position = chunk.Get(&ver, magicpos);
139 
140  return ver;
141  }
142 
147  inline int PutBytes(const void* pSrc, int nBytesToCopy)
148  {
149  int n = mBytes.GetSize();
150  mBytes.Resize(n + nBytesToCopy);
151  memcpy(mBytes.Get() + n, pSrc, nBytesToCopy);
152  return mBytes.GetSize();
153  }
154 
160  inline int GetBytes(void* pDst, int nBytesToCopy, int startPos) const
161  {
162  return IByteGetter::GetBytes(mBytes.Get(), Size(), pDst, nBytesToCopy, startPos);
163  }
164 
169  template <class T>
170  inline int Put(const T* pVal)
171  {
172  return PutBytes(pVal, sizeof(T));
173  }
174 
180  template <class T>
181  inline int Get(T* pDst, int startPos) const
182  {
183  return GetBytes(pDst, sizeof(T), startPos);
184  }
185 
189  inline int PutStr(const char* str)
190  {
191  int slen = (int) strlen(str);
192  Put(&slen);
193  return PutBytes(str, slen);
194  }
195 
200  inline int GetStr(WDL_String& str, int startPos) const
201  {
202  return IByteGetter::GetStr(mBytes.Get(), Size(), str, startPos);
203  }
204 
208  inline int PutChunk(const IByteChunk* pRHS)
209  {
210  return PutBytes(pRHS->GetData(), pRHS->Size());
211  }
212 
214  inline void Clear()
215  {
216  mBytes.Resize(0);
217  }
218 
221  inline int Size() const
222  {
223  return mBytes.GetSize();
224  }
225 
229  inline int Resize(int newSize)
230  {
231  int n = mBytes.GetSize();
232  mBytes.Resize(newSize);
233  if (newSize > n)
234  {
235  memset(mBytes.Get() + n, 0, (newSize - n));
236  }
237  return n;
238  }
239 
242  inline uint8_t* GetData()
243  {
244  return mBytes.Get();
245  }
246 
249  inline const uint8_t* GetData() const
250  {
251  return mBytes.Get();
252  }
253 
257  inline bool IsEqual(IByteChunk& otherChunk) const
258  {
259  return (otherChunk.Size() == Size() && !memcmp(otherChunk.mBytes.Get(), mBytes.Get(), Size()));
260  }
261 
262 private:
263  WDL_TypedBuf<uint8_t> mBytes;
264 };
265 
267 class IByteStream : private IByteGetter
268 {
269 public:
270  IByteStream(const void *pData, int dataSize) : mBytes(reinterpret_cast<const uint8_t *>(pData)), mSize(dataSize) {}
271  ~IByteStream() {}
272 
278  inline int GetBytes(void* pDst, int nBytesToCopy, int startPos) const
279  {
280  return IByteGetter::GetBytes(mBytes, Size(), pDst, nBytesToCopy, startPos);
281  }
282 
288  template <class T>
289  inline int Get(T* pDst, int startPos) const
290  {
291  return GetBytes(pDst, sizeof(T), startPos);
292  }
293 
298  inline int GetStr(WDL_String& str, int startPos) const
299  {
300  return IByteGetter::GetStr(mBytes, Size(), str, startPos);
301  }
302 
305  inline int Size() const
306  {
307  return mSize;
308  }
309 
313  inline bool IsEqual(IByteStream& otherStream) const
314  {
315  return (otherStream.Size() == Size() && !memcmp(otherStream.mBytes, mBytes, Size()));
316  }
317 
320  inline const uint8_t* GetData()
321  {
322  return mBytes;
323  }
324 
325 private:
326  const uint8_t* mBytes;
327  int mSize;
328 };
329 
332 {
333 public:
334  IByteChunkReader(const IByteChunk& chunk, int startPos = 0)
335  : mChunk(chunk)
336  , mPos(startPos)
337  {
338  }
339 
344  inline int GetBytes(void* pBuf, int nBytesToCopy)
345  {
346  mPos = mChunk.GetBytes(pBuf, nBytesToCopy, mPos);
347  return mPos;
348  }
349 
354  template <class T>
355  inline int Get(T* pDst)
356  {
357  mPos = mChunk.Get(pDst, mPos);
358  return mPos;
359  }
360 
364  inline int GetStr(WDL_String& str)
365  {
366  mPos = mChunk.GetStr(str, mPos);
367  return mPos;
368  }
369 
372  inline int Tell() const
373  {
374  return mPos;
375  }
376 
379  inline void Seek(int pos)
380  {
381  mPos = pos;
382  }
383 
384 private:
385  const IByteChunk& mChunk;
386  int mPos;
387 };
388 
390 struct Config
391 {
392  int nParams;
393  int nPresets;
394  const char* channelIOStr;
395  const char* pluginName;
396  const char* productName;
397  const char* mfrName;
398  int vendorVersion;
399  int uniqueID;
400  int mfrID;
401  int latency;
402  bool plugDoesMidiIn;
403  bool plugDoesMidiOut;
404  bool plugDoesMPE;
405  bool plugDoesChunks;
406  int plugType;
407  bool plugHasUI;
408  int plugWidth;
409  int plugHeight;
410  int plugMinWidth;
411  int plugMaxWidth;
412  int plugMinHeight;
413  int plugMaxHeight;
414  bool plugHostResize;
415  const char* bundleID;
416 
417  Config(int nParams,
418  int nPresets,
419  const char* channelIOStr,
420  const char* pluginName,
421  const char* productName,
422  const char* mfrName,
423  int vendorVersion,
424  int uniqueID,
425  int mfrID,
426  int latency,
427  bool plugDoesMidiIn,
428  bool plugDoesMidiOut,
429  bool plugDoesMPE,
430  bool plugDoesChunks,
431  int plugType,
432  bool plugHasUI,
433  int plugWidth,
434  int plugHeight,
435  bool plugHostResize,
436  int plugMinWidth,
437  int plugMaxWidth,
438  int plugMinHeight,
439  int plugMaxHeight,
440  const char* bundleID)
441 
442  : nParams(nParams)
443  , nPresets(nPresets)
444  , channelIOStr(channelIOStr)
445  , pluginName(pluginName)
446  , productName(productName)
447  , mfrName(mfrName)
448  , vendorVersion(vendorVersion)
449  , uniqueID(uniqueID)
450  , mfrID(mfrID)
451  , latency(latency)
452  , plugDoesMidiIn(plugDoesMidiIn)
453  , plugDoesMidiOut(plugDoesMidiOut)
454  , plugDoesMPE(plugDoesMPE)
455  , plugDoesChunks(plugDoesChunks)
456  , plugType(plugType)
457  , plugHasUI(plugHasUI)
458  , plugWidth(plugWidth)
459  , plugHeight(plugHeight)
460  , plugMinWidth(plugMinWidth)
461  , plugMaxWidth(plugMaxWidth)
462  , plugMinHeight(plugMinHeight)
463  , plugMaxHeight(plugMaxHeight)
464  , plugHostResize(plugHostResize)
465  , bundleID(bundleID)
466  {};
467 };
468 
470 template<class TIN = PLUG_SAMPLE_SRC, class TOUT = PLUG_SAMPLE_DST>
472 {
473  bool mConnected = false;
474  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
475  TIN* mIncomingData = nullptr;
476  WDL_TypedBuf<TOUT> mScratchBuf;
477  WDL_String mLabel;
478 };
479 
481 class IBusInfo
482 {
483 public:
484  IBusInfo(ERoute direction, int nchans = 0)
485  : mDirection(direction)
486  , mNChans(nchans)
487  {
488  }
489 
490  int NChans() const { return mNChans; }
491 
492  ERoute GetDirection() const { return mDirection; }
493 
494 private:
495  ERoute mDirection;
496  int mNChans;
497 };
498 
500 struct IOConfig
501 {
502  WDL_PtrList<IBusInfo> mBusInfo[2]; // A particular valid io config may have multiple input buses or output busses
503 
504  ~IOConfig()
505  {
506  mBusInfo[0].Empty(true);
507  mBusInfo[1].Empty(true);
508  }
509 
514  void AddBusInfo(ERoute direction, int NChans)
515  {
516  mBusInfo[direction].Add(new IBusInfo(direction, NChans));
517  }
518 
523  const IBusInfo* GetBusInfo(ERoute direction, int index) const
524  {
525  assert(index >= 0 && index < mBusInfo[direction].GetSize());
526  return mBusInfo[direction].Get(index);
527  }
528 
533  int NChansOnBusSAFE(ERoute direction, int index) const
534  {
535  int NChans = 0;
536 
537  if(index >= 0 && index < mBusInfo[direction].GetSize())
538  NChans = mBusInfo[direction].Get(index)->NChans();
539 
540  return NChans;
541  }
542 
546  int NBuses(ERoute direction) const
547  {
548  return mBusInfo[direction].GetSize();
549  }
550 
554  int GetTotalNChannels(ERoute direction) const
555  {
556  int total = 0;
557 
558  for(int i = 0; i < mBusInfo[direction].GetSize(); i++)
559  total += mBusInfo[direction].Get(i)->NChans();
560 
561  return total;
562  }
563 
568  bool ContainsWildcard(ERoute direction) const
569  {
570  for(auto i = 0; i < mBusInfo[direction].GetSize(); i++)
571  {
572  if(mBusInfo[direction].Get(i)->NChans() < 0)
573  return true;
574  }
575 
576  return false;
577  }
578 };
579 
581 struct ITimeInfo
582 {
583  double mTempo = DEFAULT_TEMPO;
584  double mSamplePos = -1.0;
585  double mPPQPos = -1.0;
586  double mLastBar = -1.0;
587  double mCycleStart = -1.0;
588  double mCycleEnd = -1.0;
589 
590  int mNumerator = 4;
591  int mDenominator = 4;
592 
593  bool mTransportIsRunning = false;
594  bool mTransportLoopEnabled = false;
595 };
596 
598 struct IPreset
599 {
600  bool mInitialized = false;
601  char mName[MAX_PRESET_NAME_LEN];
602 
603  IByteChunk mChunk;
604 
605  IPreset()
606  {
607  sprintf(mName, "%s", UNUSED_PRESET_NAME);
608  }
609 };
610 
612 struct IKeyPress
613 {
614  int VK; // Windows VK_XXX
615  char utf8[5] = { 0 }; // UTF8 key
616  bool S, C, A; // SHIFT / CTRL(WIN) or CMD (MAC) / ALT
617 
624  IKeyPress(const char* _utf8, int vk, bool s = false, bool c = false, bool a = false)
625  : VK(vk)
626  , S(s), C(c), A(a)
627  {
628  strcpy(utf8, _utf8);
629  }
630 
631  void DBGPrint() const { DBGMSG("VK: %i\n", VK); }
632 };
633 
634 END_IPLUG_NAMESPACE
635 
int Get(T *pDst, int startPos) const
Get arbitary typed data from the stream.
Definition: IPlugStructs.h:289
Utility functions and macros.
int GetStr(WDL_String &str)
Retrieve a string from the managed IByteChunk and put it in str .
Definition: IPlugStructs.h:364
int GetTotalNChannels(ERoute direction) const
Get the total number of channels across all direction buses for this IOConfig.
Definition: IPlugStructs.h:554
Helper class to maintain a read position whilst extracting data from an IByteChunk.
Definition: IPlugStructs.h:331
int Size() const
Returns the size of the stream.
Definition: IPlugStructs.h:305
void Clear()
Clears the chunk (resizes to 0)
Definition: IPlugStructs.h:214
Include to get consistently named preprocessor macros for different platforms and logging functionali...
int PutStr(const char *str)
Put a string into the IByteChunk.
Definition: IPlugStructs.h:189
int Get(T *pDst, int startPos) const
Get arbitary typed data from the IByteChunk.
Definition: IPlugStructs.h:181
static int GetIPlugVerFromChunk(const IByteChunk &chunk, int &position)
Helper method to retrieve the IPlug version number from the beginning of the byte chunk...
Definition: IPlugStructs.h:132
void Seek(int pos)
Set the current position in the managed IByteChunk.
Definition: IPlugStructs.h:379
An IOConfig is used to store bus info for each input/output configuration defined in the channel io s...
Definition: IPlugStructs.h:500
Manages a non-owned block of memory, for receiving arbitrary message byte streams.
Definition: IPlugStructs.h:267
const IBusInfo * GetBusInfo(ERoute direction, int index) const
Definition: IPlugStructs.h:523
int Put(const T *pVal)
Copies arbitary typed data into the IByteChunk.
Definition: IPlugStructs.h:170
Manages a block of memory, for plug-in settings store/recall.
Definition: IPlugStructs.h:111
bool IsEqual(IByteChunk &otherChunk) const
Compares the size & values of the data of another chunk with this one.
Definition: IPlugStructs.h:257
static int GetBytes(const uint8_t *pSrc, int srcSize, void *pDst, int nBytesToCopy, int startPos)
Copy raw bytes from a byte array, returning the new position for subsequent calls.
Definition: IPlugStructs.h:73
IPlug Constant definitions, Types, magic numbers.
bool IsEqual(IByteStream &otherStream) const
Compares the size & values of the data of another stream with this one.
Definition: IPlugStructs.h:313
A struct used for specifying baked-in factory presets.
Definition: IPlugStructs.h:598
int NBuses(ERoute direction) const
Definition: IPlugStructs.h:546
void AddBusInfo(ERoute direction, int NChans)
Definition: IPlugStructs.h:514
ERoute
Used to identify whether a bus/channel connection is an input or an output.
int Size() const
Returns the current size of the chunk.
Definition: IPlugStructs.h:221
int GetStr(WDL_String &str, int startPos) const
Get a string from the stream.
Definition: IPlugStructs.h:298
int Resize(int newSize)
Resizes the chunk.
Definition: IPlugStructs.h:229
Used to manage information about a bus such as whether it&#39;s an input or output, channel count...
Definition: IPlugStructs.h:481
Used to manage scratch buffers for each channel of I/O, which may involve converting from single to d...
Definition: IPlugStructs.h:471
IKeyPress(const char *_utf8, int vk, bool s=false, bool c=false, bool a=false)
Definition: IPlugStructs.h:624
static void InitChunkWithIPlugVer(IByteChunk &chunk)
This method is used in order to place the IPlug version number in the chunk when serialising data...
Definition: IPlugStructs.h:119
In certain cases we need to queue parameter changes for transferral between threads.
Definition: IPlugStructs.h:32
bool ContainsWildcard(ERoute direction) const
Definition: IPlugStructs.h:568
int Tell() const
Return the current position in the managed IByteChunk.
Definition: IPlugStructs.h:372
int Get(T *pDst)
Copy arbitary typed data out of the managed IByteChunk at the current position and update the positio...
Definition: IPlugStructs.h:355
int GetBytes(void *pDst, int nBytesToCopy, int startPos) const
Copy raw bytes from the stream, returning the new position for subsequent calls.
Definition: IPlugStructs.h:278
int GetStr(WDL_String &str, int startPos) const
Get a string from the IByteChunk.
Definition: IPlugStructs.h:200
MIDI and sysex structs/utilites.
uint8_t * GetData()
Gets a ptr to the chunk data.
Definition: IPlugStructs.h:242
int PutChunk(const IByteChunk *pRHS)
Put another IByteChunk into this one.
Definition: IPlugStructs.h:208
This structure is used when queueing Sysex messages.
Definition: IPlugStructs.h:44
const uint8_t * GetData() const
Gets a const ptr to the chunk data.
Definition: IPlugStructs.h:249
int PutBytes(const void *pSrc, int nBytesToCopy)
Copies data into the chunk, placing it at the end, resizing if nessecary.
Definition: IPlugStructs.h:147
int GetBytes(void *pDst, int nBytesToCopy, int startPos) const
Copy raw bytes from the IByteChunk, returning the new position for subsequent calls.
Definition: IPlugStructs.h:160
const uint8_t * GetData()
Gets a const ptr to the stream data.
Definition: IPlugStructs.h:320
Encapsulates information about the host transport state.
Definition: IPlugStructs.h:581
A helper class for IByteChunk and IByteStream that avoids code duplication.
Definition: IPlugStructs.h:64
static int GetStr(const uint8_t *pSrc, int srcSize, WDL_String &str, int startPos)
Get a string from a byte array, to a WDL_String, returning the new position for subsequent calls...
Definition: IPlugStructs.h:90
Used for key press info, such as ASCII representation, virtual key (mapped to win32 codes) and modifi...
Definition: IPlugStructs.h:612
int NChansOnBusSAFE(ERoute direction, int index) const
Definition: IPlugStructs.h:533
int GetBytes(void *pBuf, int nBytesToCopy)
Copy nBytesToCopy bytes from the managed IByteChunk into pBuf .
Definition: IPlugStructs.h:344