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.

692 lines
39KB

  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_AUDIODATACONVERTERS_H_INCLUDED
  18. #define JUCE_AUDIODATACONVERTERS_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. This class a container which holds all the classes pertaining to the AudioData::Pointer
  22. audio sample format class.
  23. @see AudioData::Pointer.
  24. */
  25. class JUCE_API AudioData
  26. {
  27. public:
  28. //==============================================================================
  29. // These types can be used as the SampleFormat template parameter for the AudioData::Pointer class.
  30. class Int8; /**< Used as a template parameter for AudioData::Pointer. Indicates an 8-bit integer packed data format. */
  31. class UInt8; /**< Used as a template parameter for AudioData::Pointer. Indicates an 8-bit unsigned integer packed data format. */
  32. class Int16; /**< Used as a template parameter for AudioData::Pointer. Indicates an 16-bit integer packed data format. */
  33. class Int24; /**< Used as a template parameter for AudioData::Pointer. Indicates an 24-bit integer packed data format. */
  34. class Int32; /**< Used as a template parameter for AudioData::Pointer. Indicates an 32-bit integer packed data format. */
  35. class Float32; /**< Used as a template parameter for AudioData::Pointer. Indicates an 32-bit float data format. */
  36. //==============================================================================
  37. // These types can be used as the Endianness template parameter for the AudioData::Pointer class.
  38. class BigEndian; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples are stored in big-endian order. */
  39. class LittleEndian; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples are stored in little-endian order. */
  40. class NativeEndian; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples are stored in the CPU's native endianness. */
  41. //==============================================================================
  42. // These types can be used as the InterleavingType template parameter for the AudioData::Pointer class.
  43. class NonInterleaved; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples are stored contiguously. */
  44. class Interleaved; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples are interleaved with a number of other channels. */
  45. //==============================================================================
  46. // These types can be used as the Constness template parameter for the AudioData::Pointer class.
  47. class NonConst; /**< Used as a template parameter for AudioData::Pointer. Indicates that the pointer can be used for non-const data. */
  48. class Const; /**< Used as a template parameter for AudioData::Pointer. Indicates that the samples can only be used for const data.. */
  49. #ifndef DOXYGEN
  50. //==============================================================================
  51. class BigEndian
  52. {
  53. public:
  54. template <class SampleFormatType> static inline float getAsFloat (SampleFormatType& s) noexcept { return s.getAsFloatBE(); }
  55. template <class SampleFormatType> static inline void setAsFloat (SampleFormatType& s, float newValue) noexcept { s.setAsFloatBE (newValue); }
  56. template <class SampleFormatType> static inline int32 getAsInt32 (SampleFormatType& s) noexcept { return s.getAsInt32BE(); }
  57. template <class SampleFormatType> static inline void setAsInt32 (SampleFormatType& s, int32 newValue) noexcept { s.setAsInt32BE (newValue); }
  58. template <class SourceType, class DestType> static inline void copyFrom (DestType& dest, SourceType& source) noexcept { dest.copyFromBE (source); }
  59. enum { isBigEndian = 1 };
  60. };
  61. class LittleEndian
  62. {
  63. public:
  64. template <class SampleFormatType> static inline float getAsFloat (SampleFormatType& s) noexcept { return s.getAsFloatLE(); }
  65. template <class SampleFormatType> static inline void setAsFloat (SampleFormatType& s, float newValue) noexcept { s.setAsFloatLE (newValue); }
  66. template <class SampleFormatType> static inline int32 getAsInt32 (SampleFormatType& s) noexcept { return s.getAsInt32LE(); }
  67. template <class SampleFormatType> static inline void setAsInt32 (SampleFormatType& s, int32 newValue) noexcept { s.setAsInt32LE (newValue); }
  68. template <class SourceType, class DestType> static inline void copyFrom (DestType& dest, SourceType& source) noexcept { dest.copyFromLE (source); }
  69. enum { isBigEndian = 0 };
  70. };
  71. #if JUCE_BIG_ENDIAN
  72. class NativeEndian : public BigEndian {};
  73. #else
  74. class NativeEndian : public LittleEndian {};
  75. #endif
  76. //==============================================================================
  77. class Int8
  78. {
  79. public:
  80. inline Int8 (void* d) noexcept : data (static_cast <int8*> (d)) {}
  81. inline void advance() noexcept { ++data; }
  82. inline void skip (int numSamples) noexcept { data += numSamples; }
  83. inline float getAsFloatLE() const noexcept { return (float) (*data * (1.0 / (1.0 + maxValue))); }
  84. inline float getAsFloatBE() const noexcept { return getAsFloatLE(); }
  85. inline void setAsFloatLE (float newValue) noexcept { *data = (int8) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))); }
  86. inline void setAsFloatBE (float newValue) noexcept { setAsFloatLE (newValue); }
  87. inline int32 getAsInt32LE() const noexcept { return (int) (*data << 24); }
  88. inline int32 getAsInt32BE() const noexcept { return getAsInt32LE(); }
  89. inline void setAsInt32LE (int newValue) noexcept { *data = (int8) (newValue >> 24); }
  90. inline void setAsInt32BE (int newValue) noexcept { setAsInt32LE (newValue); }
  91. inline void clear() noexcept { *data = 0; }
  92. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  93. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  94. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  95. inline void copyFromSameType (Int8& source) noexcept { *data = *source.data; }
  96. int8* data;
  97. enum { bytesPerSample = 1, maxValue = 0x7f, resolution = (1 << 24), isFloat = 0 };
  98. };
  99. class UInt8
  100. {
  101. public:
  102. inline UInt8 (void* d) noexcept : data (static_cast <uint8*> (d)) {}
  103. inline void advance() noexcept { ++data; }
  104. inline void skip (int numSamples) noexcept { data += numSamples; }
  105. inline float getAsFloatLE() const noexcept { return (float) ((*data - 128) * (1.0 / (1.0 + maxValue))); }
  106. inline float getAsFloatBE() const noexcept { return getAsFloatLE(); }
  107. inline void setAsFloatLE (float newValue) noexcept { *data = (uint8) jlimit (0, 255, 128 + roundToInt (newValue * (1.0 + maxValue))); }
  108. inline void setAsFloatBE (float newValue) noexcept { setAsFloatLE (newValue); }
  109. inline int32 getAsInt32LE() const noexcept { return (int) ((*data - 128) << 24); }
  110. inline int32 getAsInt32BE() const noexcept { return getAsInt32LE(); }
  111. inline void setAsInt32LE (int newValue) noexcept { *data = (uint8) (128 + (newValue >> 24)); }
  112. inline void setAsInt32BE (int newValue) noexcept { setAsInt32LE (newValue); }
  113. inline void clear() noexcept { *data = 128; }
  114. inline void clearMultiple (int num) noexcept { memset (data, 128, (size_t) num) ;}
  115. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  116. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  117. inline void copyFromSameType (UInt8& source) noexcept { *data = *source.data; }
  118. uint8* data;
  119. enum { bytesPerSample = 1, maxValue = 0x7f, resolution = (1 << 24), isFloat = 0 };
  120. };
  121. class Int16
  122. {
  123. public:
  124. inline Int16 (void* d) noexcept : data (static_cast <uint16*> (d)) {}
  125. inline void advance() noexcept { ++data; }
  126. inline void skip (int numSamples) noexcept { data += numSamples; }
  127. inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int16) ByteOrder::swapIfBigEndian (*data)); }
  128. inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int16) ByteOrder::swapIfLittleEndian (*data)); }
  129. inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint16) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue)))); }
  130. inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint16) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue)))); }
  131. inline int32 getAsInt32LE() const noexcept { return (int32) (ByteOrder::swapIfBigEndian ((uint16) *data) << 16); }
  132. inline int32 getAsInt32BE() const noexcept { return (int32) (ByteOrder::swapIfLittleEndian ((uint16) *data) << 16); }
  133. inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint16) (newValue >> 16)); }
  134. inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint16) (newValue >> 16)); }
  135. inline void clear() noexcept { *data = 0; }
  136. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  137. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  138. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  139. inline void copyFromSameType (Int16& source) noexcept { *data = *source.data; }
  140. uint16* data;
  141. enum { bytesPerSample = 2, maxValue = 0x7fff, resolution = (1 << 16), isFloat = 0 };
  142. };
  143. class Int24
  144. {
  145. public:
  146. inline Int24 (void* d) noexcept : data (static_cast <char*> (d)) {}
  147. inline void advance() noexcept { data += 3; }
  148. inline void skip (int numSamples) noexcept { data += 3 * numSamples; }
  149. inline float getAsFloatLE() const noexcept { return (float) (ByteOrder::littleEndian24Bit (data) * (1.0 / (1.0 + maxValue))); }
  150. inline float getAsFloatBE() const noexcept { return (float) (ByteOrder::bigEndian24Bit (data) * (1.0 / (1.0 + maxValue))); }
  151. inline void setAsFloatLE (float newValue) noexcept { ByteOrder::littleEndian24BitToChars (jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))), data); }
  152. inline void setAsFloatBE (float newValue) noexcept { ByteOrder::bigEndian24BitToChars (jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))), data); }
  153. inline int32 getAsInt32LE() const noexcept { return (int32) ByteOrder::littleEndian24Bit (data) << 8; }
  154. inline int32 getAsInt32BE() const noexcept { return (int32) ByteOrder::bigEndian24Bit (data) << 8; }
  155. inline void setAsInt32LE (int32 newValue) noexcept { ByteOrder::littleEndian24BitToChars (newValue >> 8, data); }
  156. inline void setAsInt32BE (int32 newValue) noexcept { ByteOrder::bigEndian24BitToChars (newValue >> 8, data); }
  157. inline void clear() noexcept { data[0] = 0; data[1] = 0; data[2] = 0; }
  158. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  159. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  160. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  161. inline void copyFromSameType (Int24& source) noexcept { data[0] = source.data[0]; data[1] = source.data[1]; data[2] = source.data[2]; }
  162. char* data;
  163. enum { bytesPerSample = 3, maxValue = 0x7fffff, resolution = (1 << 8), isFloat = 0 };
  164. };
  165. class Int32
  166. {
  167. public:
  168. inline Int32 (void* d) noexcept : data (static_cast <uint32*> (d)) {}
  169. inline void advance() noexcept { ++data; }
  170. inline void skip (int numSamples) noexcept { data += numSamples; }
  171. inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfBigEndian (*data)); }
  172. inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfLittleEndian (*data)); }
  173. inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
  174. inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
  175. inline int32 getAsInt32LE() const noexcept { return (int32) ByteOrder::swapIfBigEndian (*data); }
  176. inline int32 getAsInt32BE() const noexcept { return (int32) ByteOrder::swapIfLittleEndian (*data); }
  177. inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) newValue); }
  178. inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) newValue); }
  179. inline void clear() noexcept { *data = 0; }
  180. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  181. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  182. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  183. inline void copyFromSameType (Int32& source) noexcept { *data = *source.data; }
  184. uint32* data;
  185. enum { bytesPerSample = 4, maxValue = 0x7fffffff, resolution = 1, isFloat = 0 };
  186. };
  187. class Float32
  188. {
  189. public:
  190. inline Float32 (void* d) noexcept : data (static_cast <float*> (d)) {}
  191. inline void advance() noexcept { ++data; }
  192. inline void skip (int numSamples) noexcept { data += numSamples; }
  193. #if JUCE_BIG_ENDIAN
  194. inline float getAsFloatBE() const noexcept { return *data; }
  195. inline void setAsFloatBE (float newValue) noexcept { *data = newValue; }
  196. inline float getAsFloatLE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
  197. inline void setAsFloatLE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
  198. #else
  199. inline float getAsFloatLE() const noexcept { return *data; }
  200. inline void setAsFloatLE (float newValue) noexcept { *data = newValue; }
  201. inline float getAsFloatBE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
  202. inline void setAsFloatBE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
  203. #endif
  204. inline int32 getAsInt32LE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatLE()) * (double) maxValue); }
  205. inline int32 getAsInt32BE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatBE()) * (double) maxValue); }
  206. inline void setAsInt32LE (int32 newValue) noexcept { setAsFloatLE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
  207. inline void setAsInt32BE (int32 newValue) noexcept { setAsFloatBE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
  208. inline void clear() noexcept { *data = 0; }
  209. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  210. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsFloatLE (source.getAsFloat()); }
  211. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsFloatBE (source.getAsFloat()); }
  212. inline void copyFromSameType (Float32& source) noexcept { *data = *source.data; }
  213. float* data;
  214. enum { bytesPerSample = 4, maxValue = 0x7fffffff, resolution = (1 << 8), isFloat = 1 };
  215. };
  216. //==============================================================================
  217. class NonInterleaved
  218. {
  219. public:
  220. inline NonInterleaved() noexcept {}
  221. inline NonInterleaved (const NonInterleaved&) noexcept {}
  222. inline NonInterleaved (const int) noexcept {}
  223. inline void copyFrom (const NonInterleaved&) noexcept {}
  224. template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.advance(); }
  225. template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numSamples); }
  226. template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { s.clearMultiple (numSamples); }
  227. template <class SampleFormatType> inline static int getNumBytesBetweenSamples (const SampleFormatType&) noexcept { return SampleFormatType::bytesPerSample; }
  228. enum { isInterleavedType = 0, numInterleavedChannels = 1 };
  229. };
  230. class Interleaved
  231. {
  232. public:
  233. inline Interleaved() noexcept : numInterleavedChannels (1) {}
  234. inline Interleaved (const Interleaved& other) noexcept : numInterleavedChannels (other.numInterleavedChannels) {}
  235. inline Interleaved (const int numInterleavedChans) noexcept : numInterleavedChannels (numInterleavedChans) {}
  236. inline void copyFrom (const Interleaved& other) noexcept { numInterleavedChannels = other.numInterleavedChannels; }
  237. template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.skip (numInterleavedChannels); }
  238. template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numInterleavedChannels * numSamples); }
  239. template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { while (--numSamples >= 0) { s.clear(); s.skip (numInterleavedChannels); } }
  240. template <class SampleFormatType> inline int getNumBytesBetweenSamples (const SampleFormatType&) const noexcept { return numInterleavedChannels * SampleFormatType::bytesPerSample; }
  241. int numInterleavedChannels;
  242. enum { isInterleavedType = 1 };
  243. };
  244. //==============================================================================
  245. class NonConst
  246. {
  247. public:
  248. typedef void VoidType;
  249. static inline void* toVoidPtr (VoidType* v) noexcept { return v; }
  250. enum { isConst = 0 };
  251. };
  252. class Const
  253. {
  254. public:
  255. typedef const void VoidType;
  256. static inline void* toVoidPtr (VoidType* v) noexcept { return const_cast <void*> (v); }
  257. enum { isConst = 1 };
  258. };
  259. #endif
  260. //==============================================================================
  261. /**
  262. A pointer to a block of audio data with a particular encoding.
  263. This object can be used to read and write from blocks of encoded audio samples. To create one, you specify
  264. the audio format as a series of template parameters, e.g.
  265. @code
  266. // this creates a pointer for reading from a const array of 16-bit little-endian packed samples.
  267. AudioData::Pointer <AudioData::Int16,
  268. AudioData::LittleEndian,
  269. AudioData::NonInterleaved,
  270. AudioData::Const> pointer (someRawAudioData);
  271. // These methods read the sample that is being pointed to
  272. float firstSampleAsFloat = pointer.getAsFloat();
  273. int32 firstSampleAsInt = pointer.getAsInt32();
  274. ++pointer; // moves the pointer to the next sample.
  275. pointer += 3; // skips the next 3 samples.
  276. @endcode
  277. The convertSamples() method lets you copy a range of samples from one format to another, automatically
  278. converting its format.
  279. @see AudioData::Converter
  280. */
  281. template <typename SampleFormat,
  282. typename Endianness,
  283. typename InterleavingType,
  284. typename Constness>
  285. class Pointer : private InterleavingType // (inherited for EBCO)
  286. {
  287. public:
  288. //==============================================================================
  289. /** Creates a non-interleaved pointer from some raw data in the appropriate format.
  290. This constructor is only used if you've specified the AudioData::NonInterleaved option -
  291. for interleaved formats, use the constructor that also takes a number of channels.
  292. */
  293. Pointer (typename Constness::VoidType* sourceData) noexcept
  294. : data (Constness::toVoidPtr (sourceData))
  295. {
  296. // If you're using interleaved data, call the other constructor! If you're using non-interleaved data,
  297. // you should pass NonInterleaved as the template parameter for the interleaving type!
  298. static_jassert (InterleavingType::isInterleavedType == 0);
  299. }
  300. /** Creates a pointer from some raw data in the appropriate format with the specified number of interleaved channels.
  301. For non-interleaved data, use the other constructor.
  302. */
  303. Pointer (typename Constness::VoidType* sourceData, int numInterleaved) noexcept
  304. : InterleavingType (numInterleaved), data (Constness::toVoidPtr (sourceData))
  305. {
  306. }
  307. /** Creates a copy of another pointer. */
  308. Pointer (const Pointer& other) noexcept
  309. : InterleavingType (other), data (other.data)
  310. {
  311. }
  312. Pointer& operator= (const Pointer& other) noexcept
  313. {
  314. InterleavingType::operator= (other);
  315. data = other.data;
  316. return *this;
  317. }
  318. //==============================================================================
  319. /** Returns the value of the first sample as a floating point value.
  320. The value will be in the range -1.0 to 1.0 for integer formats. For floating point
  321. formats, the value could be outside that range, although -1 to 1 is the standard range.
  322. */
  323. inline float getAsFloat() const noexcept { return Endianness::getAsFloat (data); }
  324. /** Sets the value of the first sample as a floating point value.
  325. (This method can only be used if the AudioData::NonConst option was used).
  326. The value should be in the range -1.0 to 1.0 - for integer formats, values outside that
  327. range will be clipped. For floating point formats, any value passed in here will be
  328. written directly, although -1 to 1 is the standard range.
  329. */
  330. inline void setAsFloat (float newValue) noexcept
  331. {
  332. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  333. Endianness::setAsFloat (data, newValue);
  334. }
  335. /** Returns the value of the first sample as a 32-bit integer.
  336. The value returned will be in the range 0x80000000 to 0x7fffffff, and shorter values will be
  337. shifted to fill this range (e.g. if you're reading from 24-bit data, the values will be shifted up
  338. by 8 bits when returned here). If the source data is floating point, values beyond -1.0 to 1.0 will
  339. be clipped so that -1.0 maps onto -0x7fffffff and 1.0 maps to 0x7fffffff.
  340. */
  341. inline int32 getAsInt32() const noexcept { return Endianness::getAsInt32 (data); }
  342. /** Sets the value of the first sample as a 32-bit integer.
  343. This will be mapped to the range of the format that is being written - see getAsInt32().
  344. */
  345. inline void setAsInt32 (int32 newValue) noexcept
  346. {
  347. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  348. Endianness::setAsInt32 (data, newValue);
  349. }
  350. /** Moves the pointer along to the next sample. */
  351. inline Pointer& operator++() noexcept { advance(); return *this; }
  352. /** Moves the pointer back to the previous sample. */
  353. inline Pointer& operator--() noexcept { this->advanceDataBy (data, -1); return *this; }
  354. /** Adds a number of samples to the pointer's position. */
  355. Pointer& operator+= (int samplesToJump) noexcept { this->advanceDataBy (data, samplesToJump); return *this; }
  356. /** Writes a stream of samples into this pointer from another pointer.
  357. This will copy the specified number of samples, converting between formats appropriately.
  358. */
  359. void convertSamples (Pointer source, int numSamples) const noexcept
  360. {
  361. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  362. for (Pointer dest (*this); --numSamples >= 0;)
  363. {
  364. dest.data.copyFromSameType (source.data);
  365. dest.advance();
  366. source.advance();
  367. }
  368. }
  369. /** Writes a stream of samples into this pointer from another pointer.
  370. This will copy the specified number of samples, converting between formats appropriately.
  371. */
  372. template <class OtherPointerType>
  373. void convertSamples (OtherPointerType source, int numSamples) const noexcept
  374. {
  375. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  376. Pointer dest (*this);
  377. if (source.getRawData() != getRawData() || source.getNumBytesBetweenSamples() >= getNumBytesBetweenSamples())
  378. {
  379. while (--numSamples >= 0)
  380. {
  381. Endianness::copyFrom (dest.data, source);
  382. dest.advance();
  383. ++source;
  384. }
  385. }
  386. else // copy backwards if we're increasing the sample width..
  387. {
  388. dest += numSamples;
  389. source += numSamples;
  390. while (--numSamples >= 0)
  391. Endianness::copyFrom ((--dest).data, --source);
  392. }
  393. }
  394. /** Sets a number of samples to zero. */
  395. void clearSamples (int numSamples) const noexcept
  396. {
  397. Pointer dest (*this);
  398. dest.clear (dest.data, numSamples);
  399. }
  400. /** Scans a block of data, returning the lowest and highest levels as floats */
  401. void findMinAndMax (size_t numSamples, float& minValue, float& maxValue) const noexcept
  402. {
  403. if (numSamples == 0)
  404. {
  405. minValue = maxValue = 0;
  406. return;
  407. }
  408. Pointer dest (*this);
  409. if (isFloatingPoint())
  410. {
  411. float mn = dest.getAsFloat();
  412. dest.advance();
  413. float mx = mn;
  414. while (--numSamples > 0)
  415. {
  416. const float v = dest.getAsFloat();
  417. dest.advance();
  418. if (mx < v) mx = v;
  419. if (v < mn) mn = v;
  420. }
  421. minValue = mn;
  422. maxValue = mx;
  423. }
  424. else
  425. {
  426. int32 mn = dest.getAsInt32();
  427. dest.advance();
  428. int32 mx = mn;
  429. while (--numSamples > 0)
  430. {
  431. const int v = dest.getAsInt32();
  432. dest.advance();
  433. if (mx < v) mx = v;
  434. if (v < mn) mn = v;
  435. }
  436. minValue = mn * (float) (1.0 / (1.0 + Int32::maxValue));
  437. maxValue = mx * (float) (1.0 / (1.0 + Int32::maxValue));
  438. }
  439. }
  440. /** Returns true if the pointer is using a floating-point format. */
  441. static bool isFloatingPoint() noexcept { return (bool) SampleFormat::isFloat; }
  442. /** Returns true if the format is big-endian. */
  443. static bool isBigEndian() noexcept { return (bool) Endianness::isBigEndian; }
  444. /** Returns the number of bytes in each sample (ignoring the number of interleaved channels). */
  445. static int getBytesPerSample() noexcept { return (int) SampleFormat::bytesPerSample; }
  446. /** Returns the number of interleaved channels in the format. */
  447. int getNumInterleavedChannels() const noexcept { return (int) this->numInterleavedChannels; }
  448. /** Returns the number of bytes between the start address of each sample. */
  449. int getNumBytesBetweenSamples() const noexcept { return InterleavingType::getNumBytesBetweenSamples (data); }
  450. /** Returns the accuracy of this format when represented as a 32-bit integer.
  451. This is the smallest number above 0 that can be represented in the sample format, converted to
  452. a 32-bit range. E,g. if the format is 8-bit, its resolution is 0x01000000; if the format is 24-bit,
  453. its resolution is 0x100.
  454. */
  455. static int get32BitResolution() noexcept { return (int) SampleFormat::resolution; }
  456. /** Returns a pointer to the underlying data. */
  457. const void* getRawData() const noexcept { return data.data; }
  458. private:
  459. //==============================================================================
  460. SampleFormat data;
  461. inline void advance() noexcept { this->advanceData (data); }
  462. Pointer operator++ (int); // private to force you to use the more efficient pre-increment!
  463. Pointer operator-- (int);
  464. };
  465. //==============================================================================
  466. /** A base class for objects that are used to convert between two different sample formats.
  467. The AudioData::ConverterInstance implements this base class and can be templated, so
  468. you can create an instance that converts between two particular formats, and then
  469. store this in the abstract base class.
  470. @see AudioData::ConverterInstance
  471. */
  472. class Converter
  473. {
  474. public:
  475. virtual ~Converter() {}
  476. /** Converts a sequence of samples from the converter's source format into the dest format. */
  477. virtual void convertSamples (void* destSamples, const void* sourceSamples, int numSamples) const = 0;
  478. /** Converts a sequence of samples from the converter's source format into the dest format.
  479. This method takes sub-channel indexes, which can be used with interleaved formats in order to choose a
  480. particular sub-channel of the data to be used.
  481. */
  482. virtual void convertSamples (void* destSamples, int destSubChannel,
  483. const void* sourceSamples, int sourceSubChannel, int numSamples) const = 0;
  484. };
  485. //==============================================================================
  486. /**
  487. A class that converts between two templated AudioData::Pointer types, and which
  488. implements the AudioData::Converter interface.
  489. This can be used as a concrete instance of the AudioData::Converter abstract class.
  490. @see AudioData::Converter
  491. */
  492. template <class SourceSampleType, class DestSampleType>
  493. class ConverterInstance : public Converter
  494. {
  495. public:
  496. ConverterInstance (int numSourceChannels = 1, int numDestChannels = 1)
  497. : sourceChannels (numSourceChannels), destChannels (numDestChannels)
  498. {}
  499. ~ConverterInstance() {}
  500. void convertSamples (void* dest, const void* source, int numSamples) const
  501. {
  502. SourceSampleType s (source, sourceChannels);
  503. DestSampleType d (dest, destChannels);
  504. d.convertSamples (s, numSamples);
  505. }
  506. void convertSamples (void* dest, int destSubChannel,
  507. const void* source, int sourceSubChannel, int numSamples) const
  508. {
  509. jassert (destSubChannel < destChannels && sourceSubChannel < sourceChannels);
  510. SourceSampleType s (addBytesToPointer (source, sourceSubChannel * SourceSampleType::getBytesPerSample()), sourceChannels);
  511. DestSampleType d (addBytesToPointer (dest, destSubChannel * DestSampleType::getBytesPerSample()), destChannels);
  512. d.convertSamples (s, numSamples);
  513. }
  514. private:
  515. JUCE_DECLARE_NON_COPYABLE (ConverterInstance)
  516. const int sourceChannels, destChannels;
  517. };
  518. };
  519. //==============================================================================
  520. /**
  521. A set of routines to convert buffers of 32-bit floating point data to and from
  522. various integer formats.
  523. Note that these functions are deprecated - the AudioData class provides a much more
  524. flexible set of conversion classes now.
  525. */
  526. class JUCE_API AudioDataConverters
  527. {
  528. public:
  529. //==============================================================================
  530. static void convertFloatToInt16LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
  531. static void convertFloatToInt16BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
  532. static void convertFloatToInt24LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
  533. static void convertFloatToInt24BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
  534. static void convertFloatToInt32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  535. static void convertFloatToInt32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  536. static void convertFloatToFloat32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  537. static void convertFloatToFloat32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  538. //==============================================================================
  539. static void convertInt16LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
  540. static void convertInt16BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
  541. static void convertInt24LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
  542. static void convertInt24BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
  543. static void convertInt32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  544. static void convertInt32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  545. static void convertFloat32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  546. static void convertFloat32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  547. //==============================================================================
  548. enum DataFormat
  549. {
  550. int16LE,
  551. int16BE,
  552. int24LE,
  553. int24BE,
  554. int32LE,
  555. int32BE,
  556. float32LE,
  557. float32BE,
  558. };
  559. static void convertFloatToFormat (DataFormat destFormat,
  560. const float* source, void* dest, int numSamples);
  561. static void convertFormatToFloat (DataFormat sourceFormat,
  562. const void* source, float* dest, int numSamples);
  563. //==============================================================================
  564. static void interleaveSamples (const float** source, float* dest,
  565. int numSamples, int numChannels);
  566. static void deinterleaveSamples (const float* source, float** dest,
  567. int numSamples, int numChannels);
  568. private:
  569. AudioDataConverters();
  570. JUCE_DECLARE_NON_COPYABLE (AudioDataConverters)
  571. };
  572. #endif // JUCE_AUDIODATACONVERTERS_H_INCLUDED