AEffectWrapper.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * AEffectWrapper.h
  3. * ----------------
  4. * Purpose: Helper functions and structs for translating the VST AEffect struct between bit boundaries.
  5. * Notes : (currently none)
  6. * Authors: Johannes Schultz (OpenMPT Devs)
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include <vector>
  12. #include "../mptrack/plugins/VstDefinitions.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. #pragma pack(push, 8)
  15. template <typename ptr_t>
  16. struct AEffectProto
  17. {
  18. int32 magic;
  19. ptr_t dispatcher;
  20. ptr_t process;
  21. ptr_t setParameter;
  22. ptr_t getParameter;
  23. int32 numPrograms;
  24. uint32 numParams;
  25. int32 numInputs;
  26. int32 numOutputs;
  27. int32 flags;
  28. ptr_t resvd1;
  29. ptr_t resvd2;
  30. int32 initialDelay;
  31. int32 realQualities;
  32. int32 offQualities;
  33. float ioRatio;
  34. ptr_t object;
  35. ptr_t user;
  36. int32 uniqueID;
  37. int32 version;
  38. ptr_t processReplacing;
  39. ptr_t processDoubleReplacing;
  40. char future[56];
  41. // Convert native representation to bridge representation.
  42. // Don't overwrite any values managed by the bridge wrapper or host in general.
  43. void FromNative(const Vst::AEffect &in)
  44. {
  45. magic = in.magic;
  46. numPrograms = in.numPrograms;
  47. numParams = in.numParams;
  48. numInputs = in.numInputs;
  49. numOutputs = in.numOutputs;
  50. flags = in.flags;
  51. initialDelay = in.initialDelay;
  52. realQualities = in.realQualities;
  53. offQualities = in.offQualities;
  54. ioRatio = in.ioRatio;
  55. uniqueID = in.uniqueID;
  56. version = in.version;
  57. if(in.processReplacing == nullptr)
  58. flags &= ~Vst::effFlagsCanReplacing;
  59. if(in.processDoubleReplacing == nullptr)
  60. flags &= ~Vst::effFlagsCanDoubleReplacing;
  61. }
  62. };
  63. using AEffect32 = AEffectProto<int32>;
  64. using AEffect64 = AEffectProto<int64>;
  65. #pragma pack(pop)
  66. // Translate a VSTEvents struct to bridge format (placed in data vector)
  67. static void TranslateVstEventsToBridge(std::vector<char> &outData, const Vst::VstEvents &events, int32 targetPtrSize)
  68. {
  69. outData.reserve(outData.size() + sizeof(int32) + sizeof(Vst::VstMidiEvent) * events.numEvents);
  70. // Write number of events
  71. PushToVector(outData, events.numEvents);
  72. // Write events
  73. for(const auto event : events)
  74. {
  75. if(event->type == Vst::kVstSysExType)
  76. {
  77. // This is going to be messy since the VstMidiSysexEvent event has a different size than other events on 64-bit platforms.
  78. // We are going to write the event using the target process pointer size.
  79. auto sysExEvent = *static_cast<const Vst::VstMidiSysexEvent *>(event);
  80. sysExEvent.byteSize = 4 * sizeof(int32) + 4 * targetPtrSize; // It's 5 int32s and 3 pointers but that means that on 64-bit platforms, the fifth int32 is padded for alignment.
  81. PushToVector(outData, sysExEvent, 5 * sizeof(int32)); // Exclude the three pointers at the end for now
  82. if(targetPtrSize > static_cast<int32>(sizeof(int32))) // Padding for 64-bit required?
  83. outData.insert(outData.end(), targetPtrSize - sizeof(int32), 0);
  84. outData.insert(outData.end(), 3 * targetPtrSize, 0); // Make space for pointer + two reserved intptr_ts
  85. // Embed SysEx dump as well...
  86. auto sysex = reinterpret_cast<const char *>(sysExEvent.sysexDump);
  87. outData.insert(outData.end(), sysex, sysex + sysExEvent.dumpBytes);
  88. } else if(event->type == Vst::kVstMidiType)
  89. {
  90. // randomid by Insert Piz Here sends events of type kVstMidiType, but with a claimed size of 24 bytes instead of 32.
  91. Vst::VstMidiEvent midiEvent;
  92. std::memcpy(&midiEvent, event, sizeof(midiEvent));
  93. midiEvent.byteSize = sizeof(midiEvent);
  94. PushToVector(outData, midiEvent, sizeof(midiEvent));
  95. } else
  96. {
  97. PushToVector(outData, *event, event->byteSize);
  98. }
  99. }
  100. }
  101. // Translate bridge format (void *ptr) back to VSTEvents struct (placed in data vector)
  102. static void TranslateBridgeToVstEvents(std::vector<char> &outData, const void *inData)
  103. {
  104. const int32 numEvents = *static_cast<const int32 *>(inData);
  105. // First element is really a int32, but in case of 64-bit builds, the next field gets aligned anyway.
  106. const size_t headerSize = sizeof(intptr_t) + sizeof(intptr_t) + sizeof(Vst::VstEvent *) * numEvents;
  107. outData.reserve(headerSize + sizeof(Vst::VstMidiEvent) * numEvents);
  108. outData.resize(headerSize, 0);
  109. if(numEvents == 0)
  110. return;
  111. // Copy over event data (this is required for dumb SynthEdit plugins that don't copy over the event data during effProcessEvents)
  112. const char *readOffset = static_cast<const char *>(inData) + sizeof(int32);
  113. for(int32 i = 0; i < numEvents; i++)
  114. {
  115. auto *event = reinterpret_cast<const Vst::VstEvent *>(readOffset);
  116. outData.insert(outData.end(), readOffset, readOffset + event->byteSize);
  117. readOffset += event->byteSize;
  118. if(event->type == Vst::kVstSysExType)
  119. {
  120. // Copy over sysex dump
  121. auto *sysExEvent = static_cast<const Vst::VstMidiSysexEvent *>(event);
  122. outData.insert(outData.end(), readOffset, readOffset + sysExEvent->dumpBytes);
  123. readOffset += sysExEvent->dumpBytes;
  124. }
  125. }
  126. // Write pointers
  127. auto events = reinterpret_cast<Vst::VstEvents *>(outData.data());
  128. events->numEvents = numEvents;
  129. char *offset = outData.data() + headerSize;
  130. for(int32 i = 0; i < numEvents; i++)
  131. {
  132. events->events[i] = reinterpret_cast<Vst::VstEvent *>(offset);
  133. offset += events->events[i]->byteSize;
  134. if(events->events[i]->type == Vst::kVstSysExType)
  135. {
  136. auto sysExEvent = static_cast<Vst::VstMidiSysexEvent *>(events->events[i]);
  137. sysExEvent->sysexDump = reinterpret_cast<const std::byte *>(offset);
  138. offset += sysExEvent->dumpBytes;
  139. }
  140. }
  141. }
  142. // Calculate the size total of the VSTEvents (without header) in bridge format
  143. static size_t BridgeVstEventsSize(const void *ptr)
  144. {
  145. const int32 numEvents = *static_cast<const int32 *>(ptr);
  146. size_t size = 0;
  147. for(int32 i = 0; i < numEvents; i++)
  148. {
  149. const auto event = reinterpret_cast<const Vst::VstEvent *>(static_cast<const char *>(ptr) + sizeof(int32) + size);
  150. size += event->byteSize;
  151. if(event->type == Vst::kVstSysExType)
  152. {
  153. size += static_cast<const Vst::VstMidiSysexEvent *>(event)->dumpBytes;
  154. }
  155. }
  156. return size;
  157. }
  158. static void TranslateVstFileSelectToBridge(std::vector<char> &outData, const Vst::VstFileSelect &fileSelect, int32 targetPtrSize)
  159. {
  160. outData.reserve(outData.size() + sizeof(Vst::VstFileSelect) + fileSelect.numFileTypes * sizeof(Vst::VstFileType));
  161. PushToVector(outData, fileSelect.command);
  162. PushToVector(outData, fileSelect.type);
  163. PushToVector(outData, fileSelect.macCreator);
  164. PushToVector(outData, fileSelect.numFileTypes);
  165. outData.insert(outData.end(), targetPtrSize, 0); // fileTypes
  166. PushToVector(outData, fileSelect.title);
  167. outData.insert(outData.end(), 2 * targetPtrSize, 0); // initialPath, returnPath
  168. PushToVector(outData, fileSelect.sizeReturnPath);
  169. if(targetPtrSize > static_cast<int32>(sizeof(int32)))
  170. outData.insert(outData.end(), targetPtrSize - sizeof(int32), 0); // padding
  171. outData.insert(outData.end(), targetPtrSize, 0); // returnMultiplePaths
  172. PushToVector(outData, fileSelect.numReturnPaths);
  173. outData.insert(outData.end(), targetPtrSize, 0); // reserved
  174. PushToVector(outData, fileSelect.reserved2);
  175. if(fileSelect.command != Vst::kVstDirectorySelect)
  176. {
  177. for(int32 i = 0; i < fileSelect.numFileTypes; i++)
  178. {
  179. PushToVector(outData, fileSelect.fileTypes[i]);
  180. }
  181. }
  182. if(fileSelect.command == Vst::kVstMultipleFilesLoad)
  183. {
  184. outData.insert(outData.end(), fileSelect.numReturnPaths * targetPtrSize, 0);
  185. for(int32 i = 0; i < fileSelect.numReturnPaths; i++)
  186. {
  187. PushZStringToVector(outData, fileSelect.returnMultiplePaths[i]);
  188. }
  189. }
  190. PushZStringToVector(outData, fileSelect.initialPath);
  191. PushZStringToVector(outData, fileSelect.returnPath);
  192. }
  193. static void TranslateBridgeToVstFileSelect(std::vector<char> &outData, const void *inData, size_t srcSize)
  194. {
  195. outData.assign(static_cast<const char *>(inData), static_cast<const char *>(inData) + srcSize);
  196. // Fixup pointers
  197. Vst::VstFileSelect &fileSelect = *reinterpret_cast<Vst::VstFileSelect *>(outData.data());
  198. auto ptrOffset = outData.data() + sizeof(Vst::VstFileSelect);
  199. if(fileSelect.command != Vst::kVstDirectorySelect)
  200. {
  201. fileSelect.fileTypes = reinterpret_cast<Vst::VstFileType *>(ptrOffset);
  202. ptrOffset += fileSelect.numFileTypes * sizeof(Vst::VstFileType);
  203. } else
  204. {
  205. fileSelect.fileTypes = nullptr;
  206. }
  207. if(fileSelect.command == Vst::kVstMultipleFilesLoad)
  208. {
  209. fileSelect.returnMultiplePaths = reinterpret_cast<char **>(ptrOffset);
  210. ptrOffset += fileSelect.numReturnPaths * sizeof(char *);
  211. for(int32 i = 0; i < fileSelect.numReturnPaths; i++)
  212. {
  213. fileSelect.returnMultiplePaths[i] = ptrOffset;
  214. ptrOffset += strlen(fileSelect.returnMultiplePaths[i]) + 1;
  215. }
  216. } else
  217. {
  218. fileSelect.returnMultiplePaths = nullptr;
  219. }
  220. fileSelect.initialPath = ptrOffset;
  221. ptrOffset += strlen(fileSelect.initialPath) + 1;
  222. fileSelect.returnPath = ptrOffset;
  223. fileSelect.sizeReturnPath = static_cast<int32>(srcSize - std::distance(outData.data(), ptrOffset));
  224. ptrOffset += strlen(fileSelect.returnPath) + 1;
  225. }
  226. OPENMPT_NAMESPACE_END