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.

343 lines
12KB

  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. class AudioBlockUnitTests : public UnitTest
  24. {
  25. public:
  26. AudioBlockUnitTests()
  27. : UnitTest ("AudioBlock", UnitTestCategories::dsp)
  28. {}
  29. void runTest() override
  30. {
  31. beginTest ("Equality");
  32. {
  33. expect (block == block);
  34. expect (block != otherBlock);
  35. }
  36. beginTest ("Constructors");
  37. {
  38. expect (block == AudioBlock<float> (data.getArrayOfWritePointers(), (size_t) data.getNumChannels(), (size_t) data.getNumSamples()));
  39. expect (block == AudioBlock<float> (data.getArrayOfWritePointers(), (size_t) data.getNumChannels(), (size_t) 0, (size_t) data.getNumSamples()));
  40. expect (block == AudioBlock<float> (block));
  41. expect (block == AudioBlock<const float> (data.getArrayOfWritePointers(), (size_t) data.getNumChannels(), (size_t) data.getNumSamples()));
  42. expect (block == AudioBlock<const float> (data.getArrayOfWritePointers(), (size_t) data.getNumChannels(), (size_t) 0, (size_t) data.getNumSamples()));
  43. expect (block == AudioBlock<const float> (block));
  44. }
  45. beginTest ("Swap");
  46. {
  47. resetBlocks();
  48. expect (block != otherBlock);
  49. expectEquals (block.getSample (0, 0), 1.0f);
  50. expectEquals (block.getSample (0, 4), 5.0f);
  51. expectEquals (otherBlock.getSample (0, 0), -1.0f);
  52. expectEquals (otherBlock.getSample (0, 3), -4.0f);
  53. block.swap (otherBlock);
  54. expect (block != otherBlock);
  55. expectEquals (otherBlock.getSample (0, 0), 1.0f);
  56. expectEquals (otherBlock.getSample (0, 4), 5.0f);
  57. expectEquals (block.getSample (0, 0), -1.0f);
  58. expectEquals (block.getSample (0, 3), -4.0f);
  59. }
  60. beginTest ("Getters and setters");
  61. {
  62. resetBlocks();
  63. expectEquals ((int) block.getNumChannels(), data.getNumChannels());
  64. expectEquals ((int) block.getNumSamples(), data.getNumSamples());
  65. expectEquals (block.getChannelPointer (0)[2], 3.0f);
  66. block.getChannelPointer (0)[2] = 999.0f;
  67. expectEquals (block.getChannelPointer (0)[2], 999.0f);
  68. expectEquals (block.getSample (0, 4), 5.0f);
  69. expectEquals (block.getSample (1, 4), 11.0f);
  70. expectEquals (block.getSingleChannelBlock (1).getSample (0, 3), block.getSample (1, 3));
  71. expectEquals (block.getSubsetChannelBlock (0, 2).getSample (1, 3), block.getSample (1, 3));
  72. expectEquals (block.getSubsetChannelBlock (1, 1).getSample (0, 3), block.getSample (1, 3));
  73. block.setSample (1, 1, 777.0f);
  74. expectEquals (block.getSample (1, 1), 777.0f);
  75. block.addSample (1, 1, 1.0f);
  76. expectEquals (block.getSample (1, 1), 778.0f);
  77. }
  78. beginTest ("Copying");
  79. {
  80. block.clear();
  81. expectEquals (block.getSample (0, 2), 0.0f);
  82. expectEquals (block.getSample (1, 4), 0.0f);
  83. block.fill (456.0f);
  84. expectEquals (block.getSample (0, 2), 456.0f);
  85. expectEquals (block.getSample (1, 4), 456.0f);
  86. block.copyFrom (otherBlock);
  87. expect (block != otherBlock);
  88. expectEquals (block.getSample (0, 2), otherBlock.getSample (0, 2));
  89. expectEquals (block.getSample (1, 4), otherBlock.getSample (1, 4));
  90. resetBlocks();
  91. AudioBuffer<float> otherBuffer ((int) block.getNumChannels(), (int) block.getNumSamples());
  92. otherBlock.copyTo (otherBuffer);
  93. expectEquals (otherBlock.getSample (0, 2), otherBuffer.getSample (0, 2));
  94. expectEquals (otherBlock.getSample (1, 4), otherBuffer.getSample (1, 4));
  95. block.copyFrom (otherBuffer);
  96. expectEquals (block.getSample (0, 2), otherBlock.getSample (0, 2));
  97. expectEquals (block.getSample (1, 4), otherBlock.getSample (1, 4));
  98. float testSample1 = block.getSample (0, 2);
  99. float testSample2 = block.getSample (1, 3);
  100. expect (testSample1 != block.getSample (0, 4));
  101. expect (testSample2 != block.getSample (1, 5));
  102. block.move (0, 2);
  103. expectEquals (block.getSample (0, 4), testSample1);
  104. expectEquals (block.getSample (1, 5), testSample2);
  105. }
  106. beginTest ("Addition");
  107. {
  108. resetBlocks();
  109. block.add (15.0f);
  110. expectEquals (block.getSample (0, 4), 20.0f);
  111. expectEquals (block.getSample (1, 4), 26.0f);
  112. block.add (otherBlock);
  113. expectEquals (block.getSample (0, 4), 15.0f);
  114. expectEquals (block.getSample (1, 4), 15.0f);
  115. block.replaceWithSumOf (otherBlock, 9.0f);
  116. expectEquals (block.getSample (0, 4), 4.0f);
  117. expectEquals (block.getSample (1, 4), -2.0f);
  118. resetBlocks();
  119. block.replaceWithSumOf (block, otherBlock);
  120. expectEquals (block.getSample (0, 4), 0.0f);
  121. expectEquals (block.getSample (1, 4), 0.0f);
  122. }
  123. beginTest ("Subtraction");
  124. {
  125. resetBlocks();
  126. block.subtract (15.0f);
  127. expectEquals (block.getSample (0, 4), -10.0f);
  128. expectEquals (block.getSample (1, 4), -4.0f);
  129. block.subtract (otherBlock);
  130. expectEquals (block.getSample (0, 4), -5.0f);
  131. expectEquals (block.getSample (1, 4), 7.0f);
  132. block.replaceWithDifferenceOf (otherBlock, 9.0f);
  133. expectEquals (block.getSample (0, 4), -14.0f);
  134. expectEquals (block.getSample (1, 4), -20.0f);
  135. resetBlocks();
  136. block.replaceWithDifferenceOf (block, otherBlock);
  137. expectEquals (block.getSample (0, 4), 10.0f);
  138. expectEquals (block.getSample (1, 4), 22.0f);
  139. }
  140. beginTest ("Multiplication");
  141. {
  142. resetBlocks();
  143. block.multiplyBy (10.0f);
  144. expectEquals (block.getSample (0, 4), 50.0f);
  145. expectEquals (block.getSample (1, 4), 110.0f);
  146. block.multiplyBy (otherBlock);
  147. expectEquals (block.getSample (0, 4), -250.0f);
  148. expectEquals (block.getSample (1, 4), -1210.0f);
  149. block.replaceWithProductOf (otherBlock, 3.0f);
  150. expectEquals (block.getSample (0, 4), -15.0f);
  151. expectEquals (block.getSample (1, 4), -33.0f);
  152. resetBlocks();
  153. block.replaceWithProductOf (block, otherBlock);
  154. expectEquals (block.getSample (0, 4), -25.0f);
  155. expectEquals (block.getSample (1, 4), -121.0f);
  156. }
  157. beginTest ("Smoothing");
  158. {
  159. block.fill (1.0f);
  160. SmoothedValue<float> sv { 1.0f };
  161. sv.reset (1, 4);
  162. sv.setTargetValue (0.0f);
  163. block.multiplyBy (sv);
  164. expect (block.getSample (0, 2) < 1.0f);
  165. expect (block.getSample (1, 2) < 1.0f);
  166. expect (block.getSample (0, 2) > 0.0f);
  167. expect (block.getSample (1, 2) > 0.0f);
  168. expectEquals (block.getSample (0, 5), 0.0f);
  169. expectEquals (block.getSample (1, 5), 0.0f);
  170. sv.setCurrentAndTargetValue (-1.0f);
  171. sv.setTargetValue (0.0f);
  172. otherBlock.fill (-1.0f);
  173. block.replaceWithProductOf (otherBlock, sv);
  174. expect (block.getSample (0, 2) < 1.0f);
  175. expect (block.getSample (1, 2) < 1.0f);
  176. expect (block.getSample (0, 2) > 0.0f);
  177. expect (block.getSample (1, 2) > 0.0f);
  178. expectEquals (block.getSample (0, 5), 0.0f);
  179. expectEquals (block.getSample (1, 5), 0.0f);
  180. }
  181. beginTest ("Multiply add");
  182. {
  183. resetBlocks();
  184. block.addProductOf (otherBlock, -1.0f);
  185. expectEquals (block.getSample (0, 4), 10.0f);
  186. expectEquals (block.getSample (1, 4), 22.0f);
  187. block.addProductOf (otherBlock, otherBlock);
  188. expectEquals (block.getSample (0, 4), 35.0f);
  189. expectEquals (block.getSample (1, 4), 143.0f);
  190. }
  191. beginTest ("Negative abs min max");
  192. {
  193. resetBlocks();
  194. otherBlock.negate();
  195. block.add (otherBlock);
  196. expectEquals (block.getSample (0, 4), 10.0f);
  197. expectEquals (block.getSample (1, 4), 22.0f);
  198. block.replaceWithNegativeOf (otherBlock);
  199. expectEquals (block.getSample (0, 4), -5.0f);
  200. expectEquals (block.getSample (1, 4), -11.0f);
  201. block.clear();
  202. otherBlock.negate();
  203. block.replaceWithAbsoluteValueOf (otherBlock);
  204. expectEquals (block.getSample (0, 4), 5.0f);
  205. expectEquals (block.getSample (1, 4), 11.0f);
  206. resetBlocks();
  207. block.replaceWithMinOf (block, otherBlock);
  208. expectEquals (block.getSample (0, 4), -5.0f);
  209. expectEquals (block.getSample (1, 4), -11.0f);
  210. resetBlocks();
  211. block.replaceWithMaxOf (block, otherBlock);
  212. expectEquals (block.getSample (0, 4), 5.0f);
  213. expectEquals (block.getSample (1, 4), 11.0f);
  214. resetBlocks();
  215. auto range = block.findMinAndMax();
  216. expectEquals (range.getStart(), 1.0f);
  217. expectEquals (range.getEnd(), 12.0f);
  218. }
  219. beginTest ("Operators");
  220. {
  221. resetBlocks();
  222. block += 10.0f;
  223. expectEquals (block.getSample (0, 4), 15.0f);
  224. expectEquals (block.getSample (1, 4), 21.0f);
  225. block += otherBlock;
  226. expectEquals (block.getSample (0, 4), 10.0f);
  227. expectEquals (block.getSample (1, 4), 10.0f);
  228. resetBlocks();
  229. block -= 10.0f;
  230. expectEquals (block.getSample (0, 4), -5.0f);
  231. expectEquals (block.getSample (1, 4), 1.0f);
  232. block -= otherBlock;
  233. expectEquals (block.getSample (0, 4), 0.0f);
  234. expectEquals (block.getSample (1, 4), 12.0f);
  235. resetBlocks();
  236. block *= 10.0f;
  237. expectEquals (block.getSample (0, 4), 50.0f);
  238. expectEquals (block.getSample (1, 4), 110.0f);
  239. block *= otherBlock;
  240. expectEquals (block.getSample (0, 4), -250.0f);
  241. expectEquals (block.getSample (1, 4), -1210.0f);
  242. }
  243. beginTest ("Process");
  244. {
  245. resetBlocks();
  246. AudioBlock<float>::process (block, otherBlock, [](float x) { return x + 1.0f; });
  247. expectEquals (otherBlock.getSample (0, 4), 6.0f);
  248. expectEquals (otherBlock.getSample (1, 4), 12.0f);
  249. }
  250. }
  251. private:
  252. AudioBuffer<float> data { 2, 6 }, otherData { 2, 6 };
  253. AudioBlock<float> block { data }, otherBlock { otherData };
  254. void resetBlocks()
  255. {
  256. auto value = 1.0f;
  257. for (size_t c = 0; c < block.getNumChannels(); ++c)
  258. {
  259. for (size_t i = 0; i < block.getNumSamples(); ++i)
  260. {
  261. block.setSample ((int) c, (int) i, value);
  262. value += 1.0f;
  263. }
  264. }
  265. otherBlock.replaceWithNegativeOf (block);
  266. }
  267. };
  268. static AudioBlockUnitTests audioBlockUnitTests;
  269. } // namespace dsp
  270. } // namespace juce