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_AudioDataConverters.h 41KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. /** A 32-bit integer type, of which only the bottom 24 bits are used. */
  188. class Int24in32 : public Int32
  189. {
  190. public:
  191. inline Int24in32 (void* d) noexcept : Int32 (d) {}
  192. inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfBigEndian (*data)); }
  193. inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfLittleEndian (*data)); }
  194. inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
  195. inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
  196. inline int32 getAsInt32LE() const noexcept { return (int32) ByteOrder::swapIfBigEndian (*data) << 8; }
  197. inline int32 getAsInt32BE() const noexcept { return (int32) ByteOrder::swapIfLittleEndian (*data) << 8; }
  198. inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) newValue >> 8); }
  199. inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) newValue >> 8); }
  200. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
  201. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
  202. inline void copyFromSameType (Int24in32& source) noexcept { *data = *source.data; }
  203. enum { bytesPerSample = 4, maxValue = 0x7fffff, resolution = (1 << 8), isFloat = 0 };
  204. };
  205. class Float32
  206. {
  207. public:
  208. inline Float32 (void* d) noexcept : data (static_cast<float*> (d)) {}
  209. inline void advance() noexcept { ++data; }
  210. inline void skip (int numSamples) noexcept { data += numSamples; }
  211. #if JUCE_BIG_ENDIAN
  212. inline float getAsFloatBE() const noexcept { return *data; }
  213. inline void setAsFloatBE (float newValue) noexcept { *data = newValue; }
  214. inline float getAsFloatLE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
  215. inline void setAsFloatLE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
  216. #else
  217. inline float getAsFloatLE() const noexcept { return *data; }
  218. inline void setAsFloatLE (float newValue) noexcept { *data = newValue; }
  219. inline float getAsFloatBE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
  220. inline void setAsFloatBE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
  221. #endif
  222. inline int32 getAsInt32LE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatLE()) * (double) maxValue); }
  223. inline int32 getAsInt32BE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatBE()) * (double) maxValue); }
  224. inline void setAsInt32LE (int32 newValue) noexcept { setAsFloatLE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
  225. inline void setAsInt32BE (int32 newValue) noexcept { setAsFloatBE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
  226. inline void clear() noexcept { *data = 0; }
  227. inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
  228. template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsFloatLE (source.getAsFloat()); }
  229. template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsFloatBE (source.getAsFloat()); }
  230. inline void copyFromSameType (Float32& source) noexcept { *data = *source.data; }
  231. float* data;
  232. enum { bytesPerSample = 4, maxValue = 0x7fffffff, resolution = (1 << 8), isFloat = 1 };
  233. };
  234. //==============================================================================
  235. class NonInterleaved
  236. {
  237. public:
  238. inline NonInterleaved() noexcept {}
  239. inline NonInterleaved (const NonInterleaved&) noexcept {}
  240. inline NonInterleaved (const int) noexcept {}
  241. inline void copyFrom (const NonInterleaved&) noexcept {}
  242. template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.advance(); }
  243. template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numSamples); }
  244. template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { s.clearMultiple (numSamples); }
  245. template <class SampleFormatType> inline static int getNumBytesBetweenSamples (const SampleFormatType&) noexcept { return SampleFormatType::bytesPerSample; }
  246. enum { isInterleavedType = 0, numInterleavedChannels = 1 };
  247. };
  248. class Interleaved
  249. {
  250. public:
  251. inline Interleaved() noexcept : numInterleavedChannels (1) {}
  252. inline Interleaved (const Interleaved& other) noexcept : numInterleavedChannels (other.numInterleavedChannels) {}
  253. inline Interleaved (const int numInterleavedChans) noexcept : numInterleavedChannels (numInterleavedChans) {}
  254. inline void copyFrom (const Interleaved& other) noexcept { numInterleavedChannels = other.numInterleavedChannels; }
  255. template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.skip (numInterleavedChannels); }
  256. template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numInterleavedChannels * numSamples); }
  257. template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { while (--numSamples >= 0) { s.clear(); s.skip (numInterleavedChannels); } }
  258. template <class SampleFormatType> inline int getNumBytesBetweenSamples (const SampleFormatType&) const noexcept { return numInterleavedChannels * SampleFormatType::bytesPerSample; }
  259. int numInterleavedChannels;
  260. enum { isInterleavedType = 1 };
  261. };
  262. //==============================================================================
  263. class NonConst
  264. {
  265. public:
  266. typedef void VoidType;
  267. static inline void* toVoidPtr (VoidType* v) noexcept { return v; }
  268. enum { isConst = 0 };
  269. };
  270. class Const
  271. {
  272. public:
  273. typedef const void VoidType;
  274. static inline void* toVoidPtr (VoidType* v) noexcept { return const_cast<void*> (v); }
  275. enum { isConst = 1 };
  276. };
  277. #endif
  278. //==============================================================================
  279. /**
  280. A pointer to a block of audio data with a particular encoding.
  281. This object can be used to read and write from blocks of encoded audio samples. To create one, you specify
  282. the audio format as a series of template parameters, e.g.
  283. @code
  284. // this creates a pointer for reading from a const array of 16-bit little-endian packed samples.
  285. AudioData::Pointer <AudioData::Int16,
  286. AudioData::LittleEndian,
  287. AudioData::NonInterleaved,
  288. AudioData::Const> pointer (someRawAudioData);
  289. // These methods read the sample that is being pointed to
  290. float firstSampleAsFloat = pointer.getAsFloat();
  291. int32 firstSampleAsInt = pointer.getAsInt32();
  292. ++pointer; // moves the pointer to the next sample.
  293. pointer += 3; // skips the next 3 samples.
  294. @endcode
  295. The convertSamples() method lets you copy a range of samples from one format to another, automatically
  296. converting its format.
  297. @see AudioData::Converter
  298. */
  299. template <typename SampleFormat,
  300. typename Endianness,
  301. typename InterleavingType,
  302. typename Constness>
  303. class Pointer : private InterleavingType // (inherited for EBCO)
  304. {
  305. public:
  306. //==============================================================================
  307. /** Creates a non-interleaved pointer from some raw data in the appropriate format.
  308. This constructor is only used if you've specified the AudioData::NonInterleaved option -
  309. for interleaved formats, use the constructor that also takes a number of channels.
  310. */
  311. Pointer (typename Constness::VoidType* sourceData) noexcept
  312. : data (Constness::toVoidPtr (sourceData))
  313. {
  314. // If you're using interleaved data, call the other constructor! If you're using non-interleaved data,
  315. // you should pass NonInterleaved as the template parameter for the interleaving type!
  316. static_jassert (InterleavingType::isInterleavedType == 0);
  317. }
  318. /** Creates a pointer from some raw data in the appropriate format with the specified number of interleaved channels.
  319. For non-interleaved data, use the other constructor.
  320. */
  321. Pointer (typename Constness::VoidType* sourceData, int numInterleaved) noexcept
  322. : InterleavingType (numInterleaved), data (Constness::toVoidPtr (sourceData))
  323. {
  324. }
  325. /** Creates a copy of another pointer. */
  326. Pointer (const Pointer& other) noexcept
  327. : InterleavingType (other), data (other.data)
  328. {
  329. }
  330. Pointer& operator= (const Pointer& other) noexcept
  331. {
  332. InterleavingType::operator= (other);
  333. data = other.data;
  334. return *this;
  335. }
  336. //==============================================================================
  337. /** Returns the value of the first sample as a floating point value.
  338. The value will be in the range -1.0 to 1.0 for integer formats. For floating point
  339. formats, the value could be outside that range, although -1 to 1 is the standard range.
  340. */
  341. inline float getAsFloat() const noexcept { return Endianness::getAsFloat (data); }
  342. /** Sets the value of the first sample as a floating point value.
  343. (This method can only be used if the AudioData::NonConst option was used).
  344. The value should be in the range -1.0 to 1.0 - for integer formats, values outside that
  345. range will be clipped. For floating point formats, any value passed in here will be
  346. written directly, although -1 to 1 is the standard range.
  347. */
  348. inline void setAsFloat (float newValue) noexcept
  349. {
  350. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  351. Endianness::setAsFloat (data, newValue);
  352. }
  353. /** Returns the value of the first sample as a 32-bit integer.
  354. The value returned will be in the range 0x80000000 to 0x7fffffff, and shorter values will be
  355. shifted to fill this range (e.g. if you're reading from 24-bit data, the values will be shifted up
  356. by 8 bits when returned here). If the source data is floating point, values beyond -1.0 to 1.0 will
  357. be clipped so that -1.0 maps onto -0x7fffffff and 1.0 maps to 0x7fffffff.
  358. */
  359. inline int32 getAsInt32() const noexcept { return Endianness::getAsInt32 (data); }
  360. /** Sets the value of the first sample as a 32-bit integer.
  361. This will be mapped to the range of the format that is being written - see getAsInt32().
  362. */
  363. inline void setAsInt32 (int32 newValue) noexcept
  364. {
  365. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  366. Endianness::setAsInt32 (data, newValue);
  367. }
  368. /** Moves the pointer along to the next sample. */
  369. inline Pointer& operator++() noexcept { advance(); return *this; }
  370. /** Moves the pointer back to the previous sample. */
  371. inline Pointer& operator--() noexcept { this->advanceDataBy (data, -1); return *this; }
  372. /** Adds a number of samples to the pointer's position. */
  373. Pointer& operator+= (int samplesToJump) noexcept { this->advanceDataBy (data, samplesToJump); return *this; }
  374. /** Writes a stream of samples into this pointer from another pointer.
  375. This will copy the specified number of samples, converting between formats appropriately.
  376. */
  377. void convertSamples (Pointer source, int numSamples) const noexcept
  378. {
  379. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  380. for (Pointer dest (*this); --numSamples >= 0;)
  381. {
  382. dest.data.copyFromSameType (source.data);
  383. dest.advance();
  384. source.advance();
  385. }
  386. }
  387. /** Writes a stream of samples into this pointer from another pointer.
  388. This will copy the specified number of samples, converting between formats appropriately.
  389. */
  390. template <class OtherPointerType>
  391. void convertSamples (OtherPointerType source, int numSamples) const noexcept
  392. {
  393. static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
  394. Pointer dest (*this);
  395. if (source.getRawData() != getRawData() || source.getNumBytesBetweenSamples() >= getNumBytesBetweenSamples())
  396. {
  397. while (--numSamples >= 0)
  398. {
  399. Endianness::copyFrom (dest.data, source);
  400. dest.advance();
  401. ++source;
  402. }
  403. }
  404. else // copy backwards if we're increasing the sample width..
  405. {
  406. dest += numSamples;
  407. source += numSamples;
  408. while (--numSamples >= 0)
  409. Endianness::copyFrom ((--dest).data, --source);
  410. }
  411. }
  412. /** Sets a number of samples to zero. */
  413. void clearSamples (int numSamples) const noexcept
  414. {
  415. Pointer dest (*this);
  416. dest.clear (dest.data, numSamples);
  417. }
  418. /** Scans a block of data, returning the lowest and highest levels as floats */
  419. Range<float> findMinAndMax (size_t numSamples) const noexcept
  420. {
  421. if (numSamples == 0)
  422. return Range<float>();
  423. Pointer dest (*this);
  424. if (isFloatingPoint())
  425. {
  426. float mn = dest.getAsFloat();
  427. dest.advance();
  428. float mx = mn;
  429. while (--numSamples > 0)
  430. {
  431. const float v = dest.getAsFloat();
  432. dest.advance();
  433. if (mx < v) mx = v;
  434. if (v < mn) mn = v;
  435. }
  436. return Range<float> (mn, mx);
  437. }
  438. int32 mn = dest.getAsInt32();
  439. dest.advance();
  440. int32 mx = mn;
  441. while (--numSamples > 0)
  442. {
  443. const int v = dest.getAsInt32();
  444. dest.advance();
  445. if (mx < v) mx = v;
  446. if (v < mn) mn = v;
  447. }
  448. return Range<float> (mn * (float) (1.0 / (1.0 + Int32::maxValue)),
  449. mx * (float) (1.0 / (1.0 + Int32::maxValue)));
  450. }
  451. /** Scans a block of data, returning the lowest and highest levels as floats */
  452. void findMinAndMax (size_t numSamples, float& minValue, float& maxValue) const noexcept
  453. {
  454. Range<float> r (findMinAndMax (numSamples));
  455. minValue = r.getStart();
  456. maxValue = r.getEnd();
  457. }
  458. /** Returns true if the pointer is using a floating-point format. */
  459. static bool isFloatingPoint() noexcept { return (bool) SampleFormat::isFloat; }
  460. /** Returns true if the format is big-endian. */
  461. static bool isBigEndian() noexcept { return (bool) Endianness::isBigEndian; }
  462. /** Returns the number of bytes in each sample (ignoring the number of interleaved channels). */
  463. static int getBytesPerSample() noexcept { return (int) SampleFormat::bytesPerSample; }
  464. /** Returns the number of interleaved channels in the format. */
  465. int getNumInterleavedChannels() const noexcept { return (int) this->numInterleavedChannels; }
  466. /** Returns the number of bytes between the start address of each sample. */
  467. int getNumBytesBetweenSamples() const noexcept { return InterleavingType::getNumBytesBetweenSamples (data); }
  468. /** Returns the accuracy of this format when represented as a 32-bit integer.
  469. This is the smallest number above 0 that can be represented in the sample format, converted to
  470. a 32-bit range. E,g. if the format is 8-bit, its resolution is 0x01000000; if the format is 24-bit,
  471. its resolution is 0x100.
  472. */
  473. static int get32BitResolution() noexcept { return (int) SampleFormat::resolution; }
  474. /** Returns a pointer to the underlying data. */
  475. const void* getRawData() const noexcept { return data.data; }
  476. private:
  477. //==============================================================================
  478. SampleFormat data;
  479. inline void advance() noexcept { this->advanceData (data); }
  480. Pointer operator++ (int); // private to force you to use the more efficient pre-increment!
  481. Pointer operator-- (int);
  482. };
  483. //==============================================================================
  484. /** A base class for objects that are used to convert between two different sample formats.
  485. The AudioData::ConverterInstance implements this base class and can be templated, so
  486. you can create an instance that converts between two particular formats, and then
  487. store this in the abstract base class.
  488. @see AudioData::ConverterInstance
  489. */
  490. class Converter
  491. {
  492. public:
  493. virtual ~Converter() {}
  494. /** Converts a sequence of samples from the converter's source format into the dest format. */
  495. virtual void convertSamples (void* destSamples, const void* sourceSamples, int numSamples) const = 0;
  496. /** Converts a sequence of samples from the converter's source format into the dest format.
  497. This method takes sub-channel indexes, which can be used with interleaved formats in order to choose a
  498. particular sub-channel of the data to be used.
  499. */
  500. virtual void convertSamples (void* destSamples, int destSubChannel,
  501. const void* sourceSamples, int sourceSubChannel, int numSamples) const = 0;
  502. };
  503. //==============================================================================
  504. /**
  505. A class that converts between two templated AudioData::Pointer types, and which
  506. implements the AudioData::Converter interface.
  507. This can be used as a concrete instance of the AudioData::Converter abstract class.
  508. @see AudioData::Converter
  509. */
  510. template <class SourceSampleType, class DestSampleType>
  511. class ConverterInstance : public Converter
  512. {
  513. public:
  514. ConverterInstance (int numSourceChannels = 1, int numDestChannels = 1)
  515. : sourceChannels (numSourceChannels), destChannels (numDestChannels)
  516. {}
  517. void convertSamples (void* dest, const void* source, int numSamples) const override
  518. {
  519. SourceSampleType s (source, sourceChannels);
  520. DestSampleType d (dest, destChannels);
  521. d.convertSamples (s, numSamples);
  522. }
  523. void convertSamples (void* dest, int destSubChannel,
  524. const void* source, int sourceSubChannel, int numSamples) const override
  525. {
  526. jassert (destSubChannel < destChannels && sourceSubChannel < sourceChannels);
  527. SourceSampleType s (addBytesToPointer (source, sourceSubChannel * SourceSampleType::getBytesPerSample()), sourceChannels);
  528. DestSampleType d (addBytesToPointer (dest, destSubChannel * DestSampleType::getBytesPerSample()), destChannels);
  529. d.convertSamples (s, numSamples);
  530. }
  531. private:
  532. JUCE_DECLARE_NON_COPYABLE (ConverterInstance)
  533. const int sourceChannels, destChannels;
  534. };
  535. };
  536. //==============================================================================
  537. /**
  538. A set of routines to convert buffers of 32-bit floating point data to and from
  539. various integer formats.
  540. Note that these functions are deprecated - the AudioData class provides a much more
  541. flexible set of conversion classes now.
  542. */
  543. class JUCE_API AudioDataConverters
  544. {
  545. public:
  546. //==============================================================================
  547. static void convertFloatToInt16LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
  548. static void convertFloatToInt16BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
  549. static void convertFloatToInt24LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
  550. static void convertFloatToInt24BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
  551. static void convertFloatToInt32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  552. static void convertFloatToInt32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  553. static void convertFloatToFloat32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  554. static void convertFloatToFloat32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
  555. //==============================================================================
  556. static void convertInt16LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
  557. static void convertInt16BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
  558. static void convertInt24LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
  559. static void convertInt24BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
  560. static void convertInt32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  561. static void convertInt32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  562. static void convertFloat32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  563. static void convertFloat32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
  564. //==============================================================================
  565. enum DataFormat
  566. {
  567. int16LE,
  568. int16BE,
  569. int24LE,
  570. int24BE,
  571. int32LE,
  572. int32BE,
  573. float32LE,
  574. float32BE,
  575. };
  576. static void convertFloatToFormat (DataFormat destFormat,
  577. const float* source, void* dest, int numSamples);
  578. static void convertFormatToFloat (DataFormat sourceFormat,
  579. const void* source, float* dest, int numSamples);
  580. //==============================================================================
  581. static void interleaveSamples (const float** source, float* dest,
  582. int numSamples, int numChannels);
  583. static void deinterleaveSamples (const float* source, float** dest,
  584. int numSamples, int numChannels);
  585. private:
  586. AudioDataConverters();
  587. JUCE_DECLARE_NON_COPYABLE (AudioDataConverters)
  588. };
  589. #endif // JUCE_AUDIODATACONVERTERS_H_INCLUDED