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

11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 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. */
  97. float* getSampleData (const int channelNumber) const noexcept
  98. {
  99. jassert (isPositiveAndBelow (channelNumber, numChannels));
  100. return channels [channelNumber];
  101. }
  102. /** Returns a pointer to a sample in one of the buffer's channels.
  103. For speed, this doesn't check whether the channel and sample number
  104. are out-of-range, so be careful when using it!
  105. */
  106. float* getSampleData (const int channelNumber,
  107. const int sampleOffset) const noexcept
  108. {
  109. jassert (isPositiveAndBelow (channelNumber, numChannels));
  110. jassert (isPositiveAndBelow (sampleOffset, size));
  111. return channels [channelNumber] + sampleOffset;
  112. }
  113. /** Returns an array of pointers to the channels in the buffer.
  114. Don't modify any of the pointers that are returned, and bear in mind that
  115. these will become invalid if the buffer is resized.
  116. */
  117. float** getArrayOfChannels() const noexcept { return channels; }
  118. //==============================================================================
  119. /** Changes the buffer's size or number of channels.
  120. This can expand or contract the buffer's length, and add or remove channels.
  121. If keepExistingContent is true, it will try to preserve as much of the
  122. old data as it can in the new buffer.
  123. If clearExtraSpace is true, then any extra channels or space that is
  124. allocated will be also be cleared. If false, then this space is left
  125. uninitialised.
  126. If avoidReallocating is true, then changing the buffer's size won't reduce the
  127. amount of memory that is currently allocated (but it will still increase it if
  128. the new size is bigger than the amount it currently has). If this is false, then
  129. a new allocation will be done so that the buffer uses takes up the minimum amount
  130. of memory that it needs.
  131. If the required memory can't be allocated, this will throw a std::bad_alloc exception.
  132. */
  133. void setSize (int newNumChannels,
  134. int newNumSamples,
  135. bool keepExistingContent = false,
  136. bool clearExtraSpace = false,
  137. bool avoidReallocating = false) noexcept;
  138. /** Makes this buffer point to a pre-allocated set of channel data arrays.
  139. There's also a constructor that lets you specify arrays like this, but this
  140. lets you change the channels dynamically.
  141. Note that if the buffer is resized or its number of channels is changed, it
  142. will re-allocate memory internally and copy the existing data to this new area,
  143. so it will then stop directly addressing this memory.
  144. @param dataToReferTo a pre-allocated array containing pointers to the data
  145. for each channel that should be used by this buffer. The
  146. buffer will only refer to this memory, it won't try to delete
  147. it when the buffer is deleted or resized.
  148. @param numChannels the number of channels to use - this must correspond to the
  149. number of elements in the array passed in
  150. @param numSamples the number of samples to use - this must correspond to the
  151. size of the arrays passed in
  152. */
  153. void setDataToReferTo (float** dataToReferTo,
  154. int numChannels,
  155. int numSamples) noexcept;
  156. //==============================================================================
  157. /** Clears all the samples in all channels. */
  158. void clear() noexcept;
  159. /** Clears a specified region of all the channels.
  160. For speed, this doesn't check whether the channel and sample number
  161. are in-range, so be careful!
  162. */
  163. void clear (int startSample,
  164. int numSamples) noexcept;
  165. /** Clears a specified region of just one channel.
  166. For speed, this doesn't check whether the channel and sample number
  167. are in-range, so be careful!
  168. */
  169. void clear (int channel,
  170. int startSample,
  171. int numSamples) noexcept;
  172. /** Applies a gain multiple to a region of one channel.
  173. For speed, this doesn't check whether the channel and sample number
  174. are in-range, so be careful!
  175. */
  176. void applyGain (int channel,
  177. int startSample,
  178. int numSamples,
  179. float gain) noexcept;
  180. /** Applies a gain multiple to a region of all the channels.
  181. For speed, this doesn't check whether the sample numbers
  182. are in-range, so be careful!
  183. */
  184. void applyGain (int startSample,
  185. int numSamples,
  186. float gain) noexcept;
  187. /** Applies a gain multiple to all the audio data. */
  188. void applyGain (float gain) noexcept;
  189. /** Applies a range of gains to a region of a channel.
  190. The gain that is applied to each sample will vary from
  191. startGain on the first sample to endGain on the last Sample,
  192. so it can be used to do basic fades.
  193. For speed, this doesn't check whether the sample numbers
  194. are in-range, so be careful!
  195. */
  196. void applyGainRamp (int channel,
  197. int startSample,
  198. int numSamples,
  199. float startGain,
  200. float endGain) noexcept;
  201. /** Applies a range of gains to a region of all channels.
  202. The gain that is applied to each sample will vary from
  203. startGain on the first sample to endGain on the last Sample,
  204. so it can be used to do basic fades.
  205. For speed, this doesn't check whether the sample numbers
  206. are in-range, so be careful!
  207. */
  208. void applyGainRamp (int startSample,
  209. int numSamples,
  210. float startGain,
  211. float endGain) noexcept;
  212. /** Adds samples from another buffer to this one.
  213. @param destChannel the channel within this buffer to add the samples to
  214. @param destStartSample the start sample within this buffer's channel
  215. @param source the source buffer to add from
  216. @param sourceChannel the channel within the source buffer to read from
  217. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  218. @param numSamples the number of samples to process
  219. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  220. added to this buffer's samples
  221. @see copyFrom
  222. */
  223. void addFrom (int destChannel,
  224. int destStartSample,
  225. const AudioSampleBuffer& source,
  226. int sourceChannel,
  227. int sourceStartSample,
  228. int numSamples,
  229. float gainToApplyToSource = 1.0f) noexcept;
  230. /** Adds samples from an array of floats to one of the channels.
  231. @param destChannel the channel within this buffer to add the samples to
  232. @param destStartSample the start sample within this buffer's channel
  233. @param source the source data to use
  234. @param numSamples the number of samples to process
  235. @param gainToApplyToSource an optional gain to apply to the source samples before they are
  236. added to this buffer's samples
  237. @see copyFrom
  238. */
  239. void addFrom (int destChannel,
  240. int destStartSample,
  241. const float* source,
  242. int numSamples,
  243. float gainToApplyToSource = 1.0f) noexcept;
  244. /** Adds samples from an array of floats, applying a gain ramp to them.
  245. @param destChannel the channel within this buffer to add the samples to
  246. @param destStartSample the start sample within this buffer's channel
  247. @param source the source data to use
  248. @param numSamples the number of samples to process
  249. @param startGain the gain to apply to the first sample (this is multiplied with
  250. the source samples before they are added to this buffer)
  251. @param endGain the gain to apply to the final sample. The gain is linearly
  252. interpolated between the first and last samples.
  253. */
  254. void addFromWithRamp (int destChannel,
  255. int destStartSample,
  256. const float* source,
  257. int numSamples,
  258. float startGain,
  259. float endGain) noexcept;
  260. /** Copies samples from another buffer to this one.
  261. @param destChannel the channel within this buffer to copy the samples to
  262. @param destStartSample the start sample within this buffer's channel
  263. @param source the source buffer to read from
  264. @param sourceChannel the channel within the source buffer to read from
  265. @param sourceStartSample the offset within the source buffer's channel to start reading samples from
  266. @param numSamples the number of samples to process
  267. @see addFrom
  268. */
  269. void copyFrom (int destChannel,
  270. int destStartSample,
  271. const AudioSampleBuffer& source,
  272. int sourceChannel,
  273. int sourceStartSample,
  274. int numSamples) noexcept;
  275. /** Copies samples from an array of floats into one of the channels.
  276. @param destChannel the channel within this buffer to copy the samples to
  277. @param destStartSample the start sample within this buffer's channel
  278. @param source the source buffer to read from
  279. @param numSamples the number of samples to process
  280. @see addFrom
  281. */
  282. void copyFrom (int destChannel,
  283. int destStartSample,
  284. const float* source,
  285. int numSamples) noexcept;
  286. /** Copies samples from an array of floats into one of the channels, applying a gain to it.
  287. @param destChannel the channel within this buffer to copy the samples to
  288. @param destStartSample the start sample within this buffer's channel
  289. @param source the source buffer to read from
  290. @param numSamples the number of samples to process
  291. @param gain the gain to apply
  292. @see addFrom
  293. */
  294. void copyFrom (int destChannel,
  295. int destStartSample,
  296. const float* source,
  297. int numSamples,
  298. float gain) noexcept;
  299. /** Copies samples from an array of floats into one of the channels, applying a gain ramp.
  300. @param destChannel the channel within this buffer to copy the samples to
  301. @param destStartSample the start sample within this buffer's channel
  302. @param source the source buffer to read from
  303. @param numSamples the number of samples to process
  304. @param startGain the gain to apply to the first sample (this is multiplied with
  305. the source samples before they are copied to this buffer)
  306. @param endGain the gain to apply to the final sample. The gain is linearly
  307. interpolated between the first and last samples.
  308. @see addFrom
  309. */
  310. void copyFromWithRamp (int destChannel,
  311. int destStartSample,
  312. const float* source,
  313. int numSamples,
  314. float startGain,
  315. float endGain) noexcept;
  316. /** Finds the highest and lowest sample values in a given range.
  317. @param channel the channel to read from
  318. @param startSample the start sample within the channel
  319. @param numSamples the number of samples to check
  320. @param minVal on return, the lowest value that was found
  321. @param maxVal on return, the highest value that was found
  322. */
  323. void findMinMax (int channel,
  324. int startSample,
  325. int numSamples,
  326. float& minVal,
  327. float& maxVal) const noexcept;
  328. /** Finds the highest absolute sample value within a region of a channel. */
  329. float getMagnitude (int channel,
  330. int startSample,
  331. int numSamples) const noexcept;
  332. /** Finds the highest absolute sample value within a region on all channels. */
  333. float getMagnitude (int startSample,
  334. int numSamples) const noexcept;
  335. /** Returns the root mean squared level for a region of a channel. */
  336. float getRMSLevel (int channel,
  337. int startSample,
  338. int numSamples) const noexcept;
  339. /** Reverses a part of a channel. */
  340. void reverse (int channel, int startSample, int numSamples) const noexcept;
  341. /** Reverses a part of the buffer. */
  342. void reverse (int startSample, 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_H_INCLUDED