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.

881 lines
46KB

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