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.

550 lines
24KB

  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 AudioSampleBuffer 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) noexcept
  84. : numChannels (static_cast<ChannelCountType> (numberOfChannels)),
  85. numSamples (numberOfSamples)
  86. {
  87. auto roundedUpNumSamples = (numberOfSamples + elementMask) & ~elementMask;
  88. auto channelSize = sizeof (SampleType) * roundedUpNumSamples;
  89. auto channelListBytes = sizeof (SampleType*) * numberOfChannels;
  90. auto extraBytes = sizeof (SampleType) - 1;
  91. heapBlockToUseForAllocation.malloc (channelListBytes + extraBytes + channelSize * numberOfChannels);
  92. auto* chanArray = reinterpret_cast<SampleType**> (heapBlockToUseForAllocation.getData());
  93. channels = chanArray;
  94. auto* data = reinterpret_cast<SampleType*> (addBytesToPointer (chanArray, channelListBytes));
  95. data = snapPointerToAlignment (data, sizeof (SampleType));
  96. for (ChannelCountType i = 0; i < numChannels; ++i)
  97. {
  98. chanArray[i] = data;
  99. data += roundedUpNumSamples;
  100. }
  101. }
  102. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  103. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  104. Therefore it is the user's responsibility to ensure that the buffer is retained
  105. throughout the life-time of the AudioBlock without being modified.
  106. */
  107. AudioBlock (AudioBuffer<SampleType>& buffer) noexcept
  108. : channels (buffer.getArrayOfWritePointers()),
  109. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  110. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  111. {
  112. }
  113. /** Creates an AudioBlock that points to the data in an AudioBuffer.
  114. AudioBlock does not copy nor own the memory pointed to by dataToUse.
  115. Therefore it is the user's responsibility to ensure that the buffer is retained
  116. throughout the life-time of the AudioBlock without being modified.
  117. */
  118. AudioBlock (AudioBuffer<SampleType>& buffer, size_t startSampleIndex) noexcept
  119. : channels (buffer.getArrayOfWritePointers()),
  120. numChannels (static_cast<ChannelCountType> (buffer.getNumChannels())),
  121. startSample (startSampleIndex),
  122. numSamples (static_cast<size_t> (buffer.getNumSamples()))
  123. {
  124. jassert (startSample < numSamples);
  125. }
  126. AudioBlock (const AudioBlock& other) noexcept = default;
  127. AudioBlock& operator= (const AudioBlock& other) noexcept = default;
  128. //==============================================================================
  129. forcedinline size_t getNumSamples() const noexcept { return numSamples; }
  130. forcedinline size_t getNumChannels() const noexcept { return static_cast<size_t> (numChannels); }
  131. /** Returns a raw pointer into one of the channels in this block. */
  132. forcedinline const SampleType* getChannelPointer (size_t channel) const noexcept
  133. {
  134. jassert (channel < numChannels);
  135. jassert (numSamples > 0);
  136. return *(channels + channel) + startSample;
  137. }
  138. /** Returns a raw pointer into one of the channels in this block. */
  139. forcedinline SampleType* getChannelPointer (size_t channel) noexcept
  140. {
  141. jassert (channel < numChannels);
  142. jassert (numSamples > 0);
  143. return *(channels + channel) + startSample;
  144. }
  145. /** Returns an AudioBlock that represents one of the channels in this block. */
  146. forcedinline AudioBlock<SampleType> getSingleChannelBlock (size_t channel) const noexcept
  147. {
  148. jassert (channel < numChannels);
  149. return AudioBlock (channels + channel, 1, startSample, numSamples);
  150. }
  151. /** Returns a subset of continguous channels
  152. @param channelStart First channel of the subset
  153. @param numChannelsToUse Count of channels in the subset
  154. */
  155. forcedinline AudioBlock<SampleType> getSubsetChannelBlock (size_t channelStart, size_t numChannelsToUse) noexcept
  156. {
  157. jassert (channelStart < numChannels);
  158. jassert ((channelStart + numChannelsToUse) <= numChannels);
  159. return AudioBlock (channels + channelStart, numChannelsToUse, startSample, numSamples);
  160. }
  161. //==============================================================================
  162. /** Clear the memory described by this AudioBlock. */
  163. forcedinline AudioBlock& clear() noexcept
  164. {
  165. auto n = static_cast<int> (numSamples * sizeFactor);
  166. for (size_t ch = 0; ch < numChannels; ++ch)
  167. FloatVectorOperations::clear (channelPtr (ch), n);
  168. return *this;
  169. }
  170. /** Fill memory with value. */
  171. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE fill (SampleType value) noexcept
  172. {
  173. auto n = static_cast<int> (numSamples * sizeFactor);
  174. for (size_t ch = 0; ch < numChannels; ++ch)
  175. FloatVectorOperations::fill (channelPtr (ch), value, n);
  176. return *this;
  177. }
  178. /** Copy the values in src to the receiver. */
  179. forcedinline AudioBlock& copy (const AudioBlock& src) noexcept
  180. {
  181. auto maxChannels = jmin (src.numChannels, numChannels);
  182. auto n = static_cast<int> (jmin (src.numSamples, numSamples) * sizeFactor);
  183. for (size_t ch = 0; ch < maxChannels; ++ch)
  184. FloatVectorOperations::copy (channelPtr (ch), src.channelPtr (ch), n);
  185. return *this;
  186. }
  187. /** Move memory within the receiver from the position srcPos to the position dstPos.
  188. If numElements is not specified then move will move the maximum amount of memory.
  189. */
  190. forcedinline AudioBlock& move (size_t srcPos, size_t dstPos,
  191. size_t numElements = std::numeric_limits<size_t>::max()) noexcept
  192. {
  193. jassert (srcPos <= numSamples && dstPos <= numSamples);
  194. auto len = jmin (numSamples - srcPos, numSamples - dstPos, numElements) * sizeof (SampleType);
  195. if (len != 0)
  196. for (size_t ch = 0; ch < numChannels; ++ch)
  197. ::memmove (getChannelPointer (ch) + dstPos,
  198. getChannelPointer (ch) + srcPos, len);
  199. return *this;
  200. }
  201. //==============================================================================
  202. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  203. function does not copy the memory and you must ensure that the original memory
  204. pointed to by the receiver remains valid through-out the life-time of the
  205. returned sub-block.
  206. @param newOffset The index of an element inside the reciever which will
  207. will become the first element of the return value.
  208. @param newLength The number of elements of the newly created sub-block.
  209. */
  210. inline AudioBlock getSubBlock (size_t newOffset, size_t newLength) const noexcept
  211. {
  212. jassert (newOffset < numSamples);
  213. jassert (newOffset + newLength <= numSamples);
  214. return AudioBlock (channels, numChannels, startSample + newOffset, newLength);
  215. }
  216. /** Return a new AudioBlock pointing to a sub-block inside the receiver. This
  217. function does not copy the memory and you must ensure that the original memory
  218. pointed to by the receiver remains valid through-out the life-time of the
  219. returned sub-block.
  220. @param newOffset The index of an element inside the reciever which will
  221. will become the first element of the return value.
  222. The return value will include all subsequent elements
  223. of the receiver.
  224. */
  225. inline AudioBlock getSubBlock (size_t newOffset) const noexcept
  226. {
  227. return getSubBlock (newOffset, getNumSamples() - newOffset);
  228. }
  229. //==============================================================================
  230. /** Adds a fixed value to the receiver. */
  231. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (SampleType value) noexcept
  232. {
  233. auto n = static_cast<int> (numSamples * sizeFactor);
  234. for (size_t ch = 0; ch < numChannels; ++ch)
  235. FloatVectorOperations::add (channelPtr (ch), value, n);
  236. return *this;
  237. }
  238. /** Adds the source values to the receiver. */
  239. forcedinline AudioBlock& add (const AudioBlock& src) noexcept
  240. {
  241. jassert (numChannels == src.numChannels);
  242. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  243. for (size_t ch = 0; ch < numChannels; ++ch)
  244. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), n);
  245. return *this;
  246. }
  247. /** Adds a fixed value to each source value and stores it in the destination array of the receiver. */
  248. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE add (const AudioBlock& src, SampleType value) noexcept
  249. {
  250. jassert (numChannels == src.numChannels);
  251. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  252. for (size_t ch = 0; ch < numChannels; ++ch)
  253. FloatVectorOperations::add (channelPtr (ch), src.channelPtr (ch), value, n);
  254. return *this;
  255. }
  256. /** Adds each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  257. forcedinline AudioBlock& add (const AudioBlock& src1, const AudioBlock& src2) noexcept
  258. {
  259. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  260. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  261. for (size_t ch = 0; ch < numChannels; ++ch)
  262. FloatVectorOperations::add (channelPtr (ch), src1.channelPtr (ch), src2.getChannelPointer (ch), n);
  263. return *this;
  264. }
  265. /** Subtracts a fixed value from the receiver. */
  266. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (SampleType value) noexcept
  267. {
  268. return add (value * static_cast<SampleType> (-1.0));
  269. }
  270. /** Subtracts the source values from the receiver. */
  271. forcedinline AudioBlock& subtract (const AudioBlock& src) noexcept
  272. {
  273. jassert (numChannels == src.numChannels);
  274. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  275. for (size_t ch = 0; ch < numChannels; ++ch)
  276. FloatVectorOperations::subtract (channelPtr (ch), src.channelPtr (ch), n);
  277. return *this;
  278. }
  279. /** Subtracts a fixed value from each source value and stores it in the destination array of the receiver. */
  280. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE subtract (const AudioBlock& src, SampleType value) noexcept
  281. {
  282. return add (src, static_cast<SampleType> (-1.0) * value);
  283. }
  284. /** Subtracts each source2 value from the corresponding source1 value and stores it in the destination array of the receiver. */
  285. forcedinline AudioBlock& subtract (const AudioBlock& src1, const AudioBlock& src2) noexcept
  286. {
  287. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  288. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  289. for (size_t ch = 0; ch < numChannels; ++ch)
  290. FloatVectorOperations::subtract (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  291. return *this;
  292. }
  293. /** Multiplies a fixed value to the receiver. */
  294. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (SampleType value) noexcept
  295. {
  296. auto n = static_cast<int> (numSamples * sizeFactor);
  297. for (size_t ch = 0; ch < numChannels; ++ch)
  298. FloatVectorOperations::multiply (channelPtr (ch), value, n);
  299. return *this;
  300. }
  301. /** Multiplies the source values to the receiver. */
  302. forcedinline AudioBlock& multiply (const AudioBlock& src) noexcept
  303. {
  304. jassert (numChannels == src.numChannels);
  305. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  306. for (size_t ch = 0; ch < numChannels; ++ch)
  307. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), n);
  308. return *this;
  309. }
  310. /** Multiplies a fixed value to each source value and stores it in the destination array of the receiver. */
  311. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE multiply (const AudioBlock& src, SampleType value) noexcept
  312. {
  313. jassert (numChannels == src.numChannels);
  314. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  315. for (size_t ch = 0; ch < numChannels; ++ch)
  316. FloatVectorOperations::multiply (channelPtr (ch), src.channelPtr (ch), value, n);
  317. return *this;
  318. }
  319. /** Multiplies each source1 value to the corresponding source2 value and stores it in the destination array of the receiver. */
  320. forcedinline AudioBlock& multiply (const AudioBlock& src1, const AudioBlock& src2) noexcept
  321. {
  322. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  323. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  324. for (size_t ch = 0; ch < numChannels; ++ch)
  325. FloatVectorOperations::multiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  326. return *this;
  327. }
  328. /** Multiplies each value in src with factor and adds the result to the receiver. */
  329. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE addWithMultiply (const AudioBlock& src, SampleType factor) noexcept
  330. {
  331. jassert (numChannels == src.numChannels);
  332. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  333. for (size_t ch = 0; ch < numChannels; ++ch)
  334. FloatVectorOperations::addWithMultiply (channelPtr (ch), src.channelPtr (ch), factor, n);
  335. return *this;
  336. }
  337. /** Multiplies each value in srcA with the corresponding value in srcB and adds the result to the receiver. */
  338. forcedinline AudioBlock& addWithMultiply (const AudioBlock& src1, const AudioBlock& src2) noexcept
  339. {
  340. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  341. auto n = static_cast<int> (jmin (numSamples, src1.numSamples, src2.numSamples) * sizeFactor);
  342. for (size_t ch = 0; ch < numChannels; ++ch)
  343. FloatVectorOperations::addWithMultiply (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  344. return *this;
  345. }
  346. /** Negates each value of the receiver. */
  347. forcedinline AudioBlock& negate() noexcept
  348. {
  349. return multiply (static_cast<SampleType> (-1.0));
  350. }
  351. /** Negates each value of source and stores it in the receiver. */
  352. forcedinline AudioBlock& replaceWithNegativeOf (const AudioBlock& src) noexcept
  353. {
  354. jassert (numChannels == src.numChannels);
  355. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  356. for (size_t ch = 0; ch < numChannels; ++ch)
  357. FloatVectorOperations::negate (channelPtr (ch), src.channelPtr (ch), n);
  358. return *this;
  359. }
  360. /** Takes the absolute value of each element of src and stores it inside the receiver. */
  361. forcedinline AudioBlock& replaceWithAbsoluteValueOf (const AudioBlock& src) noexcept
  362. {
  363. jassert (numChannels == src.numChannels);
  364. auto n = static_cast<int> (jmin (numSamples, src.numSamples) * sizeFactor);
  365. for (size_t ch = 0; ch < numChannels; ++ch)
  366. FloatVectorOperations::abs (channelPtr (ch), src.channelPtr (ch), n);
  367. return *this;
  368. }
  369. /** Each element of receiver will be the minimum of the corresponding element of the source arrays. */
  370. forcedinline AudioBlock& min (const AudioBlock& src1, const AudioBlock& src2) noexcept
  371. {
  372. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  373. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  374. for (size_t ch = 0; ch < numChannels; ++ch)
  375. FloatVectorOperations::min (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  376. return *this;
  377. }
  378. /** Each element of the receiver will be the maximum of the corresponding element of the source arrays. */
  379. forcedinline AudioBlock& max (AudioBlock src1, AudioBlock src2) noexcept
  380. {
  381. jassert (numChannels == src1.numChannels && src1.numChannels == src2.numChannels);
  382. auto n = static_cast<int> (jmin (src1.numSamples, src2.numSamples, numSamples) * sizeFactor);
  383. for (size_t ch = 0; ch < numChannels; ++ch)
  384. FloatVectorOperations::max (channelPtr (ch), src1.channelPtr (ch), src2.channelPtr (ch), n);
  385. return *this;
  386. }
  387. /** Finds the minimum and maximum value of the buffer. */
  388. forcedinline Range<NumericType> findMinAndMax() const noexcept
  389. {
  390. Range<NumericType> minmax;
  391. auto n = static_cast<int> (numSamples * sizeFactor);
  392. for (size_t ch = 0; ch < numChannels; ++ch)
  393. minmax = minmax.getUnionWith (FloatVectorOperations::findMinAndMax (channelPtr (ch), n));
  394. return minmax;
  395. }
  396. //==============================================================================
  397. // convenient operator wrappers
  398. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator+= (SampleType src) noexcept { return add (src); }
  399. forcedinline AudioBlock& operator+= (AudioBlock src) noexcept { return add (src); }
  400. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator-= (SampleType src) noexcept { return subtract (src); }
  401. forcedinline AudioBlock& operator-= (AudioBlock src) noexcept { return subtract (src); }
  402. forcedinline AudioBlock& JUCE_VECTOR_CALLTYPE operator*= (SampleType src) noexcept { return multiply (src); }
  403. forcedinline AudioBlock& operator*= (AudioBlock src) noexcept { return multiply (src); }
  404. //==============================================================================
  405. // This class can only be used with floating point types
  406. static_assert (std::is_same<SampleType, float>::value || std::is_same<SampleType, double>::value
  407. #if JUCE_USE_SIMD
  408. || std::is_same<SampleType, SIMDRegister<float> >::value || std::is_same<SampleType, SIMDRegister<double> >::value
  409. #endif
  410. , "AudioBlock only supports single or double precision floating point types");
  411. //==============================================================================
  412. template <typename FunctionType>
  413. static void process (const AudioBlock<SampleType>& inBlock,
  414. AudioBlock<SampleType>& outBlock,
  415. const FunctionType& function)
  416. {
  417. auto len = inBlock.getNumSamples();
  418. auto numChans = inBlock.getNumChannels();
  419. for (ChannelCountType c = 0; c < numChans; ++c)
  420. {
  421. auto* src = inBlock.getChannelPointer(c);
  422. auto* dst = outBlock.getChannelPointer(c);
  423. for (size_t i = 0; i < len; ++i)
  424. dst[i] = function (src[i]);
  425. }
  426. }
  427. private:
  428. //==============================================================================
  429. NumericType* channelPtr (size_t ch) noexcept { return reinterpret_cast<NumericType*> (getChannelPointer (ch)); }
  430. const NumericType* channelPtr (size_t ch) const noexcept { return reinterpret_cast<const NumericType*> (getChannelPointer (ch)); }
  431. //==============================================================================
  432. using ChannelCountType = unsigned int;
  433. //==============================================================================
  434. static constexpr size_t sizeFactor = sizeof (SampleType) / sizeof (NumericType);
  435. static constexpr size_t elementMask = sizeFactor - 1;
  436. static constexpr size_t byteMask = (sizeFactor * sizeof (NumericType)) - 1;
  437. SampleType* const* channels;
  438. ChannelCountType numChannels = 0;
  439. size_t startSample = 0, numSamples = 0;
  440. };
  441. } // namespace dsp
  442. } // namespace juce