Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_AudioSampleBuffer.h 41KB

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