The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

448 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_AUDIOSAMPLEBUFFER_JUCEHEADER__
  19. #define __JUCE_AUDIOSAMPLEBUFFER_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. A multi-channel buffer of 32-bit floating point audio samples.
  23. */
  24. class JUCE_API AudioSampleBuffer
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a buffer with a specified number of channels and samples.
  29. The contents of the buffer will initially be undefined, so use clear() to
  30. set all the samples to zero.
  31. The buffer will allocate its memory internally, and this will be released
  32. when the buffer is deleted. If the memory can't be allocated, this will
  33. throw a std::bad_alloc exception.
  34. */
  35. AudioSampleBuffer (int numChannels,
  36. int numSamples) noexcept;
  37. /** Creates a buffer using a pre-allocated block of memory.
  38. Note that if the buffer is resized or its number of channels is changed, it
  39. will re-allocate memory internally and copy the existing data to this new area,
  40. so it will then stop directly addressing this memory.
  41. @param dataToReferTo a pre-allocated array containing pointers to the data
  42. for each channel that should be used by this buffer. The
  43. buffer will only refer to this memory, it won't try to delete
  44. it when the buffer is deleted or resized.
  45. @param numChannels the number of channels to use - this must correspond to the
  46. number of elements in the array passed in
  47. @param numSamples the number of samples to use - this must correspond to the
  48. size of the arrays passed in
  49. */
  50. AudioSampleBuffer (float* const* dataToReferTo,
  51. int numChannels,
  52. int numSamples) noexcept;
  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 numChannels the number of channels to use - this must correspond to the
  62. number of elements in the array passed in
  63. @param startSample the offset within the arrays at which the data begins
  64. @param numSamples the number of samples to use - this must correspond to the
  65. size of the arrays passed in
  66. */
  67. AudioSampleBuffer (float* const* dataToReferTo,
  68. int numChannels,
  69. int startSample,
  70. int numSamples) noexcept;
  71. /** Copies another buffer.
  72. This buffer will make its own copy of the other's data, unless the buffer was created
  73. using an external data buffer, in which case boths buffers will just point to the same
  74. shared block of data.
  75. */
  76. AudioSampleBuffer (const AudioSampleBuffer& other) noexcept;
  77. /** Copies another buffer onto this one.
  78. This buffer's size will be changed to that of the other buffer.
  79. */
  80. AudioSampleBuffer& operator= (const AudioSampleBuffer& other) noexcept;
  81. /** Destructor.
  82. This will free any memory allocated by the buffer.
  83. */
  84. virtual ~AudioSampleBuffer() noexcept;
  85. //==============================================================================
  86. /** Returns the number of channels of audio data that this buffer contains.
  87. @see getSampleData
  88. */
  89. int getNumChannels() const noexcept { return numChannels; }
  90. /** Returns the number of samples allocated in each of the buffer's channels.
  91. @see getSampleData
  92. */
  93. int getNumSamples() const noexcept { return size; }
  94. /** Returns a pointer one of the buffer's channels.
  95. For speed, this doesn't check whether the channel number is out of range,
  96. so be careful when using it!
  97. */
  98. float* getSampleData (const int channelNumber) const noexcept
  99. {
  100. jassert (isPositiveAndBelow (channelNumber, numChannels));
  101. return channels [channelNumber];
  102. }
  103. /** Returns a pointer to a sample in one of the buffer's channels.
  104. For speed, this doesn't check whether the channel and sample number
  105. are out-of-range, so be careful when using it!
  106. */
  107. float* getSampleData (const int channelNumber,
  108. const int sampleOffset) const noexcept
  109. {
  110. jassert (isPositiveAndBelow (channelNumber, numChannels));
  111. jassert (isPositiveAndBelow (sampleOffset, size));
  112. return channels [channelNumber] + sampleOffset;
  113. }
  114. /** Returns an array of pointers to the channels in the buffer.
  115. Don't modify any of the pointers that are returned, and bear in mind that
  116. these will become invalid if the buffer is resized.
  117. */
  118. float** getArrayOfChannels() const noexcept { return channels; }
  119. //==============================================================================
  120. /** Changes the buffer's size or number of channels.
  121. This can expand or contract the buffer's length, and add or remove channels.
  122. If keepExistingContent is true, it will try to preserve as much of the
  123. old data as it can in the new buffer.
  124. If clearExtraSpace is true, then any extra channels or space that is
  125. allocated will be also be cleared. If false, then this space is left
  126. uninitialised.
  127. If avoidReallocating is true, then changing the buffer's size won't reduce the
  128. amount of memory that is currently allocated (but it will still increase it if
  129. the new size is bigger than the amount it currently has). If this is false, then
  130. a new allocation will be done so that the buffer uses takes up the minimum amount
  131. of memory that it needs.
  132. If the required memory can't be allocated, this will throw a std::bad_alloc exception.
  133. */
  134. void setSize (int newNumChannels,
  135. int newNumSamples,
  136. bool keepExistingContent = false,
  137. bool clearExtraSpace = false,
  138. bool avoidReallocating = false) noexcept;
  139. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  140. There's also a constructor that lets you specify arrays like this, but this
  141. lets you change the channels dynamically.
  142. Note that if the buffer is resized or its number of channels is changed, it
  143. will re-allocate memory internally and copy the existing data to this new area,
  144. so it will then stop directly addressing this memory.
  145. @param dataToReferTo a pre-allocated array containing pointers to the data
  146. for each channel that should be used by this buffer. The
  147. buffer will only refer to this memory, it won't try to delete
  148. it when the buffer is deleted or resized.
  149. @param numChannels the number of channels to use - this must correspond to the
  150. number of elements in the array passed in
  151. @param numSamples the number of samples to use - this must correspond to the
  152. size of the arrays passed in
  153. */
  154. void setDataToReferTo (float** dataToReferTo,
  155. int numChannels,
  156. int numSamples) noexcept;
  157. //==============================================================================
  158. /** Clears all the samples in all channels. */
  159. void clear() noexcept;
  160. /** Clears a specified region of all the channels.
  161. For speed, this doesn't check whether the channel and sample number
  162. are in-range, so be careful!
  163. */
  164. void clear (int startSample,
  165. int numSamples) noexcept;
  166. /** Clears a specified region of just one channel.
  167. For speed, this doesn't check whether the channel and sample number
  168. are in-range, so be careful!
  169. */
  170. void clear (int channel,
  171. int startSample,
  172. int numSamples) noexcept;
  173. /** Applies a gain multiple to a region of one channel.
  174. For speed, this doesn't check whether the channel and sample number
  175. are in-range, so be careful!
  176. */
  177. void applyGain (int channel,
  178. int startSample,
  179. int numSamples,
  180. float gain) noexcept;
  181. /** Applies a gain multiple to a region of all the channels.
  182. For speed, this doesn't check whether the sample numbers
  183. are in-range, so be careful!
  184. */
  185. void applyGain (int startSample,
  186. int numSamples,
  187. float gain) noexcept;
  188. /** Applies a gain multiple to all the audio data. */
  189. void applyGain (float gain) noexcept;
  190. /** Applies a range of gains to a region of a channel.
  191. The gain that is applied to each sample will vary from
  192. startGain on the first sample to endGain on the last Sample,
  193. so it can be used to do basic fades.
  194. For speed, this doesn't check whether the sample numbers
  195. are in-range, so be careful!
  196. */
  197. void applyGainRamp (int channel,
  198. int startSample,
  199. int numSamples,
  200. float startGain,
  201. float endGain) noexcept;
  202. /** Applies a range of gains to a region of all channels.
  203. The gain that is applied to each sample will vary from
  204. startGain on the first sample to endGain on the last Sample,
  205. so it can be used to do basic fades.
  206. For speed, this doesn't check whether the sample numbers
  207. are in-range, so be careful!
  208. */
  209. void applyGainRamp (int startSample,
  210. int numSamples,
  211. float startGain,
  212. float endGain) noexcept;
  213. /** Adds samples from another buffer to this one.
  214. @param destChannel the channel within this buffer to add the samples to
  215. @param destStartSample the start sample within this buffer's channel
  216. @param source the source buffer to add from
  217. @param sourceChannel the channel within the source buffer to read from
  218. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  219. @param numSamples the number of samples to process
  220. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  221. added to this buffer's samples
  222. @see copyFrom
  223. */
  224. void addFrom (int destChannel,
  225. int destStartSample,
  226. const AudioSampleBuffer& source,
  227. int sourceChannel,
  228. int sourceStartSample,
  229. int numSamples,
  230. float gainToApplyToSource = 1.0f) noexcept;
  231. /** Adds samples from an array of floats to one of the channels.
  232. @param destChannel the channel within this buffer to add the samples to
  233. @param destStartSample the start sample within this buffer's channel
  234. @param source the source data to use
  235. @param numSamples the number of samples to process
  236. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  237. added to this buffer's samples
  238. @see copyFrom
  239. */
  240. void addFrom (int destChannel,
  241. int destStartSample,
  242. const float* source,
  243. int numSamples,
  244. float gainToApplyToSource = 1.0f) noexcept;
  245. /** Adds samples from an array of floats, applying a gain ramp to them.
  246. @param destChannel the channel within this buffer to add the samples to
  247. @param destStartSample the start sample within this buffer's channel
  248. @param source the source data to use
  249. @param numSamples the number of samples to process
  250. @param startGain the gain to apply to the first sample (this is multiplied with
  251. the source samples before they are added to this buffer)
  252. @param endGain the gain to apply to the final sample. The gain is linearly
  253. interpolated between the first and last samples.
  254. */
  255. void addFromWithRamp (int destChannel,
  256. int destStartSample,
  257. const float* source,
  258. int numSamples,
  259. float startGain,
  260. float endGain) noexcept;
  261. /** Copies samples from another buffer to this one.
  262. @param destChannel the channel within this buffer to copy the samples to
  263. @param destStartSample the start sample within this buffer's channel
  264. @param source the source buffer to read from
  265. @param sourceChannel the channel within the source buffer to read from
  266. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  267. @param numSamples the number of samples to process
  268. @see addFrom
  269. */
  270. void copyFrom (int destChannel,
  271. int destStartSample,
  272. const AudioSampleBuffer& source,
  273. int sourceChannel,
  274. int sourceStartSample,
  275. int numSamples) noexcept;
  276. /** Copies samples from an array of floats into one of the channels.
  277. @param destChannel the channel within this buffer to copy the samples to
  278. @param destStartSample the start sample within this buffer's channel
  279. @param source the source buffer to read from
  280. @param numSamples the number of samples to process
  281. @see addFrom
  282. */
  283. void copyFrom (int destChannel,
  284. int destStartSample,
  285. const float* source,
  286. int numSamples) noexcept;
  287. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  288. @param destChannel the channel within this buffer to copy the samples to
  289. @param destStartSample the start sample within this buffer's channel
  290. @param source the source buffer to read from
  291. @param numSamples the number of samples to process
  292. @param gain the gain to apply
  293. @see addFrom
  294. */
  295. void copyFrom (int destChannel,
  296. int destStartSample,
  297. const float* source,
  298. int numSamples,
  299. float gain) noexcept;
  300. /** Copies samples from an array of floats into one of the channels, applying a gain ramp.
  301. @param destChannel the channel within this buffer to copy the samples to
  302. @param destStartSample the start sample within this buffer's channel
  303. @param source the source buffer to read from
  304. @param numSamples the number of samples to process
  305. @param startGain the gain to apply to the first sample (this is multiplied with
  306. the source samples before they are copied to this buffer)
  307. @param endGain the gain to apply to the final sample. The gain is linearly
  308. interpolated between the first and last samples.
  309. @see addFrom
  310. */
  311. void copyFromWithRamp (int destChannel,
  312. int destStartSample,
  313. const float* source,
  314. int numSamples,
  315. float startGain,
  316. float endGain) noexcept;
  317. /** Finds the highest and lowest sample values in a given range.
  318. @param channel the channel to read from
  319. @param startSample the start sample within the channel
  320. @param numSamples the number of samples to check
  321. @param minVal on return, the lowest value that was found
  322. @param maxVal on return, the highest value that was found
  323. */
  324. void findMinMax (int channel,
  325. int startSample,
  326. int numSamples,
  327. float& minVal,
  328. float& maxVal) const noexcept;
  329. /** Finds the highest absolute sample value within a region of a channel.
  330. */
  331. float getMagnitude (int channel,
  332. int startSample,
  333. int numSamples) const noexcept;
  334. /** Finds the highest absolute sample value within a region on all channels.
  335. */
  336. float getMagnitude (int startSample,
  337. int numSamples) const noexcept;
  338. /** Returns the root mean squared level for a region of a channel.
  339. */
  340. float getRMSLevel (int channel,
  341. int startSample,
  342. int numSamples) const noexcept;
  343. private:
  344. //==============================================================================
  345. int numChannels, size;
  346. size_t allocatedBytes;
  347. float** channels;
  348. HeapBlock <char, true> allocatedData;
  349. float* preallocatedChannelSpace [32];
  350. void allocateData();
  351. void allocateChannels (float* const* dataToReferTo, int offset);
  352. JUCE_LEAK_DETECTOR (AudioSampleBuffer)
  353. };
  354. #endif // __JUCE_AUDIOSAMPLEBUFFER_JUCEHEADER__