The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

542 lines
24KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #ifndef DOXYGEN
  20. namespace SampleTypeHelpers // Internal classes needed for handling sample type classes
  21. {
  22. template <typename Container> struct ElementType { using Type = typename Container::value_type; };
  23. template <> struct ElementType<float> { using Type = float; };
  24. template <> struct ElementType<double> { using Type = double; };
  25. template <> struct ElementType<long double> { using Type = long double; };
  26. }
  27. #endif
  28. //==============================================================================
  29. /**
  30. Minimal and lightweight data-structure which contains a list of pointers to
  31. channels containing some kind of sample data.
  32. This class doesn't own any of the data which it points to, it's simply a view
  33. into data that is owned elsewhere. You can construct one from some raw data
  34. that you've allocated yourself, or give it a HeapBlock to use, or give it
  35. an AudioSampleBuffer which it can refer to, but in all cases the user is
  36. responsible for making sure that the data doesn't get deleted while there's
  37. still an AudioBlock using it.
  38. */
  39. template <typename SampleType>
  40. class AudioBlock
  41. {
  42. public:
  43. //==============================================================================
  44. using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
  45. //==============================================================================
  46. /** Create a zero-sized AudioBlock. */
  47. forcedinline AudioBlock() noexcept {}
  48. /** Creates an AudioBlock from a pointer to an array of channels.
  49. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  50. Therefore it is the user's responsibility to ensure that the memory is retained
  51. throughout the life-time of the AudioBlock and released when no longer needed.
  52. */
  53. forcedinline AudioBlock (SampleType* const* channelData,
  54. size_t numberOfChannels, size_t numberOfSamples) noexcept
  55. : channels (channelData),
  56. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  57. numSamples (numberOfSamples)
  58. {
  59. }
  60. /** Creates an AudioBlock from a pointer to an array of channels.
  61. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  62. Therefore it is the user's responsibility to ensure that the memory is retained
  63. throughout the life-time of the AudioBlock and released when no longer needed.
  64. */
  65. forcedinline AudioBlock (SampleType* const* channelData, size_t numberOfChannels,
  66. size_t startSampleIndex, size_t numberOfSamples) noexcept
  67. : channels (channelData),
  68. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  69. startSample (startSampleIndex),
  70. numSamples (numberOfSamples)
  71. {
  72. }
  73. /** Allocates a suitable amount of space in a HeapBlock, and initialises this object
  74. to point into it.
  75. The HeapBlock must of course not be freed or re-allocated while this object is still in
  76. use, because it will be referencing its data.
  77. */
  78. AudioBlock (HeapBlock<char>& heapBlockToUseForAllocation,
  79. size_t numberOfChannels, size_t numberOfSamples) noexcept
  80. : numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  81. numSamples (numberOfSamples)
  82. {
  83. auto roundedUpNumSamples = (numberOfSamples + elementMask) & ~elementMask;
  84. auto channelSize = sizeof (SampleType) * roundedUpNumSamples;
  85. auto channelListBytes = sizeof (SampleType*) * numberOfChannels;
  86. auto extraBytes = sizeof (SampleType) - 1;
  87. heapBlockToUseForAllocation.malloc (channelListBytes + extraBytes + channelSize * numberOfChannels);
  88. auto* chanArray = reinterpret_cast<SampleType**> (heapBlockToUseForAllocation.getData());
  89. channels = chanArray;
  90. auto* data = reinterpret_cast<SampleType*> (addBytesToPointer (chanArray, channelListBytes));
  91. data = snapPointerToAlignment (data, sizeof (SampleType));
  92. for (ChannelCountType i = 0; i < numChannels; ++i)
  93. {
  94. chanArray[i] = data;
  95. data += roundedUpNumSamples;
  96. }
  97. }
  98. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  99. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  100. Therefore it is the user's responsibility to ensure that the buffer is retained
  101. throughout the life-time of the AudioBlock without being modified.
  102. */
  103. AudioBlock (AudioBuffer<SampleType>& buffer) noexcept
  104. : channels (buffer.getArrayOfWritePointers()),
  105. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  106. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  107. {
  108. }
  109. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  110. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  111. Therefore it is the user's responsibility to ensure that the buffer is retained
  112. throughout the life-time of the AudioBlock without being modified.
  113. */
  114. AudioBlock (AudioBuffer<SampleType>& buffer, size_t startSampleIndex) noexcept
  115. : channels (buffer.getArrayOfWritePointers()),
  116. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  117. startSample (startSampleIndex),
  118. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  119. {
  120. jassert (startSample < numSamples);
  121. }
  122. AudioBlock (const AudioBlock& other) noexcept = default;
  123. AudioBlock& operator= (const AudioBlock& other) noexcept = default;
  124. //==============================================================================
  125. forcedinline size_t getNumSamples() const noexcept { return numSamples; }
  126. forcedinline size_t getNumChannels() const noexcept { return static_cast<size_t> (numChannels); }
  127. /** Returns a raw pointer into one of the channels in this block. */
  128. forcedinline const SampleType* getChannelPointer (size_t channel) const noexcept
  129. {
  130. jassert (channel < numChannels);
  131. jassert (numSamples > 0);
  132. return *(channels + channel) + startSample;
  133. }
  134. /** Returns a raw pointer into one of the channels in this block. */
  135. forcedinline SampleType* getChannelPointer (size_t channel) noexcept
  136. {
  137. jassert (channel < numChannels);
  138. jassert (numSamples > 0);
  139. return *(channels + channel) + startSample;
  140. }
  141. /** Returns an AudioBlock that represents one of the channels in this block. */
  142. forcedinline AudioBlock<SampleType> getSingleChannelBlock (size_t channel) const noexcept
  143. {
  144. jassert (channel < numChannels);
  145. return AudioBlock (channels + channel, 1, startSample, numSamples);
  146. }
  147. /** Returns a subset of continguous channels
  148. @param channelStart First channel of the subset
  149. @param numChannelsToUse Count of channels in the subset
  150. */
  151. forcedinline AudioBlock<SampleType> getSubsetChannelBlock (size_t channelStart, size_t numChannelsToUse) noexcept
  152. {
  153. jassert (channelStart < numChannels);
  154. jassert ((channelStart + numChannelsToUse) <= numChannels);
  155. return AudioBlock (channels + channelStart, numChannelsToUse, startSample, numSamples);
  156. }
  157. //==============================================================================
  158. /** Clear the memory described by this AudioBlock. */
  159. forcedinline AudioBlock& clear() noexcept
  160. {
  161. auto n = static_cast<int> (numSamples * sizeFactor);
  162. for (size_t ch = 0; ch < numChannels; ++ch)
  163. FloatVectorOperations::clear (channelPtr (ch), n);
  164. return *this;
  165. }
  166. /** Fill memory with value. */
  167. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE fill (SampleType value) noexcept
  168. {
  169. auto n = static_cast<int> (numSamples * sizeFactor);
  170. for (size_t ch = 0; ch < numChannels; ++ch)
  171. FloatVectorOperations::fill (channelPtr (ch), value, n);
  172. return *this;
  173. }
  174. /** Copy the values in src to the receiver. */
  175. forcedinline AudioBlock& copy (const AudioBlock& src) noexcept
  176. {
  177. auto maxChannels = jmin (src.numChannels, numChannels);
  178. auto n = static_cast<int> (jmin (src.numSamples, numSamples) * sizeFactor);
  179. for (size_t ch = 0; ch < maxChannels; ++ch)
  180. FloatVectorOperations::copy (channelPtr (ch), src.channelPtr (ch), n);
  181. return *this;
  182. }
  183. /** Move memory within the receiver from the position srcPos to the position dstPos.
  184. If numElements is not specified then move will move the maximum amount of memory.
  185. */
  186. forcedinline AudioBlock& move (size_t srcPos, size_t dstPos,
  187. size_t numElements = std::numeric_limits<size_t>::max()) noexcept
  188. {
  189. jassert (srcPos <= numSamples && dstPos <= numSamples);
  190. auto len = jmin (numSamples - srcPos, numSamples - dstPos, numElements) * sizeof (SampleType);
  191. if (len != 0)
  192. for (size_t ch = 0; ch < numChannels; ++ch)
  193. ::memmove (getChannelPointer (ch) + dstPos,
  194. getChannelPointer (ch) + srcPos, len);
  195. return *this;
  196. }
  197. //==============================================================================
  198. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  199. function does not copy the memory and you must ensure that the original memory
  200. pointed to by the receiver remains valid through-out the life-time of the
  201. returned sub-block.
  202. @param newOffset The index of an element inside the reciever which will
  203. will become the first element of the return value.
  204. @param newLength The number of elements of the newly created sub-block.
  205. */
  206. inline AudioBlock getSubBlock (size_t newOffset, size_t newLength) const noexcept
  207. {
  208. jassert (newOffset < numSamples);
  209. jassert (newOffset + newLength <= numSamples);
  210. return AudioBlock (channels, numChannels, startSample + newOffset, newLength);
  211. }
  212. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  213. function does not copy the memory and you must ensure that the original memory
  214. pointed to by the receiver remains valid through-out the life-time of the
  215. returned sub-block.
  216. @param newOffset The index of an element inside the reciever which will
  217. will become the first element of the return value.
  218. The return value will include all subsequent elements
  219. of the receiver.
  220. */
  221. inline AudioBlock getSubBlock (size_t newOffset) const noexcept
  222. {
  223. return getSubBlock (newOffset, getNumSamples() - newOffset);
  224. }
  225. //==============================================================================
  226. /** Adds a fixed value to the receiver. */
  227. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (SampleType value) noexcept
  228. {
  229. auto n = static_cast<int> (numSamples * sizeFactor);
  230. for (size_t ch = 0; ch < numChannels; ++ch)
  231. FloatVectorOperations::add (channelPtr (ch), value, n);
  232. return *this;
  233. }
  234. /** Adds the source values to the receiver. */
  235. forcedinline AudioBlock& add (const AudioBlock& src) noexcept
  236. {
  237. jassert (numChannels == src.numChannels);
  238. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  239. for (size_t ch = 0; ch < numChannels; ++ch)
  240. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), n);
  241. return *this;
  242. }
  243. /** Adds a fixed value to each source value and stores it in the destination array of the receiver. */
  244. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (const AudioBlock& src, SampleType value) noexcept
  245. {
  246. jassert (numChannels == src.numChannels);
  247. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  248. for (size_t ch = 0; ch < numChannels; ++ch)
  249. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), value, n);
  250. return *this;
  251. }
  252. /** Adds each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  253. forcedinline AudioBlock& add (const AudioBlock& src1, const AudioBlock& src2) noexcept
  254. {
  255. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  256. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  257. for (size_t ch = 0; ch < numChannels; ++ch)
  258. FloatVectorOperations::add (channelPtr (ch), src1.channelPtr (ch), src2.getChannelPointer (ch), n);
  259. return *this;
  260. }
  261. /** Subtracts a fixed value from the receiver. */
  262. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (SampleType value) noexcept
  263. {
  264. return add (value * static_cast<SampleType> (-1.0));
  265. }
  266. /** Subtracts the source values from the receiver. */
  267. forcedinline AudioBlock& subtract (const AudioBlock& src) noexcept
  268. {
  269. jassert (numChannels == src.numChannels);
  270. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  271. for (size_t ch = 0; ch < numChannels; ++ch)
  272. FloatVectorOperations::subtract (channelPtr (ch), src.channelPtr (ch), n);
  273. return *this;
  274. }
  275. /** Subtracts a fixed value from each source value and stores it in the destination array of the receiver. */
  276. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (const AudioBlock& src, SampleType value) noexcept
  277. {
  278. return add (src, static_cast<SampleType> (-1.0) * value);
  279. }
  280. /** Subtracts each source2 value from the corresponding source1 value and stores it in the destination array of the receiver. */
  281. forcedinline AudioBlock& subtract (const AudioBlock& src1, const AudioBlock& src2) noexcept
  282. {
  283. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  284. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  285. for (size_t ch = 0; ch < numChannels; ++ch)
  286. FloatVectorOperations::subtract (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  287. return *this;
  288. }
  289. /** Multiplies a fixed value to the receiver. */
  290. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (SampleType value) noexcept
  291. {
  292. auto n = static_cast<int> (numSamples * sizeFactor);
  293. for (size_t ch = 0; ch < numChannels; ++ch)
  294. FloatVectorOperations::multiply (channelPtr (ch), value, n);
  295. return *this;
  296. }
  297. /** Multiplies the source values to the receiver. */
  298. forcedinline AudioBlock& multiply (const AudioBlock& src) noexcept
  299. {
  300. jassert (numChannels == src.numChannels);
  301. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  302. for (size_t ch = 0; ch < numChannels; ++ch)
  303. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), n);
  304. return *this;
  305. }
  306. /** Multiplies a fixed value to each source value and stores it in the destination array of the receiver. */
  307. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (const AudioBlock& src, SampleType value) noexcept
  308. {
  309. jassert (numChannels == src.numChannels);
  310. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  311. for (size_t ch = 0; ch < numChannels; ++ch)
  312. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), value, n);
  313. return *this;
  314. }
  315. /** Multiplies each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  316. forcedinline AudioBlock& multiply (const AudioBlock& src1, const AudioBlock& src2) noexcept
  317. {
  318. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  319. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  320. for (size_t ch = 0; ch < numChannels; ++ch)
  321. FloatVectorOperations::multiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  322. return *this;
  323. }
  324. /** Multiplies each value in src with factor and adds the result to the receiver. */
  325. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE addWithMultiply (const AudioBlock& src, SampleType factor) noexcept
  326. {
  327. jassert (numChannels == src.numChannels);
  328. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  329. for (size_t ch = 0; ch < numChannels; ++ch)
  330. FloatVectorOperations::addWithMultiply (channelPtr (ch), src.channelPtr (ch), factor, n);
  331. return *this;
  332. }
  333. /** Multiplies each value in srcA with the corresponding value in srcB and adds the result to the receiver. */
  334. forcedinline AudioBlock& addWithMultiply (const AudioBlock& src1, const AudioBlock& src2) noexcept
  335. {
  336. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  337. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  338. for (size_t ch = 0; ch < numChannels; ++ch)
  339. FloatVectorOperations::addWithMultiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  340. return *this;
  341. }
  342. /* negates each value of the receiver */
  343. forcedinline AudioBlock& negate() noexcept
  344. {
  345. return multiply (static_cast<SampleType> (-1.0));
  346. }
  347. /* Negates each value of source and stores it in the receiver */
  348. forcedinline AudioBlock& replaceWithNegativeOf (const AudioBlock& src) noexcept
  349. {
  350. jassert (numChannels == src.numChannels);
  351. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  352. for (size_t ch = 0; ch < numChannels; ++ch)
  353. FloatVectorOperations::negate (channelPtr (ch), src.channelPtr (ch), n);
  354. return *this;
  355. }
  356. /* takes the absolute value of each element of src and stores it inside the receiver. */
  357. forcedinline AudioBlock& replaceWithAbsoluteValueOf (const AudioBlock& src) noexcept
  358. {
  359. jassert (numChannels == src.numChannels);
  360. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  361. for (size_t ch = 0; ch < numChannels; ++ch)
  362. FloatVectorOperations::abs (channelPtr (ch), src.channelPtr (ch), n);
  363. return *this;
  364. }
  365. /** Each element of receiver will be the minimum of the corresponding element of the source arrays. */
  366. forcedinline AudioBlock& min (const AudioBlock& src1, const AudioBlock& src2) noexcept
  367. {
  368. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  369. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  370. for (size_t ch = 0; ch < numChannels; ++ch)
  371. FloatVectorOperations::min (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  372. return *this;
  373. }
  374. /** Each element of receiver will be the maximum of the corresponding element of the source arrays. */
  375. forcedinline AudioBlock& max (AudioBlock src1, AudioBlock src2) noexcept
  376. {
  377. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  378. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  379. for (size_t ch = 0; ch < numChannels; ++ch)
  380. FloatVectorOperations::max (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  381. return *this;
  382. }
  383. /** Find minimum and maximum value of the buffer. */
  384. forcedinline Range<NumericType> findMinAndMax() const noexcept
  385. {
  386. Range<NumericType> minmax;
  387. auto n = static_cast<int> (numSamples * sizeFactor);
  388. for (size_t ch = 0; ch < numChannels; ++ch)
  389. minmax = minmax.getUnionWith (FloatVectorOperations::findMinAndMax (channelPtr (ch), n));
  390. return minmax;
  391. }
  392. //==============================================================================
  393. // convenient operator wrappers
  394. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (SampleType src) noexcept { return add (src); }
  395. forcedinline AudioBlock& operator+= (AudioBlock src) noexcept { return add (src); }
  396. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (SampleType src) noexcept { return subtract (src); }
  397. forcedinline AudioBlock& operator-= (AudioBlock src) noexcept { return subtract (src); }
  398. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (SampleType src) noexcept { return multiply (src); }
  399. forcedinline AudioBlock& operator*= (AudioBlock src) noexcept { return multiply (src); }
  400. //==============================================================================
  401. // This class can only be used with floating point types
  402. static_assert (std::is_same<SampleType, float>::value || std::is_same<SampleType, double>::value
  403. #if JUCE_USE_SIMD
  404. || std::is_same<SampleType, SIMDRegister<float> >::value || std::is_same<SampleType, SIMDRegister<double> >::value
  405. #endif
  406. , "AudioBlock only supports single or double precision floating point types");
  407. //==============================================================================
  408. template <typename FunctionType>
  409. static void process (const AudioBlock<SampleType>& inBlock,
  410. AudioBlock<SampleType>& outBlock,
  411. const FunctionType& function)
  412. {
  413. auto len = inBlock.getNumSamples();
  414. auto numChans = inBlock.getNumChannels();
  415. for (ChannelCountType c = 0; c < numChans; ++c)
  416. {
  417. auto* src = inBlock.getChannelPointer(c);
  418. auto* dst = outBlock.getChannelPointer(c);
  419. for (size_t i = 0; i < len; ++i)
  420. dst[i] = function (src[i]);
  421. }
  422. }
  423. private:
  424. //==============================================================================
  425. NumericType* channelPtr (size_t ch) noexcept { return reinterpret_cast<NumericType*> (getChannelPointer (ch)); }
  426. const NumericType* channelPtr (size_t ch) const noexcept { return reinterpret_cast<const NumericType*> (getChannelPointer (ch)); }
  427. //==============================================================================
  428. using ChannelCountType = unsigned int;
  429. //==============================================================================
  430. static constexpr size_t sizeFactor = sizeof (SampleType) / sizeof (NumericType);
  431. static constexpr size_t elementMask = sizeFactor - 1;
  432. static constexpr size_t byteMask = (sizeFactor * sizeof (NumericType)) - 1;
  433. SampleType* const* channels;
  434. ChannelCountType numChannels = 0;
  435. size_t startSample = 0, numSamples = 0;
  436. };