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.

1113 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_AUDIOSAMPLEBUFFER_H_INCLUDED
  24. #define JUCE_AUDIOSAMPLEBUFFER_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. A multi-channel buffer of floating point audio samples.
  28. @see AudioSampleBuffer
  29. */
  30. template <typename Type>
  31. class AudioBuffer
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty buffer with 0 channels and 0 length. */
  36. AudioBuffer() noexcept
  37. : numChannels (0), size (0), allocatedBytes (0),
  38. channels (static_cast<Type**> (preallocatedChannelSpace)),
  39. isClear (false)
  40. {
  41. }
  42. //==============================================================================
  43. /** Creates a buffer with a specified number of channels and samples.
  44. The contents of the buffer will initially be undefined, so use clear() to
  45. set all the samples to zero.
  46. The buffer will allocate its memory internally, and this will be released
  47. when the buffer is deleted. If the memory can't be allocated, this will
  48. throw a std::bad_alloc exception.
  49. */
  50. AudioBuffer (int numChannelsToAllocate,
  51. int numSamplesToAllocate) noexcept
  52. : numChannels (numChannelsToAllocate),
  53. size (numSamplesToAllocate)
  54. {
  55. jassert (size >= 0);
  56. jassert (numChannels >= 0);
  57. allocateData();
  58. }
  59. /** Creates a buffer using a pre-allocated block of memory.
  60. Note that if the buffer is resized or its number of channels is changed, it
  61. will re-allocate memory internally and copy the existing data to this new area,
  62. so it will then stop directly addressing this memory.
  63. @param dataToReferTo a pre-allocated array containing pointers to the data
  64. for each channel that should be used by this buffer. The
  65. buffer will only refer to this memory, it won't try to delete
  66. it when the buffer is deleted or resized.
  67. @param numChannelsToUse the number of channels to use - this must correspond to the
  68. number of elements in the array passed in
  69. @param numSamples the number of samples to use - this must correspond to the
  70. size of the arrays passed in
  71. */
  72. AudioBuffer (Type* const* dataToReferTo,
  73. int numChannelsToUse,
  74. int numSamples) noexcept
  75. : numChannels (numChannelsToUse),
  76. size (numSamples),
  77. allocatedBytes (0)
  78. {
  79. jassert (dataToReferTo != nullptr);
  80. jassert (numChannelsToUse >= 0 && numSamples >= 0);
  81. allocateChannels (dataToReferTo, 0);
  82. }
  83. /** Creates a buffer using a pre-allocated block of memory.
  84. Note that if the buffer is resized or its number of channels is changed, it
  85. will re-allocate memory internally and copy the existing data to this new area,
  86. so it will then stop directly addressing this memory.
  87. @param dataToReferTo a pre-allocated array containing pointers to the data
  88. for each channel that should be used by this buffer. The
  89. buffer will only refer to this memory, it won't try to delete
  90. it when the buffer is deleted or resized.
  91. @param numChannelsToUse the number of channels to use - this must correspond to the
  92. number of elements in the array passed in
  93. @param startSample the offset within the arrays at which the data begins
  94. @param numSamples the number of samples to use - this must correspond to the
  95. size of the arrays passed in
  96. */
  97. AudioBuffer (Type* const* dataToReferTo,
  98. int numChannelsToUse,
  99. int startSample,
  100. int numSamples) noexcept
  101. : numChannels (numChannelsToUse),
  102. size (numSamples),
  103. allocatedBytes (0),
  104. isClear (false)
  105. {
  106. jassert (dataToReferTo != nullptr);
  107. jassert (numChannelsToUse >= 0 && startSample >= 0 && numSamples >= 0);
  108. allocateChannels (dataToReferTo, startSample);
  109. }
  110. /** Copies another buffer.
  111. This buffer will make its own copy of the other's data, unless the buffer was created
  112. using an external data buffer, in which case boths buffers will just point to the same
  113. shared block of data.
  114. */
  115. AudioBuffer (const AudioBuffer& other) noexcept
  116. : numChannels (other.numChannels),
  117. size (other.size),
  118. allocatedBytes (other.allocatedBytes)
  119. {
  120. if (allocatedBytes == 0)
  121. {
  122. allocateChannels (other.channels, 0);
  123. }
  124. else
  125. {
  126. allocateData();
  127. if (other.isClear)
  128. {
  129. clear();
  130. }
  131. else
  132. {
  133. for (int i = 0; i < numChannels; ++i)
  134. FloatVectorOperations::copy (channels[i], other.channels[i], size);
  135. }
  136. }
  137. }
  138. /** Copies another buffer onto this one.
  139. This buffer's size will be changed to that of the other buffer.
  140. */
  141. AudioBuffer& operator= (const AudioBuffer& other) noexcept
  142. {
  143. if (this != &other)
  144. {
  145. setSize (other.getNumChannels(), other.getNumSamples(), false, false, false);
  146. if (other.isClear)
  147. {
  148. clear();
  149. }
  150. else
  151. {
  152. isClear = false;
  153. for (int i = 0; i < numChannels; ++i)
  154. FloatVectorOperations::copy (channels[i], other.channels[i], size);
  155. }
  156. }
  157. return *this;
  158. }
  159. /** Destructor.
  160. This will free any memory allocated by the buffer.
  161. */
  162. ~AudioBuffer() noexcept {}
  163. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  164. /** Move constructor */
  165. AudioBuffer (AudioBuffer&& other) noexcept
  166. : numChannels (other.numChannels),
  167. size (other.size),
  168. allocatedBytes (other.allocatedBytes),
  169. channels (other.channels),
  170. allocatedData (static_cast<HeapBlock<char, true>&&> (other.allocatedData)),
  171. isClear (other.isClear)
  172. {
  173. memcpy (preallocatedChannelSpace, other.preallocatedChannelSpace, sizeof (preallocatedChannelSpace));
  174. other.numChannels = 0;
  175. other.size = 0;
  176. other.allocatedBytes = 0;
  177. }
  178. /** Move assignment */
  179. AudioBuffer& operator= (AudioBuffer&& other) noexcept
  180. {
  181. numChannels = other.numChannels;
  182. size = other.size;
  183. allocatedBytes = other.allocatedBytes;
  184. channels = other.channels;
  185. allocatedData = static_cast<HeapBlock<char, true>&&> (other.allocatedData);
  186. isClear = other.isClear;
  187. memcpy (preallocatedChannelSpace, other.preallocatedChannelSpace, sizeof (preallocatedChannelSpace));
  188. other.numChannels = 0;
  189. other.size = 0;
  190. other.allocatedBytes = 0;
  191. return *this;
  192. }
  193. #endif
  194. //==============================================================================
  195. /** Returns the number of channels of audio data that this buffer contains.
  196. @see getSampleData
  197. */
  198. int getNumChannels() const noexcept { return numChannels; }
  199. /** Returns the number of samples allocated in each of the buffer's channels.
  200. @see getSampleData
  201. */
  202. int getNumSamples() const noexcept { return size; }
  203. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  204. For speed, this doesn't check whether the channel number is out of range,
  205. so be careful when using it!
  206. If you need to write to the data, do NOT call this method and const_cast the
  207. result! Instead, you must call getWritePointer so that the buffer knows you're
  208. planning on modifying the data.
  209. */
  210. const Type* getReadPointer (int channelNumber) const noexcept
  211. {
  212. jassert (isPositiveAndBelow (channelNumber, numChannels));
  213. return channels [channelNumber];
  214. }
  215. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  216. For speed, this doesn't check whether the channel number or index are out of range,
  217. so be careful when using it!
  218. If you need to write to the data, do NOT call this method and const_cast the
  219. result! Instead, you must call getWritePointer so that the buffer knows you're
  220. planning on modifying the data.
  221. */
  222. const Type* getReadPointer (int channelNumber, int sampleIndex) const noexcept
  223. {
  224. jassert (isPositiveAndBelow (channelNumber, numChannels));
  225. jassert (isPositiveAndBelow (sampleIndex, size));
  226. return channels [channelNumber] + sampleIndex;
  227. }
  228. /** Returns a writeable pointer to one of the buffer's channels.
  229. For speed, this doesn't check whether the channel number is out of range,
  230. so be careful when using it!
  231. Note that if you're not planning on writing to the data, you should always
  232. use getReadPointer instead.
  233. */
  234. Type* getWritePointer (int channelNumber) noexcept
  235. {
  236. jassert (isPositiveAndBelow (channelNumber, numChannels));
  237. isClear = false;
  238. return channels [channelNumber];
  239. }
  240. /** Returns a writeable pointer to one of the buffer's channels.
  241. For speed, this doesn't check whether the channel number or index are out of range,
  242. so be careful when using it!
  243. Note that if you're not planning on writing to the data, you should
  244. use getReadPointer instead.
  245. */
  246. Type* getWritePointer (int channelNumber, int sampleIndex) noexcept
  247. {
  248. jassert (isPositiveAndBelow (channelNumber, numChannels));
  249. jassert (isPositiveAndBelow (sampleIndex, size));
  250. isClear = false;
  251. return channels [channelNumber] + sampleIndex;
  252. }
  253. /** Returns an array of pointers to the channels in the buffer.
  254. Don't modify any of the pointers that are returned, and bear in mind that
  255. these will become invalid if the buffer is resized.
  256. */
  257. const Type** getArrayOfReadPointers() const noexcept { return const_cast<const Type**> (channels); }
  258. /** Returns an array of pointers to the channels in the buffer.
  259. Don't modify any of the pointers that are returned, and bear in mind that
  260. these will become invalid if the buffer is resized.
  261. */
  262. Type** getArrayOfWritePointers() noexcept { isClear = false; return channels; }
  263. //==============================================================================
  264. /** Changes the buffer's size or number of channels.
  265. This can expand or contract the buffer's length, and add or remove channels.
  266. If keepExistingContent is true, it will try to preserve as much of the
  267. old data as it can in the new buffer.
  268. If clearExtraSpace is true, then any extra channels or space that is
  269. allocated will be also be cleared. If false, then this space is left
  270. uninitialised.
  271. If avoidReallocating is true, then changing the buffer's size won't reduce the
  272. amount of memory that is currently allocated (but it will still increase it if
  273. the new size is bigger than the amount it currently has). If this is false, then
  274. a new allocation will be done so that the buffer uses takes up the minimum amount
  275. of memory that it needs.
  276. If the required memory can't be allocated, this will throw a std::bad_alloc exception.
  277. */
  278. void setSize (int newNumChannels,
  279. int newNumSamples,
  280. bool keepExistingContent = false,
  281. bool clearExtraSpace = false,
  282. bool avoidReallocating = false) noexcept
  283. {
  284. jassert (newNumChannels >= 0);
  285. jassert (newNumSamples >= 0);
  286. if (newNumSamples != size || newNumChannels != numChannels)
  287. {
  288. const size_t allocatedSamplesPerChannel = ((size_t) newNumSamples + 3) & ~3u;
  289. const size_t channelListSize = ((sizeof (Type*) * (size_t) (newNumChannels + 1)) + 15) & ~15u;
  290. const size_t newTotalBytes = ((size_t) newNumChannels * (size_t) allocatedSamplesPerChannel * sizeof (Type))
  291. + channelListSize + 32;
  292. if (keepExistingContent)
  293. {
  294. HeapBlock<char, true> newData;
  295. newData.allocate (newTotalBytes, clearExtraSpace || isClear);
  296. const size_t numSamplesToCopy = (size_t) jmin (newNumSamples, size);
  297. Type** const newChannels = reinterpret_cast<Type**> (newData.getData());
  298. Type* newChan = reinterpret_cast<Type*> (newData + channelListSize);
  299. for (int j = 0; j < newNumChannels; ++j)
  300. {
  301. newChannels[j] = newChan;
  302. newChan += allocatedSamplesPerChannel;
  303. }
  304. if (! isClear)
  305. {
  306. const int numChansToCopy = jmin (numChannels, newNumChannels);
  307. for (int i = 0; i < numChansToCopy; ++i)
  308. FloatVectorOperations::copy (newChannels[i], channels[i], (int) numSamplesToCopy);
  309. }
  310. allocatedData.swapWith (newData);
  311. allocatedBytes = newTotalBytes;
  312. channels = newChannels;
  313. }
  314. else
  315. {
  316. if (avoidReallocating && allocatedBytes >= newTotalBytes)
  317. {
  318. if (clearExtraSpace || isClear)
  319. allocatedData.clear (newTotalBytes);
  320. }
  321. else
  322. {
  323. allocatedBytes = newTotalBytes;
  324. allocatedData.allocate (newTotalBytes, clearExtraSpace || isClear);
  325. channels = reinterpret_cast<Type**> (allocatedData.getData());
  326. }
  327. Type* chan = reinterpret_cast<Type*> (allocatedData + channelListSize);
  328. for (int i = 0; i < newNumChannels; ++i)
  329. {
  330. channels[i] = chan;
  331. chan += allocatedSamplesPerChannel;
  332. }
  333. }
  334. channels [newNumChannels] = 0;
  335. size = newNumSamples;
  336. numChannels = newNumChannels;
  337. }
  338. }
  339. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  340. There's also a constructor that lets you specify arrays like this, but this
  341. lets you change the channels dynamically.
  342. Note that if the buffer is resized or its number of channels is changed, it
  343. will re-allocate memory internally and copy the existing data to this new area,
  344. so it will then stop directly addressing this memory.
  345. @param dataToReferTo a pre-allocated array containing pointers to the data
  346. for each channel that should be used by this buffer. The
  347. buffer will only refer to this memory, it won't try to delete
  348. it when the buffer is deleted or resized.
  349. @param newNumChannels the number of channels to use - this must correspond to the
  350. number of elements in the array passed in
  351. @param newNumSamples the number of samples to use - this must correspond to the
  352. size of the arrays passed in
  353. */
  354. void setDataToReferTo (Type** dataToReferTo,
  355. const int newNumChannels,
  356. const int newNumSamples) noexcept
  357. {
  358. jassert (dataToReferTo != nullptr);
  359. jassert (newNumChannels >= 0 && newNumSamples >= 0);
  360. if (allocatedBytes != 0)
  361. {
  362. allocatedBytes = 0;
  363. allocatedData.free();
  364. }
  365. numChannels = newNumChannels;
  366. size = newNumSamples;
  367. allocateChannels (dataToReferTo, 0);
  368. jassert (! isClear);
  369. }
  370. /** Resizes this buffer to match the given one, and copies all of its content across.
  371. The source buffer can contain a different floating point type, so this can be used to
  372. convert between 32 and 64 bit float buffer types.
  373. */
  374. template <typename OtherType>
  375. void makeCopyOf (const AudioBuffer<OtherType>& other, bool avoidReallocating = false)
  376. {
  377. setSize (other.getNumChannels(), other.getNumSamples(), false, false, avoidReallocating);
  378. if (other.hasBeenCleared())
  379. {
  380. clear();
  381. }
  382. else
  383. {
  384. for (int chan = 0; chan < numChannels; ++chan)
  385. {
  386. Type* const dest = channels[chan];
  387. const OtherType* const src = other.getReadPointer (chan);
  388. for (int i = 0; i < size; ++i)
  389. dest[i] = static_cast<Type> (src[i]);
  390. }
  391. }
  392. }
  393. //==============================================================================
  394. /** Clears all the samples in all channels. */
  395. void clear() noexcept
  396. {
  397. if (! isClear)
  398. {
  399. for (int i = 0; i < numChannels; ++i)
  400. FloatVectorOperations::clear (channels[i], size);
  401. isClear = true;
  402. }
  403. }
  404. /** Clears a specified region of all the channels.
  405. For speed, this doesn't check whether the channel and sample number
  406. are in-range, so be careful!
  407. */
  408. void clear (int startSample,
  409. int numSamples) noexcept
  410. {
  411. jassert (startSample >= 0 && startSample + numSamples <= size);
  412. if (! isClear)
  413. {
  414. if (startSample == 0 && numSamples == size)
  415. isClear = true;
  416. for (int i = 0; i < numChannels; ++i)
  417. FloatVectorOperations::clear (channels[i] + startSample, numSamples);
  418. }
  419. }
  420. /** Clears a specified region of just one channel.
  421. For speed, this doesn't check whether the channel and sample number
  422. are in-range, so be careful!
  423. */
  424. void clear (int channel,
  425. int startSample,
  426. int numSamples) noexcept
  427. {
  428. jassert (isPositiveAndBelow (channel, numChannels));
  429. jassert (startSample >= 0 && startSample + numSamples <= size);
  430. if (! isClear)
  431. FloatVectorOperations::clear (channels [channel] + startSample, numSamples);
  432. }
  433. /** Returns true if the buffer has been entirely cleared.
  434. Note that this does not actually measure the contents of the buffer - it simply
  435. returns a flag that is set when the buffer is cleared, and which is reset whenever
  436. functions like getWritePointer() are invoked. That means the method does not take
  437. any time, but it may return false negatives when in fact the buffer is still empty.
  438. */
  439. bool hasBeenCleared() const noexcept { return isClear; }
  440. //==============================================================================
  441. /** Returns a sample from the buffer.
  442. The channel and index are not checked - they are expected to be in-range. If not,
  443. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  444. territory.
  445. */
  446. Type getSample (int channel, int sampleIndex) const noexcept
  447. {
  448. jassert (isPositiveAndBelow (channel, numChannels));
  449. jassert (isPositiveAndBelow (sampleIndex, size));
  450. return *(channels [channel] + sampleIndex);
  451. }
  452. /** Sets a sample in the buffer.
  453. The channel and index are not checked - they are expected to be in-range. If not,
  454. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  455. territory.
  456. */
  457. void setSample (int destChannel, int destSample, Type newValue) noexcept
  458. {
  459. jassert (isPositiveAndBelow (destChannel, numChannels));
  460. jassert (isPositiveAndBelow (destSample, size));
  461. *(channels [destChannel] + destSample) = newValue;
  462. isClear = false;
  463. }
  464. /** Adds a value to a sample in the buffer.
  465. The channel and index are not checked - they are expected to be in-range. If not,
  466. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  467. territory.
  468. */
  469. void addSample (int destChannel, int destSample, Type valueToAdd) noexcept
  470. {
  471. jassert (isPositiveAndBelow (destChannel, numChannels));
  472. jassert (isPositiveAndBelow (destSample, size));
  473. *(channels [destChannel] + destSample) += valueToAdd;
  474. isClear = false;
  475. }
  476. /** Applies a gain multiple to a region of one channel.
  477. For speed, this doesn't check whether the channel and sample number
  478. are in-range, so be careful!
  479. */
  480. void applyGain (int channel,
  481. int startSample,
  482. int numSamples,
  483. Type gain) noexcept
  484. {
  485. jassert (isPositiveAndBelow (channel, numChannels));
  486. jassert (startSample >= 0 && startSample + numSamples <= size);
  487. if (gain != 1.0f && ! isClear)
  488. {
  489. Type* const d = channels [channel] + startSample;
  490. if (gain == 0.0f)
  491. FloatVectorOperations::clear (d, numSamples);
  492. else
  493. FloatVectorOperations::multiply (d, gain, numSamples);
  494. }
  495. }
  496. /** Applies a gain multiple to a region of all the channels.
  497. For speed, this doesn't check whether the sample numbers
  498. are in-range, so be careful!
  499. */
  500. void applyGain (int startSample,
  501. int numSamples,
  502. Type gain) noexcept
  503. {
  504. for (int i = 0; i < numChannels; ++i)
  505. applyGain (i, startSample, numSamples, gain);
  506. }
  507. /** Applies a gain multiple to all the audio data. */
  508. void applyGain (Type gain) noexcept
  509. {
  510. applyGain (0, size, gain);
  511. }
  512. /** Applies a range of gains to a region of a channel.
  513. The gain that is applied to each sample will vary from
  514. startGain on the first sample to endGain on the last Sample,
  515. so it can be used to do basic fades.
  516. For speed, this doesn't check whether the sample numbers
  517. are in-range, so be careful!
  518. */
  519. void applyGainRamp (int channel,
  520. int startSample,
  521. int numSamples,
  522. Type startGain,
  523. Type endGain) noexcept
  524. {
  525. if (! isClear)
  526. {
  527. if (startGain == endGain)
  528. {
  529. applyGain (channel, startSample, numSamples, startGain);
  530. }
  531. else
  532. {
  533. jassert (isPositiveAndBelow (channel, numChannels));
  534. jassert (startSample >= 0 && startSample + numSamples <= size);
  535. const Type increment = (endGain - startGain) / numSamples;
  536. Type* d = channels [channel] + startSample;
  537. while (--numSamples >= 0)
  538. {
  539. *d++ *= startGain;
  540. startGain += increment;
  541. }
  542. }
  543. }
  544. }
  545. /** Applies a range of gains to a region of all channels.
  546. The gain that is applied to each sample will vary from
  547. startGain on the first sample to endGain on the last Sample,
  548. so it can be used to do basic fades.
  549. For speed, this doesn't check whether the sample numbers
  550. are in-range, so be careful!
  551. */
  552. void applyGainRamp (int startSample,
  553. int numSamples,
  554. Type startGain,
  555. Type endGain) noexcept
  556. {
  557. for (int i = 0; i < numChannels; ++i)
  558. applyGainRamp (i, startSample, numSamples, startGain, endGain);
  559. }
  560. /** Adds samples from another buffer to this one.
  561. @param destChannel the channel within this buffer to add the samples to
  562. @param destStartSample the start sample within this buffer's channel
  563. @param source the source buffer to add from
  564. @param sourceChannel the channel within the source buffer to read from
  565. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  566. @param numSamples the number of samples to process
  567. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  568. added to this buffer's samples
  569. @see copyFrom
  570. */
  571. void addFrom (int destChannel,
  572. int destStartSample,
  573. const AudioBuffer& source,
  574. int sourceChannel,
  575. int sourceStartSample,
  576. int numSamples,
  577. Type gainToApplyToSource = (Type) 1) noexcept
  578. {
  579. jassert (&source != this || sourceChannel != destChannel);
  580. jassert (isPositiveAndBelow (destChannel, numChannels));
  581. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  582. jassert (isPositiveAndBelow (sourceChannel, source.numChannels));
  583. jassert (sourceStartSample >= 0 && sourceStartSample + numSamples <= source.size);
  584. if (gainToApplyToSource != 0.0f && numSamples > 0 && ! source.isClear)
  585. {
  586. Type* const d = channels [destChannel] + destStartSample;
  587. const Type* const s = source.channels [sourceChannel] + sourceStartSample;
  588. if (isClear)
  589. {
  590. isClear = false;
  591. if (gainToApplyToSource != 1.0f)
  592. FloatVectorOperations::copyWithMultiply (d, s, gainToApplyToSource, numSamples);
  593. else
  594. FloatVectorOperations::copy (d, s, numSamples);
  595. }
  596. else
  597. {
  598. if (gainToApplyToSource != 1.0f)
  599. FloatVectorOperations::addWithMultiply (d, s, gainToApplyToSource, numSamples);
  600. else
  601. FloatVectorOperations::add (d, s, numSamples);
  602. }
  603. }
  604. }
  605. /** Adds samples from an array of floats to one of the channels.
  606. @param destChannel the channel within this buffer to add the samples to
  607. @param destStartSample the start sample within this buffer's channel
  608. @param source the source data to use
  609. @param numSamples the number of samples to process
  610. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  611. added to this buffer's samples
  612. @see copyFrom
  613. */
  614. void addFrom (int destChannel,
  615. int destStartSample,
  616. const Type* source,
  617. int numSamples,
  618. Type gainToApplyToSource = (Type) 1) noexcept
  619. {
  620. jassert (isPositiveAndBelow (destChannel, numChannels));
  621. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  622. jassert (source != nullptr);
  623. if (gainToApplyToSource != 0.0f && numSamples > 0)
  624. {
  625. Type* const d = channels [destChannel] + destStartSample;
  626. if (isClear)
  627. {
  628. isClear = false;
  629. if (gainToApplyToSource != 1.0f)
  630. FloatVectorOperations::copyWithMultiply (d, source, gainToApplyToSource, numSamples);
  631. else
  632. FloatVectorOperations::copy (d, source, numSamples);
  633. }
  634. else
  635. {
  636. if (gainToApplyToSource != 1.0f)
  637. FloatVectorOperations::addWithMultiply (d, source, gainToApplyToSource, numSamples);
  638. else
  639. FloatVectorOperations::add (d, source, numSamples);
  640. }
  641. }
  642. }
  643. /** Adds samples from an array of floats, applying a gain ramp to them.
  644. @param destChannel the channel within this buffer to add the samples to
  645. @param destStartSample the start sample within this buffer's channel
  646. @param source the source data to use
  647. @param numSamples the number of samples to process
  648. @param startGain the gain to apply to the first sample (this is multiplied with
  649. the source samples before they are added to this buffer)
  650. @param endGain the gain to apply to the final sample. The gain is linearly
  651. interpolated between the first and last samples.
  652. */
  653. void addFromWithRamp (int destChannel,
  654. int destStartSample,
  655. const Type* source,
  656. int numSamples,
  657. Type startGain,
  658. Type endGain) noexcept
  659. {
  660. jassert (isPositiveAndBelow (destChannel, numChannels));
  661. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  662. jassert (source != nullptr);
  663. if (startGain == endGain)
  664. {
  665. addFrom (destChannel, destStartSample, source, numSamples, startGain);
  666. }
  667. else
  668. {
  669. if (numSamples > 0 && (startGain != 0.0f || endGain != 0.0f))
  670. {
  671. isClear = false;
  672. const Type increment = (endGain - startGain) / numSamples;
  673. Type* d = channels [destChannel] + destStartSample;
  674. while (--numSamples >= 0)
  675. {
  676. *d++ += startGain * *source++;
  677. startGain += increment;
  678. }
  679. }
  680. }
  681. }
  682. /** Copies samples from another buffer to this one.
  683. @param destChannel the channel within this buffer to copy the samples to
  684. @param destStartSample the start sample within this buffer's channel
  685. @param source the source buffer to read from
  686. @param sourceChannel the channel within the source buffer to read from
  687. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  688. @param numSamples the number of samples to process
  689. @see addFrom
  690. */
  691. void copyFrom (int destChannel,
  692. int destStartSample,
  693. const AudioBuffer& source,
  694. int sourceChannel,
  695. int sourceStartSample,
  696. int numSamples) noexcept
  697. {
  698. jassert (&source != this || sourceChannel != destChannel);
  699. jassert (isPositiveAndBelow (destChannel, numChannels));
  700. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  701. jassert (isPositiveAndBelow (sourceChannel, source.numChannels));
  702. jassert (sourceStartSample >= 0 && sourceStartSample + numSamples <= source.size);
  703. if (numSamples > 0)
  704. {
  705. if (source.isClear)
  706. {
  707. if (! isClear)
  708. FloatVectorOperations::clear (channels [destChannel] + destStartSample, numSamples);
  709. }
  710. else
  711. {
  712. isClear = false;
  713. FloatVectorOperations::copy (channels [destChannel] + destStartSample,
  714. source.channels [sourceChannel] + sourceStartSample,
  715. numSamples);
  716. }
  717. }
  718. }
  719. /** Copies samples from an array of floats into one of the channels.
  720. @param destChannel the channel within this buffer to copy the samples to
  721. @param destStartSample the start sample within this buffer's channel
  722. @param source the source buffer to read from
  723. @param numSamples the number of samples to process
  724. @see addFrom
  725. */
  726. void copyFrom (int destChannel,
  727. int destStartSample,
  728. const Type* source,
  729. int numSamples) noexcept
  730. {
  731. jassert (isPositiveAndBelow (destChannel, numChannels));
  732. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  733. jassert (source != nullptr);
  734. if (numSamples > 0)
  735. {
  736. isClear = false;
  737. FloatVectorOperations::copy (channels [destChannel] + destStartSample, source, numSamples);
  738. }
  739. }
  740. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  741. @param destChannel the channel within this buffer to copy the samples to
  742. @param destStartSample the start sample within this buffer's channel
  743. @param source the source buffer to read from
  744. @param numSamples the number of samples to process
  745. @param gain the gain to apply
  746. @see addFrom
  747. */
  748. void copyFrom (int destChannel,
  749. int destStartSample,
  750. const Type* source,
  751. int numSamples,
  752. Type gain) noexcept
  753. {
  754. jassert (isPositiveAndBelow (destChannel, numChannels));
  755. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  756. jassert (source != nullptr);
  757. if (numSamples > 0)
  758. {
  759. Type* const d = channels [destChannel] + destStartSample;
  760. if (gain != 1.0f)
  761. {
  762. if (gain == 0)
  763. {
  764. if (! isClear)
  765. FloatVectorOperations::clear (d, numSamples);
  766. }
  767. else
  768. {
  769. isClear = false;
  770. FloatVectorOperations::copyWithMultiply (d, source, gain, numSamples);
  771. }
  772. }
  773. else
  774. {
  775. isClear = false;
  776. FloatVectorOperations::copy (d, source, numSamples);
  777. }
  778. }
  779. }
  780. /** Copies samples from an array of floats into one of the channels, applying a gain ramp.
  781. @param destChannel the channel within this buffer to copy the samples to
  782. @param destStartSample the start sample within this buffer's channel
  783. @param source the source buffer to read from
  784. @param numSamples the number of samples to process
  785. @param startGain the gain to apply to the first sample (this is multiplied with
  786. the source samples before they are copied to this buffer)
  787. @param endGain the gain to apply to the final sample. The gain is linearly
  788. interpolated between the first and last samples.
  789. @see addFrom
  790. */
  791. void copyFromWithRamp (int destChannel,
  792. int destStartSample,
  793. const Type* source,
  794. int numSamples,
  795. Type startGain,
  796. Type endGain) noexcept
  797. {
  798. jassert (isPositiveAndBelow (destChannel, numChannels));
  799. jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
  800. jassert (source != nullptr);
  801. if (startGain == endGain)
  802. {
  803. copyFrom (destChannel, destStartSample, source, numSamples, startGain);
  804. }
  805. else
  806. {
  807. if (numSamples > 0 && (startGain != 0.0f || endGain != 0.0f))
  808. {
  809. isClear = false;
  810. const Type increment = (endGain - startGain) / numSamples;
  811. Type* d = channels [destChannel] + destStartSample;
  812. while (--numSamples >= 0)
  813. {
  814. *d++ = startGain * *source++;
  815. startGain += increment;
  816. }
  817. }
  818. }
  819. }
  820. /** Returns a Range indicating the lowest and highest sample values in a given section.
  821. @param channel the channel to read from
  822. @param startSample the start sample within the channel
  823. @param numSamples the number of samples to check
  824. */
  825. Range<Type> findMinMax (int channel,
  826. int startSample,
  827. int numSamples) const noexcept
  828. {
  829. jassert (isPositiveAndBelow (channel, numChannels));
  830. jassert (startSample >= 0 && startSample + numSamples <= size);
  831. if (isClear)
  832. return Range<Type>();
  833. return FloatVectorOperations::findMinAndMax (channels [channel] + startSample, numSamples);
  834. }
  835. /** Finds the highest absolute sample value within a region of a channel. */
  836. Type getMagnitude (int channel,
  837. int startSample,
  838. int numSamples) const noexcept
  839. {
  840. jassert (isPositiveAndBelow (channel, numChannels));
  841. jassert (startSample >= 0 && startSample + numSamples <= size);
  842. if (isClear)
  843. return 0.0f;
  844. const Range<Type> r (findMinMax (channel, startSample, numSamples));
  845. return jmax (r.getStart(), -r.getStart(), r.getEnd(), -r.getEnd());
  846. }
  847. /** Finds the highest absolute sample value within a region on all channels. */
  848. Type getMagnitude (int startSample,
  849. int numSamples) const noexcept
  850. {
  851. Type mag = 0.0f;
  852. if (! isClear)
  853. for (int i = 0; i < numChannels; ++i)
  854. mag = jmax (mag, getMagnitude (i, startSample, numSamples));
  855. return mag;
  856. }
  857. /** Returns the root mean squared level for a region of a channel. */
  858. Type getRMSLevel (int channel,
  859. int startSample,
  860. int numSamples) const noexcept
  861. {
  862. jassert (isPositiveAndBelow (channel, numChannels));
  863. jassert (startSample >= 0 && startSample + numSamples <= size);
  864. if (numSamples <= 0 || channel < 0 || channel >= numChannels || isClear)
  865. return 0.0f;
  866. const Type* const data = channels [channel] + startSample;
  867. double sum = 0.0;
  868. for (int i = 0; i < numSamples; ++i)
  869. {
  870. const Type sample = data [i];
  871. sum += sample * sample;
  872. }
  873. return (Type) std::sqrt (sum / numSamples);
  874. }
  875. /** Reverses a part of a channel. */
  876. void reverse (int channel, int startSample, int numSamples) const noexcept
  877. {
  878. jassert (isPositiveAndBelow (channel, numChannels));
  879. jassert (startSample >= 0 && startSample + numSamples <= size);
  880. if (! isClear)
  881. std::reverse (channels[channel] + startSample,
  882. channels[channel] + startSample + numSamples);
  883. }
  884. /** Reverses a part of the buffer. */
  885. void reverse (int startSample, int numSamples) const noexcept
  886. {
  887. for (int i = 0; i < numChannels; ++i)
  888. reverse (i, startSample, numSamples);
  889. }
  890. private:
  891. //==============================================================================
  892. int numChannels, size;
  893. size_t allocatedBytes;
  894. Type** channels;
  895. HeapBlock<char, true> allocatedData;
  896. Type* preallocatedChannelSpace [32];
  897. bool isClear;
  898. void allocateData()
  899. {
  900. const size_t channelListSize = sizeof (Type*) * (size_t) (numChannels + 1);
  901. allocatedBytes = (size_t) numChannels * (size_t) size * sizeof (Type) + channelListSize + 32;
  902. allocatedData.malloc (allocatedBytes);
  903. channels = reinterpret_cast<Type**> (allocatedData.getData());
  904. Type* chan = (Type*) (allocatedData + channelListSize);
  905. for (int i = 0; i < numChannels; ++i)
  906. {
  907. channels[i] = chan;
  908. chan += size;
  909. }
  910. channels [numChannels] = nullptr;
  911. isClear = false;
  912. }
  913. void allocateChannels (Type* const* const dataToReferTo, int offset)
  914. {
  915. jassert (offset >= 0);
  916. // (try to avoid doing a malloc here, as that'll blow up things like Pro-Tools)
  917. if (numChannels < (int) numElementsInArray (preallocatedChannelSpace))
  918. {
  919. channels = static_cast<Type**> (preallocatedChannelSpace);
  920. }
  921. else
  922. {
  923. allocatedData.malloc ((size_t) numChannels + 1, sizeof (Type*));
  924. channels = reinterpret_cast<Type**> (allocatedData.getData());
  925. }
  926. for (int i = 0; i < numChannels; ++i)
  927. {
  928. // you have to pass in the same number of valid pointers as numChannels
  929. jassert (dataToReferTo[i] != nullptr);
  930. channels[i] = dataToReferTo[i] + offset;
  931. }
  932. channels [numChannels] = nullptr;
  933. isClear = false;
  934. }
  935. JUCE_LEAK_DETECTOR (AudioBuffer)
  936. };
  937. //==============================================================================
  938. /**
  939. A multi-channel buffer of 32-bit floating point audio samples.
  940. This typedef is here for backwards compatibility with the older AudioSampleBuffer
  941. class, which was fixed for 32-bit data, but is otherwise the same as the new
  942. templated AudioBuffer class.
  943. @see AudioBuffer
  944. */
  945. typedef AudioBuffer<float> AudioSampleBuffer;
  946. #endif // JUCE_AUDIOSAMPLEBUFFER_H_INCLUDED