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.

431 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 range of gains to a region of a channel.
  189. The gain that is applied to each sample will vary from
  190. startGain on the first sample to endGain on the last Sample,
  191. so it can be used to do basic fades.
  192. For speed, this doesn't check whether the sample numbers
  193. are in-range, so be careful!
  194. */
  195. void applyGainRamp (int channel,
  196. int startSample,
  197. int numSamples,
  198. float startGain,
  199. float endGain) noexcept;
  200. /** Adds samples from another buffer to this one.
  201. @param destChannel the channel within this buffer to add the samples to
  202. @param destStartSample the start sample within this buffer's channel
  203. @param source the source buffer to add from
  204. @param sourceChannel the channel within the source buffer to read from
  205. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  206. @param numSamples the number of samples to process
  207. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  208. added to this buffer's samples
  209. @see copyFrom
  210. */
  211. void addFrom (int destChannel,
  212. int destStartSample,
  213. const AudioSampleBuffer& source,
  214. int sourceChannel,
  215. int sourceStartSample,
  216. int numSamples,
  217. float gainToApplyToSource = 1.0f) noexcept;
  218. /** Adds samples from an array of floats to one of the channels.
  219. @param destChannel the channel within this buffer to add the samples to
  220. @param destStartSample the start sample within this buffer's channel
  221. @param source the source data to use
  222. @param numSamples the number of samples to process
  223. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  224. added to this buffer's samples
  225. @see copyFrom
  226. */
  227. void addFrom (int destChannel,
  228. int destStartSample,
  229. const float* source,
  230. int numSamples,
  231. float gainToApplyToSource = 1.0f) noexcept;
  232. /** Adds samples from an array of floats, applying a gain ramp to them.
  233. @param destChannel the channel within this buffer to add the samples to
  234. @param destStartSample the start sample within this buffer's channel
  235. @param source the source data to use
  236. @param numSamples the number of samples to process
  237. @param startGain the gain to apply to the first sample (this is multiplied with
  238. the source samples before they are added to this buffer)
  239. @param endGain the gain to apply to the final sample. The gain is linearly
  240. interpolated between the first and last samples.
  241. */
  242. void addFromWithRamp (int destChannel,
  243. int destStartSample,
  244. const float* source,
  245. int numSamples,
  246. float startGain,
  247. float endGain) noexcept;
  248. /** Copies samples from another buffer to this one.
  249. @param destChannel the channel within this buffer to copy the samples to
  250. @param destStartSample the start sample within this buffer's channel
  251. @param source the source buffer to read from
  252. @param sourceChannel the channel within the source buffer to read from
  253. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  254. @param numSamples the number of samples to process
  255. @see addFrom
  256. */
  257. void copyFrom (int destChannel,
  258. int destStartSample,
  259. const AudioSampleBuffer& source,
  260. int sourceChannel,
  261. int sourceStartSample,
  262. int numSamples) noexcept;
  263. /** Copies samples from an array of floats into one of the channels.
  264. @param destChannel the channel within this buffer to copy the samples to
  265. @param destStartSample the start sample within this buffer's channel
  266. @param source the source buffer to read from
  267. @param numSamples the number of samples to process
  268. @see addFrom
  269. */
  270. void copyFrom (int destChannel,
  271. int destStartSample,
  272. const float* source,
  273. int numSamples) noexcept;
  274. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  275. @param destChannel the channel within this buffer to copy the samples to
  276. @param destStartSample the start sample within this buffer's channel
  277. @param source the source buffer to read from
  278. @param numSamples the number of samples to process
  279. @param gain the gain to apply
  280. @see addFrom
  281. */
  282. void copyFrom (int destChannel,
  283. int destStartSample,
  284. const float* source,
  285. int numSamples,
  286. float gain) noexcept;
  287. /** Copies samples from an array of floats into one of the channels, applying a gain ramp.
  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 startGain the gain to apply to the first sample (this is multiplied with
  293. the source samples before they are copied to this buffer)
  294. @param endGain the gain to apply to the final sample. The gain is linearly
  295. interpolated between the first and last samples.
  296. @see addFrom
  297. */
  298. void copyFromWithRamp (int destChannel,
  299. int destStartSample,
  300. const float* source,
  301. int numSamples,
  302. float startGain,
  303. float endGain) noexcept;
  304. /** Finds the highest and lowest sample values in a given range.
  305. @param channel the channel to read from
  306. @param startSample the start sample within the channel
  307. @param numSamples the number of samples to check
  308. @param minVal on return, the lowest value that was found
  309. @param maxVal on return, the highest value that was found
  310. */
  311. void findMinMax (int channel,
  312. int startSample,
  313. int numSamples,
  314. float& minVal,
  315. float& maxVal) const noexcept;
  316. /** Finds the highest absolute sample value within a region of a channel.
  317. */
  318. float getMagnitude (int channel,
  319. int startSample,
  320. int numSamples) const noexcept;
  321. /** Finds the highest absolute sample value within a region on all channels.
  322. */
  323. float getMagnitude (int startSample,
  324. int numSamples) const noexcept;
  325. /** Returns the root mean squared level for a region of a channel.
  326. */
  327. float getRMSLevel (int channel,
  328. int startSample,
  329. int numSamples) const noexcept;
  330. private:
  331. //==============================================================================
  332. int numChannels, size;
  333. size_t allocatedBytes;
  334. float** channels;
  335. HeapBlock <char, true> allocatedData;
  336. float* preallocatedChannelSpace [32];
  337. void allocateData();
  338. void allocateChannels (float* const* dataToReferTo, int offset);
  339. JUCE_LEAK_DETECTOR (AudioSampleBuffer);
  340. };
  341. #endif // __JUCE_AUDIOSAMPLEBUFFER_JUCEHEADER__