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
46KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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::dsp
  19. {
  20. #ifndef DOXYGEN
  21. namespace SampleTypeHelpers // Internal classes needed for handling sample type classes
  22. {
  23. template <typename T, bool = std::is_floating_point_v<T>>
  24. struct ElementType
  25. {
  26. using Type = T;
  27. };
  28. template <typename T>
  29. struct ElementType<const T, false>
  30. {
  31. using Type = const typename T::value_type;
  32. };
  33. template <typename T>
  34. struct ElementType<T, false>
  35. {
  36. using Type = typename T::value_type;
  37. };
  38. }
  39. #endif
  40. //==============================================================================
  41. /**
  42. Minimal and lightweight data-structure which contains a list of pointers to
  43. channels containing some kind of sample data.
  44. This class doesn't own any of the data which it points to, it's simply a view
  45. into data that is owned elsewhere. You can construct one from some raw data
  46. that you've allocated yourself, or give it a HeapBlock to use, or give it
  47. an AudioBuffer which it can refer to, but in all cases the user is
  48. responsible for making sure that the data doesn't get deleted while there's
  49. still an AudioBlock using it.
  50. @tags{DSP}
  51. */
  52. template <typename SampleType>
  53. class AudioBlock
  54. {
  55. private:
  56. template <typename OtherSampleType>
  57. using MayUseConvertingConstructor =
  58. std::enable_if_t<std::is_same_v<std::remove_const_t<SampleType>,
  59. std::remove_const_t<OtherSampleType>>
  60. && std::is_const_v<SampleType>
  61. && ! std::is_const_v<OtherSampleType>,
  62. int>;
  63. public:
  64. //==============================================================================
  65. using NumericType = typename SampleTypeHelpers::ElementType<SampleType>::Type;
  66. //==============================================================================
  67. /** Create a zero-sized AudioBlock. */
  68. AudioBlock() noexcept = default;
  69. /** Creates an AudioBlock from a pointer to an array of channels.
  70. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  71. Therefore it is the user's responsibility to ensure that the memory is retained
  72. throughout the life-time of the AudioBlock and released when no longer needed.
  73. */
  74. constexpr AudioBlock (SampleType* const* channelData,
  75. size_t numberOfChannels, size_t numberOfSamples) noexcept
  76. : channels (channelData),
  77. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  78. numSamples (numberOfSamples)
  79. {
  80. }
  81. /** Creates an AudioBlock from a pointer to an array of channels.
  82. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  83. Therefore it is the user's responsibility to ensure that the memory is retained
  84. throughout the life-time of the AudioBlock and released when no longer needed.
  85. */
  86. constexpr AudioBlock (SampleType* const* channelData, size_t numberOfChannels,
  87. size_t startSampleIndex, size_t numberOfSamples) noexcept
  88. : channels (channelData),
  89. numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  90. startSample (startSampleIndex),
  91. numSamples (numberOfSamples)
  92. {
  93. }
  94. /** Allocates a suitable amount of space in a HeapBlock, and initialises this object
  95. to point into it.
  96. The HeapBlock must of course not be freed or re-allocated while this object is still in
  97. use, because it will be referencing its data.
  98. */
  99. AudioBlock (HeapBlock<char>& heapBlockToUseForAllocation,
  100. size_t numberOfChannels, size_t numberOfSamples,
  101. size_t alignmentInBytes = defaultAlignment) noexcept
  102. : numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  103. numSamples (numberOfSamples)
  104. {
  105. auto roundedUpNumSamples = (numberOfSamples + elementMask) & ~elementMask;
  106. auto channelSize = sizeof (SampleType) * roundedUpNumSamples;
  107. auto channelListBytes = sizeof (SampleType*) * numberOfChannels;
  108. auto extraBytes = alignmentInBytes - 1;
  109. heapBlockToUseForAllocation.malloc (channelListBytes + extraBytes + channelSize * numberOfChannels);
  110. auto* chanArray = unalignedPointerCast<SampleType**> (heapBlockToUseForAllocation.getData());
  111. channels = chanArray;
  112. auto* data = unalignedPointerCast<SampleType*> (addBytesToPointer (chanArray, channelListBytes));
  113. data = snapPointerToAlignment (data, alignmentInBytes);
  114. for (ChannelCountType i = 0; i < numChannels; ++i)
  115. {
  116. chanArray[i] = data;
  117. data += roundedUpNumSamples;
  118. }
  119. }
  120. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  121. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  122. Therefore it is the user's responsibility to ensure that the buffer is retained
  123. throughout the life-time of the AudioBlock without being modified.
  124. */
  125. template <typename OtherSampleType>
  126. constexpr AudioBlock (AudioBuffer<OtherSampleType>& buffer) noexcept
  127. : channels (buffer.getArrayOfWritePointers()),
  128. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  129. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  130. {
  131. }
  132. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  133. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  134. Therefore it is the user's responsibility to ensure that the buffer is retained
  135. throughout the life-time of the AudioBlock without being modified.
  136. */
  137. template <typename OtherSampleType>
  138. constexpr AudioBlock (const AudioBuffer<OtherSampleType>& buffer) noexcept
  139. : channels (buffer.getArrayOfReadPointers()),
  140. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  141. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  142. {
  143. }
  144. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  145. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  146. Therefore it is the user's responsibility to ensure that the buffer is retained
  147. throughout the life-time of the AudioBlock without being modified.
  148. */
  149. template <typename OtherSampleType>
  150. AudioBlock (AudioBuffer<OtherSampleType>& buffer, size_t startSampleIndex) noexcept
  151. : channels (buffer.getArrayOfWritePointers()),
  152. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  153. startSample (startSampleIndex),
  154. numSamples (static_cast<size_t> (buffer.getNumSamples()) - startSampleIndex)
  155. {
  156. jassert (startSample < static_cast<size_t> (buffer.getNumSamples()));
  157. }
  158. AudioBlock (const AudioBlock& other) noexcept = default;
  159. AudioBlock& operator= (const AudioBlock& other) noexcept = default;
  160. template <typename OtherSampleType, MayUseConvertingConstructor<OtherSampleType> = 0>
  161. AudioBlock (const AudioBlock<OtherSampleType>& other) noexcept
  162. : channels { other.channels },
  163. numChannels { other.numChannels },
  164. startSample { other.startSample },
  165. numSamples { other.numSamples }
  166. {
  167. }
  168. template <typename OtherSampleType, MayUseConvertingConstructor<OtherSampleType> = 0>
  169. AudioBlock& operator= (const AudioBlock<OtherSampleType>& other) noexcept
  170. {
  171. AudioBlock blockCopy { other };
  172. swap (blockCopy);
  173. return *this;
  174. }
  175. void swap (AudioBlock& other) noexcept
  176. {
  177. std::swap (other.channels, channels);
  178. std::swap (other.numChannels, numChannels);
  179. std::swap (other.startSample, startSample);
  180. std::swap (other.numSamples, numSamples);
  181. }
  182. //==============================================================================
  183. template <typename OtherSampleType>
  184. constexpr bool operator== (const AudioBlock<OtherSampleType>& other) const noexcept
  185. {
  186. return std::equal (channels,
  187. channels + numChannels,
  188. other.channels,
  189. other.channels + other.numChannels)
  190. && startSample == other.startSample
  191. && numSamples == other.numSamples;
  192. }
  193. template <typename OtherSampleType>
  194. constexpr bool operator!= (const AudioBlock<OtherSampleType>& other) const noexcept
  195. {
  196. return ! (*this == other);
  197. }
  198. //==============================================================================
  199. /** Returns the number of channels referenced by this block. */
  200. constexpr size_t getNumChannels() const noexcept { return static_cast<size_t> (numChannels); }
  201. /** Returns the number of samples referenced by this block. */
  202. constexpr size_t getNumSamples() const noexcept { return numSamples; }
  203. /** Returns a raw pointer into one of the channels in this block. */
  204. SampleType* getChannelPointer (size_t channel) const noexcept
  205. {
  206. jassert (channel < numChannels);
  207. jassert (numSamples > 0);
  208. return channels[channel] + startSample;
  209. }
  210. /** Returns an AudioBlock that represents one of the channels in this block. */
  211. AudioBlock getSingleChannelBlock (size_t channel) const noexcept
  212. {
  213. jassert (channel < numChannels);
  214. return AudioBlock (channels + channel, 1, startSample, numSamples);
  215. }
  216. /** Returns a subset of contiguous channels
  217. @param channelStart First channel of the subset
  218. @param numChannelsToUse Count of channels in the subset
  219. */
  220. AudioBlock getSubsetChannelBlock (size_t channelStart, size_t numChannelsToUse) const noexcept
  221. {
  222. jassert (channelStart < numChannels);
  223. jassert ((channelStart + numChannelsToUse) <= numChannels);
  224. return AudioBlock (channels + channelStart, numChannelsToUse, startSample, numSamples);
  225. }
  226. /** Returns a sample from the buffer.
  227. The channel and index are not checked - they are expected to be in-range. If not,
  228. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  229. territory.
  230. */
  231. SampleType getSample (int channel, int sampleIndex) const noexcept
  232. {
  233. jassert (isPositiveAndBelow (channel, numChannels));
  234. jassert (isPositiveAndBelow (sampleIndex, numSamples));
  235. return channels[channel][(size_t) startSample + (size_t) sampleIndex];
  236. }
  237. /** Modifies a sample in the buffer.
  238. The channel and index are not checked - they are expected to be in-range. If not,
  239. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  240. territory.
  241. */
  242. void setSample (int destChannel, int destSample, SampleType newValue) const noexcept
  243. {
  244. jassert (isPositiveAndBelow (destChannel, numChannels));
  245. jassert (isPositiveAndBelow (destSample, numSamples));
  246. channels[destChannel][(size_t) startSample + (size_t) destSample] = newValue;
  247. }
  248. /** Adds a value to a sample in the buffer.
  249. The channel and index are not checked - they are expected to be in-range. If not,
  250. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  251. territory.
  252. */
  253. void addSample (int destChannel, int destSample, SampleType valueToAdd) const noexcept
  254. {
  255. jassert (isPositiveAndBelow (destChannel, numChannels));
  256. jassert (isPositiveAndBelow (destSample, numSamples));
  257. channels[destChannel][(size_t) startSample + (size_t) destSample] += valueToAdd;
  258. }
  259. //==============================================================================
  260. /** Clears the memory referenced by this AudioBlock. */
  261. AudioBlock& clear() noexcept { clearInternal(); return *this; }
  262. const AudioBlock& clear() const noexcept { clearInternal(); return *this; }
  263. /** Fills the memory referenced by this AudioBlock with value. */
  264. AudioBlock& JUCE_VECTOR_CALLTYPE fill (NumericType value) noexcept { fillInternal (value); return *this; }
  265. const AudioBlock& JUCE_VECTOR_CALLTYPE fill (NumericType value) const noexcept { fillInternal (value); return *this; }
  266. /** Copies the values in src to this block. */
  267. template <typename OtherSampleType>
  268. AudioBlock& copyFrom (const AudioBlock<OtherSampleType>& src) noexcept { copyFromInternal (src); return *this; }
  269. template <typename OtherSampleType>
  270. const AudioBlock& copyFrom (const AudioBlock<OtherSampleType>& src) const noexcept { copyFromInternal (src); return *this; }
  271. /** Copy the values from an AudioBuffer to this block.
  272. All indices and sizes are in this AudioBlock's units, i.e. if SampleType is a
  273. SIMDRegister then incrementing srcPos by one will increase the sample position
  274. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  275. */
  276. template <typename OtherNumericType>
  277. AudioBlock& copyFrom (const AudioBuffer<OtherNumericType>& src,
  278. size_t srcPos = 0, size_t dstPos = 0,
  279. size_t numElements = std::numeric_limits<size_t>::max()) { copyFromInternal (src, srcPos, dstPos, numElements); return *this; }
  280. template <typename OtherNumericType>
  281. const AudioBlock& copyFrom (const AudioBuffer<OtherNumericType>& src,
  282. size_t srcPos = 0, size_t dstPos = 0,
  283. size_t numElements = std::numeric_limits<size_t>::max()) const { copyFromInternal (src, srcPos, dstPos, numElements); return *this; }
  284. /** Copies the values from this block to an AudioBuffer.
  285. All indices and sizes are in this AudioBlock's units, i.e. if SampleType is a
  286. SIMDRegister then incrementing dstPos by one will increase the sample position
  287. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  288. */
  289. void copyTo (AudioBuffer<std::remove_const_t<NumericType>>& dst, size_t srcPos = 0, size_t dstPos = 0,
  290. size_t numElements = std::numeric_limits<size_t>::max()) const
  291. {
  292. auto dstlen = static_cast<size_t> (dst.getNumSamples()) / sizeFactor;
  293. auto n = jmin (numSamples - srcPos, dstlen - dstPos, numElements) * sizeFactor;
  294. auto maxChannels = jmin (static_cast<size_t> (dst.getNumChannels()), static_cast<size_t> (numChannels));
  295. for (size_t ch = 0; ch < maxChannels; ++ch)
  296. FloatVectorOperations::copy (dst.getWritePointer ((int) ch, (int) (dstPos * sizeFactor)),
  297. getDataPointer (ch) + (srcPos * sizeFactor),
  298. n);
  299. }
  300. /** Move memory within this block from the position srcPos to the position dstPos.
  301. If numElements is not specified then move will move the maximum amount of memory.
  302. */
  303. AudioBlock& move (size_t srcPos, size_t dstPos,
  304. size_t numElements = std::numeric_limits<size_t>::max()) noexcept { moveInternal (srcPos, dstPos, numElements); return *this; }
  305. const AudioBlock& move (size_t srcPos, size_t dstPos,
  306. size_t numElements = std::numeric_limits<size_t>::max()) const noexcept { moveInternal (srcPos, dstPos, numElements); return *this; }
  307. //==============================================================================
  308. /** Return a new AudioBlock pointing to a sub-block inside this block. This
  309. function does not copy the memory and you must ensure that the original memory
  310. pointed to by the receiver remains valid through-out the life-time of the
  311. returned sub-block.
  312. @param newOffset The index of an element inside the receiver which will
  313. will become the first element of the return value.
  314. @param newLength The number of elements of the newly created sub-block.
  315. */
  316. AudioBlock getSubBlock (size_t newOffset, size_t newLength) const noexcept
  317. {
  318. jassert (newOffset < numSamples);
  319. jassert (newOffset + newLength <= numSamples);
  320. return AudioBlock (channels, numChannels, startSample + newOffset, newLength);
  321. }
  322. /** Return a new AudioBlock pointing to a sub-block inside this block. This
  323. function does not copy the memory and you must ensure that the original memory
  324. pointed to by the receiver remains valid through-out the life-time of the
  325. returned sub-block.
  326. @param newOffset The index of an element inside the block which will
  327. will become the first element of the return value.
  328. The return value will include all subsequent elements
  329. of the receiver.
  330. */
  331. AudioBlock getSubBlock (size_t newOffset) const noexcept
  332. {
  333. return getSubBlock (newOffset, getNumSamples() - newOffset);
  334. }
  335. //==============================================================================
  336. /** Adds a fixed value to the elements in this block. */
  337. AudioBlock& JUCE_VECTOR_CALLTYPE add (NumericType value) noexcept { addInternal (value); return *this; }
  338. const AudioBlock& JUCE_VECTOR_CALLTYPE add (NumericType value) const noexcept { addInternal (value); return *this; }
  339. /** Adds the elements in the src block to the elements in this block. */
  340. template <typename OtherSampleType>
  341. AudioBlock& add (AudioBlock<OtherSampleType> src) noexcept { addInternal (src); return *this; }
  342. template <typename OtherSampleType>
  343. const AudioBlock& add (AudioBlock<OtherSampleType> src) const noexcept { addInternal (src); return *this; }
  344. /** Adds a fixed value to each source value and replaces the contents of this block. */
  345. template <typename OtherSampleType>
  346. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithSumOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithSumOfInternal (src, value); return *this; }
  347. template <typename OtherSampleType>
  348. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithSumOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithSumOfInternal (src, value); return *this; }
  349. /** Adds each source1 value to the corresponding source2 value and replaces the contents of this block. */
  350. template <typename Src1SampleType, typename Src2SampleType>
  351. AudioBlock& replaceWithSumOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithSumOfInternal (src1, src2); return *this; }
  352. template <typename Src1SampleType, typename Src2SampleType>
  353. const AudioBlock& replaceWithSumOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithSumOfInternal (src1, src2); return *this; }
  354. //==============================================================================
  355. /** Subtracts a fixed value from the elements in this block. */
  356. AudioBlock& JUCE_VECTOR_CALLTYPE subtract (NumericType value) noexcept { subtractInternal (value); return *this; }
  357. const AudioBlock& JUCE_VECTOR_CALLTYPE subtract (NumericType value) const noexcept { subtractInternal (value); return *this; }
  358. /** Subtracts the source values from the elements in this block. */
  359. template <typename OtherSampleType>
  360. AudioBlock& subtract (AudioBlock<OtherSampleType> src) noexcept { subtractInternal (src); return *this; }
  361. template <typename OtherSampleType>
  362. const AudioBlock& subtract (AudioBlock<OtherSampleType> src) const noexcept { subtractInternal (src); return *this; }
  363. /** Subtracts a fixed value from each source value and replaces the contents of this block. */
  364. template <typename OtherSampleType>
  365. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithDifferenceOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithDifferenceOfInternal (src, value); return *this; }
  366. template <typename OtherSampleType>
  367. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithDifferenceOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithDifferenceOfInternal (src, value); return *this; }
  368. /** Subtracts each source2 value from the corresponding source1 value and replaces the contents of this block. */
  369. template <typename Src1SampleType, typename Src2SampleType>
  370. AudioBlock& replaceWithDifferenceOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithDifferenceOfInternal (src1, src2); return *this; }
  371. template <typename Src1SampleType, typename Src2SampleType>
  372. const AudioBlock& replaceWithDifferenceOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithDifferenceOfInternal (src1, src2); return *this; }
  373. //==============================================================================
  374. /** Multiplies the elements in this block by a fixed value. */
  375. AudioBlock& JUCE_VECTOR_CALLTYPE multiplyBy (NumericType value) noexcept { multiplyByInternal (value); return *this; }
  376. const AudioBlock& JUCE_VECTOR_CALLTYPE multiplyBy (NumericType value) const noexcept { multiplyByInternal (value); return *this; }
  377. /** Multiplies the elements in this block by the elements in the src block */
  378. template <typename OtherSampleType>
  379. AudioBlock& multiplyBy (AudioBlock<OtherSampleType> src) noexcept { multiplyByInternal (src); return *this; }
  380. template <typename OtherSampleType>
  381. const AudioBlock& multiplyBy (AudioBlock<OtherSampleType> src) const noexcept { multiplyByInternal (src); return *this; }
  382. /** Replaces the elements in this block with the product of the elements in the source src block and a fixed value. */
  383. template <typename OtherSampleType>
  384. AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithProductOf (AudioBlock<OtherSampleType> src, NumericType value) noexcept { replaceWithProductOfInternal (src, value); return *this; }
  385. template <typename OtherSampleType>
  386. const AudioBlock& JUCE_VECTOR_CALLTYPE replaceWithProductOf (AudioBlock<OtherSampleType> src, NumericType value) const noexcept { replaceWithProductOfInternal (src, value); return *this; }
  387. /** Replaces the elements in this block with the product of the elements in the src1 and scr2 blocks. */
  388. template <typename Src1SampleType, typename Src2SampleType>
  389. AudioBlock& replaceWithProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithProductOfInternal (src1, src2); return *this; }
  390. template <typename Src1SampleType, typename Src2SampleType>
  391. const AudioBlock& replaceWithProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithProductOfInternal (src1, src2); return *this; }
  392. //==============================================================================
  393. /** Multiplies each channels of this block by a smoothly changing value. */
  394. template <typename OtherSampleType, typename SmoothingType>
  395. AudioBlock& multiplyBy (SmoothedValue<OtherSampleType, SmoothingType>& value) noexcept { multiplyByInternal (value); return *this; }
  396. template <typename OtherSampleType, typename SmoothingType>
  397. const AudioBlock& multiplyBy (SmoothedValue<OtherSampleType, SmoothingType>& value) const noexcept { multiplyByInternal (value); return *this; }
  398. /** Replaces each channel of this block with the product of the src block and a smoothed value. */
  399. template <typename BlockSampleType, typename SmootherSampleType, typename SmoothingType>
  400. AudioBlock& replaceWithProductOf (AudioBlock<BlockSampleType> src, SmoothedValue<SmootherSampleType, SmoothingType>& value) noexcept { replaceWithProductOfInternal (src, value); return *this; }
  401. template <typename BlockSampleType, typename SmootherSampleType, typename SmoothingType>
  402. const AudioBlock& replaceWithProductOf (AudioBlock<BlockSampleType> src, SmoothedValue<SmootherSampleType, SmoothingType>& value) const noexcept { replaceWithProductOfInternal (src, value); return *this; }
  403. //==============================================================================
  404. /** Multiplies each value in src by a fixed value and adds the result to this block. */
  405. template <typename OtherSampleType>
  406. AudioBlock& JUCE_VECTOR_CALLTYPE addProductOf (AudioBlock<OtherSampleType> src, NumericType factor) noexcept { addProductOfInternal (src, factor); return *this; }
  407. template <typename OtherSampleType>
  408. const AudioBlock& JUCE_VECTOR_CALLTYPE addProductOf (AudioBlock<OtherSampleType> src, NumericType factor) const noexcept { addProductOfInternal (src, factor); return *this; }
  409. /** Multiplies each value in srcA with the corresponding value in srcB and adds the result to this block. */
  410. template <typename Src1SampleType, typename Src2SampleType>
  411. AudioBlock& addProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { addProductOfInternal (src1, src2); return *this; }
  412. template <typename Src1SampleType, typename Src2SampleType>
  413. const AudioBlock& addProductOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { addProductOfInternal (src1, src2); return *this; }
  414. //==============================================================================
  415. /** Negates each value of this block. */
  416. AudioBlock& negate() noexcept { negateInternal(); return *this; }
  417. const AudioBlock& negate() const noexcept { negateInternal(); return *this; }
  418. /** Replaces the contents of this block with the negative of the values in the src block. */
  419. template <typename OtherSampleType>
  420. AudioBlock& replaceWithNegativeOf (AudioBlock<OtherSampleType> src) noexcept { replaceWithNegativeOfInternal (src); return *this; }
  421. template <typename OtherSampleType>
  422. const AudioBlock& replaceWithNegativeOf (AudioBlock<OtherSampleType> src) const noexcept { replaceWithNegativeOfInternal (src); return *this; }
  423. /** Replaces the contents of this block with the absolute values of the src block. */
  424. template <typename OtherSampleType>
  425. AudioBlock& replaceWithAbsoluteValueOf (AudioBlock<OtherSampleType> src) noexcept { replaceWithAbsoluteValueOfInternal (src); return *this; }
  426. template <typename OtherSampleType>
  427. const AudioBlock& replaceWithAbsoluteValueOf (AudioBlock<OtherSampleType> src) const noexcept { replaceWithAbsoluteValueOfInternal (src); return *this; }
  428. //==============================================================================
  429. /** Replaces each element of this block with the minimum of the corresponding element of the source arrays. */
  430. template <typename Src1SampleType, typename Src2SampleType>
  431. AudioBlock& replaceWithMinOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithMinOfInternal (src1, src2); return *this; }
  432. template <typename Src1SampleType, typename Src2SampleType>
  433. const AudioBlock& replaceWithMinOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithMinOfInternal (src1, src2); return *this; }
  434. /** Replaces each element of this block with the maximum of the corresponding element of the source arrays. */
  435. template <typename Src1SampleType, typename Src2SampleType>
  436. AudioBlock& replaceWithMaxOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) noexcept { replaceWithMaxOfInternal (src1, src2); return *this; }
  437. template <typename Src1SampleType, typename Src2SampleType>
  438. const AudioBlock& replaceWithMaxOf (AudioBlock<Src1SampleType> src1, AudioBlock<Src2SampleType> src2) const noexcept { replaceWithMaxOfInternal (src1, src2); return *this; }
  439. //==============================================================================
  440. /** Finds the minimum and maximum value of the buffer. */
  441. Range<std::remove_const_t<NumericType>> findMinAndMax() const noexcept
  442. {
  443. if (numChannels == 0)
  444. return {};
  445. auto n = numSamples * sizeFactor;
  446. auto minmax = FloatVectorOperations::findMinAndMax (getDataPointer (0), n);
  447. for (size_t ch = 1; ch < numChannels; ++ch)
  448. minmax = minmax.getUnionWith (FloatVectorOperations::findMinAndMax (getDataPointer (ch), n));
  449. return minmax;
  450. }
  451. //==============================================================================
  452. // Convenient operator wrappers.
  453. AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (NumericType value) noexcept { return add (value); }
  454. const AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (NumericType value) const noexcept { return add (value); }
  455. AudioBlock& operator+= (AudioBlock src) noexcept { return add (src); }
  456. const AudioBlock& operator+= (AudioBlock src) const noexcept { return add (src); }
  457. AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (NumericType value) noexcept { return subtract (value); }
  458. const AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (NumericType value) const noexcept { return subtract (value); }
  459. AudioBlock& operator-= (AudioBlock src) noexcept { return subtract (src); }
  460. const AudioBlock& operator-= (AudioBlock src) const noexcept { return subtract (src); }
  461. AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (NumericType value) noexcept { return multiplyBy (value); }
  462. const AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (NumericType value) const noexcept { return multiplyBy (value); }
  463. AudioBlock& operator*= (AudioBlock src) noexcept { return multiplyBy (src); }
  464. const AudioBlock& operator*= (AudioBlock src) const noexcept { return multiplyBy (src); }
  465. template <typename OtherSampleType, typename SmoothingType>
  466. AudioBlock& operator*= (SmoothedValue<OtherSampleType, SmoothingType>& value) noexcept { return multiplyBy (value); }
  467. template <typename OtherSampleType, typename SmoothingType>
  468. const AudioBlock& operator*= (SmoothedValue<OtherSampleType, SmoothingType>& value) const noexcept { return multiplyBy (value); }
  469. //==============================================================================
  470. // This class can only be used with floating point types
  471. static_assert (std::is_same_v<std::remove_const_t<SampleType>, float>
  472. || std::is_same_v<std::remove_const_t<SampleType>, double>
  473. #if JUCE_USE_SIMD
  474. || std::is_same_v<std::remove_const_t<SampleType>, SIMDRegister<float>>
  475. || std::is_same_v<std::remove_const_t<SampleType>, SIMDRegister<double>>
  476. #endif
  477. , "AudioBlock only supports single or double precision floating point types");
  478. //==============================================================================
  479. /** Applies a function to each value in an input block, putting the result into an output block.
  480. The function supplied must take a SampleType as its parameter, and return a SampleType.
  481. The two blocks must have the same number of channels and samples.
  482. */
  483. template <typename Src1SampleType, typename Src2SampleType, typename FunctionType>
  484. static void process (AudioBlock<Src1SampleType> inBlock, AudioBlock<Src2SampleType> outBlock, FunctionType&& function)
  485. {
  486. auto len = inBlock.getNumSamples();
  487. auto numChans = inBlock.getNumChannels();
  488. jassert (len == outBlock.getNumSamples());
  489. jassert (numChans == outBlock.getNumChannels());
  490. for (ChannelCountType c = 0; c < numChans; ++c)
  491. {
  492. auto* src = inBlock.getChannelPointer (c);
  493. auto* dst = outBlock.getChannelPointer (c);
  494. for (size_t i = 0; i < len; ++i)
  495. dst[i] = function (src[i]);
  496. }
  497. }
  498. private:
  499. NumericType* getDataPointer (size_t channel) const noexcept
  500. {
  501. return reinterpret_cast<NumericType*> (getChannelPointer (channel));
  502. }
  503. //==============================================================================
  504. void JUCE_VECTOR_CALLTYPE clearInternal() const noexcept
  505. {
  506. auto n = numSamples * sizeFactor;
  507. for (size_t ch = 0; ch < numChannels; ++ch)
  508. FloatVectorOperations::clear (getDataPointer (ch), n);
  509. }
  510. void JUCE_VECTOR_CALLTYPE fillInternal (NumericType value) const noexcept
  511. {
  512. auto n = numSamples * sizeFactor;
  513. for (size_t ch = 0; ch < numChannels; ++ch)
  514. FloatVectorOperations::fill (getDataPointer (ch), value, n);
  515. }
  516. template <typename OtherSampleType>
  517. void copyFromInternal (const AudioBlock<OtherSampleType>& src) const noexcept
  518. {
  519. auto maxChannels = jmin (src.numChannels, numChannels);
  520. auto n = jmin (src.numSamples * src.sizeFactor, numSamples * sizeFactor);
  521. for (size_t ch = 0; ch < maxChannels; ++ch)
  522. FloatVectorOperations::copy (getDataPointer (ch), src.getDataPointer (ch), n);
  523. }
  524. template <typename OtherNumericType>
  525. void copyFromInternal (const AudioBuffer<OtherNumericType>& src, size_t srcPos, size_t dstPos, size_t numElements) const
  526. {
  527. auto srclen = static_cast<size_t> (src.getNumSamples()) / sizeFactor;
  528. auto n = jmin (srclen - srcPos, numSamples - dstPos, numElements) * sizeFactor;
  529. auto maxChannels = jmin (static_cast<size_t> (src.getNumChannels()), static_cast<size_t> (numChannels));
  530. for (size_t ch = 0; ch < maxChannels; ++ch)
  531. FloatVectorOperations::copy (getDataPointer (ch) + (dstPos * sizeFactor),
  532. src.getReadPointer ((int) ch, (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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 juce::dsp