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.

893 lines
47KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace dsp
  21. {
  22. #ifndef DOXYGEN
  23. namespace SampleTypeHelpers // Internal classes needed for handling sample type classes
  24. {
  25. template <typename T, bool = std::is_floating_point<T>::value>
  26. struct ElementType
  27. {
  28. using Type = T;
  29. };
  30. template <typename T>
  31. struct ElementType<T, false>
  32. {
  33. using Type = typename T::value_type;
  34. };
  35. }
  36. #endif
  37. //==============================================================================
  38. /**
  39. Minimal and lightweight data-structure which contains a list of pointers to
  40. channels containing some kind of sample data.
  41. This class doesn't own any of the data which it points to, it's simply a view
  42. into data that is owned elsewhere. You can construct one from some raw data
  43. that you've allocated yourself, or give it a HeapBlock to use, or give it
  44. an AudioBuffer which it can refer to, but in all cases the user is
  45. responsible for making sure that the data doesn't get deleted while there's
  46. still an AudioBlock using it.
  47. @tags{DSP}
  48. */
  49. template <typename SampleType>
  50. class AudioBlock
  51. {
  52. private:
  53. template <typename OtherSampleType>
  54. using MayUseConvertingConstructor =
  55. std::enable_if_t<std::is_same<std::remove_const_t<SampleType>,
  56. std::remove_const_t<OtherSampleType>>::value
  57. && std::is_const<SampleType>::value
  58. && ! std::is_const<OtherSampleType>::value,
  59. int>;
  60. public:
  61. //==============================================================================
  62. using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
  63. //==============================================================================
  64. /** Create a zero-sized AudioBlock. */
  65. AudioBlock() noexcept = default;
  66. /** Creates an AudioBlock from a pointer to an array of channels.
  67. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  68. Therefore it is the user's responsibility to ensure that the memory is retained
  69. throughout the life-time of the AudioBlock and released when no longer needed.
  70. */
  71. constexpr AudioBlock (SampleType* const* channelData,
  72. size_t numberOfChannels, size_t numberOfSamples) noexcept
  73. : channels (channelData),
  74. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  75. numSamples (numberOfSamples)
  76. {
  77. }
  78. /** Creates an AudioBlock from a pointer to an array of channels.
  79. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  80. Therefore it is the user's responsibility to ensure that the memory is retained
  81. throughout the life-time of the AudioBlock and released when no longer needed.
  82. */
  83. constexpr AudioBlock (SampleType* const* channelData, size_t numberOfChannels,
  84. size_t startSampleIndex, size_t numberOfSamples) noexcept
  85. : channels (channelData),
  86. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  87. startSample (startSampleIndex),
  88. numSamples (numberOfSamples)
  89. {
  90. }
  91. /** Allocates a suitable amount of space in a HeapBlock, and initialises this object
  92. to point into it.
  93. The HeapBlock must of course not be freed or re-allocated while this object is still in
  94. use, because it will be referencing its data.
  95. */
  96. AudioBlock (HeapBlock<char>& heapBlockToUseForAllocation,
  97. size_t numberOfChannels, size_t numberOfSamples,
  98. size_t alignmentInBytes = defaultAlignment) noexcept
  99. : numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  100. numSamples (numberOfSamples)
  101. {
  102. auto roundedUpNumSamples = (numberOfSamples + elementMask) & ~elementMask;
  103. auto channelSize = sizeof (SampleType) * roundedUpNumSamples;
  104. auto channelListBytes = sizeof (SampleType*) * numberOfChannels;
  105. auto extraBytes = alignmentInBytes - 1;
  106. heapBlockToUseForAllocation.malloc (channelListBytes + extraBytes + channelSize * numberOfChannels);
  107. auto* chanArray = unalignedPointerCast<SampleType**> (heapBlockToUseForAllocation.getData());
  108. channels = chanArray;
  109. auto* data = unalignedPointerCast<SampleType*> (addBytesToPointer (chanArray, channelListBytes));
  110. data = snapPointerToAlignment (data, alignmentInBytes);
  111. for (ChannelCountType i = 0; i < numChannels; ++i)
  112. {
  113. chanArray[i] = data;
  114. data += roundedUpNumSamples;
  115. }
  116. }
  117. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  118. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  119. Therefore it is the user's responsibility to ensure that the buffer is retained
  120. throughout the life-time of the AudioBlock without being modified.
  121. */
  122. template <typename OtherSampleType>
  123. constexpr AudioBlock (AudioBuffer<OtherSampleType>& buffer) noexcept
  124. : channels (buffer.getArrayOfWritePointers()),
  125. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  126. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  127. {
  128. }
  129. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  130. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  131. Therefore it is the user's responsibility to ensure that the buffer is retained
  132. throughout the life-time of the AudioBlock without being modified.
  133. */
  134. template <typename OtherSampleType>
  135. constexpr AudioBlock (const AudioBuffer<OtherSampleType>& buffer) noexcept
  136. : channels (buffer.getArrayOfReadPointers()),
  137. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  138. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  139. {
  140. }
  141. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  142. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  143. Therefore it is the user's responsibility to ensure that the buffer is retained
  144. throughout the life-time of the AudioBlock without being modified.
  145. */
  146. template <typename OtherSampleType>
  147. AudioBlock (AudioBuffer<OtherSampleType>& buffer, size_t startSampleIndex) noexcept
  148. : channels (buffer.getArrayOfWritePointers()),
  149. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  150. startSample (startSampleIndex),
  151. numSamples (static_cast<size_t> (buffer.getNumSamples()) - startSampleIndex)
  152. {
  153. jassert (startSample < static_cast<size_t> (buffer.getNumSamples()));
  154. }
  155. AudioBlock (const AudioBlock& other) noexcept = default;
  156. AudioBlock& operator= (const AudioBlock& other) noexcept = default;
  157. template <typename OtherSampleType, MayUseConvertingConstructor<OtherSampleType> = 0>
  158. AudioBlock (const AudioBlock<OtherSampleType>& other) noexcept
  159. : channels { other.channels },
  160. numChannels { other.numChannels },
  161. startSample { other.startSample },
  162. numSamples { other.numSamples }
  163. {
  164. }
  165. template <typename OtherSampleType, MayUseConvertingConstructor<OtherSampleType> = 0>
  166. AudioBlock& operator= (const AudioBlock<OtherSampleType>& other) noexcept
  167. {
  168. AudioBlock blockCopy { other };
  169. swap (blockCopy);
  170. return *this;
  171. }
  172. void swap (AudioBlock& other) noexcept
  173. {
  174. std::swap (other.channels, channels);
  175. std::swap (other.numChannels, numChannels);
  176. std::swap (other.startSample, startSample);
  177. std::swap (other.numSamples, numSamples);
  178. }
  179. //==============================================================================
  180. template <typename OtherSampleType>
  181. constexpr bool operator== (const AudioBlock<OtherSampleType>& other) const noexcept
  182. {
  183. return std::equal (channels,
  184. channels + numChannels,
  185. other.channels,
  186. other.channels + other.numChannels)
  187. && startSample == other.startSample
  188. && numSamples == other.numSamples;
  189. }
  190. template <typename OtherSampleType>
  191. constexpr bool operator!= (const AudioBlock<OtherSampleType>& other) const noexcept
  192. {
  193. return ! (*this == other);
  194. }
  195. //==============================================================================
  196. /** Returns the number of channels referenced by this block. */
  197. constexpr size_t getNumChannels() const noexcept { return static_cast<size_t> (numChannels); }
  198. /** Returns the number of samples referenced by this block. */
  199. constexpr size_t getNumSamples() const noexcept { return numSamples; }
  200. /** Returns a raw pointer into one of the channels in this block. */
  201. SampleType* getChannelPointer (size_t channel) const noexcept
  202. {
  203. jassert (channel < numChannels);
  204. jassert (numSamples > 0);
  205. return channels[channel] + startSample;
  206. }
  207. /** Returns an AudioBlock that represents one of the channels in this block. */
  208. AudioBlock getSingleChannelBlock (size_t channel) const noexcept
  209. {
  210. jassert (channel < numChannels);
  211. return AudioBlock (channels + channel, 1, startSample, numSamples);
  212. }
  213. /** Returns a subset of contiguous channels
  214. @param channelStart First channel of the subset
  215. @param numChannelsToUse Count of channels in the subset
  216. */
  217. AudioBlock getSubsetChannelBlock (size_t channelStart, size_t numChannelsToUse) const noexcept
  218. {
  219. jassert (channelStart < numChannels);
  220. jassert ((channelStart + numChannelsToUse) <= numChannels);
  221. return AudioBlock (channels + channelStart, numChannelsToUse, startSample, numSamples);
  222. }
  223. /** Returns a sample from the buffer.
  224. The channel and index are not checked - they are expected to be in-range. If not,
  225. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  226. territory.
  227. */
  228. SampleType getSample (int channel, int sampleIndex) const noexcept
  229. {
  230. jassert (isPositiveAndBelow (channel, numChannels));
  231. jassert (isPositiveAndBelow (sampleIndex, numSamples));
  232. return channels[channel][(size_t) startSample + (size_t) sampleIndex];
  233. }
  234. /** Modifies a sample in the buffer.
  235. The channel and index are not checked - they are expected to be in-range. If not,
  236. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  237. territory.
  238. */
  239. void setSample (int destChannel, int destSample, SampleType newValue) const noexcept
  240. {
  241. jassert (isPositiveAndBelow (destChannel, numChannels));
  242. jassert (isPositiveAndBelow (destSample, numSamples));
  243. channels[destChannel][(size_t) startSample + (size_t) destSample] = newValue;
  244. }
  245. /** Adds a value to a sample in the buffer.
  246. The channel and index are not checked - they are expected to be in-range. If not,
  247. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  248. territory.
  249. */
  250. void addSample (int destChannel, int destSample, SampleType valueToAdd) const noexcept
  251. {
  252. jassert (isPositiveAndBelow (destChannel, numChannels));
  253. jassert (isPositiveAndBelow (destSample, numSamples));
  254. channels[destChannel][(size_t) startSample + (size_t) destSample] += valueToAdd;
  255. }
  256. //==============================================================================
  257. /** Clears the memory referenced by this AudioBlock. */
  258. AudioBlock& clear() noexcept { clearInternal(); return *this; }
  259. const AudioBlock& clear() const noexcept { clearInternal(); return *this; }
  260. /** Fills the memory referenced by this AudioBlock with value. */
  261. AudioBlock& JUCE_VECTOR_CALLTYPE fill (NumericType value) noexcept { fillInternal (value); return *this; }
  262. const AudioBlock& JUCE_VECTOR_CALLTYPE fill (NumericType value) const noexcept { fillInternal (value); return *this; }
  263. /** Copies the values in src to this block. */
  264. template <typename OtherSampleType>
  265. AudioBlock& copyFrom (const AudioBlock<OtherSampleType>& src) noexcept { copyFromInternal (src); return *this; }
  266. template <typename OtherSampleType>
  267. const AudioBlock& copyFrom (const AudioBlock<OtherSampleType>& src) const noexcept { copyFromInternal (src); return *this; }
  268. /** Copy the values from an AudioBuffer to this block.
  269. All indices and sizes are in this AudioBlock's units, i.e. if SampleType is a
  270. SIMDRegister then incrementing srcPos by one will increase the sample position
  271. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  272. */
  273. template <typename OtherNumericType>
  274. AudioBlock& copyFrom (const AudioBuffer<OtherNumericType>& src,
  275. size_t srcPos = 0, size_t dstPos = 0,
  276. size_t numElements = std::numeric_limits<size_t>::max()) { copyFromInternal (src, srcPos, dstPos, numElements); return *this; }
  277. template <typename OtherNumericType>
  278. const AudioBlock& copyFrom (const AudioBuffer<OtherNumericType>& src,
  279. size_t srcPos = 0, size_t dstPos = 0,
  280. size_t numElements = std::numeric_limits<size_t>::max()) const { copyFromInternal (src, srcPos, dstPos, numElements); return *this; }
  281. /** Copies the values from this block to an AudioBuffer.
  282. All indices and sizes are in this AudioBlock's units, i.e. if SampleType is a
  283. SIMDRegister then incrementing dstPos by one will increase the sample position
  284. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  285. */
  286. void copyTo (AudioBuffer<typename std::remove_const<NumericType>::type>& dst, size_t srcPos = 0, size_t dstPos = 0,
  287. size_t numElements = std::numeric_limits<size_t>::max()) const
  288. {
  289. auto dstlen = static_cast<size_t> (dst.getNumSamples()) / sizeFactor;
  290. auto n = static_cast<int> (jmin (numSamples - srcPos, dstlen - dstPos, numElements) * sizeFactor);
  291. auto maxChannels = jmin (static_cast<size_t> (dst.getNumChannels()), static_cast<size_t> (numChannels));
  292. for (size_t ch = 0; ch < maxChannels; ++ch)
  293. FloatVectorOperations::copy (dst.getWritePointer (static_cast<int> (ch),
  294. static_cast<int> (dstPos * sizeFactor)),
  295. getDataPointer (ch) + (srcPos * sizeFactor),
  296. n);
  297. }
  298. /** Move memory within this block from the position srcPos to the position dstPos.
  299. If numElements is not specified then move will move the maximum amount of memory.
  300. */
  301. AudioBlock& move (size_t srcPos, size_t dstPos,
  302. size_t numElements = std::numeric_limits<size_t>::max()) noexcept { moveInternal (srcPos, dstPos, numElements); return *this; }
  303. const AudioBlock& move (size_t srcPos, size_t dstPos,
  304. size_t numElements = std::numeric_limits<size_t>::max()) const noexcept { moveInternal (srcPos, dstPos, numElements); return *this; }
  305. //==============================================================================
  306. /** Return a new AudioBlock pointing to a sub-block inside this block. This
  307. function does not copy the memory and you must ensure that the original memory
  308. pointed to by the receiver remains valid through-out the life-time of the
  309. returned sub-block.
  310. @param newOffset The index of an element inside the receiver which will
  311. will become the first element of the return value.
  312. @param newLength The number of elements of the newly created sub-block.
  313. */
  314. AudioBlock getSubBlock (size_t newOffset, size_t newLength) const noexcept
  315. {
  316. jassert (newOffset < numSamples);
  317. jassert (newOffset + newLength <= numSamples);
  318. return AudioBlock (channels, numChannels, startSample + newOffset, newLength);
  319. }
  320. /** Return a new AudioBlock pointing to a sub-block inside this block. This
  321. function does not copy the memory and you must ensure that the original memory
  322. pointed to by the receiver remains valid through-out the life-time of the
  323. returned sub-block.
  324. @param newOffset The index of an element inside the block which will
  325. will become the first element of the return value.
  326. The return value will include all subsequent elements
  327. of the receiver.
  328. */
  329. AudioBlock getSubBlock (size_t newOffset) const noexcept
  330. {
  331. return getSubBlock (newOffset, getNumSamples() - newOffset);
  332. }
  333. //==============================================================================
  334. /** Adds a fixed value to the elements in this block. */
  335. AudioBlock& JUCE_VECTOR_CALLTYPE add (NumericType value) noexcept { addInternal (value); return *this; }
  336. const AudioBlock& JUCE_VECTOR_CALLTYPE add (NumericType value) const noexcept { addInternal (value); return *this; }
  337. /** Adds the elements in the src block to the elements in this block. */
  338. template <typename OtherSampleType>
  339. AudioBlock& add (AudioBlock<OtherSampleType> src) noexcept { addInternal (src); return *this; }
  340. template <typename OtherSampleType>
  341. const AudioBlock& add (AudioBlock<OtherSampleType> src) const noexcept { addInternal (src); return *this; }
  342. /** Adds a fixed value to each source value and replaces the contents of this block. */
  343. template <typename OtherSampleType>
  344. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithSumOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithSumOfInternal (src, value); return *this; }
  345. template <typename OtherSampleType>
  346. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithSumOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithSumOfInternal (src, value); return *this; }
  347. /** Adds each source1 value to the corresponding source2 value and replaces the contents of this block. */
  348. template <typename Src1SampleType, typename Src2SampleType>
  349. AudioBlock& replaceWithSumOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithSumOfInternal (src1, src2); return *this; }
  350. template <typename Src1SampleType, typename Src2SampleType>
  351. const AudioBlock& replaceWithSumOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithSumOfInternal (src1, src2); return *this; }
  352. //==============================================================================
  353. /** Subtracts a fixed value from the elements in this block. */
  354. AudioBlock& JUCE_VECTOR_CALLTYPE subtract (NumericType value) noexcept { subtractInternal (value); return *this; }
  355. const AudioBlock& JUCE_VECTOR_CALLTYPE subtract (NumericType value) const noexcept { subtractInternal (value); return *this; }
  356. /** Subtracts the source values from the elements in this block. */
  357. template <typename OtherSampleType>
  358. AudioBlock& subtract (AudioBlock<OtherSampleType> src) noexcept { subtractInternal (src); return *this; }
  359. template <typename OtherSampleType>
  360. const AudioBlock& subtract (AudioBlock<OtherSampleType> src) const noexcept { subtractInternal (src); return *this; }
  361. /** Subtracts a fixed value from each source value and replaces the contents of this block. */
  362. template <typename OtherSampleType>
  363. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithDifferenceOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithDifferenceOfInternal (src, value); return *this; }
  364. template <typename OtherSampleType>
  365. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithDifferenceOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithDifferenceOfInternal (src, value); return *this; }
  366. /** Subtracts each source2 value from the corresponding source1 value and replaces the contents of this block. */
  367. template <typename Src1SampleType, typename Src2SampleType>
  368. AudioBlock& replaceWithDifferenceOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithDifferenceOfInternal (src1, src2); return *this; }
  369. template <typename Src1SampleType, typename Src2SampleType>
  370. const AudioBlock& replaceWithDifferenceOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithDifferenceOfInternal (src1, src2); return *this; }
  371. //==============================================================================
  372. /** Multiplies the elements in this block by a fixed value. */
  373. AudioBlock& JUCE_VECTOR_CALLTYPE multiplyBy (NumericType value) noexcept { multiplyByInternal (value); return *this; }
  374. const AudioBlock& JUCE_VECTOR_CALLTYPE multiplyBy (NumericType value) const noexcept { multiplyByInternal (value); return *this; }
  375. /** Multiplies the elements in this block by the elements in the src block */
  376. template <typename OtherSampleType>
  377. AudioBlock& multiplyBy (AudioBlock<OtherSampleType> src) noexcept { multiplyByInternal (src); return *this; }
  378. template <typename OtherSampleType>
  379. const AudioBlock& multiplyBy (AudioBlock<OtherSampleType> src) const noexcept { multiplyByInternal (src); return *this; }
  380. /** Replaces the elements in this block with the product of the elements in the source src block and a fixed value. */
  381. template <typename OtherSampleType>
  382. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithProductOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithProductOfInternal (src, value); return *this; }
  383. template <typename OtherSampleType>
  384. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithProductOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithProductOfInternal (src, value); return *this; }
  385. /** Replaces the elements in this block with the product of the elements in the src1 and scr2 blocks. */
  386. template <typename Src1SampleType, typename Src2SampleType>
  387. AudioBlock& replaceWithProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithProductOfInternal (src1, src2); return *this; }
  388. template <typename Src1SampleType, typename Src2SampleType>
  389. const AudioBlock& replaceWithProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithProductOfInternal (src1, src2); return *this; }
  390. //==============================================================================
  391. /** Multiplies each channels of this block by a smoothly changing value. */
  392. template <typename OtherSampleType, typename SmoothingType>
  393. AudioBlock& multiplyBy (SmoothedValue<OtherSampleType, SmoothingType>& value) noexcept { multiplyByInternal (value); return *this; }
  394. template <typename OtherSampleType, typename SmoothingType>
  395. const AudioBlock& multiplyBy (SmoothedValue<OtherSampleType, SmoothingType>& value) const noexcept { multiplyByInternal (value); return *this; }
  396. /** Replaces each channel of this block with the product of the src block and a smoothed value. */
  397. template <typename BlockSampleType, typename SmootherSampleType, typename SmoothingType>
  398. AudioBlock& replaceWithProductOf (AudioBlock<BlockSampleType> src, SmoothedValue<SmootherSampleType, SmoothingType>& value) noexcept { replaceWithProductOfInternal (src, value); return *this; }
  399. template <typename BlockSampleType, typename SmootherSampleType, typename SmoothingType>
  400. const AudioBlock& replaceWithProductOf (AudioBlock<BlockSampleType> src, SmoothedValue<SmootherSampleType, SmoothingType>& value) const noexcept { replaceWithProductOfInternal (src, value); return *this; }
  401. //==============================================================================
  402. /** Multiplies each value in src by a fixed value and adds the result to this block. */
  403. template <typename OtherSampleType>
  404. AudioBlock& JUCE_VECTOR_CALLTYPE addProductOf (AudioBlock<OtherSampleType> src, NumericType factor) noexcept { addProductOfInternal (src, factor); return *this; }
  405. template <typename OtherSampleType>
  406. const AudioBlock& JUCE_VECTOR_CALLTYPE addProductOf (AudioBlock<OtherSampleType> src, NumericType factor) const noexcept { addProductOfInternal (src, factor); return *this; }
  407. /** Multiplies each value in srcA with the corresponding value in srcB and adds the result to this block. */
  408. template <typename Src1SampleType, typename Src2SampleType>
  409. AudioBlock& addProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { addProductOfInternal (src1, src2); return *this; }
  410. template <typename Src1SampleType, typename Src2SampleType>
  411. const AudioBlock& addProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { addProductOfInternal (src1, src2); return *this; }
  412. //==============================================================================
  413. /** Negates each value of this block. */
  414. AudioBlock& negate() noexcept { negateInternal(); return *this; }
  415. const AudioBlock& negate() const noexcept { negateInternal(); return *this; }
  416. /** Replaces the contents of this block with the negative of the values in the src block. */
  417. template <typename OtherSampleType>
  418. AudioBlock& replaceWithNegativeOf (AudioBlock<OtherSampleType> src) noexcept { replaceWithNegativeOfInternal (src); return *this; }
  419. template <typename OtherSampleType>
  420. const AudioBlock& replaceWithNegativeOf (AudioBlock<OtherSampleType> src) const noexcept { replaceWithNegativeOfInternal (src); return *this; }
  421. /** Replaces the contents of this block with the absolute values of the src block. */
  422. template <typename OtherSampleType>
  423. AudioBlock& replaceWithAbsoluteValueOf (AudioBlock<OtherSampleType> src) noexcept { replaceWithAbsoluteValueOfInternal (src); return *this; }
  424. template <typename OtherSampleType>
  425. const AudioBlock& replaceWithAbsoluteValueOf (AudioBlock<OtherSampleType> src) const noexcept { replaceWithAbsoluteValueOfInternal (src); return *this; }
  426. //==============================================================================
  427. /** Replaces each element of this block with the minimum of the corresponding element of the source arrays. */
  428. template <typename Src1SampleType, typename Src2SampleType>
  429. AudioBlock& replaceWithMinOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithMinOfInternal (src1, src2); return *this; }
  430. template <typename Src1SampleType, typename Src2SampleType>
  431. const AudioBlock& replaceWithMinOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithMinOfInternal (src1, src2); return *this; }
  432. /** Replaces each element of this block with the maximum of the corresponding element of the source arrays. */
  433. template <typename Src1SampleType, typename Src2SampleType>
  434. AudioBlock& replaceWithMaxOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithMaxOfInternal (src1, src2); return *this; }
  435. template <typename Src1SampleType, typename Src2SampleType>
  436. const AudioBlock& replaceWithMaxOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithMaxOfInternal (src1, src2); return *this; }
  437. //==============================================================================
  438. /** Finds the minimum and maximum value of the buffer. */
  439. Range<typename std::remove_const<NumericType>::type> findMinAndMax() const noexcept
  440. {
  441. if (numChannels == 0)
  442. return {};
  443. auto n = static_cast<int> (numSamples * sizeFactor);
  444. auto minmax = FloatVectorOperations::findMinAndMax (getDataPointer (0), n);
  445. for (size_t ch = 1; ch < numChannels; ++ch)
  446. minmax = minmax.getUnionWith (FloatVectorOperations::findMinAndMax (getDataPointer (ch), n));
  447. return minmax;
  448. }
  449. //==============================================================================
  450. // Convenient operator wrappers.
  451. AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (NumericType value) noexcept { return add (value); }
  452. const AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (NumericType value) const noexcept { return add (value); }
  453. AudioBlock& operator+= (AudioBlock src) noexcept { return add (src); }
  454. const AudioBlock& operator+= (AudioBlock src) const noexcept { return add (src); }
  455. AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (NumericType value) noexcept { return subtract (value); }
  456. const AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (NumericType value) const noexcept { return subtract (value); }
  457. AudioBlock& operator-= (AudioBlock src) noexcept { return subtract (src); }
  458. const AudioBlock& operator-= (AudioBlock src) const noexcept { return subtract (src); }
  459. AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (NumericType value) noexcept { return multiplyBy (value); }
  460. const AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (NumericType value) const noexcept { return multiplyBy (value); }
  461. AudioBlock& operator*= (AudioBlock src) noexcept { return multiplyBy (src); }
  462. const AudioBlock& operator*= (AudioBlock src) const noexcept { return multiplyBy (src); }
  463. template <typename OtherSampleType, typename SmoothingType>
  464. AudioBlock& operator*= (SmoothedValue<OtherSampleType, SmoothingType>& value) noexcept { return multiplyBy (value); }
  465. template <typename OtherSampleType, typename SmoothingType>
  466. const AudioBlock& operator*= (SmoothedValue<OtherSampleType, SmoothingType>& value) const noexcept { return multiplyBy (value); }
  467. //==============================================================================
  468. // This class can only be used with floating point types
  469. static_assert (std::is_same<std::remove_const_t<SampleType>, float>::value
  470. || std::is_same<std::remove_const_t<SampleType>, double>::value
  471. #if JUCE_USE_SIMD
  472. || std::is_same<std::remove_const_t<SampleType>, SIMDRegister<float>>::value
  473. || std::is_same<std::remove_const_t<SampleType>, SIMDRegister<double>>::value
  474. #endif
  475. , "AudioBlock only supports single or double precision floating point types");
  476. //==============================================================================
  477. /** Applies a function to each value in an input block, putting the result into an output block.
  478. The function supplied must take a SampleType as its parameter, and return a SampleType.
  479. The two blocks must have the same number of channels and samples.
  480. */
  481. template <typename Src1SampleType, typename Src2SampleType, typename FunctionType>
  482. static void process (AudioBlock<Src1SampleType> inBlock, AudioBlock<Src2SampleType> outBlock, FunctionType&& function)
  483. {
  484. auto len = inBlock.getNumSamples();
  485. auto numChans = inBlock.getNumChannels();
  486. jassert (len == outBlock.getNumSamples());
  487. jassert (numChans == outBlock.getNumChannels());
  488. for (ChannelCountType c = 0; c < numChans; ++c)
  489. {
  490. auto* src = inBlock.getChannelPointer (c);
  491. auto* dst = outBlock.getChannelPointer (c);
  492. for (size_t i = 0; i < len; ++i)
  493. dst[i] = function (src[i]);
  494. }
  495. }
  496. private:
  497. NumericType* getDataPointer (size_t channel) const noexcept
  498. {
  499. return reinterpret_cast<NumericType*> (getChannelPointer (channel));
  500. }
  501. //==============================================================================
  502. void JUCE_VECTOR_CALLTYPE clearInternal() const noexcept
  503. {
  504. auto n = static_cast<int> (numSamples * sizeFactor);
  505. for (size_t ch = 0; ch < numChannels; ++ch)
  506. FloatVectorOperations::clear (getDataPointer (ch), n);
  507. }
  508. void JUCE_VECTOR_CALLTYPE fillInternal (NumericType value) const noexcept
  509. {
  510. auto n = static_cast<int> (numSamples * sizeFactor);
  511. for (size_t ch = 0; ch < numChannels; ++ch)
  512. FloatVectorOperations::fill (getDataPointer (ch), value, n);
  513. }
  514. template <typename OtherSampleType>
  515. void copyFromInternal (const AudioBlock<OtherSampleType>& src) const noexcept
  516. {
  517. auto maxChannels = jmin (src.numChannels, numChannels);
  518. auto n = static_cast<int> (jmin (src.numSamples * src.sizeFactor,
  519. numSamples * sizeFactor));
  520. for (size_t ch = 0; ch < maxChannels; ++ch)
  521. FloatVectorOperations::copy (getDataPointer (ch), src.getDataPointer (ch), n);
  522. }
  523. template <typename OtherNumericType>
  524. void copyFromInternal (const AudioBuffer<OtherNumericType>& src, size_t srcPos, size_t dstPos, size_t numElements) const
  525. {
  526. auto srclen = static_cast<size_t> (src.getNumSamples()) / sizeFactor;
  527. auto n = static_cast<int> (jmin (srclen - srcPos, numSamples - dstPos, numElements) * sizeFactor);
  528. auto maxChannels = jmin (static_cast<size_t> (src.getNumChannels()), static_cast<size_t> (numChannels));
  529. for (size_t ch = 0; ch < maxChannels; ++ch)
  530. FloatVectorOperations::copy (getDataPointer (ch) + (dstPos * sizeFactor),
  531. src.getReadPointer (static_cast<int> (ch),
  532. static_cast<int> (srcPos * sizeFactor)),
  533. n);
  534. }
  535. void moveInternal (size_t srcPos, size_t dstPos,
  536. size_t numElements = std::numeric_limits<size_t>::max()) const noexcept
  537. {
  538. jassert (srcPos <= numSamples && dstPos <= numSamples);
  539. auto len = jmin (numSamples - srcPos, numSamples - dstPos, numElements) * sizeof (SampleType);
  540. if (len != 0)
  541. for (size_t ch = 0; ch < numChannels; ++ch)
  542. ::memmove (getChannelPointer (ch) + dstPos,
  543. getChannelPointer (ch) + srcPos, len);
  544. }
  545. //==============================================================================
  546. void JUCE_VECTOR_CALLTYPE addInternal (NumericType value) const noexcept
  547. {
  548. auto n = static_cast<int> (numSamples * sizeFactor);
  549. for (size_t ch = 0; ch < numChannels; ++ch)
  550. FloatVectorOperations::add (getDataPointer (ch), value, n);
  551. }
  552. template <typename OtherSampleType>
  553. void addInternal (AudioBlock<OtherSampleType> src) const noexcept
  554. {
  555. jassert (numChannels == src.numChannels);
  556. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  557. for (size_t ch = 0; ch < numChannels; ++ch)
  558. FloatVectorOperations::add (getDataPointer (ch), src.getDataPointer (ch), n);
  559. }
  560. template <typename OtherSampleType>
  561. void JUCE_VECTOR_CALLTYPE replaceWithSumOfInternal (AudioBlock<OtherSampleType> src, NumericType value) const noexcept
  562. {
  563. jassert (numChannels == src.numChannels);
  564. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  565. for (size_t ch = 0; ch < numChannels; ++ch)
  566. FloatVectorOperations::add (getDataPointer (ch), src.getDataPointer (ch), value, n);
  567. }
  568. template <typename Src1SampleType, typename Src2SampleType>
  569. void replaceWithSumOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  570. {
  571. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  572. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  573. for (size_t ch = 0; ch < numChannels; ++ch)
  574. FloatVectorOperations::add (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  575. }
  576. //==============================================================================
  577. constexpr void JUCE_VECTOR_CALLTYPE subtractInternal (NumericType value) const noexcept
  578. {
  579. addInternal (value * static_cast<NumericType> (-1.0));
  580. }
  581. template <typename OtherSampleType>
  582. void subtractInternal (AudioBlock<OtherSampleType> src) const noexcept
  583. {
  584. jassert (numChannels == src.numChannels);
  585. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  586. for (size_t ch = 0; ch < numChannels; ++ch)
  587. FloatVectorOperations::subtract (getDataPointer (ch), src.getDataPointer (ch), n);
  588. }
  589. template <typename OtherSampleType>
  590. void JUCE_VECTOR_CALLTYPE replaceWithDifferenceOfInternal (AudioBlock<OtherSampleType> src, NumericType value) const noexcept
  591. {
  592. replaceWithSumOfInternal (src, static_cast<NumericType> (-1.0) * value);
  593. }
  594. template <typename Src1SampleType, typename Src2SampleType>
  595. void replaceWithDifferenceOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  596. {
  597. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  598. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  599. for (size_t ch = 0; ch < numChannels; ++ch)
  600. FloatVectorOperations::subtract (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  601. }
  602. //==============================================================================
  603. void JUCE_VECTOR_CALLTYPE multiplyByInternal (NumericType value) const noexcept
  604. {
  605. auto n = static_cast<int> (numSamples * sizeFactor);
  606. for (size_t ch = 0; ch < numChannels; ++ch)
  607. FloatVectorOperations::multiply (getDataPointer (ch), value, n);
  608. }
  609. template <typename OtherSampleType>
  610. void multiplyByInternal (AudioBlock<OtherSampleType> src) const noexcept
  611. {
  612. jassert (numChannels == src.numChannels);
  613. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  614. for (size_t ch = 0; ch < numChannels; ++ch)
  615. FloatVectorOperations::multiply (getDataPointer (ch), src.getDataPointer (ch), n);
  616. }
  617. template <typename OtherSampleType>
  618. void JUCE_VECTOR_CALLTYPE replaceWithProductOfInternal (AudioBlock<OtherSampleType> src, NumericType value) const noexcept
  619. {
  620. jassert (numChannels == src.numChannels);
  621. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  622. for (size_t ch = 0; ch < numChannels; ++ch)
  623. FloatVectorOperations::multiply (getDataPointer (ch), src.getDataPointer (ch), value, n);
  624. }
  625. template <typename Src1SampleType, typename Src2SampleType>
  626. void replaceWithProductOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  627. {
  628. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  629. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  630. for (size_t ch = 0; ch < numChannels; ++ch)
  631. FloatVectorOperations::multiply (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  632. }
  633. template <typename OtherSampleType, typename SmoothingType>
  634. void multiplyByInternal (SmoothedValue<OtherSampleType, SmoothingType>& value) const noexcept
  635. {
  636. if (! value.isSmoothing())
  637. {
  638. multiplyByInternal ((NumericType) value.getTargetValue());
  639. }
  640. else
  641. {
  642. for (size_t i = 0; i < numSamples; ++i)
  643. {
  644. const auto scaler = (NumericType) value.getNextValue();
  645. for (size_t ch = 0; ch < numChannels; ++ch)
  646. getDataPointer (ch)[i] *= scaler;
  647. }
  648. }
  649. }
  650. template <typename BlockSampleType, typename SmootherSampleType, typename SmoothingType>
  651. void replaceWithProductOfInternal (AudioBlock<BlockSampleType> src, SmoothedValue<SmootherSampleType, SmoothingType>& value) const noexcept
  652. {
  653. jassert (numChannels == src.numChannels);
  654. if (! value.isSmoothing())
  655. {
  656. replaceWithProductOfInternal (src, (NumericType) value.getTargetValue());
  657. }
  658. else
  659. {
  660. auto n = jmin (numSamples, src.numSamples) * sizeFactor;
  661. for (size_t i = 0; i < n; ++i)
  662. {
  663. const auto scaler = (NumericType) value.getNextValue();
  664. for (size_t ch = 0; ch < numChannels; ++ch)
  665. getDataPointer (ch)[i] = scaler * src.getChannelPointer (ch)[i];
  666. }
  667. }
  668. }
  669. //==============================================================================
  670. template <typename OtherSampleType>
  671. void JUCE_VECTOR_CALLTYPE addProductOfInternal (AudioBlock<OtherSampleType> src, NumericType factor) const noexcept
  672. {
  673. jassert (numChannels == src.numChannels);
  674. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  675. for (size_t ch = 0; ch < numChannels; ++ch)
  676. FloatVectorOperations::addWithMultiply (getDataPointer (ch), src.getDataPointer (ch), factor, n);
  677. }
  678. template <typename Src1SampleType, typename Src2SampleType>
  679. void addProductOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  680. {
  681. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  682. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  683. for (size_t ch = 0; ch < numChannels; ++ch)
  684. FloatVectorOperations::addWithMultiply (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  685. }
  686. //==============================================================================
  687. constexpr void negateInternal() const noexcept
  688. {
  689. multiplyByInternal (static_cast<NumericType> (-1.0));
  690. }
  691. template <typename OtherSampleType>
  692. void replaceWithNegativeOfInternal (AudioBlock<OtherSampleType> src) const noexcept
  693. {
  694. jassert (numChannels == src.numChannels);
  695. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  696. for (size_t ch = 0; ch < numChannels; ++ch)
  697. FloatVectorOperations::negate (getDataPointer (ch), src.getDataPointer (ch), n);
  698. }
  699. template <typename OtherSampleType>
  700. void replaceWithAbsoluteValueOfInternal (AudioBlock<OtherSampleType> src) const noexcept
  701. {
  702. jassert (numChannels == src.numChannels);
  703. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  704. for (size_t ch = 0; ch < numChannels; ++ch)
  705. FloatVectorOperations::abs (getDataPointer (ch), src.getDataPointer (ch), n);
  706. }
  707. //==============================================================================
  708. template <typename Src1SampleType, typename Src2SampleType>
  709. void replaceWithMinOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  710. {
  711. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  712. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  713. for (size_t ch = 0; ch < numChannels; ++ch)
  714. FloatVectorOperations::min (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  715. }
  716. template <typename Src1SampleType, typename Src2SampleType>
  717. void replaceWithMaxOfInternal (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept
  718. {
  719. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  720. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  721. for (size_t ch = 0; ch < numChannels; ++ch)
  722. FloatVectorOperations::max (getDataPointer (ch), src1.getDataPointer (ch), src2.getDataPointer (ch), n);
  723. }
  724. //==============================================================================
  725. using ChannelCountType = unsigned int;
  726. //==============================================================================
  727. static constexpr size_t sizeFactor = sizeof (SampleType) / sizeof (NumericType);
  728. static constexpr size_t elementMask = sizeFactor - 1;
  729. static constexpr size_t byteMask = (sizeFactor * sizeof (NumericType)) - 1;
  730. #if JUCE_USE_SIMD
  731. static constexpr size_t defaultAlignment = sizeof (SIMDRegister<NumericType>);
  732. #else
  733. static constexpr size_t defaultAlignment = sizeof (NumericType);
  734. #endif
  735. SampleType* const* channels;
  736. ChannelCountType numChannels = 0;
  737. size_t startSample = 0, numSamples = 0;
  738. template <typename OtherSampleType>
  739. friend class AudioBlock;
  740. };
  741. } // namespace dsp
  742. } // namespace juce