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.

714 lines
31KB

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