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

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