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.

523 lines
24KB

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