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.

780 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_AUDIOSAMPLEBUFFER_H_INCLUDED
  21. #define WATER_AUDIOSAMPLEBUFFER_H_INCLUDED
  22. #include "../memory/HeapBlock.h"
  23. #include "CarlaMathUtils.hpp"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. A multi-channel buffer of floating point audio samples.
  28. @see AudioSampleBuffer
  29. */
  30. class AudioSampleBuffer
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates an empty buffer with 0 channels and 0 length. */
  35. AudioSampleBuffer() noexcept
  36. : numChannels(0),
  37. size(0),
  38. allocatedBytes(0),
  39. channels(static_cast<float**> (preallocatedChannelSpace)),
  40. allocatedData(),
  41. isClear(false)
  42. {
  43. }
  44. //==============================================================================
  45. /** Creates a buffer with a specified number of channels and samples.
  46. The contents of the buffer will initially be undefined, so use clear() to
  47. set all the samples to zero.
  48. The buffer will allocate its memory internally, and this will be released
  49. when the buffer is deleted. If the memory can't be allocated, this will
  50. throw a std::bad_alloc exception.
  51. */
  52. AudioSampleBuffer (const uint32_t numChannelsToAllocate,
  53. const uint32_t numSamplesToAllocate,
  54. const bool clearData = false) noexcept
  55. : numChannels(numChannelsToAllocate),
  56. size(numSamplesToAllocate),
  57. allocatedBytes(0),
  58. channels(nullptr),
  59. allocatedData(),
  60. isClear(false)
  61. {
  62. allocateData (clearData);
  63. }
  64. /** Creates a buffer using a pre-allocated block of memory.
  65. Note that if the buffer is resized or its number of channels is changed, it
  66. will re-allocate memory internally and copy the existing data to this new area,
  67. so it will then stop directly addressing this memory.
  68. @param dataToReferTo a pre-allocated array containing pointers to the data
  69. for each channel that should be used by this buffer. The
  70. buffer will only refer to this memory, it won't try to delete
  71. it when the buffer is deleted or resized.
  72. @param numChannelsToUse the number of channels to use - this must correspond to the
  73. number of elements in the array passed in
  74. @param numSamples the number of samples to use - this must correspond to the
  75. size of the arrays passed in
  76. */
  77. AudioSampleBuffer (float* const* dataToReferTo,
  78. const uint32_t numChannelsToUse,
  79. const uint32_t numSamples) noexcept
  80. : numChannels(numChannelsToUse),
  81. size(numSamples),
  82. allocatedBytes(0),
  83. channels(nullptr),
  84. allocatedData(),
  85. isClear(false)
  86. {
  87. CARLA_SAFE_ASSERT_RETURN (dataToReferTo != nullptr,);
  88. allocateChannels (dataToReferTo, 0);
  89. }
  90. /** Creates a buffer using a pre-allocated block of memory.
  91. Note that if the buffer is resized or its number of channels is changed, it
  92. will re-allocate memory internally and copy the existing data to this new area,
  93. so it will then stop directly addressing this memory.
  94. @param dataToReferTo a pre-allocated array containing pointers to the data
  95. for each channel that should be used by this buffer. The
  96. buffer will only refer to this memory, it won't try to delete
  97. it when the buffer is deleted or resized.
  98. @param numChannelsToUse the number of channels to use - this must correspond to the
  99. number of elements in the array passed in
  100. @param startSample the offset within the arrays at which the data begins
  101. @param numSamples the number of samples to use - this must correspond to the
  102. size of the arrays passed in
  103. */
  104. AudioSampleBuffer (float* const* dataToReferTo,
  105. const uint32_t numChannelsToUse,
  106. const uint32_t startSample,
  107. const uint32_t numSamples) noexcept
  108. : numChannels(numChannelsToUse),
  109. size(numSamples),
  110. allocatedBytes(0),
  111. channels(nullptr),
  112. allocatedData(),
  113. isClear(false)
  114. {
  115. CARLA_SAFE_ASSERT_RETURN (dataToReferTo != nullptr,);
  116. allocateChannels (dataToReferTo, startSample);
  117. }
  118. /** Copies another buffer.
  119. This buffer will make its own copy of the other's data, unless the buffer was created
  120. using an external data buffer, in which case boths buffers will just point to the same
  121. shared block of data.
  122. */
  123. AudioSampleBuffer (const AudioSampleBuffer& other) noexcept
  124. : numChannels(other.numChannels),
  125. size(other.size),
  126. allocatedBytes(other.allocatedBytes),
  127. channels(nullptr),
  128. allocatedData(),
  129. isClear(false)
  130. {
  131. if (allocatedBytes == 0)
  132. {
  133. allocateChannels (other.channels, 0);
  134. }
  135. else
  136. {
  137. allocateData();
  138. if (other.isClear)
  139. {
  140. clear();
  141. }
  142. else
  143. {
  144. for (uint32_t i = 0; i < numChannels; ++i)
  145. carla_copyFloats (channels[i], other.channels[i], size);
  146. }
  147. }
  148. }
  149. AudioSampleBuffer& operator= (const AudioSampleBuffer& other) noexcept
  150. {
  151. numChannels = other.numChannels;
  152. size = other.size;
  153. allocatedBytes = other.allocatedBytes;
  154. channels = other.channels;
  155. if (allocatedBytes == 0)
  156. {
  157. allocateChannels (other.channels, 0);
  158. }
  159. else
  160. {
  161. allocateData();
  162. if (other.isClear)
  163. {
  164. clear();
  165. }
  166. else
  167. {
  168. for (uint32_t i = 0; i < numChannels; ++i)
  169. carla_copyFloats (channels[i], other.channels[i], size);
  170. }
  171. }
  172. return *this;
  173. }
  174. /** Destructor.
  175. This will free any memory allocated by the buffer.
  176. */
  177. ~AudioSampleBuffer() noexcept {}
  178. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  179. /** Move constructor */
  180. AudioSampleBuffer (AudioSampleBuffer&& other) noexcept
  181. : numChannels (other.numChannels),
  182. size (other.size),
  183. allocatedBytes (other.allocatedBytes),
  184. channels (other.channels),
  185. allocatedData (static_cast<HeapBlock<char>&&> (other.allocatedData)),
  186. isClear (other.isClear)
  187. {
  188. std::memcpy (preallocatedChannelSpace, other.preallocatedChannelSpace, sizeof (preallocatedChannelSpace));
  189. other.numChannels = 0;
  190. other.size = 0;
  191. other.allocatedBytes = 0;
  192. }
  193. /** Move assignment */
  194. AudioSampleBuffer& operator= (AudioSampleBuffer&& other) noexcept
  195. {
  196. numChannels = other.numChannels;
  197. size = other.size;
  198. allocatedBytes = other.allocatedBytes;
  199. channels = other.channels;
  200. allocatedData = static_cast<HeapBlock<char>&&> (other.allocatedData);
  201. isClear = other.isClear;
  202. memcpy (preallocatedChannelSpace, other.preallocatedChannelSpace, sizeof (preallocatedChannelSpace));
  203. other.numChannels = 0;
  204. other.size = 0;
  205. other.allocatedBytes = 0;
  206. return *this;
  207. }
  208. #endif
  209. //==============================================================================
  210. /** Returns the number of channels of audio data that this buffer contains.
  211. @see getSampleData
  212. */
  213. uint32_t getNumChannels() const noexcept { return numChannels; }
  214. /** Returns the number of samples allocated in each of the buffer's channels.
  215. @see getSampleData
  216. */
  217. uint32_t getNumSamples() const noexcept { return size; }
  218. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  219. For speed, this doesn't check whether the channel number is out of range,
  220. so be careful when using it!
  221. If you need to write to the data, do NOT call this method and const_cast the
  222. result! Instead, you must call getWritePointer so that the buffer knows you're
  223. planning on modifying the data.
  224. */
  225. const float* getReadPointer (const uint32_t channelNumber) const noexcept
  226. {
  227. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  228. return channels [channelNumber];
  229. }
  230. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  231. For speed, this doesn't check whether the channel number or index are out of range,
  232. so be careful when using it!
  233. If you need to write to the data, do NOT call this method and const_cast the
  234. result! Instead, you must call getWritePointer so that the buffer knows you're
  235. planning on modifying the data.
  236. */
  237. const float* getReadPointer (const uint32_t channelNumber, const uint32_t sampleIndex) const noexcept
  238. {
  239. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  240. CARLA_SAFE_ASSERT_RETURN (sampleIndex < size, nullptr);
  241. return channels [channelNumber] + sampleIndex;
  242. }
  243. /** Returns a writeable pointer to one of the buffer's channels.
  244. For speed, this doesn't check whether the channel number is out of range,
  245. so be careful when using it!
  246. Note that if you're not planning on writing to the data, you should always
  247. use getReadPointer instead.
  248. */
  249. float* getWritePointer (const uint32_t channelNumber) noexcept
  250. {
  251. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  252. isClear = false;
  253. return channels [channelNumber];
  254. }
  255. /** Returns a writeable pointer to one of the buffer's channels.
  256. For speed, this doesn't check whether the channel number or index are out of range,
  257. so be careful when using it!
  258. Note that if you're not planning on writing to the data, you should
  259. use getReadPointer instead.
  260. */
  261. float* getWritePointer (const uint32_t channelNumber, const uint32_t sampleIndex) noexcept
  262. {
  263. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  264. CARLA_SAFE_ASSERT_RETURN (sampleIndex < size, nullptr);
  265. isClear = false;
  266. return channels [channelNumber] + sampleIndex;
  267. }
  268. /** Returns an array of pointers to the channels in the buffer.
  269. Don't modify any of the pointers that are returned, and bear in mind that
  270. these will become invalid if the buffer is resized.
  271. */
  272. const float** getArrayOfReadPointers() const noexcept { return const_cast<const float**> (channels); }
  273. /** Returns an array of pointers to the channels in the buffer.
  274. Don't modify any of the pointers that are returned, and bear in mind that
  275. these will become invalid if the buffer is resized.
  276. */
  277. float** getArrayOfWritePointers() noexcept { isClear = false; return channels; }
  278. //==============================================================================
  279. /** Changes the buffer's size or number of channels.
  280. This can expand the buffer's length, and add or remove channels.
  281. */
  282. bool setSize (const uint32_t newNumChannels, const uint32_t newNumSamples) noexcept
  283. {
  284. if (newNumSamples != size || newNumChannels != numChannels)
  285. {
  286. const uint32_t allocatedSamplesPerChannel = (newNumSamples + 3) & ~3u;
  287. const uint32_t channelListSize = ((sizeof (float*) * (newNumChannels + 1)) + 15) & ~15u;
  288. const size_t newTotalBytes = newNumChannels * allocatedSamplesPerChannel * sizeof(float) + channelListSize + 32u;
  289. if (allocatedBytes >= newTotalBytes)
  290. {
  291. if (isClear)
  292. allocatedData.clear (newTotalBytes);
  293. }
  294. else
  295. {
  296. CARLA_SAFE_ASSERT_RETURN (allocatedData.allocate (newTotalBytes, isClear), false);
  297. allocatedBytes = newTotalBytes;
  298. channels = reinterpret_cast<float**> (allocatedData.getData());
  299. }
  300. float* chan = reinterpret_cast<float*> (allocatedData + channelListSize);
  301. for (uint32_t i = 0; i < newNumChannels; ++i)
  302. {
  303. channels[i] = chan;
  304. chan += allocatedSamplesPerChannel;
  305. }
  306. channels [newNumChannels] = nullptr;
  307. size = newNumSamples;
  308. numChannels = newNumChannels;
  309. }
  310. return true;
  311. }
  312. //==============================================================================
  313. /** Changes the buffer's size in a real-time safe manner.
  314. Returns true if the required memory is available.
  315. */
  316. bool setSizeRT (const uint32_t newNumSamples) noexcept
  317. {
  318. if (newNumSamples != size)
  319. {
  320. const uint32_t allocatedSamplesPerChannel = (newNumSamples + 3) & ~3u;
  321. const uint32_t channelListSize = ((sizeof (float*) * (numChannels + 1)) + 15) & ~15u;
  322. const size_t newTotalBytes = numChannels * allocatedSamplesPerChannel * sizeof(float) + channelListSize + 32u;
  323. CARLA_SAFE_ASSERT_RETURN(allocatedBytes >= newTotalBytes, false);
  324. float* chan = reinterpret_cast<float*> (allocatedData + channelListSize);
  325. for (uint32_t i = 0; i < numChannels; ++i)
  326. {
  327. channels[i] = chan;
  328. chan += allocatedSamplesPerChannel;
  329. }
  330. size = newNumSamples;
  331. }
  332. return true;
  333. }
  334. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  335. There's also a constructor that lets you specify arrays like this, but this
  336. lets you change the channels dynamically.
  337. Note that if the buffer is resized or its number of channels is changed, it
  338. will re-allocate memory internally and copy the existing data to this new area,
  339. so it will then stop directly addressing this memory.
  340. @param dataToReferTo a pre-allocated array containing pointers to the data
  341. for each channel that should be used by this buffer. The
  342. buffer will only refer to this memory, it won't try to delete
  343. it when the buffer is deleted or resized.
  344. @param newNumChannels the number of channels to use - this must correspond to the
  345. number of elements in the array passed in
  346. @param newNumSamples the number of samples to use - this must correspond to the
  347. size of the arrays passed in
  348. */
  349. bool setDataToReferTo (float** dataToReferTo,
  350. const uint32_t newNumChannels,
  351. const uint32_t newNumSamples) noexcept
  352. {
  353. CARLA_SAFE_ASSERT_RETURN(dataToReferTo != nullptr, false);
  354. if (allocatedBytes != 0)
  355. {
  356. allocatedBytes = 0;
  357. allocatedData.free();
  358. }
  359. numChannels = newNumChannels;
  360. size = newNumSamples;
  361. return allocateChannels (dataToReferTo, 0);
  362. }
  363. //==============================================================================
  364. /** Clears all the samples in all channels. */
  365. void clear() noexcept
  366. {
  367. if (! isClear)
  368. {
  369. for (uint32_t i = 0; i < numChannels; ++i)
  370. carla_zeroFloats (channels[i], size);
  371. isClear = true;
  372. }
  373. }
  374. /** Clears a specified region of all the channels.
  375. For speed, this doesn't check whether the channel and sample number
  376. are in-range, so be careful!
  377. */
  378. void clear (const uint32_t startSample,
  379. const uint32_t numSamples) noexcept
  380. {
  381. CARLA_SAFE_ASSERT_UINT2_RETURN(startSample + numSamples <= size, numSamples, size,);
  382. if (! isClear)
  383. {
  384. if (startSample == 0 && numSamples == size)
  385. isClear = true;
  386. for (uint32_t i = 0; i < numChannels; ++i)
  387. carla_zeroFloats (channels[i] + startSample, numSamples);
  388. }
  389. }
  390. /** Clears a specified region of just one channel.
  391. For speed, this doesn't check whether the channel and sample number
  392. are in-range, so be careful!
  393. */
  394. void clear (const uint32_t channel,
  395. const uint32_t startSample,
  396. const uint32_t numSamples) noexcept
  397. {
  398. CARLA_SAFE_ASSERT_UINT2_RETURN(channel < numChannels, channel, numChannels,);
  399. CARLA_SAFE_ASSERT_UINT2_RETURN(startSample + numSamples <= size, numSamples, size,);
  400. if (! isClear)
  401. carla_zeroFloats (channels [channel] + startSample, numSamples);
  402. }
  403. /** Returns true if the buffer has been entirely cleared.
  404. Note that this does not actually measure the contents of the buffer - it simply
  405. returns a flag that is set when the buffer is cleared, and which is reset whenever
  406. functions like getWritePointer() are invoked. That means the method does not take
  407. any time, but it may return false negatives when in fact the buffer is still empty.
  408. */
  409. bool hasBeenCleared() const noexcept { return isClear; }
  410. //==============================================================================
  411. /** Adds samples from another buffer to this one.
  412. @param destChannel the channel within this buffer to add the samples to
  413. @param destStartSample the start sample within this buffer's channel
  414. @param source the source buffer to add from
  415. @param sourceChannel the channel within the source buffer to read from
  416. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  417. @param numSamples the number of samples to process
  418. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  419. added to this buffer's samples
  420. @see copyFrom
  421. */
  422. void addFrom (const uint32_t destChannel,
  423. const uint32_t destStartSample,
  424. const AudioSampleBuffer& source,
  425. const uint32_t sourceChannel,
  426. const uint32_t sourceStartSample,
  427. const uint32_t numSamples,
  428. const float gainToApplyToSource = 1.0f) noexcept
  429. {
  430. CARLA_SAFE_ASSERT_UINT2_RETURN(&source != this || sourceChannel != destChannel, sourceChannel, destChannel,);
  431. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  432. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceChannel < source.numChannels, sourceChannel, source.numChannels,);
  433. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  434. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceStartSample + numSamples <= source.size, numSamples, source.size,);
  435. if (carla_isNotZero(gainToApplyToSource) && numSamples != 0 && ! source.isClear)
  436. {
  437. float* const d = channels [destChannel] + destStartSample;
  438. const float* const s = source.channels [sourceChannel] + sourceStartSample;
  439. if (isClear)
  440. {
  441. isClear = false;
  442. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  443. carla_copyWithMultiply (d, s, gainToApplyToSource, numSamples);
  444. else
  445. carla_copyFloats (d, s, numSamples);
  446. }
  447. else
  448. {
  449. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  450. carla_addWithMultiply (d, s, gainToApplyToSource, numSamples);
  451. else
  452. carla_add (d, s, numSamples);
  453. }
  454. }
  455. }
  456. /** Adds samples from an array of floats to one of the channels.
  457. @param destChannel the channel within this buffer to add the samples to
  458. @param destStartSample the start sample within this buffer's channel
  459. @param source the source data to use
  460. @param numSamples the number of samples to process
  461. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  462. added to this buffer's samples
  463. @see copyFrom
  464. */
  465. void addFrom (const uint32_t destChannel,
  466. const uint32_t destStartSample,
  467. const float* source,
  468. const uint32_t numSamples,
  469. float gainToApplyToSource = 1.0f) noexcept
  470. {
  471. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  472. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  473. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  474. if (carla_isNotZero(gainToApplyToSource) && numSamples != 0)
  475. {
  476. float* const d = channels [destChannel] + destStartSample;
  477. if (isClear)
  478. {
  479. isClear = false;
  480. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  481. carla_copyWithMultiply (d, source, gainToApplyToSource, numSamples);
  482. else
  483. carla_copyFloats (d, source, numSamples);
  484. }
  485. else
  486. {
  487. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  488. carla_addWithMultiply (d, source, gainToApplyToSource, numSamples);
  489. else
  490. carla_add (d, source, numSamples);
  491. }
  492. }
  493. }
  494. /** Copies samples from another buffer to this one.
  495. @param destChannel the channel within this buffer to copy the samples to
  496. @param destStartSample the start sample within this buffer's channel
  497. @param source the source buffer to read from
  498. @param sourceChannel the channel within the source buffer to read from
  499. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  500. @param numSamples the number of samples to process
  501. @see addFrom
  502. */
  503. void copyFrom (const uint32_t destChannel,
  504. const uint32_t destStartSample,
  505. const AudioSampleBuffer& source,
  506. const uint32_t sourceChannel,
  507. const uint32_t sourceStartSample,
  508. const uint32_t numSamples) noexcept
  509. {
  510. CARLA_SAFE_ASSERT_UINT2_RETURN(&source != this || sourceChannel != destChannel, sourceChannel, destChannel,);
  511. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  512. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceChannel < source.numChannels, sourceChannel, source.numChannels,);
  513. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  514. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceStartSample + numSamples <= source.size, numSamples, source.size,);
  515. if (numSamples > 0)
  516. {
  517. if (source.isClear)
  518. {
  519. if (! isClear)
  520. carla_zeroFloats (channels [destChannel] + destStartSample, numSamples);
  521. }
  522. else
  523. {
  524. isClear = false;
  525. carla_copyFloats (channels [destChannel] + destStartSample,
  526. source.channels [sourceChannel] + sourceStartSample,
  527. numSamples);
  528. }
  529. }
  530. }
  531. /** Copies samples from an array of floats into one of the channels.
  532. @param destChannel the channel within this buffer to copy the samples to
  533. @param destStartSample the start sample within this buffer's channel
  534. @param source the source buffer to read from
  535. @param numSamples the number of samples to process
  536. @see addFrom
  537. */
  538. void copyFrom (const uint32_t destChannel,
  539. const uint32_t destStartSample,
  540. const float* source,
  541. const uint32_t numSamples) noexcept
  542. {
  543. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  544. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  545. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  546. if (numSamples > 0)
  547. {
  548. isClear = false;
  549. carla_copyFloats (channels [destChannel] + destStartSample, source, numSamples);
  550. }
  551. }
  552. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  553. @param destChannel the channel within this buffer to copy the samples to
  554. @param destStartSample the start sample within this buffer's channel
  555. @param source the source buffer to read from
  556. @param numSamples the number of samples to process
  557. @param gain the gain to apply
  558. @see addFrom
  559. */
  560. void copyFrom (const uint32_t destChannel,
  561. const uint32_t destStartSample,
  562. const float* source,
  563. const uint32_t numSamples,
  564. float gain) noexcept
  565. {
  566. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  567. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  568. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  569. if (numSamples > 0)
  570. {
  571. float* const d = channels [destChannel] + destStartSample;
  572. if (carla_isZero(gain))
  573. {
  574. if (! isClear)
  575. carla_zeroFloats (d, numSamples);
  576. }
  577. else if (carla_isZero(gain - 1.0f))
  578. {
  579. isClear = false;
  580. carla_copyFloats (d, source, numSamples);
  581. }
  582. else
  583. {
  584. isClear = false;
  585. carla_copyWithMultiply (d, source, gain, numSamples);
  586. }
  587. }
  588. }
  589. /** Copies samples from an array of floats into one of the channels.
  590. @param destChannel the channel within this buffer to copy the samples to
  591. @param destStartSample the start sample within this buffer's channel
  592. @param source the source buffer to read from
  593. @param numSamples the number of samples to process
  594. @see addFrom
  595. */
  596. void copyFromInterleavedSource (const uint32_t destChannel,
  597. const float* source,
  598. const uint32_t totalNumSamples) noexcept
  599. {
  600. CARLA_SAFE_ASSERT_RETURN(destChannel < numChannels,);
  601. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  602. if (const uint32_t numSamples = totalNumSamples / numChannels)
  603. {
  604. CARLA_SAFE_ASSERT_RETURN(numSamples <= size,);
  605. isClear = false;
  606. float* d = channels [destChannel];
  607. for (uint32_t i=0; i < numSamples; ++i)
  608. d[i] = source[i * numChannels + destChannel];
  609. }
  610. }
  611. private:
  612. //==============================================================================
  613. uint32_t numChannels, size;
  614. size_t allocatedBytes;
  615. float** channels;
  616. HeapBlock<char> allocatedData;
  617. float* preallocatedChannelSpace [32];
  618. bool isClear;
  619. bool allocateData (bool clearData = false)
  620. {
  621. const size_t channelListSize = sizeof (float*) * (numChannels + 1);
  622. const size_t nextAllocatedBytes = numChannels * size * sizeof (float) + channelListSize + 32;
  623. CARLA_SAFE_ASSERT_RETURN (allocatedData.allocate (nextAllocatedBytes, clearData), false);
  624. allocatedBytes = nextAllocatedBytes;
  625. channels = reinterpret_cast<float**> (allocatedData.getData());
  626. float* chan = (float*) (allocatedData + channelListSize);
  627. for (uint32_t i = 0; i < numChannels; ++i)
  628. {
  629. channels[i] = chan;
  630. chan += size;
  631. }
  632. channels [numChannels] = nullptr;
  633. isClear = clearData;
  634. return true;
  635. }
  636. bool allocateChannels (float* const* const dataToReferTo, const uint32_t offset)
  637. {
  638. // (try to avoid doing a malloc here, as that'll blow up things like Pro-Tools)
  639. if (numChannels < numElementsInArray (preallocatedChannelSpace))
  640. {
  641. channels = static_cast<float**> (preallocatedChannelSpace);
  642. }
  643. else
  644. {
  645. CARLA_SAFE_ASSERT_RETURN( allocatedData.malloc (numChannels + 1, sizeof (float*)), false);
  646. channels = reinterpret_cast<float**> (allocatedData.getData());
  647. }
  648. for (uint32_t i = 0; i < numChannels; ++i)
  649. {
  650. // you have to pass in the same number of valid pointers as numChannels
  651. CARLA_SAFE_ASSERT_CONTINUE (dataToReferTo[i] != nullptr);
  652. channels[i] = dataToReferTo[i] + offset;
  653. }
  654. channels [numChannels] = nullptr;
  655. isClear = false;
  656. return true;
  657. }
  658. // CARLA_DECLARE_NON_COPY_CLASS(AudioSampleBuffer)
  659. };
  660. }
  661. #endif // WATER_AUDIOSAMPLEBUFFER_H_INCLUDED