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.

753 lines
30KB

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