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.

652 lines
28KB

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