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.

697 lines
30KB

  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 {}
  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()))
  125. {
  126. jassert (startSample < numSamples);
  127. }
  128. AudioBlock (const AudioBlock& other) noexcept = default;
  129. AudioBlock& operator= (const AudioBlock& other) noexcept = default;
  130. //==============================================================================
  131. forcedinline size_t getNumSamples() const noexcept { return numSamples; }
  132. forcedinline size_t getNumChannels() const noexcept { return static_cast<size_t> (numChannels); }
  133. /** Returns a raw pointer into one of the channels in this block. */
  134. forcedinline const SampleType* getChannelPointer (size_t channel) const noexcept
  135. {
  136. jassert (channel < numChannels);
  137. jassert (numSamples > 0);
  138. return channels[channel] + startSample;
  139. }
  140. /** Returns a raw pointer into one of the channels in this block. */
  141. forcedinline SampleType* getChannelPointer (size_t channel) noexcept
  142. {
  143. jassert (channel < numChannels);
  144. jassert (numSamples > 0);
  145. return channels[channel] + startSample;
  146. }
  147. /** Returns an AudioBlock that represents one of the channels in this block. */
  148. forcedinline AudioBlock getSingleChannelBlock (size_t channel) const noexcept
  149. {
  150. jassert (channel < numChannels);
  151. return AudioBlock (channels + channel, 1, startSample, numSamples);
  152. }
  153. /** Returns a subset of continguous channels
  154. @param channelStart First channel of the subset
  155. @param numChannelsToUse Count of channels in the subset
  156. */
  157. forcedinline AudioBlock getSubsetChannelBlock (size_t channelStart, size_t numChannelsToUse) noexcept
  158. {
  159. jassert (channelStart < numChannels);
  160. jassert ((channelStart + numChannelsToUse) <= numChannels);
  161. return AudioBlock (channels + channelStart, numChannelsToUse, startSample, numSamples);
  162. }
  163. /** Returns a sample from the buffer.
  164. The channel and index are not checked - they are expected to be in-range. If not,
  165. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  166. territory.
  167. */
  168. SampleType getSample (int channel, int sampleIndex) const noexcept
  169. {
  170. jassert (isPositiveAndBelow (channel, numChannels));
  171. jassert (isPositiveAndBelow (sampleIndex, numSamples));
  172. return channels[channel][startSample + sampleIndex];
  173. }
  174. /** Modifies a sample in the buffer.
  175. The channel and index are not checked - they are expected to be in-range. If not,
  176. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  177. territory.
  178. */
  179. void setSample (int destChannel, int destSample, SampleType newValue) noexcept
  180. {
  181. jassert (isPositiveAndBelow (destChannel, numChannels));
  182. jassert (isPositiveAndBelow (destSample, numSamples));
  183. channels[destChannel][startSample + destSample] = newValue;
  184. }
  185. /** Adds a value to a sample in the buffer.
  186. The channel and index are not checked - they are expected to be in-range. If not,
  187. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  188. territory.
  189. */
  190. void addSample (int destChannel, int destSample, SampleType valueToAdd) noexcept
  191. {
  192. jassert (isPositiveAndBelow (destChannel, numChannels));
  193. jassert (isPositiveAndBelow (destSample, numSamples));
  194. channels[destChannel][startSample + destSample] += valueToAdd;
  195. }
  196. //==============================================================================
  197. /** Clear the memory described by this AudioBlock. */
  198. forcedinline AudioBlock& clear() noexcept
  199. {
  200. auto n = static_cast<int> (numSamples * sizeFactor);
  201. for (size_t ch = 0; ch < numChannels; ++ch)
  202. FloatVectorOperations::clear (channelPtr (ch), n);
  203. return *this;
  204. }
  205. /** Fill memory with value. */
  206. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE fill (SampleType value) noexcept
  207. {
  208. auto n = static_cast<int> (numSamples * sizeFactor);
  209. for (size_t ch = 0; ch < numChannels; ++ch)
  210. FloatVectorOperations::fill (channelPtr (ch), value, n);
  211. return *this;
  212. }
  213. /** Copy the values in src to the receiver. */
  214. forcedinline AudioBlock& copy (AudioBlock src) noexcept
  215. {
  216. auto maxChannels = jmin (src.numChannels, numChannels);
  217. auto n = static_cast<int> (jmin (src.numSamples, numSamples) * sizeFactor);
  218. for (size_t ch = 0; ch < maxChannels; ++ch)
  219. FloatVectorOperations::copy (channelPtr (ch), src.channelPtr (ch), n);
  220. return *this;
  221. }
  222. /** Copy the values from a JUCE's AudioBuffer to the receiver.
  223. All indices and sizes are in the receiver's units, i.e. if SampleType is a
  224. SIMDRegister then incrementing srcPos by one will increase the sample position
  225. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  226. */
  227. forcedinline AudioBlock& copyFrom (const AudioBuffer<NumericType>& src, size_t srcPos = 0, size_t dstPos = 0,
  228. size_t numElements = std::numeric_limits<size_t>::max())
  229. {
  230. auto srclen = static_cast<size_t> (src.getNumSamples()) / sizeFactor;
  231. auto n = static_cast<int> (jmin (srclen - srcPos, numSamples - dstPos, numElements) * sizeFactor);
  232. auto maxChannels = jmin (static_cast<size_t> (src.getNumChannels()), static_cast<size_t> (numChannels));
  233. for (size_t ch = 0; ch < maxChannels; ++ch)
  234. FloatVectorOperations::copy (channelPtr (ch),
  235. src.getReadPointer (static_cast<int> (ch),
  236. static_cast<int> (srcPos * sizeFactor)),
  237. n);
  238. return *this;
  239. }
  240. /** Copy the values from the receiver to a JUCE's AudioBuffer.
  241. All indices and sizes are in the receiver's units, i.e. if SampleType is a
  242. SIMDRegister then incrementing dstPos by one will increase the sample position
  243. in the AudioBuffer's units by a factor of SIMDRegister<SampleType>::SIMDNumElements.
  244. */
  245. forcedinline const AudioBlock& copyTo (AudioBuffer<NumericType>& dst, size_t srcPos = 0, size_t dstPos = 0,
  246. size_t numElements = std::numeric_limits<size_t>::max()) const
  247. {
  248. auto dstlen = static_cast<size_t> (dst.getNumSamples()) / sizeFactor;
  249. auto n = static_cast<int> (jmin (numSamples - srcPos, dstlen - dstPos, numElements) * sizeFactor);
  250. auto maxChannels = jmin (static_cast<size_t> (dst.getNumChannels()), static_cast<size_t> (numChannels));
  251. for (size_t ch = 0; ch < maxChannels; ++ch)
  252. FloatVectorOperations::copy (dst.getWritePointer (static_cast<int> (ch),
  253. static_cast<int> (dstPos * sizeFactor)),
  254. channelPtr (ch), n);
  255. return *this;
  256. }
  257. /** Move memory within the receiver from the position srcPos to the position dstPos.
  258. If numElements is not specified then move will move the maximum amount of memory.
  259. */
  260. forcedinline AudioBlock& move (size_t srcPos, size_t dstPos,
  261. size_t numElements = std::numeric_limits<size_t>::max()) noexcept
  262. {
  263. jassert (srcPos <= numSamples && dstPos <= numSamples);
  264. auto len = jmin (numSamples - srcPos, numSamples - dstPos, numElements) * sizeof (SampleType);
  265. if (len != 0)
  266. for (size_t ch = 0; ch < numChannels; ++ch)
  267. ::memmove (getChannelPointer (ch) + dstPos,
  268. getChannelPointer (ch) + srcPos, len);
  269. return *this;
  270. }
  271. //==============================================================================
  272. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  273. function does not copy the memory and you must ensure that the original memory
  274. pointed to by the receiver remains valid through-out the life-time of the
  275. returned sub-block.
  276. @param newOffset The index of an element inside the reciever which will
  277. will become the first element of the return value.
  278. @param newLength The number of elements of the newly created sub-block.
  279. */
  280. inline AudioBlock getSubBlock (size_t newOffset, size_t newLength) const noexcept
  281. {
  282. jassert (newOffset < numSamples);
  283. jassert (newOffset + newLength <= numSamples);
  284. return AudioBlock (channels, numChannels, startSample + newOffset, newLength);
  285. }
  286. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  287. function does not copy the memory and you must ensure that the original memory
  288. pointed to by the receiver remains valid through-out the life-time of the
  289. returned sub-block.
  290. @param newOffset The index of an element inside the reciever which will
  291. will become the first element of the return value.
  292. The return value will include all subsequent elements
  293. of the receiver.
  294. */
  295. inline AudioBlock getSubBlock (size_t newOffset) const noexcept
  296. {
  297. return getSubBlock (newOffset, getNumSamples() - newOffset);
  298. }
  299. //==============================================================================
  300. /** Adds a fixed value to the receiver. */
  301. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (SampleType value) noexcept
  302. {
  303. auto n = static_cast<int> (numSamples * sizeFactor);
  304. for (size_t ch = 0; ch < numChannels; ++ch)
  305. FloatVectorOperations::add (channelPtr (ch), value, n);
  306. return *this;
  307. }
  308. /** Adds the source values to the receiver. */
  309. forcedinline AudioBlock& add (AudioBlock src) noexcept
  310. {
  311. jassert (numChannels == src.numChannels);
  312. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  313. for (size_t ch = 0; ch < numChannels; ++ch)
  314. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), n);
  315. return *this;
  316. }
  317. /** Adds a fixed value to each source value and stores it in the destination array of the receiver. */
  318. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (AudioBlock src, SampleType value) noexcept
  319. {
  320. jassert (numChannels == src.numChannels);
  321. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  322. for (size_t ch = 0; ch < numChannels; ++ch)
  323. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), value, n);
  324. return *this;
  325. }
  326. /** Adds each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  327. forcedinline AudioBlock& add (AudioBlock src1, AudioBlock src2) noexcept
  328. {
  329. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  330. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  331. for (size_t ch = 0; ch < numChannels; ++ch)
  332. FloatVectorOperations::add (channelPtr (ch), src1.channelPtr (ch), src2.getChannelPointer (ch), n);
  333. return *this;
  334. }
  335. /** Subtracts a fixed value from the receiver. */
  336. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (SampleType value) noexcept
  337. {
  338. return add (value * static_cast<SampleType> (-1.0));
  339. }
  340. /** Subtracts the source values from the receiver. */
  341. forcedinline AudioBlock& subtract (AudioBlock src) noexcept
  342. {
  343. jassert (numChannels == src.numChannels);
  344. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  345. for (size_t ch = 0; ch < numChannels; ++ch)
  346. FloatVectorOperations::subtract (channelPtr (ch), src.channelPtr (ch), n);
  347. return *this;
  348. }
  349. /** Subtracts a fixed value from each source value and stores it in the destination array of the receiver. */
  350. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (AudioBlock src, SampleType value) noexcept
  351. {
  352. return add (src, static_cast<SampleType> (-1.0) * value);
  353. }
  354. /** Subtracts each source2 value from the corresponding source1 value and stores it in the destination array of the receiver. */
  355. forcedinline AudioBlock& subtract (AudioBlock src1, AudioBlock src2) noexcept
  356. {
  357. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  358. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  359. for (size_t ch = 0; ch < numChannels; ++ch)
  360. FloatVectorOperations::subtract (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  361. return *this;
  362. }
  363. /** Multiplies a fixed value to the receiver. */
  364. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (SampleType value) noexcept
  365. {
  366. auto n = static_cast<int> (numSamples * sizeFactor);
  367. for (size_t ch = 0; ch < numChannels; ++ch)
  368. FloatVectorOperations::multiply (channelPtr (ch), value, n);
  369. return *this;
  370. }
  371. /** Multiplies the source values to the receiver. */
  372. forcedinline AudioBlock& multiply (AudioBlock src) noexcept
  373. {
  374. jassert (numChannels == src.numChannels);
  375. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  376. for (size_t ch = 0; ch < numChannels; ++ch)
  377. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), n);
  378. return *this;
  379. }
  380. /** Multiplies a fixed value to each source value and stores it in the destination array of the receiver. */
  381. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (AudioBlock src, SampleType value) noexcept
  382. {
  383. jassert (numChannels == src.numChannels);
  384. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  385. for (size_t ch = 0; ch < numChannels; ++ch)
  386. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), value, n);
  387. return *this;
  388. }
  389. /** Multiplies each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  390. forcedinline AudioBlock& multiply (AudioBlock src1, AudioBlock src2) noexcept
  391. {
  392. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  393. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  394. for (size_t ch = 0; ch < numChannels; ++ch)
  395. FloatVectorOperations::multiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  396. return *this;
  397. }
  398. /** Multiplies all channels of the AudioBlock by a smoothly changing value and stores them . */
  399. AudioBlock& multiply (LinearSmoothedValue<SampleType>& value) noexcept
  400. {
  401. if (! value.isSmoothing())
  402. {
  403. *this *= value.getTargetValue();
  404. }
  405. else
  406. {
  407. for (size_t i = 0; i < numSamples; ++i)
  408. {
  409. const auto scaler = value.getNextValue();
  410. for (size_t ch = 0; ch < numChannels; ++ch)
  411. channelPtr (ch)[i] *= scaler;
  412. }
  413. }
  414. return *this;
  415. }
  416. /** Multiplies all channels of the source by a smoothly changing value and stores them in the receiver. */
  417. AudioBlock& multiply (AudioBlock src, LinearSmoothedValue<SampleType>& value) noexcept
  418. {
  419. jassert (numChannels == src.numChannels);
  420. if (! value.isSmoothing())
  421. {
  422. multiply (src, value.getTargetValue());
  423. }
  424. else
  425. {
  426. auto n = jmin (numSamples, src.numSamples) * sizeFactor;
  427. for (size_t i = 0; i < n; ++i)
  428. {
  429. const auto scaler = value.getNextValue();
  430. for (size_t ch = 0; ch < numChannels; ++ch)
  431. channelPtr (ch)[i] = scaler * src.getChannelPointer (ch)[i];
  432. }
  433. }
  434. return *this;
  435. }
  436. /** Multiplies each value in src with factor and adds the result to the receiver. */
  437. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE addWithMultiply (AudioBlock src, SampleType factor) noexcept
  438. {
  439. jassert (numChannels == src.numChannels);
  440. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  441. for (size_t ch = 0; ch < numChannels; ++ch)
  442. FloatVectorOperations::addWithMultiply (channelPtr (ch), src.channelPtr (ch), factor, n);
  443. return *this;
  444. }
  445. /** Multiplies each value in srcA with the corresponding value in srcB and adds the result to the receiver. */
  446. forcedinline AudioBlock& addWithMultiply (AudioBlock src1, AudioBlock src2) noexcept
  447. {
  448. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  449. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  450. for (size_t ch = 0; ch < numChannels; ++ch)
  451. FloatVectorOperations::addWithMultiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  452. return *this;
  453. }
  454. /** Negates each value of the receiver. */
  455. forcedinline AudioBlock& negate() noexcept
  456. {
  457. return multiply (static_cast<SampleType> (-1.0));
  458. }
  459. /** Negates each value of source and stores it in the receiver. */
  460. forcedinline AudioBlock& replaceWithNegativeOf (AudioBlock src) noexcept
  461. {
  462. jassert (numChannels == src.numChannels);
  463. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  464. for (size_t ch = 0; ch < numChannels; ++ch)
  465. FloatVectorOperations::negate (channelPtr (ch), src.channelPtr (ch), n);
  466. return *this;
  467. }
  468. /** Takes the absolute value of each element of src and stores it inside the receiver. */
  469. forcedinline AudioBlock& replaceWithAbsoluteValueOf (AudioBlock src) noexcept
  470. {
  471. jassert (numChannels == src.numChannels);
  472. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  473. for (size_t ch = 0; ch < numChannels; ++ch)
  474. FloatVectorOperations::abs (channelPtr (ch), src.channelPtr (ch), n);
  475. return *this;
  476. }
  477. /** Each element of receiver will be the minimum of the corresponding element of the source arrays. */
  478. forcedinline AudioBlock& min (AudioBlock src1, AudioBlock src2) noexcept
  479. {
  480. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  481. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  482. for (size_t ch = 0; ch < numChannels; ++ch)
  483. FloatVectorOperations::min (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  484. return *this;
  485. }
  486. /** Each element of the receiver will be the maximum of the corresponding element of the source arrays. */
  487. forcedinline AudioBlock& max (AudioBlock src1, AudioBlock src2) noexcept
  488. {
  489. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  490. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  491. for (size_t ch = 0; ch < numChannels; ++ch)
  492. FloatVectorOperations::max (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  493. return *this;
  494. }
  495. /** Finds the minimum and maximum value of the buffer. */
  496. forcedinline Range<NumericType> findMinAndMax() const noexcept
  497. {
  498. if (numChannels == 0)
  499. return {};
  500. auto n = static_cast<int> (numSamples * sizeFactor);
  501. auto minmax = FloatVectorOperations::findMinAndMax (channelPtr (0), n);
  502. for (size_t ch = 1; ch < numChannels; ++ch)
  503. minmax = minmax.getUnionWith (FloatVectorOperations::findMinAndMax (channelPtr (ch), n));
  504. return minmax;
  505. }
  506. //==============================================================================
  507. // convenient operator wrappers
  508. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (SampleType src) noexcept { return add (src); }
  509. forcedinline AudioBlock& operator+= (AudioBlock src) noexcept { return add (src); }
  510. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (SampleType src) noexcept { return subtract (src); }
  511. forcedinline AudioBlock& operator-= (AudioBlock src) noexcept { return subtract (src); }
  512. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (SampleType src) noexcept { return multiply (src); }
  513. forcedinline AudioBlock& operator*= (AudioBlock src) noexcept { return multiply (src); }
  514. forcedinline AudioBlock& operator*= (LinearSmoothedValue<SampleType>& value) noexcept { return multiply (value); }
  515. //==============================================================================
  516. // This class can only be used with floating point types
  517. static_assert (std::is_same<SampleType, float>::value
  518. || std::is_same<SampleType, double>::value
  519. #if JUCE_USE_SIMD
  520. || std::is_same<SampleType, SIMDRegister<float>>::value
  521. || std::is_same<SampleType, SIMDRegister<double>>::value
  522. #endif
  523. , "AudioBlock only supports single or double precision floating point types");
  524. //==============================================================================
  525. /** Applies a function to each value in an input block, putting the result into an output block.
  526. The function supplied must take a SampleType as its parameter, and return a SampleType.
  527. The two blocks must have the same number of channels and samples.
  528. */
  529. template <typename FunctionType>
  530. static void process (AudioBlock inBlock, AudioBlock outBlock, FunctionType&& function)
  531. {
  532. auto len = inBlock.getNumSamples();
  533. auto numChans = inBlock.getNumChannels();
  534. jassert (len == outBlock.getNumSamples());
  535. jassert (numChans == outBlock.getNumChannels());
  536. for (ChannelCountType c = 0; c < numChans; ++c)
  537. {
  538. auto* src = inBlock.getChannelPointer (c);
  539. auto* dst = outBlock.getChannelPointer (c);
  540. for (size_t i = 0; i < len; ++i)
  541. dst[i] = function (src[i]);
  542. }
  543. }
  544. private:
  545. //==============================================================================
  546. NumericType* channelPtr (size_t ch) noexcept { return reinterpret_cast<NumericType*> (getChannelPointer (ch)); }
  547. const NumericType* channelPtr (size_t ch) const noexcept { return reinterpret_cast<const NumericType*> (getChannelPointer (ch)); }
  548. //==============================================================================
  549. using ChannelCountType = unsigned int;
  550. //==============================================================================
  551. static constexpr size_t sizeFactor = sizeof (SampleType) / sizeof (NumericType);
  552. static constexpr size_t elementMask = sizeFactor - 1;
  553. static constexpr size_t byteMask = (sizeFactor * sizeof (NumericType)) - 1;
  554. #if JUCE_USE_SIMD
  555. static constexpr size_t defaultAlignment = sizeof (SIMDRegister<NumericType>);
  556. #else
  557. static constexpr size_t defaultAlignment = sizeof (NumericType);
  558. #endif
  559. SampleType* const* channels;
  560. ChannelCountType numChannels = 0;
  561. size_t startSample = 0, numSamples = 0;
  562. };
  563. } // namespace dsp
  564. } // namespace juce