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 24KB

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
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 32-bit floating point audio samples.
  22. */
  23. class JUCE_API AudioSampleBuffer
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates an empty buffer with 0 channels and 0 length. */
  28. AudioSampleBuffer() noexcept;
  29. //==============================================================================
  30. /** Creates a buffer with a specified number of channels and samples.
  31. The contents of the buffer will initially be undefined, so use clear() to
  32. set all the samples to zero.
  33. The buffer will allocate its memory internally, and this will be released
  34. when the buffer is deleted. If the memory can't be allocated, this will
  35. throw a std::bad_alloc exception.
  36. */
  37. AudioSampleBuffer (int numChannels,
  38. int numSamples) noexcept;
  39. /** Creates a buffer using a pre-allocated block of memory.
  40. Note that if the buffer is resized or its number of channels is changed, it
  41. will re-allocate memory internally and copy the existing data to this new area,
  42. so it will then stop directly addressing this memory.
  43. @param dataToReferTo a pre-allocated array containing pointers to the data
  44. for each channel that should be used by this buffer. The
  45. buffer will only refer to this memory, it won't try to delete
  46. it when the buffer is deleted or resized.
  47. @param numChannels the number of channels to use - this must correspond to the
  48. number of elements in the array passed in
  49. @param numSamples the number of samples to use - this must correspond to the
  50. size of the arrays passed in
  51. */
  52. AudioSampleBuffer (float* const* dataToReferTo,
  53. int numChannels,
  54. int numSamples) noexcept;
  55. /** Creates a buffer using a pre-allocated block of memory.
  56. Note that if the buffer is resized or its number of channels is changed, it
  57. will re-allocate memory internally and copy the existing data to this new area,
  58. so it will then stop directly addressing this memory.
  59. @param dataToReferTo a pre-allocated array containing pointers to the data
  60. for each channel that should be used by this buffer. The
  61. buffer will only refer to this memory, it won't try to delete
  62. it when the buffer is deleted or resized.
  63. @param numChannels the number of channels to use - this must correspond to the
  64. number of elements in the array passed in
  65. @param startSample the offset within the arrays at which the data begins
  66. @param numSamples the number of samples to use - this must correspond to the
  67. size of the arrays passed in
  68. */
  69. AudioSampleBuffer (float* const* dataToReferTo,
  70. int numChannels,
  71. int startSample,
  72. int numSamples) noexcept;
  73. /** Copies another buffer.
  74. This buffer will make its own copy of the other's data, unless the buffer was created
  75. using an external data buffer, in which case boths buffers will just point to the same
  76. shared block of data.
  77. */
  78. AudioSampleBuffer (const AudioSampleBuffer&) noexcept;
  79. /** Copies another buffer onto this one.
  80. This buffer's size will be changed to that of the other buffer.
  81. */
  82. AudioSampleBuffer& operator= (const AudioSampleBuffer&) noexcept;
  83. /** Destructor.
  84. This will free any memory allocated by the buffer.
  85. */
  86. ~AudioSampleBuffer() noexcept;
  87. //==============================================================================
  88. /** Returns the number of channels of audio data that this buffer contains.
  89. @see getSampleData
  90. */
  91. int getNumChannels() const noexcept { return numChannels; }
  92. /** Returns the number of samples allocated in each of the buffer's channels.
  93. @see getSampleData
  94. */
  95. int getNumSamples() const noexcept { return size; }
  96. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  97. For speed, this doesn't check whether the channel number is out of range,
  98. so be careful when using it!
  99. If you need to write to the data, do NOT call this method and const_cast the
  100. result! Instead, you must call getWritePointer so that the buffer knows you're
  101. planning on modifying the data.
  102. */
  103. const float* getReadPointer (int channelNumber) const noexcept
  104. {
  105. jassert (isPositiveAndBelow (channelNumber, numChannels));
  106. return channels [channelNumber];
  107. }
  108. /** Returns a pointer to an array of read-only samples in one of the buffer's channels.
  109. For speed, this doesn't check whether the channel number or index are out of range,
  110. so be careful when using it!
  111. If you need to write to the data, do NOT call this method and const_cast the
  112. result! Instead, you must call getWritePointer so that the buffer knows you're
  113. planning on modifying the data.
  114. */
  115. const float* getReadPointer (int channelNumber, int sampleIndex) const noexcept
  116. {
  117. jassert (isPositiveAndBelow (channelNumber, numChannels));
  118. jassert (isPositiveAndBelow (sampleIndex, size));
  119. return channels [channelNumber] + sampleIndex;
  120. }
  121. /** Returns a writeable pointer to one of the buffer's channels.
  122. For speed, this doesn't check whether the channel number is out of range,
  123. so be careful when using it!
  124. Note that if you're not planning on writing to the data, you should always
  125. use getReadPointer instead.
  126. */
  127. float* getWritePointer (int channelNumber) noexcept
  128. {
  129. jassert (isPositiveAndBelow (channelNumber, numChannels));
  130. isClear = false;
  131. return channels [channelNumber];
  132. }
  133. /** Returns a writeable pointer to one of the buffer's channels.
  134. For speed, this doesn't check whether the channel number or index are out of range,
  135. so be careful when using it!
  136. Note that if you're not planning on writing to the data, you should
  137. use getReadPointer instead.
  138. */
  139. float* getWritePointer (int channelNumber, int sampleIndex) noexcept
  140. {
  141. jassert (isPositiveAndBelow (channelNumber, numChannels));
  142. jassert (isPositiveAndBelow (sampleIndex, size));
  143. isClear = false;
  144. return channels [channelNumber] + sampleIndex;
  145. }
  146. /** Returns an array of pointers to the channels in the buffer.
  147. Don't modify any of the pointers that are returned, and bear in mind that
  148. these will become invalid if the buffer is resized.
  149. */
  150. const float** getArrayOfReadPointers() const noexcept { return const_cast<const float**> (channels); }
  151. /** Returns an array of pointers to the channels in the buffer.
  152. Don't modify any of the pointers that are returned, and bear in mind that
  153. these will become invalid if the buffer is resized.
  154. */
  155. float** getArrayOfWritePointers() noexcept { isClear = false; return channels; }
  156. //==============================================================================
  157. /** Changes the buffer's size or number of channels.
  158. This can expand or contract the buffer's length, and add or remove channels.
  159. If keepExistingContent is true, it will try to preserve as much of the
  160. old data as it can in the new buffer.
  161. If clearExtraSpace is true, then any extra channels or space that is
  162. allocated will be also be cleared. If false, then this space is left
  163. uninitialised.
  164. If avoidReallocating is true, then changing the buffer's size won't reduce the
  165. amount of memory that is currently allocated (but it will still increase it if
  166. the new size is bigger than the amount it currently has). If this is false, then
  167. a new allocation will be done so that the buffer uses takes up the minimum amount
  168. of memory that it needs.
  169. If the required memory can't be allocated, this will throw a std::bad_alloc exception.
  170. */
  171. void setSize (int newNumChannels,
  172. int newNumSamples,
  173. bool keepExistingContent = false,
  174. bool clearExtraSpace = false,
  175. bool avoidReallocating = false) noexcept;
  176. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  177. There's also a constructor that lets you specify arrays like this, but this
  178. lets you change the channels dynamically.
  179. Note that if the buffer is resized or its number of channels is changed, it
  180. will re-allocate memory internally and copy the existing data to this new area,
  181. so it will then stop directly addressing this memory.
  182. @param dataToReferTo a pre-allocated array containing pointers to the data
  183. for each channel that should be used by this buffer. The
  184. buffer will only refer to this memory, it won't try to delete
  185. it when the buffer is deleted or resized.
  186. @param numChannels the number of channels to use - this must correspond to the
  187. number of elements in the array passed in
  188. @param numSamples the number of samples to use - this must correspond to the
  189. size of the arrays passed in
  190. */
  191. void setDataToReferTo (float** dataToReferTo,
  192. int numChannels,
  193. int numSamples) noexcept;
  194. //==============================================================================
  195. /** Clears all the samples in all channels. */
  196. void clear() noexcept;
  197. /** Clears a specified region of all the channels.
  198. For speed, this doesn't check whether the channel and sample number
  199. are in-range, so be careful!
  200. */
  201. void clear (int startSample,
  202. int numSamples) noexcept;
  203. /** Clears a specified region of just one channel.
  204. For speed, this doesn't check whether the channel and sample number
  205. are in-range, so be careful!
  206. */
  207. void clear (int channel,
  208. int startSample,
  209. int numSamples) noexcept;
  210. /** Returns true if the buffer has been entirely cleared.
  211. Note that this does not actually measure the contents of the buffer - it simply
  212. returns a flag that is set when the buffer is cleared, and which is reset whenever
  213. functions like getWritePointer() are invoked. That means the method does not take
  214. any time, but it may return false negatives when in fact the buffer is still empty.
  215. */
  216. bool hasBeenCleared() const noexcept { return isClear; }
  217. //==============================================================================
  218. /** Returns a sample from the buffer.
  219. The channel and index are not checked - they are expected to be in-range. If not,
  220. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  221. territory.
  222. */
  223. float getSample (int channel, int sampleIndex) const noexcept;
  224. /** Sets a sample in the buffer.
  225. The channel and index are not checked - they are expected to be in-range. If not,
  226. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  227. territory.
  228. */
  229. void setSample (int destChannel, int destSample, float newValue) noexcept;
  230. /** Adds a value to a sample in the buffer.
  231. The channel and index are not checked - they are expected to be in-range. If not,
  232. an assertion will be thrown, but in a release build, you're into 'undefined behaviour'
  233. territory.
  234. */
  235. void addSample (int destChannel, int destSample, float valueToAdd) noexcept;
  236. /** Applies a gain multiple to a region of one channel.
  237. For speed, this doesn't check whether the channel and sample number
  238. are in-range, so be careful!
  239. */
  240. void applyGain (int channel,
  241. int startSample,
  242. int numSamples,
  243. float gain) noexcept;
  244. /** Applies a gain multiple to a region of all the channels.
  245. For speed, this doesn't check whether the sample numbers
  246. are in-range, so be careful!
  247. */
  248. void applyGain (int startSample,
  249. int numSamples,
  250. float gain) noexcept;
  251. /** Applies a gain multiple to all the audio data. */
  252. void applyGain (float gain) noexcept;
  253. /** Applies a range of gains to a region of a channel.
  254. The gain that is applied to each sample will vary from
  255. startGain on the first sample to endGain on the last Sample,
  256. so it can be used to do basic fades.
  257. For speed, this doesn't check whether the sample numbers
  258. are in-range, so be careful!
  259. */
  260. void applyGainRamp (int channel,
  261. int startSample,
  262. int numSamples,
  263. float startGain,
  264. float endGain) noexcept;
  265. /** Applies a range of gains to a region of all channels.
  266. The gain that is applied to each sample will vary from
  267. startGain on the first sample to endGain on the last Sample,
  268. so it can be used to do basic fades.
  269. For speed, this doesn't check whether the sample numbers
  270. are in-range, so be careful!
  271. */
  272. void applyGainRamp (int startSample,
  273. int numSamples,
  274. float startGain,
  275. float endGain) noexcept;
  276. /** Adds samples from another buffer to this one.
  277. @param destChannel the channel within this buffer to add the samples to
  278. @param destStartSample the start sample within this buffer's channel
  279. @param source the source buffer to add from
  280. @param sourceChannel the channel within the source buffer to read from
  281. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  282. @param numSamples the number of samples to process
  283. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  284. added to this buffer's samples
  285. @see copyFrom
  286. */
  287. void addFrom (int destChannel,
  288. int destStartSample,
  289. const AudioSampleBuffer& source,
  290. int sourceChannel,
  291. int sourceStartSample,
  292. int numSamples,
  293. float gainToApplyToSource = 1.0f) noexcept;
  294. /** Adds samples from an array of floats to one of the channels.
  295. @param destChannel the channel within this buffer to add the samples to
  296. @param destStartSample the start sample within this buffer's channel
  297. @param source the source data to use
  298. @param numSamples the number of samples to process
  299. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  300. added to this buffer's samples
  301. @see copyFrom
  302. */
  303. void addFrom (int destChannel,
  304. int destStartSample,
  305. const float* source,
  306. int numSamples,
  307. float gainToApplyToSource = 1.0f) noexcept;
  308. /** Adds samples from an array of floats, applying a gain ramp to them.
  309. @param destChannel the channel within this buffer to add the samples to
  310. @param destStartSample the start sample within this buffer's channel
  311. @param source the source data to use
  312. @param numSamples the number of samples to process
  313. @param startGain the gain to apply to the first sample (this is multiplied with
  314. the source samples before they are added to this buffer)
  315. @param endGain the gain to apply to the final sample. The gain is linearly
  316. interpolated between the first and last samples.
  317. */
  318. void addFromWithRamp (int destChannel,
  319. int destStartSample,
  320. const float* source,
  321. int numSamples,
  322. float startGain,
  323. float endGain) noexcept;
  324. /** Copies samples from another buffer to this one.
  325. @param destChannel the channel within this buffer to copy the samples to
  326. @param destStartSample the start sample within this buffer's channel
  327. @param source the source buffer to read from
  328. @param sourceChannel the channel within the source buffer to read from
  329. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  330. @param numSamples the number of samples to process
  331. @see addFrom
  332. */
  333. void copyFrom (int destChannel,
  334. int destStartSample,
  335. const AudioSampleBuffer& source,
  336. int sourceChannel,
  337. int sourceStartSample,
  338. int numSamples) noexcept;
  339. /** Copies samples from an array of floats into one of the channels.
  340. @param destChannel the channel within this buffer to copy the samples to
  341. @param destStartSample the start sample within this buffer's channel
  342. @param source the source buffer to read from
  343. @param numSamples the number of samples to process
  344. @see addFrom
  345. */
  346. void copyFrom (int destChannel,
  347. int destStartSample,
  348. const float* source,
  349. int numSamples) noexcept;
  350. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  351. @param destChannel the channel within this buffer to copy the samples to
  352. @param destStartSample the start sample within this buffer's channel
  353. @param source the source buffer to read from
  354. @param numSamples the number of samples to process
  355. @param gain the gain to apply
  356. @see addFrom
  357. */
  358. void copyFrom (int destChannel,
  359. int destStartSample,
  360. const float* source,
  361. int numSamples,
  362. float gain) noexcept;
  363. /** Copies samples from an array of floats into one of the channels, applying a gain ramp.
  364. @param destChannel the channel within this buffer to copy the samples to
  365. @param destStartSample the start sample within this buffer's channel
  366. @param source the source buffer to read from
  367. @param numSamples the number of samples to process
  368. @param startGain the gain to apply to the first sample (this is multiplied with
  369. the source samples before they are copied to this buffer)
  370. @param endGain the gain to apply to the final sample. The gain is linearly
  371. interpolated between the first and last samples.
  372. @see addFrom
  373. */
  374. void copyFromWithRamp (int destChannel,
  375. int destStartSample,
  376. const float* source,
  377. int numSamples,
  378. float startGain,
  379. float endGain) noexcept;
  380. /** Returns a Range indicating the lowest and highest sample values in a given section.
  381. @param channel the channel to read from
  382. @param startSample the start sample within the channel
  383. @param numSamples the number of samples to check
  384. */
  385. Range<float> findMinMax (int channel,
  386. int startSample,
  387. int numSamples) const noexcept;
  388. /** Finds the highest absolute sample value within a region of a channel. */
  389. float getMagnitude (int channel,
  390. int startSample,
  391. int numSamples) const noexcept;
  392. /** Finds the highest absolute sample value within a region on all channels. */
  393. float getMagnitude (int startSample,
  394. int numSamples) const noexcept;
  395. /** Returns the root mean squared level for a region of a channel. */
  396. float getRMSLevel (int channel,
  397. int startSample,
  398. int numSamples) const noexcept;
  399. /** Reverses a part of a channel. */
  400. void reverse (int channel, int startSample, int numSamples) const noexcept;
  401. /** Reverses a part of the buffer. */
  402. void reverse (int startSample, int numSamples) const noexcept;
  403. //==============================================================================
  404. #ifndef DOXYGEN
  405. // Note that these methods have now been replaced by getReadPointer() and getWritePointer()
  406. JUCE_DEPRECATED_WITH_BODY (const float* getSampleData (int channel) const, { return getReadPointer (channel); })
  407. JUCE_DEPRECATED_WITH_BODY (const float* getSampleData (int channel, int index) const, { return getReadPointer (channel, index); })
  408. JUCE_DEPRECATED_WITH_BODY (float* getSampleData (int channel), { return getWritePointer (channel); })
  409. JUCE_DEPRECATED_WITH_BODY (float* getSampleData (int channel, int index), { return getWritePointer (channel, index); })
  410. // These have been replaced by getArrayOfReadPointers() and getArrayOfWritePointers()
  411. JUCE_DEPRECATED_WITH_BODY (const float** getArrayOfChannels() const, { return getArrayOfReadPointers(); })
  412. JUCE_DEPRECATED_WITH_BODY (float** getArrayOfChannels(), { return getArrayOfWritePointers(); })
  413. #endif
  414. private:
  415. //==============================================================================
  416. int numChannels, size;
  417. size_t allocatedBytes;
  418. float** channels;
  419. HeapBlock<char, true> allocatedData;
  420. float* preallocatedChannelSpace [32];
  421. bool isClear;
  422. void allocateData();
  423. void allocateChannels (float* const*, int offset);
  424. JUCE_LEAK_DETECTOR (AudioSampleBuffer)
  425. };
  426. #endif // JUCE_AUDIOSAMPLEBUFFER_H_INCLUDED