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.

645 lines
38KB

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