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.

AudioSampleBuffer.h 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. //==============================================================================
  179. /** Returns the number of channels of audio data that this buffer contains.
  180. @see getSampleData
  181. */
  182. uint32_t getNumChannels() const noexcept { return numChannels; }
  183. /** Returns the number of samples allocated in each of the buffer's channels.
  184. @see getSampleData
  185. */
  186. uint32_t getNumSamples() const noexcept { return size; }
  187. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  188. For speed, this doesn't check whether the channel number is out of range,
  189. so be careful when using it!
  190. If you need to write to the data, do NOT call this method and const_cast the
  191. result! Instead, you must call getWritePointer so that the buffer knows you're
  192. planning on modifying the data.
  193. */
  194. const float* getReadPointer (const uint32_t channelNumber) const noexcept
  195. {
  196. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  197. return channels [channelNumber];
  198. }
  199. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  200. For speed, this doesn't check whether the channel number or index are out of range,
  201. so be careful when using it!
  202. If you need to write to the data, do NOT call this method and const_cast the
  203. result! Instead, you must call getWritePointer so that the buffer knows you're
  204. planning on modifying the data.
  205. */
  206. const float* getReadPointer (const uint32_t channelNumber, const uint32_t sampleIndex) const noexcept
  207. {
  208. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  209. CARLA_SAFE_ASSERT_RETURN (sampleIndex < size, nullptr);
  210. return channels [channelNumber] + sampleIndex;
  211. }
  212. /** Returns a writeable pointer to one of the buffer's channels.
  213. For speed, this doesn't check whether the channel number is out of range,
  214. so be careful when using it!
  215. Note that if you're not planning on writing to the data, you should always
  216. use getReadPointer instead.
  217. */
  218. float* getWritePointer (const uint32_t channelNumber) noexcept
  219. {
  220. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  221. isClear = false;
  222. return channels [channelNumber];
  223. }
  224. /** Returns a writeable pointer to one of the buffer's channels.
  225. For speed, this doesn't check whether the channel number or index are out of range,
  226. so be careful when using it!
  227. Note that if you're not planning on writing to the data, you should
  228. use getReadPointer instead.
  229. */
  230. float* getWritePointer (const uint32_t channelNumber, const uint32_t sampleIndex) noexcept
  231. {
  232. CARLA_SAFE_ASSERT_RETURN (channelNumber < numChannels, nullptr);
  233. CARLA_SAFE_ASSERT_RETURN (sampleIndex < size, nullptr);
  234. isClear = false;
  235. return channels [channelNumber] + sampleIndex;
  236. }
  237. /** Returns an array of pointers to the channels in the buffer.
  238. Don't modify any of the pointers that are returned, and bear in mind that
  239. these will become invalid if the buffer is resized.
  240. */
  241. const float** getArrayOfReadPointers() const noexcept { return const_cast<const float**> (channels); }
  242. /** Returns an array of pointers to the channels in the buffer.
  243. Don't modify any of the pointers that are returned, and bear in mind that
  244. these will become invalid if the buffer is resized.
  245. */
  246. float** getArrayOfWritePointers() noexcept { isClear = false; return channels; }
  247. //==============================================================================
  248. /** Changes the buffer's size or number of channels.
  249. This can expand the buffer's length, and add or remove channels.
  250. */
  251. bool setSize (const uint32_t newNumChannels, const uint32_t newNumSamples) noexcept
  252. {
  253. if (newNumSamples != size || newNumChannels != numChannels)
  254. {
  255. const uint32_t allocatedSamplesPerChannel = (newNumSamples + 3) & ~3u;
  256. const uint32_t channelListSize = ((sizeof (float*) * (newNumChannels + 1)) + 15) & ~15u;
  257. const size_t newTotalBytes = newNumChannels * allocatedSamplesPerChannel * sizeof(float) + channelListSize + 32u;
  258. if (allocatedBytes >= newTotalBytes)
  259. {
  260. if (isClear)
  261. allocatedData.clear (newTotalBytes);
  262. }
  263. else
  264. {
  265. CARLA_SAFE_ASSERT_RETURN (allocatedData.allocate (newTotalBytes, isClear), false);
  266. allocatedBytes = newTotalBytes;
  267. channels = reinterpret_cast<float**> (allocatedData.getData());
  268. }
  269. float* chan = reinterpret_cast<float*> (allocatedData + channelListSize);
  270. for (uint32_t i = 0; i < newNumChannels; ++i)
  271. {
  272. channels[i] = chan;
  273. chan += allocatedSamplesPerChannel;
  274. }
  275. channels [newNumChannels] = nullptr;
  276. size = newNumSamples;
  277. numChannels = newNumChannels;
  278. }
  279. return true;
  280. }
  281. //==============================================================================
  282. /** Changes the buffer's size in a real-time safe manner.
  283. Returns true if the required memory is available.
  284. */
  285. bool setSizeRT (const uint32_t newNumSamples) noexcept
  286. {
  287. if (newNumSamples != size)
  288. {
  289. const uint32_t allocatedSamplesPerChannel = (newNumSamples + 3) & ~3u;
  290. const uint32_t channelListSize = ((sizeof (float*) * (numChannels + 1)) + 15) & ~15u;
  291. const size_t newTotalBytes = numChannels * allocatedSamplesPerChannel * sizeof(float) + channelListSize + 32u;
  292. CARLA_SAFE_ASSERT_RETURN(allocatedBytes >= newTotalBytes, false);
  293. float* chan = reinterpret_cast<float*> (allocatedData + channelListSize);
  294. for (uint32_t i = 0; i < numChannels; ++i)
  295. {
  296. channels[i] = chan;
  297. chan += allocatedSamplesPerChannel;
  298. }
  299. size = newNumSamples;
  300. }
  301. return true;
  302. }
  303. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  304. There's also a constructor that lets you specify arrays like this, but this
  305. lets you change the channels dynamically.
  306. Note that if the buffer is resized or its number of channels is changed, it
  307. will re-allocate memory internally and copy the existing data to this new area,
  308. so it will then stop directly addressing this memory.
  309. @param dataToReferTo a pre-allocated array containing pointers to the data
  310. for each channel that should be used by this buffer. The
  311. buffer will only refer to this memory, it won't try to delete
  312. it when the buffer is deleted or resized.
  313. @param newNumChannels the number of channels to use - this must correspond to the
  314. number of elements in the array passed in
  315. @param newNumSamples the number of samples to use - this must correspond to the
  316. size of the arrays passed in
  317. */
  318. bool setDataToReferTo (float** dataToReferTo,
  319. const uint32_t newNumChannels,
  320. const uint32_t newNumSamples) noexcept
  321. {
  322. CARLA_SAFE_ASSERT_RETURN(dataToReferTo != nullptr, false);
  323. if (allocatedBytes != 0)
  324. {
  325. allocatedBytes = 0;
  326. allocatedData.free();
  327. }
  328. numChannels = newNumChannels;
  329. size = newNumSamples;
  330. return allocateChannels (dataToReferTo, 0);
  331. }
  332. //==============================================================================
  333. /** Clears all the samples in all channels. */
  334. void clear() noexcept
  335. {
  336. if (! isClear)
  337. {
  338. for (uint32_t i = 0; i < numChannels; ++i)
  339. carla_zeroFloats (channels[i], size);
  340. isClear = true;
  341. }
  342. }
  343. /** Clears a specified region of all the channels.
  344. For speed, this doesn't check whether the channel and sample number
  345. are in-range, so be careful!
  346. */
  347. void clear (const uint32_t startSample,
  348. const uint32_t numSamples) noexcept
  349. {
  350. CARLA_SAFE_ASSERT_UINT2_RETURN(startSample + numSamples <= size, numSamples, size,);
  351. if (! isClear)
  352. {
  353. if (startSample == 0 && numSamples == size)
  354. isClear = true;
  355. for (uint32_t i = 0; i < numChannels; ++i)
  356. carla_zeroFloats (channels[i] + startSample, numSamples);
  357. }
  358. }
  359. /** Clears a specified region of just one channel.
  360. For speed, this doesn't check whether the channel and sample number
  361. are in-range, so be careful!
  362. */
  363. void clear (const uint32_t channel,
  364. const uint32_t startSample,
  365. const uint32_t numSamples) noexcept
  366. {
  367. CARLA_SAFE_ASSERT_UINT2_RETURN(channel < numChannels, channel, numChannels,);
  368. CARLA_SAFE_ASSERT_UINT2_RETURN(startSample + numSamples <= size, numSamples, size,);
  369. if (! isClear)
  370. carla_zeroFloats (channels [channel] + startSample, numSamples);
  371. }
  372. /** Returns true if the buffer has been entirely cleared.
  373. Note that this does not actually measure the contents of the buffer - it simply
  374. returns a flag that is set when the buffer is cleared, and which is reset whenever
  375. functions like getWritePointer() are invoked. That means the method does not take
  376. any time, but it may return false negatives when in fact the buffer is still empty.
  377. */
  378. bool hasBeenCleared() const noexcept { return isClear; }
  379. //==============================================================================
  380. /** Adds samples from another buffer to this one.
  381. @param destChannel the channel within this buffer to add the samples to
  382. @param destStartSample the start sample within this buffer's channel
  383. @param source the source buffer to add from
  384. @param sourceChannel the channel within the source buffer to read from
  385. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  386. @param numSamples the number of samples to process
  387. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  388. added to this buffer's samples
  389. @see copyFrom
  390. */
  391. void addFrom (const uint32_t destChannel,
  392. const uint32_t destStartSample,
  393. const AudioSampleBuffer& source,
  394. const uint32_t sourceChannel,
  395. const uint32_t sourceStartSample,
  396. const uint32_t numSamples,
  397. const float gainToApplyToSource = 1.0f) noexcept
  398. {
  399. CARLA_SAFE_ASSERT_UINT2_RETURN(&source != this || sourceChannel != destChannel, sourceChannel, destChannel,);
  400. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  401. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceChannel < source.numChannels, sourceChannel, source.numChannels,);
  402. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  403. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceStartSample + numSamples <= source.size, numSamples, source.size,);
  404. if (carla_isNotZero(gainToApplyToSource) && numSamples != 0 && ! source.isClear)
  405. {
  406. float* const d = channels [destChannel] + destStartSample;
  407. const float* const s = source.channels [sourceChannel] + sourceStartSample;
  408. if (isClear)
  409. {
  410. isClear = false;
  411. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  412. carla_copyWithMultiply (d, s, gainToApplyToSource, numSamples);
  413. else
  414. carla_copyFloats (d, s, numSamples);
  415. }
  416. else
  417. {
  418. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  419. carla_addWithMultiply (d, s, gainToApplyToSource, numSamples);
  420. else
  421. carla_add (d, s, numSamples);
  422. }
  423. }
  424. }
  425. /** Adds samples from an array of floats to one of the channels.
  426. @param destChannel the channel within this buffer to add the samples to
  427. @param destStartSample the start sample within this buffer's channel
  428. @param source the source data to use
  429. @param numSamples the number of samples to process
  430. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  431. added to this buffer's samples
  432. @see copyFrom
  433. */
  434. void addFrom (const uint32_t destChannel,
  435. const uint32_t destStartSample,
  436. const float* source,
  437. const uint32_t numSamples,
  438. float gainToApplyToSource = 1.0f) noexcept
  439. {
  440. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  441. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  442. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  443. if (carla_isNotZero(gainToApplyToSource) && numSamples != 0)
  444. {
  445. float* const d = channels [destChannel] + destStartSample;
  446. if (isClear)
  447. {
  448. isClear = false;
  449. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  450. carla_copyWithMultiply (d, source, gainToApplyToSource, numSamples);
  451. else
  452. carla_copyFloats (d, source, numSamples);
  453. }
  454. else
  455. {
  456. if (carla_isNotZero(gainToApplyToSource - 1.0f))
  457. carla_addWithMultiply (d, source, gainToApplyToSource, numSamples);
  458. else
  459. carla_add (d, source, numSamples);
  460. }
  461. }
  462. }
  463. /** Copies samples from another buffer to this one.
  464. @param destChannel the channel within this buffer to copy the samples to
  465. @param destStartSample the start sample within this buffer's channel
  466. @param source the source buffer to read from
  467. @param sourceChannel the channel within the source buffer to read from
  468. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  469. @param numSamples the number of samples to process
  470. @see addFrom
  471. */
  472. void copyFrom (const uint32_t destChannel,
  473. const uint32_t destStartSample,
  474. const AudioSampleBuffer& source,
  475. const uint32_t sourceChannel,
  476. const uint32_t sourceStartSample,
  477. const uint32_t numSamples) noexcept
  478. {
  479. CARLA_SAFE_ASSERT_UINT2_RETURN(&source != this || sourceChannel != destChannel, sourceChannel, destChannel,);
  480. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  481. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceChannel < source.numChannels, sourceChannel, source.numChannels,);
  482. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  483. CARLA_SAFE_ASSERT_UINT2_RETURN(sourceStartSample + numSamples <= source.size, numSamples, source.size,);
  484. if (numSamples > 0)
  485. {
  486. if (source.isClear)
  487. {
  488. if (! isClear)
  489. carla_zeroFloats (channels [destChannel] + destStartSample, numSamples);
  490. }
  491. else
  492. {
  493. isClear = false;
  494. carla_copyFloats (channels [destChannel] + destStartSample,
  495. source.channels [sourceChannel] + sourceStartSample,
  496. numSamples);
  497. }
  498. }
  499. }
  500. /** Copies samples from an array of floats into one of the channels.
  501. @param destChannel the channel within this buffer to copy the samples to
  502. @param destStartSample the start sample within this buffer's channel
  503. @param source the source buffer to read from
  504. @param numSamples the number of samples to process
  505. @see addFrom
  506. */
  507. void copyFrom (const uint32_t destChannel,
  508. const uint32_t destStartSample,
  509. const float* source,
  510. const uint32_t numSamples) noexcept
  511. {
  512. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  513. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  514. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  515. if (numSamples > 0)
  516. {
  517. isClear = false;
  518. carla_copyFloats (channels [destChannel] + destStartSample, source, numSamples);
  519. }
  520. }
  521. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  522. @param destChannel the channel within this buffer to copy the samples to
  523. @param destStartSample the start sample within this buffer's channel
  524. @param source the source buffer to read from
  525. @param numSamples the number of samples to process
  526. @param gain the gain to apply
  527. @see addFrom
  528. */
  529. void copyFrom (const uint32_t destChannel,
  530. const uint32_t destStartSample,
  531. const float* source,
  532. const uint32_t numSamples,
  533. float gain) noexcept
  534. {
  535. CARLA_SAFE_ASSERT_UINT2_RETURN(destChannel < numChannels, destChannel, numChannels,);
  536. CARLA_SAFE_ASSERT_UINT2_RETURN(destStartSample + numSamples <= size, numSamples, size,);
  537. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  538. if (numSamples > 0)
  539. {
  540. float* const d = channels [destChannel] + destStartSample;
  541. if (carla_isZero(gain))
  542. {
  543. if (! isClear)
  544. carla_zeroFloats (d, numSamples);
  545. }
  546. else if (carla_isZero(gain - 1.0f))
  547. {
  548. isClear = false;
  549. carla_copyFloats (d, source, numSamples);
  550. }
  551. else
  552. {
  553. isClear = false;
  554. carla_copyWithMultiply (d, source, gain, numSamples);
  555. }
  556. }
  557. }
  558. /** Copies samples from an array of floats into one of the channels.
  559. @param destChannel the channel within this buffer to copy the samples to
  560. @param destStartSample the start sample within this buffer's channel
  561. @param source the source buffer to read from
  562. @param numSamples the number of samples to process
  563. @see addFrom
  564. */
  565. void copyFromInterleavedSource (const uint32_t destChannel,
  566. const float* source,
  567. const uint32_t totalNumSamples) noexcept
  568. {
  569. CARLA_SAFE_ASSERT_RETURN(destChannel < numChannels,);
  570. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  571. if (const uint32_t numSamples = totalNumSamples / numChannels)
  572. {
  573. CARLA_SAFE_ASSERT_RETURN(numSamples <= size,);
  574. isClear = false;
  575. float* d = channels [destChannel];
  576. for (uint32_t i=0; i < numSamples; ++i)
  577. d[i] = source[i * numChannels + destChannel];
  578. }
  579. }
  580. private:
  581. //==============================================================================
  582. uint32_t numChannels, size;
  583. size_t allocatedBytes;
  584. float** channels;
  585. HeapBlock<char> allocatedData;
  586. float* preallocatedChannelSpace [32];
  587. bool isClear;
  588. bool allocateData (bool clearData = false)
  589. {
  590. const size_t channelListSize = sizeof (float*) * (numChannels + 1);
  591. const size_t nextAllocatedBytes = numChannels * size * sizeof (float) + channelListSize + 32;
  592. CARLA_SAFE_ASSERT_RETURN (allocatedData.allocate (nextAllocatedBytes, clearData), false);
  593. allocatedBytes = nextAllocatedBytes;
  594. channels = reinterpret_cast<float**> (allocatedData.getData());
  595. float* chan = (float*) (allocatedData + channelListSize);
  596. for (uint32_t i = 0; i < numChannels; ++i)
  597. {
  598. channels[i] = chan;
  599. chan += size;
  600. }
  601. channels [numChannels] = nullptr;
  602. isClear = clearData;
  603. return true;
  604. }
  605. bool allocateChannels (float* const* const dataToReferTo, const uint32_t offset)
  606. {
  607. // (try to avoid doing a malloc here, as that'll blow up things like Pro-Tools)
  608. if (numChannels < numElementsInArray (preallocatedChannelSpace))
  609. {
  610. channels = static_cast<float**> (preallocatedChannelSpace);
  611. }
  612. else
  613. {
  614. CARLA_SAFE_ASSERT_RETURN( allocatedData.malloc (numChannels + 1, sizeof (float*)), false);
  615. channels = reinterpret_cast<float**> (allocatedData.getData());
  616. }
  617. for (uint32_t i = 0; i < numChannels; ++i)
  618. {
  619. // you have to pass in the same number of valid pointers as numChannels
  620. CARLA_SAFE_ASSERT_CONTINUE (dataToReferTo[i] != nullptr);
  621. channels[i] = dataToReferTo[i] + offset;
  622. }
  623. channels [numChannels] = nullptr;
  624. isClear = false;
  625. return true;
  626. }
  627. // CARLA_DECLARE_NON_COPYABLE(AudioSampleBuffer)
  628. };
  629. }
  630. #endif // WATER_AUDIOSAMPLEBUFFER_H_INCLUDED