Audio plugin host https://kx.studio/carla
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.

juce_MemoryAudioSource.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. MemoryAudioSource::MemoryAudioSource (AudioBuffer<float>& bufferToUse, bool copyMemory, bool shouldLoop)
  20. : isCurrentlyLooping (shouldLoop)
  21. {
  22. if (copyMemory)
  23. buffer.makeCopyOf (bufferToUse);
  24. else
  25. buffer.setDataToReferTo (bufferToUse.getArrayOfWritePointers(),
  26. bufferToUse.getNumChannels(),
  27. bufferToUse.getNumSamples());
  28. }
  29. //==============================================================================
  30. void MemoryAudioSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/)
  31. {
  32. position = 0;
  33. }
  34. void MemoryAudioSource::releaseResources() {}
  35. void MemoryAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  36. {
  37. if (buffer.getNumSamples() == 0)
  38. {
  39. bufferToFill.clearActiveBufferRegion();
  40. return;
  41. }
  42. auto& dst = *bufferToFill.buffer;
  43. auto channels = jmin (dst.getNumChannels(), buffer.getNumChannels());
  44. int max = 0, pos = 0;
  45. auto n = buffer.getNumSamples();
  46. auto m = bufferToFill.numSamples;
  47. int i = position;
  48. for (; (i < n || isCurrentlyLooping) && (pos < m); i += max)
  49. {
  50. max = jmin (m - pos, n - (i % n));
  51. int ch = 0;
  52. for (; ch < channels; ++ch)
  53. dst.copyFrom (ch, bufferToFill.startSample + pos, buffer, ch, i % n, max);
  54. for (; ch < dst.getNumChannels(); ++ch)
  55. dst.clear (ch, bufferToFill.startSample + pos, max);
  56. pos += max;
  57. }
  58. if (pos < m)
  59. dst.clear (bufferToFill.startSample + pos, m - pos);
  60. position = i;
  61. }
  62. //==============================================================================
  63. void MemoryAudioSource::setNextReadPosition (int64 newPosition)
  64. {
  65. position = (int) newPosition;
  66. }
  67. int64 MemoryAudioSource::getNextReadPosition() const
  68. {
  69. return position;
  70. }
  71. int64 MemoryAudioSource::getTotalLength() const
  72. {
  73. return buffer.getNumSamples();
  74. }
  75. //==============================================================================
  76. bool MemoryAudioSource::isLooping() const
  77. {
  78. return isCurrentlyLooping;
  79. }
  80. void MemoryAudioSource::setLooping (bool shouldLoop)
  81. {
  82. isCurrentlyLooping = shouldLoop;
  83. }
  84. //==============================================================================
  85. //==============================================================================
  86. #if JUCE_UNIT_TESTS
  87. static bool operator== (const AudioBuffer<float>& a, const AudioBuffer<float>& b)
  88. {
  89. if (a.getNumChannels() != b.getNumChannels())
  90. return false;
  91. for (int channel = 0; channel < a.getNumChannels(); ++channel)
  92. {
  93. auto* aPtr = a.getReadPointer (channel);
  94. auto* bPtr = b.getReadPointer (channel);
  95. if (std::vector<float> (aPtr, aPtr + a.getNumSamples())
  96. != std::vector<float> (bPtr, bPtr + b.getNumSamples()))
  97. {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. struct MemoryAudioSourceTests : public UnitTest
  104. {
  105. MemoryAudioSourceTests() : UnitTest ("MemoryAudioSource", UnitTestCategories::audio) {}
  106. void runTest() override
  107. {
  108. constexpr int blockSize = 512;
  109. AudioBuffer<float> bufferToFill { 2, blockSize };
  110. AudioSourceChannelInfo channelInfo { bufferToFill };
  111. beginTest ("A zero-length buffer produces silence, whether or not looping is enabled");
  112. {
  113. for (const bool enableLooping : { false, true })
  114. {
  115. AudioBuffer<float> buffer;
  116. MemoryAudioSource source { buffer, true, false };
  117. source.setLooping (enableLooping);
  118. source.prepareToPlay (blockSize, 44100.0);
  119. for (int i = 0; i < 2; ++i)
  120. {
  121. play (source, channelInfo);
  122. expect (isSilent (bufferToFill));
  123. }
  124. }
  125. }
  126. beginTest ("A short buffer without looping is played once and followed by silence");
  127. {
  128. auto buffer = getShortBuffer();
  129. MemoryAudioSource source { buffer, true, false };
  130. source.setLooping (false);
  131. source.prepareToPlay (blockSize, 44100.0);
  132. play (source, channelInfo);
  133. auto copy = buffer;
  134. copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
  135. expect (bufferToFill == copy);
  136. play (source, channelInfo);
  137. expect (isSilent (bufferToFill));
  138. }
  139. beginTest ("A short buffer with looping is played multiple times");
  140. {
  141. auto buffer = getShortBuffer();
  142. MemoryAudioSource source { buffer, true, false };
  143. source.setLooping (true);
  144. source.prepareToPlay (blockSize, 44100.0);
  145. play (source, channelInfo);
  146. for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
  147. expect (bufferToFill.getSample (0, sample + buffer.getNumSamples()) == buffer.getSample (0, sample));
  148. expect (! isSilent (bufferToFill));
  149. }
  150. beginTest ("A long buffer without looping is played once");
  151. {
  152. auto buffer = getLongBuffer();
  153. MemoryAudioSource source { buffer, true, false };
  154. source.setLooping (false);
  155. source.prepareToPlay (blockSize, 44100.0);
  156. play (source, channelInfo);
  157. auto copy = buffer;
  158. copy.setSize (buffer.getNumChannels(), blockSize, true, true, false);
  159. expect (bufferToFill == copy);
  160. for (int i = 0; i < 10; ++i)
  161. play (source, channelInfo);
  162. expect (isSilent (bufferToFill));
  163. }
  164. beginTest ("A long buffer with looping is played multiple times");
  165. {
  166. auto buffer = getLongBuffer();
  167. MemoryAudioSource source { buffer, true, false };
  168. source.setLooping (true);
  169. source.prepareToPlay (blockSize, 44100.0);
  170. for (int i = 0; i < 100; ++i)
  171. {
  172. play (source, channelInfo);
  173. expect (bufferToFill.getSample (0, 0) == buffer.getSample (0, (i * blockSize) % buffer.getNumSamples()));
  174. }
  175. }
  176. }
  177. static AudioBuffer<float> getTestBuffer (int length)
  178. {
  179. AudioBuffer<float> buffer { 2, length };
  180. for (int channel = 0; channel < buffer.getNumChannels(); ++channel)
  181. for (int sample = 0; sample < buffer.getNumSamples(); ++sample)
  182. buffer.setSample (channel, sample, jmap ((float) sample, 0.0f, (float) length, -1.0f, 1.0f));
  183. return buffer;
  184. }
  185. static AudioBuffer<float> getShortBuffer() { return getTestBuffer (5); }
  186. static AudioBuffer<float> getLongBuffer() { return getTestBuffer (1000); }
  187. static void play (MemoryAudioSource& source, AudioSourceChannelInfo& info)
  188. {
  189. info.clearActiveBufferRegion();
  190. source.getNextAudioBlock (info);
  191. }
  192. static bool isSilent (const AudioBuffer<float>& b)
  193. {
  194. for (int channel = 0; channel < b.getNumChannels(); ++channel)
  195. if (b.findMinMax (channel, 0, b.getNumSamples()) != Range<float>{})
  196. return false;
  197. return true;
  198. }
  199. };
  200. static MemoryAudioSourceTests memoryAudioSourceTests;
  201. #endif
  202. } // namespace juce