Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.2KB

  1. //------------------------------------------------------------------------
  2. // Project : VST SDK
  3. //
  4. // Category : Helpers
  5. // Filename : public.sdk/source/vst/auwrapper/NSDataIBStream.mm
  6. // Created by : Steinberg, 12/2007
  7. // Description : VST 3 -> AU Wrapper
  8. //
  9. //-----------------------------------------------------------------------------
  10. // LICENSE
  11. // (c) 2017, Steinberg Media Technologies GmbH, All Rights Reserved
  12. //-----------------------------------------------------------------------------
  13. // Redistribution and use in source and binary forms, with or without modification,
  14. // are permitted provided that the following conditions are met:
  15. //
  16. // * Redistributions of source code must retain the above copyright notice,
  17. // this list of conditions and the following disclaimer.
  18. // * Redistributions in binary form must reproduce the above copyright notice,
  19. // this list of conditions and the following disclaimer in the documentation
  20. // and/or other materials provided with the distribution.
  21. // * Neither the name of the Steinberg Media Technologies nor the names of its
  22. // contributors may be used to endorse or promote products derived from this
  23. // software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  26. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  34. // OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //-----------------------------------------------------------------------------
  36. /// \cond ignore
  37. #include "NSDataIBStream.h"
  38. #include "pluginterfaces/vst/ivstattributes.h"
  39. #include <algorithm>
  40. #if __clang__
  41. #if __has_feature(objc_arc) && __clang_major__ >= 3
  42. #define ARC_ENABLED 1
  43. #endif // __has_feature(objc_arc)
  44. #endif // __clang__
  45. namespace Steinberg {
  46. namespace Vst {
  47. DEF_CLASS_IID(IStreamAttributes);
  48. //------------------------------------------------------------------------
  49. NSDataIBStream::NSDataIBStream (NSData* data, bool hideAttributes)
  50. : data (data)
  51. , currentPos (0)
  52. , hideAttributes (hideAttributes)
  53. {
  54. FUNKNOWN_CTOR
  55. #if !ARC_ENABLED
  56. [data retain];
  57. #endif
  58. }
  59. //------------------------------------------------------------------------
  60. NSDataIBStream::~NSDataIBStream ()
  61. {
  62. #if !ARC_ENABLED
  63. [data release];
  64. #endif
  65. FUNKNOWN_DTOR
  66. }
  67. //------------------------------------------------------------------------
  68. IMPLEMENT_REFCOUNT (NSDataIBStream)
  69. //------------------------------------------------------------------------
  70. tresult PLUGIN_API NSDataIBStream::queryInterface (const TUID iid, void** obj)
  71. {
  72. QUERY_INTERFACE (iid, obj, FUnknown::iid, IBStream)
  73. QUERY_INTERFACE (iid, obj, IBStream::iid, IBStream)
  74. if (!hideAttributes)
  75. QUERY_INTERFACE (iid, obj, IStreamAttributes::iid, IStreamAttributes)
  76. *obj = 0;
  77. return kNoInterface;
  78. }
  79. //------------------------------------------------------------------------
  80. tresult PLUGIN_API NSDataIBStream::read (void* buffer, int32 numBytes, int32* numBytesRead)
  81. {
  82. int32 useBytes = std::min (numBytes, (int32)([data length] - currentPos));
  83. if (useBytes > 0)
  84. {
  85. [data getBytes: buffer range: NSMakeRange (currentPos, useBytes)];
  86. if (numBytesRead)
  87. *numBytesRead = useBytes;
  88. currentPos += useBytes;
  89. return kResultTrue;
  90. }
  91. return kResultFalse;
  92. }
  93. //------------------------------------------------------------------------
  94. tresult PLUGIN_API NSDataIBStream::write (void* buffer, int32 numBytes, int32* numBytesWritten)
  95. {
  96. return kResultFalse;
  97. }
  98. //------------------------------------------------------------------------
  99. tresult PLUGIN_API NSDataIBStream::seek (int64 pos, int32 mode, int64* result)
  100. {
  101. switch (mode)
  102. {
  103. case kIBSeekSet:
  104. {
  105. if (pos <= [data length])
  106. {
  107. currentPos = pos;
  108. if (result)
  109. tell (result);
  110. return kResultTrue;
  111. }
  112. break;
  113. }
  114. case kIBSeekCur:
  115. {
  116. if (currentPos + pos <= [data length])
  117. {
  118. currentPos += pos;
  119. if (result)
  120. tell (result);
  121. return kResultTrue;
  122. }
  123. break;
  124. }
  125. case kIBSeekEnd:
  126. {
  127. if ([data length] + pos <= [data length])
  128. {
  129. currentPos = [data length] + pos;
  130. if (result)
  131. tell (result);
  132. return kResultTrue;
  133. }
  134. break;
  135. }
  136. }
  137. return kResultFalse;
  138. }
  139. //------------------------------------------------------------------------
  140. tresult PLUGIN_API NSDataIBStream::tell (int64* pos)
  141. {
  142. if (pos)
  143. {
  144. *pos = currentPos;
  145. return kResultTrue;
  146. }
  147. return kResultFalse;
  148. }
  149. //------------------------------------------------------------------------
  150. tresult PLUGIN_API NSDataIBStream::getFileName (String128 name)
  151. {
  152. return kNotImplemented;
  153. }
  154. //------------------------------------------------------------------------
  155. IAttributeList* PLUGIN_API NSDataIBStream::getAttributes ()
  156. {
  157. return hideAttributes ? 0 : &attrList;
  158. }
  159. //------------------------------------------------------------------------
  160. //------------------------------------------------------------------------
  161. //------------------------------------------------------------------------
  162. NSMutableDataIBStream::NSMutableDataIBStream (NSMutableData* data)
  163. : NSDataIBStream (data, true)
  164. , mdata (data)
  165. {
  166. }
  167. //------------------------------------------------------------------------
  168. NSMutableDataIBStream::~NSMutableDataIBStream ()
  169. {
  170. [mdata setLength:currentPos];
  171. }
  172. //------------------------------------------------------------------------
  173. tresult PLUGIN_API NSMutableDataIBStream::write (void* buffer, int32 numBytes, int32* numBytesWritten)
  174. {
  175. [mdata replaceBytesInRange:NSMakeRange (currentPos, numBytes) withBytes:buffer];
  176. if (numBytesWritten)
  177. *numBytesWritten = numBytes;
  178. currentPos += numBytes;
  179. return kResultTrue;
  180. }
  181. //------------------------------------------------------------------------
  182. } // namespace Vst
  183. } // namespace Steinberg
  184. /// \endcond