openshot-audio  0.1.6
juce_AudioDataConverters.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_AUDIODATACONVERTERS_H_INCLUDED
26 #define JUCE_AUDIODATACONVERTERS_H_INCLUDED
27 
28 
29 //==============================================================================
37 {
38 public:
39  //==============================================================================
40  // These types can be used as the SampleFormat template parameter for the AudioData::Pointer class.
41 
42  class Int8;
43  class UInt8;
44  class Int16;
45  class Int24;
46  class Int32;
47  class Float32;
49  //==============================================================================
50  // These types can be used as the Endianness template parameter for the AudioData::Pointer class.
51 
52  class BigEndian;
53  class LittleEndian;
54  class NativeEndian;
56  //==============================================================================
57  // These types can be used as the InterleavingType template parameter for the AudioData::Pointer class.
58 
59  class NonInterleaved;
60  class Interleaved;
62  //==============================================================================
63  // These types can be used as the Constness template parameter for the AudioData::Pointer class.
64 
65  class NonConst;
66  class Const;
68  #ifndef DOXYGEN
69  //==============================================================================
70  class BigEndian
71  {
72  public:
73  template <class SampleFormatType> static inline float getAsFloat (SampleFormatType& s) noexcept { return s.getAsFloatBE(); }
74  template <class SampleFormatType> static inline void setAsFloat (SampleFormatType& s, float newValue) noexcept { s.setAsFloatBE (newValue); }
75  template <class SampleFormatType> static inline int32 getAsInt32 (SampleFormatType& s) noexcept { return s.getAsInt32BE(); }
76  template <class SampleFormatType> static inline void setAsInt32 (SampleFormatType& s, int32 newValue) noexcept { s.setAsInt32BE (newValue); }
77  template <class SourceType, class DestType> static inline void copyFrom (DestType& dest, SourceType& source) noexcept { dest.copyFromBE (source); }
78  enum { isBigEndian = 1 };
79  };
80 
82  {
83  public:
84  template <class SampleFormatType> static inline float getAsFloat (SampleFormatType& s) noexcept { return s.getAsFloatLE(); }
85  template <class SampleFormatType> static inline void setAsFloat (SampleFormatType& s, float newValue) noexcept { s.setAsFloatLE (newValue); }
86  template <class SampleFormatType> static inline int32 getAsInt32 (SampleFormatType& s) noexcept { return s.getAsInt32LE(); }
87  template <class SampleFormatType> static inline void setAsInt32 (SampleFormatType& s, int32 newValue) noexcept { s.setAsInt32LE (newValue); }
88  template <class SourceType, class DestType> static inline void copyFrom (DestType& dest, SourceType& source) noexcept { dest.copyFromLE (source); }
89  enum { isBigEndian = 0 };
90  };
91 
92  #if JUCE_BIG_ENDIAN
93  class NativeEndian : public BigEndian {};
94  #else
95  class NativeEndian : public LittleEndian {};
96  #endif
97 
98  //==============================================================================
99  class Int8
100  {
101  public:
102  inline Int8 (void* d) noexcept : data (static_cast <int8*> (d)) {}
103 
104  inline void advance() noexcept { ++data; }
105  inline void skip (int numSamples) noexcept { data += numSamples; }
106  inline float getAsFloatLE() const noexcept { return (float) (*data * (1.0 / (1.0 + maxValue))); }
107  inline float getAsFloatBE() const noexcept { return getAsFloatLE(); }
108  inline void setAsFloatLE (float newValue) noexcept { *data = (int8) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))); }
109  inline void setAsFloatBE (float newValue) noexcept { setAsFloatLE (newValue); }
110  inline int32 getAsInt32LE() const noexcept { return (int) (*data << 24); }
111  inline int32 getAsInt32BE() const noexcept { return getAsInt32LE(); }
112  inline void setAsInt32LE (int newValue) noexcept { *data = (int8) (newValue >> 24); }
113  inline void setAsInt32BE (int newValue) noexcept { setAsInt32LE (newValue); }
114  inline void clear() noexcept { *data = 0; }
115  inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
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 (Int8& source) noexcept { *data = *source.data; }
119 
121  enum { bytesPerSample = 1, maxValue = 0x7f, resolution = (1 << 24), isFloat = 0 };
122  };
123 
124  class UInt8
125  {
126  public:
127  inline UInt8 (void* d) noexcept : data (static_cast <uint8*> (d)) {}
128 
129  inline void advance() noexcept { ++data; }
130  inline void skip (int numSamples) noexcept { data += numSamples; }
131  inline float getAsFloatLE() const noexcept { return (float) ((*data - 128) * (1.0 / (1.0 + maxValue))); }
132  inline float getAsFloatBE() const noexcept { return getAsFloatLE(); }
133  inline void setAsFloatLE (float newValue) noexcept { *data = (uint8) jlimit (0, 255, 128 + roundToInt (newValue * (1.0 + maxValue))); }
134  inline void setAsFloatBE (float newValue) noexcept { setAsFloatLE (newValue); }
135  inline int32 getAsInt32LE() const noexcept { return (int) ((*data - 128) << 24); }
136  inline int32 getAsInt32BE() const noexcept { return getAsInt32LE(); }
137  inline void setAsInt32LE (int newValue) noexcept { *data = (uint8) (128 + (newValue >> 24)); }
138  inline void setAsInt32BE (int newValue) noexcept { setAsInt32LE (newValue); }
139  inline void clear() noexcept { *data = 128; }
140  inline void clearMultiple (int num) noexcept { memset (data, 128, (size_t) num) ;}
141  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
142  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
143  inline void copyFromSameType (UInt8& source) noexcept { *data = *source.data; }
144 
146  enum { bytesPerSample = 1, maxValue = 0x7f, resolution = (1 << 24), isFloat = 0 };
147  };
148 
149  class Int16
150  {
151  public:
152  inline Int16 (void* d) noexcept : data (static_cast <uint16*> (d)) {}
153 
154  inline void advance() noexcept { ++data; }
155  inline void skip (int numSamples) noexcept { data += numSamples; }
156  inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int16) ByteOrder::swapIfBigEndian (*data)); }
157  inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int16) ByteOrder::swapIfLittleEndian (*data)); }
158  inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint16) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue)))); }
159  inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint16) jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue)))); }
162  inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint16) (newValue >> 16)); }
163  inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint16) (newValue >> 16)); }
164  inline void clear() noexcept { *data = 0; }
165  inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
166  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
167  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
168  inline void copyFromSameType (Int16& source) noexcept { *data = *source.data; }
169 
171  enum { bytesPerSample = 2, maxValue = 0x7fff, resolution = (1 << 16), isFloat = 0 };
172  };
173 
174  class Int24
175  {
176  public:
177  inline Int24 (void* d) noexcept : data (static_cast <char*> (d)) {}
178 
179  inline void advance() noexcept { data += 3; }
180  inline void skip (int numSamples) noexcept { data += 3 * numSamples; }
181  inline float getAsFloatLE() const noexcept { return (float) (ByteOrder::littleEndian24Bit (data) * (1.0 / (1.0 + maxValue))); }
182  inline float getAsFloatBE() const noexcept { return (float) (ByteOrder::bigEndian24Bit (data) * (1.0 / (1.0 + maxValue))); }
183  inline void setAsFloatLE (float newValue) noexcept { ByteOrder::littleEndian24BitToChars (jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))), data); }
184  inline void setAsFloatBE (float newValue) noexcept { ByteOrder::bigEndian24BitToChars (jlimit ((int) -maxValue, (int) maxValue, roundToInt (newValue * (1.0 + maxValue))), data); }
187  inline void setAsInt32LE (int32 newValue) noexcept { ByteOrder::littleEndian24BitToChars (newValue >> 8, data); }
188  inline void setAsInt32BE (int32 newValue) noexcept { ByteOrder::bigEndian24BitToChars (newValue >> 8, data); }
189  inline void clear() noexcept { data[0] = 0; data[1] = 0; data[2] = 0; }
190  inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
191  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
192  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
193  inline void copyFromSameType (Int24& source) noexcept { data[0] = source.data[0]; data[1] = source.data[1]; data[2] = source.data[2]; }
194 
195  char* data;
196  enum { bytesPerSample = 3, maxValue = 0x7fffff, resolution = (1 << 8), isFloat = 0 };
197  };
198 
199  class Int32
200  {
201  public:
202  inline Int32 (void* d) noexcept : data (static_cast <uint32*> (d)) {}
203 
204  inline void advance() noexcept { ++data; }
205  inline void skip (int numSamples) noexcept { data += numSamples; }
206  inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfBigEndian (*data)); }
207  inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfLittleEndian (*data)); }
208  inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
209  inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
212  inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) newValue); }
213  inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) newValue); }
214  inline void clear() noexcept { *data = 0; }
215  inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
216  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
217  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
218  inline void copyFromSameType (Int32& source) noexcept { *data = *source.data; }
219 
221  enum { bytesPerSample = 4, maxValue = 0x7fffffff, resolution = 1, isFloat = 0 };
222  };
223 
225  class Int24in32 : public Int32
226  {
227  public:
228  inline Int24in32 (void* d) noexcept : Int32 (d) {}
229 
230  inline float getAsFloatLE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfBigEndian (*data)); }
231  inline float getAsFloatBE() const noexcept { return (float) ((1.0 / (1.0 + maxValue)) * (int32) ByteOrder::swapIfLittleEndian (*data)); }
232  inline void setAsFloatLE (float newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
233  inline void setAsFloatBE (float newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) (maxValue * jlimit (-1.0, 1.0, (double) newValue))); }
236  inline void setAsInt32LE (int32 newValue) noexcept { *data = ByteOrder::swapIfBigEndian ((uint32) newValue >> 8); }
237  inline void setAsInt32BE (int32 newValue) noexcept { *data = ByteOrder::swapIfLittleEndian ((uint32) newValue >> 8); }
238  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsInt32LE (source.getAsInt32()); }
239  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsInt32BE (source.getAsInt32()); }
240  inline void copyFromSameType (Int24in32& source) noexcept { *data = *source.data; }
241 
242  enum { bytesPerSample = 4, maxValue = 0x7fffff, resolution = (1 << 8), isFloat = 0 };
243  };
244 
245  class Float32
246  {
247  public:
248  inline Float32 (void* d) noexcept : data (static_cast <float*> (d)) {}
249 
250  inline void advance() noexcept { ++data; }
251  inline void skip (int numSamples) noexcept { data += numSamples; }
252  #if JUCE_BIG_ENDIAN
253  inline float getAsFloatBE() const noexcept { return *data; }
254  inline void setAsFloatBE (float newValue) noexcept { *data = newValue; }
255  inline float getAsFloatLE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
256  inline void setAsFloatLE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
257  #else
258  inline float getAsFloatLE() const noexcept { return *data; }
259  inline void setAsFloatLE (float newValue) noexcept { *data = newValue; }
260  inline float getAsFloatBE() const noexcept { union { uint32 asInt; float asFloat; } n; n.asInt = ByteOrder::swap (*(uint32*) data); return n.asFloat; }
261  inline void setAsFloatBE (float newValue) noexcept { union { uint32 asInt; float asFloat; } n; n.asFloat = newValue; *(uint32*) data = ByteOrder::swap (n.asInt); }
262  #endif
263  inline int32 getAsInt32LE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatLE()) * (double) maxValue); }
264  inline int32 getAsInt32BE() const noexcept { return (int32) roundToInt (jlimit (-1.0, 1.0, (double) getAsFloatBE()) * (double) maxValue); }
265  inline void setAsInt32LE (int32 newValue) noexcept { setAsFloatLE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
266  inline void setAsInt32BE (int32 newValue) noexcept { setAsFloatBE ((float) (newValue * (1.0 / (1.0 + maxValue)))); }
267  inline void clear() noexcept { *data = 0; }
268  inline void clearMultiple (int num) noexcept { zeromem (data, (size_t) (num * bytesPerSample)) ;}
269  template <class SourceType> inline void copyFromLE (SourceType& source) noexcept { setAsFloatLE (source.getAsFloat()); }
270  template <class SourceType> inline void copyFromBE (SourceType& source) noexcept { setAsFloatBE (source.getAsFloat()); }
271  inline void copyFromSameType (Float32& source) noexcept { *data = *source.data; }
272 
273  float* data;
274  enum { bytesPerSample = 4, maxValue = 0x7fffffff, resolution = (1 << 8), isFloat = 1 };
275  };
276 
277  //==============================================================================
279  {
280  public:
283  inline NonInterleaved (const int) noexcept {}
284  inline void copyFrom (const NonInterleaved&) noexcept {}
285  template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.advance(); }
286  template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numSamples); }
287  template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { s.clearMultiple (numSamples); }
288  template <class SampleFormatType> inline static int getNumBytesBetweenSamples (const SampleFormatType&) noexcept { return SampleFormatType::bytesPerSample; }
289 
290  enum { isInterleavedType = 0, numInterleavedChannels = 1 };
291  };
292 
294  {
295  public:
296  inline Interleaved() noexcept : numInterleavedChannels (1) {}
297  inline Interleaved (const Interleaved& other) noexcept : numInterleavedChannels (other.numInterleavedChannels) {}
298  inline Interleaved (const int numInterleavedChans) noexcept : numInterleavedChannels (numInterleavedChans) {}
299  inline void copyFrom (const Interleaved& other) noexcept { numInterleavedChannels = other.numInterleavedChannels; }
300  template <class SampleFormatType> inline void advanceData (SampleFormatType& s) noexcept { s.skip (numInterleavedChannels); }
301  template <class SampleFormatType> inline void advanceDataBy (SampleFormatType& s, int numSamples) noexcept { s.skip (numInterleavedChannels * numSamples); }
302  template <class SampleFormatType> inline void clear (SampleFormatType& s, int numSamples) noexcept { while (--numSamples >= 0) { s.clear(); s.skip (numInterleavedChannels); } }
303  template <class SampleFormatType> inline int getNumBytesBetweenSamples (const SampleFormatType&) const noexcept { return numInterleavedChannels * SampleFormatType::bytesPerSample; }
305  enum { isInterleavedType = 1 };
306  };
307 
308  //==============================================================================
309  class NonConst
310  {
311  public:
312  typedef void VoidType;
313  static inline void* toVoidPtr (VoidType* v) noexcept { return v; }
314  enum { isConst = 0 };
315  };
316 
317  class Const
318  {
319  public:
320  typedef const void VoidType;
321  static inline void* toVoidPtr (VoidType* v) noexcept { return const_cast <void*> (v); }
322  enum { isConst = 1 };
323  };
324  #endif
325 
326  //==============================================================================
351  template <typename SampleFormat,
352  typename Endianness,
353  typename InterleavingType,
354  typename Constness>
355  class Pointer : private InterleavingType // (inherited for EBCO)
356  {
357  public:
358  //==============================================================================
363  Pointer (typename Constness::VoidType* sourceData) noexcept
364  : data (Constness::toVoidPtr (sourceData))
365  {
366  // If you're using interleaved data, call the other constructor! If you're using non-interleaved data,
367  // you should pass NonInterleaved as the template parameter for the interleaving type!
368  static_jassert (InterleavingType::isInterleavedType == 0);
369  }
370 
374  Pointer (typename Constness::VoidType* sourceData, int numInterleaved) noexcept
375  : InterleavingType (numInterleaved), data (Constness::toVoidPtr (sourceData))
376  {
377  }
378 
380  Pointer (const Pointer& other) noexcept
381  : InterleavingType (other), data (other.data)
382  {
383  }
384 
385  Pointer& operator= (const Pointer& other) noexcept
386  {
387  InterleavingType::operator= (other);
388  data = other.data;
389  return *this;
390  }
391 
392  //==============================================================================
397  inline float getAsFloat() const noexcept { return Endianness::getAsFloat (data); }
398 
406  inline void setAsFloat (float newValue) noexcept
407  {
408  static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
409  Endianness::setAsFloat (data, newValue);
410  }
411 
418  inline int32 getAsInt32() const noexcept { return Endianness::getAsInt32 (data); }
419 
423  inline void setAsInt32 (int32 newValue) noexcept
424  {
425  static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
426  Endianness::setAsInt32 (data, newValue);
427  }
428 
430  inline Pointer& operator++() noexcept { advance(); return *this; }
431 
433  inline Pointer& operator--() noexcept { this->advanceDataBy (data, -1); return *this; }
434 
436  Pointer& operator+= (int samplesToJump) noexcept { this->advanceDataBy (data, samplesToJump); return *this; }
437 
441  void convertSamples (Pointer source, int numSamples) const noexcept
442  {
443  static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
444 
445  for (Pointer dest (*this); --numSamples >= 0;)
446  {
447  dest.data.copyFromSameType (source.data);
448  dest.advance();
449  source.advance();
450  }
451  }
452 
456  template <class OtherPointerType>
457  void convertSamples (OtherPointerType source, int numSamples) const noexcept
458  {
459  static_jassert (Constness::isConst == 0); // trying to write to a const pointer! For a writeable one, use AudioData::NonConst instead!
460 
461  Pointer dest (*this);
462 
463  if (source.getRawData() != getRawData() || source.getNumBytesBetweenSamples() >= getNumBytesBetweenSamples())
464  {
465  while (--numSamples >= 0)
466  {
467  Endianness::copyFrom (dest.data, source);
468  dest.advance();
469  ++source;
470  }
471  }
472  else // copy backwards if we're increasing the sample width..
473  {
474  dest += numSamples;
475  source += numSamples;
476 
477  while (--numSamples >= 0)
478  Endianness::copyFrom ((--dest).data, --source);
479  }
480  }
481 
483  void clearSamples (int numSamples) const noexcept
484  {
485  Pointer dest (*this);
486  dest.clear (dest.data, numSamples);
487  }
488 
490  Range<float> findMinAndMax (size_t numSamples) const noexcept
491  {
492  if (numSamples == 0)
493  return Range<float>();
494 
495  Pointer dest (*this);
496 
497  if (isFloatingPoint())
498  {
499  float mn = dest.getAsFloat();
500  dest.advance();
501  float mx = mn;
502 
503  while (--numSamples > 0)
504  {
505  const float v = dest.getAsFloat();
506  dest.advance();
507 
508  if (mx < v) mx = v;
509  if (v < mn) mn = v;
510  }
511 
512  return Range<float> (mn, mx);
513  }
514 
515  int32 mn = dest.getAsInt32();
516  dest.advance();
517  int32 mx = mn;
518 
519  while (--numSamples > 0)
520  {
521  const int v = dest.getAsInt32();
522  dest.advance();
523 
524  if (mx < v) mx = v;
525  if (v < mn) mn = v;
526  }
527 
528  return Range<float> (mn * (float) (1.0 / (1.0 + Int32::maxValue)),
529  mx * (float) (1.0 / (1.0 + Int32::maxValue)));
530  }
531 
533  void findMinAndMax (size_t numSamples, float& minValue, float& maxValue) const noexcept
534  {
535  Range<float> r (findMinAndMax (numSamples));
536  minValue = r.getStart();
537  maxValue = r.getEnd();
538  }
539 
541  static bool isFloatingPoint() noexcept { return (bool) SampleFormat::isFloat; }
542 
544  static bool isBigEndian() noexcept { return (bool) Endianness::isBigEndian; }
545 
547  static int getBytesPerSample() noexcept { return (int) SampleFormat::bytesPerSample; }
548 
550  int getNumInterleavedChannels() const noexcept { return (int) this->numInterleavedChannels; }
551 
553  int getNumBytesBetweenSamples() const noexcept { return InterleavingType::getNumBytesBetweenSamples (data); }
554 
560  static int get32BitResolution() noexcept { return (int) SampleFormat::resolution; }
561 
563  const void* getRawData() const noexcept { return data.data; }
564 
565  private:
566  //==============================================================================
567  SampleFormat data;
568 
569  inline void advance() noexcept { this->advanceData (data); }
570 
571  Pointer operator++ (int); // private to force you to use the more efficient pre-increment!
572  Pointer operator-- (int);
573  };
574 
575  //==============================================================================
584  class Converter
585  {
586  public:
587  virtual ~Converter() {}
588 
590  virtual void convertSamples (void* destSamples, const void* sourceSamples, int numSamples) const = 0;
591 
596  virtual void convertSamples (void* destSamples, int destSubChannel,
597  const void* sourceSamples, int sourceSubChannel, int numSamples) const = 0;
598  };
599 
600  //==============================================================================
609  template <class SourceSampleType, class DestSampleType>
611  {
612  public:
613  ConverterInstance (int numSourceChannels = 1, int numDestChannels = 1)
614  : sourceChannels (numSourceChannels), destChannels (numDestChannels)
615  {}
616 
617  void convertSamples (void* dest, const void* source, int numSamples) const override
618  {
619  SourceSampleType s (source, sourceChannels);
620  DestSampleType d (dest, destChannels);
621  d.convertSamples (s, numSamples);
622  }
623 
624  void convertSamples (void* dest, int destSubChannel,
625  const void* source, int sourceSubChannel, int numSamples) const override
626  {
627  jassert (destSubChannel < destChannels && sourceSubChannel < sourceChannels);
628 
629  SourceSampleType s (addBytesToPointer (source, sourceSubChannel * SourceSampleType::getBytesPerSample()), sourceChannels);
630  DestSampleType d (addBytesToPointer (dest, destSubChannel * DestSampleType::getBytesPerSample()), destChannels);
631  d.convertSamples (s, numSamples);
632  }
633 
634  private:
636 
637  const int sourceChannels, destChannels;
638  };
639 };
640 
641 
642 
643 //==============================================================================
652 {
653 public:
654  //==============================================================================
655  static void convertFloatToInt16LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
656  static void convertFloatToInt16BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 2);
657 
658  static void convertFloatToInt24LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
659  static void convertFloatToInt24BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 3);
660 
661  static void convertFloatToInt32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
662  static void convertFloatToInt32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
663 
664  static void convertFloatToFloat32LE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
665  static void convertFloatToFloat32BE (const float* source, void* dest, int numSamples, int destBytesPerSample = 4);
666 
667  //==============================================================================
668  static void convertInt16LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
669  static void convertInt16BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 2);
670 
671  static void convertInt24LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
672  static void convertInt24BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 3);
673 
674  static void convertInt32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
675  static void convertInt32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
676 
677  static void convertFloat32LEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
678  static void convertFloat32BEToFloat (const void* source, float* dest, int numSamples, int srcBytesPerSample = 4);
679 
680  //==============================================================================
682  {
691  };
692 
693  static void convertFloatToFormat (DataFormat destFormat,
694  const float* source, void* dest, int numSamples);
695 
696  static void convertFormatToFloat (DataFormat sourceFormat,
697  const void* source, float* dest, int numSamples);
698 
699  //==============================================================================
700  static void interleaveSamples (const float** source, float* dest,
701  int numSamples, int numChannels);
702 
703  static void deinterleaveSamples (const float* source, float** dest,
704  int numSamples, int numChannels);
705 
706 private:
709 };
710 
711 
712 #endif // JUCE_AUDIODATACONVERTERS_H_INCLUDED
void setAsInt32BE(int newValue) noexcept
Definition: juce_AudioDataConverters.h:138
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:234
float getAsFloat() const noexcept
Definition: juce_AudioDataConverters.h:397
int32 getAsInt32() const noexcept
Definition: juce_AudioDataConverters.h:418
void clear() noexcept
Definition: juce_AudioDataConverters.h:139
static int bigEndian24Bit(const void *bytes) noexcept
Definition: juce_ByteOrder.h:191
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:159
int getNumBytesBetweenSamples(const SampleFormatType &) const noexcept
Definition: juce_AudioDataConverters.h:303
void setAsInt32LE(int newValue) noexcept
Definition: juce_AudioDataConverters.h:112
Float32(void *d) noexcept
Definition: juce_AudioDataConverters.h:248
void setAsInt32BE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:188
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:107
void clear() noexcept
Definition: juce_AudioDataConverters.h:189
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:231
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:184
Int8(void *d) noexcept
Definition: juce_AudioDataConverters.h:102
void advanceDataBy(SampleFormatType &s, int numSamples) noexcept
Definition: juce_AudioDataConverters.h:286
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:134
uint16 * data
Definition: juce_AudioDataConverters.h:170
uint8 * data
Definition: juce_AudioDataConverters.h:145
static int32 getAsInt32(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:75
void advance() noexcept
Definition: juce_AudioDataConverters.h:129
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:141
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:216
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:161
int getNumBytesBetweenSamples() const noexcept
Definition: juce_AudioDataConverters.h:553
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:259
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:132
#define static_jassert(expression)
Definition: juce_PlatformDefs.h:165
static int littleEndian24Bit(const void *bytes) noexcept
Definition: juce_ByteOrder.h:190
ConverterInstance(int numSourceChannels=1, int numDestChannels=1)
Definition: juce_AudioDataConverters.h:613
static void setAsInt32(SampleFormatType &s, int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:76
#define noexcept
Definition: juce_CompilerSupport.h:141
void advanceData(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:285
signed short int16
Definition: juce_MathsFunctions.h:45
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:155
void copyFromSameType(Int32 &source) noexcept
Definition: juce_AudioDataConverters.h:218
Definition: juce_AudioDataConverters.h:355
unsigned short uint16
Definition: juce_MathsFunctions.h:47
static uint16 swap(uint16 value) noexcept
Definition: juce_ByteOrder.h:117
void setAsInt32LE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:265
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:116
UInt8(void *d) noexcept
Definition: juce_AudioDataConverters.h:127
const void VoidType
Definition: juce_AudioDataConverters.h:320
void zeromem(void *memory, size_t numBytes) noexcept
Definition: juce_Memory.h:34
Pointer(typename Constness::VoidType *sourceData, int numInterleaved) noexcept
Definition: juce_AudioDataConverters.h:374
Pointer & operator--() noexcept
Definition: juce_AudioDataConverters.h:433
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:105
Pointer(typename Constness::VoidType *sourceData) noexcept
Definition: juce_AudioDataConverters.h:363
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:115
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:215
Definition: juce_Range.h:44
void setAsInt32BE(int newValue) noexcept
Definition: juce_AudioDataConverters.h:113
Definition: juce_AudioDataConverters.h:70
int roundToInt(const FloatType value) noexcept
Definition: juce_core.h:418
void copyFromSameType(Int16 &source) noexcept
Definition: juce_AudioDataConverters.h:168
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:209
Definition: juce_AudioDataConverters.h:685
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:183
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
Definition: juce_Memory.h:53
Definition: juce_AudioDataConverters.h:317
void clear() noexcept
Definition: juce_AudioDataConverters.h:214
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:268
static void copyFrom(DestType &dest, SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:88
Definition: juce_AudioDataConverters.h:293
Interleaved(const Interleaved &other) noexcept
Definition: juce_AudioDataConverters.h:297
void advance() noexcept
Definition: juce_AudioDataConverters.h:250
Definition: juce_AudioDataConverters.h:684
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:109
void copyFromSameType(Int24in32 &source) noexcept
Definition: juce_AudioDataConverters.h:240
void copyFrom(const Interleaved &other) noexcept
Definition: juce_AudioDataConverters.h:299
void setAsInt32LE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:236
void convertSamples(OtherPointerType source, int numSamples) const noexcept
Definition: juce_AudioDataConverters.h:457
void setAsInt32(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:423
const void * getRawData() const noexcept
Definition: juce_AudioDataConverters.h:563
void convertSamples(void *dest, int destSubChannel, const void *source, int sourceSubChannel, int numSamples) const override
Definition: juce_AudioDataConverters.h:624
Definition: juce_AudioDataConverters.h:225
void copyFrom(const NonInterleaved &) noexcept
Definition: juce_AudioDataConverters.h:284
Int32(void *d) noexcept
Definition: juce_AudioDataConverters.h:202
Definition: juce_AudioDataConverters.h:610
Definition: juce_AudioDataConverters.h:81
static void setAsFloat(SampleFormatType &s, float newValue) noexcept
Definition: juce_AudioDataConverters.h:74
Int24(void *d) noexcept
Definition: juce_AudioDataConverters.h:177
Definition: juce_AudioDataConverters.h:688
static int get32BitResolution() noexcept
Definition: juce_AudioDataConverters.h:560
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:135
static int getNumBytesBetweenSamples(const SampleFormatType &) noexcept
Definition: juce_AudioDataConverters.h:288
void clear(SampleFormatType &s, int numSamples) noexcept
Definition: juce_AudioDataConverters.h:287
int8 * data
Definition: juce_AudioDataConverters.h:120
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:269
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:230
#define JUCE_API
Definition: juce_StandardHeader.h:139
Definition: juce_AudioDataConverters.h:99
#define const
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:186
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:106
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:130
void copyFromSameType(Int8 &source) noexcept
Definition: juce_AudioDataConverters.h:118
static int32 getAsInt32(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:86
void copyFromSameType(Int24 &source) noexcept
Definition: juce_AudioDataConverters.h:193
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:232
static bool isFloatingPoint() noexcept
Definition: juce_AudioDataConverters.h:541
void setAsInt32BE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:163
void setAsInt32BE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:266
void findMinAndMax(const Type *values, int numValues, Type &lowest, Type &highest)
Definition: juce_MathsFunctions.h:176
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:206
void setAsInt32LE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:162
Definition: juce_AudioDataConverters.h:686
void advance() noexcept
Definition: juce_AudioDataConverters.h:104
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:157
Definition: juce_AudioDataConverters.h:687
void VoidType
Definition: juce_AudioDataConverters.h:312
void clear() noexcept
Definition: juce_AudioDataConverters.h:114
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:261
static uint16 swapIfBigEndian(uint16 value) noexcept
Definition: juce_ByteOrder.h:175
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:260
void copyFromSameType(UInt8 &source) noexcept
Definition: juce_AudioDataConverters.h:143
Definition: juce_AudioDataConverters.h:683
void clear() noexcept
Definition: juce_AudioDataConverters.h:164
unsigned int uint32
Definition: juce_MathsFunctions.h:51
Definition: juce_AudioDataConverters.h:690
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:185
void clear(SampleFormatType &s, int numSamples) noexcept
Definition: juce_AudioDataConverters.h:302
static float getAsFloat(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:84
void advanceDataBy(SampleFormatType &s, int numSamples) noexcept
Definition: juce_AudioDataConverters.h:301
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:158
Definition: juce_AudioDataConverters.h:95
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:208
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:182
Definition: juce_AudioDataConverters.h:309
void setAsFloatBE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:233
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:180
virtual ~Converter()
Definition: juce_AudioDataConverters.h:587
void convertSamples(Pointer source, int numSamples) const noexcept
Definition: juce_AudioDataConverters.h:441
NonInterleaved() noexcept
Definition: juce_AudioDataConverters.h:281
void setAsInt32BE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:213
Definition: juce_AudioDataConverters.h:149
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:192
static void * toVoidPtr(VoidType *v) noexcept
Definition: juce_AudioDataConverters.h:321
Definition: juce_AudioDataConverters.h:584
void convertSamples(void *dest, const void *source, int numSamples) const override
Definition: juce_AudioDataConverters.h:617
Definition: juce_AudioDataConverters.h:651
Definition: juce_AudioDataConverters.h:689
Definition: juce_AudioDataConverters.h:124
void advance() noexcept
Definition: juce_AudioDataConverters.h:154
void setAsInt32BE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:237
Interleaved() noexcept
Definition: juce_AudioDataConverters.h:296
Definition: juce_AudioDataConverters.h:36
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:142
void setAsFloat(float newValue) noexcept
Definition: juce_AudioDataConverters.h:406
Int16(void *d) noexcept
Definition: juce_AudioDataConverters.h:152
NonInterleaved(const int) noexcept
Definition: juce_AudioDataConverters.h:283
void advance() noexcept
Definition: juce_AudioDataConverters.h:179
float * data
Definition: juce_AudioDataConverters.h:273
void setAsInt32LE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:187
void setAsInt32LE(int newValue) noexcept
Definition: juce_AudioDataConverters.h:137
static float getAsFloat(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:73
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:117
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:133
Definition: juce_AudioDataConverters.h:174
static void * toVoidPtr(VoidType *v) noexcept
Definition: juce_AudioDataConverters.h:313
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition: juce_MathsFunctions.h:220
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:131
signed char int8
Definition: juce_MathsFunctions.h:41
void copyFromSameType(Float32 &source) noexcept
Definition: juce_AudioDataConverters.h:271
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:211
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:235
static void setAsFloat(SampleFormatType &s, float newValue) noexcept
Definition: juce_AudioDataConverters.h:85
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:110
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:217
void clear() noexcept
Definition: juce_AudioDataConverters.h:267
void advance() noexcept
Definition: juce_AudioDataConverters.h:204
#define jassert(a)
Definition: juce_PlatformDefs.h:146
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:166
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:136
NonInterleaved(const NonInterleaved &) noexcept
Definition: juce_AudioDataConverters.h:282
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
static bool isBigEndian() noexcept
Definition: juce_AudioDataConverters.h:544
Pointer(const Pointer &other) noexcept
Definition: juce_AudioDataConverters.h:380
Range< float > findMinAndMax(size_t numSamples) const noexcept
Definition: juce_AudioDataConverters.h:490
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:205
float getAsFloatBE() const noexcept
Definition: juce_AudioDataConverters.h:207
static void littleEndian24BitToChars(int value, void *destBytes) noexcept
Definition: juce_ByteOrder.h:192
static void bigEndian24BitToChars(int value, void *destBytes) noexcept
Definition: juce_ByteOrder.h:193
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:156
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:165
Definition: juce_AudioDataConverters.h:199
static uint16 swapIfLittleEndian(uint16 value) noexcept
Definition: juce_ByteOrder.h:178
DataFormat
Definition: juce_AudioDataConverters.h:681
static void setAsInt32(SampleFormatType &s, int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:87
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:258
static int getBytesPerSample() noexcept
Definition: juce_AudioDataConverters.h:547
int getNumInterleavedChannels() const noexcept
Definition: juce_AudioDataConverters.h:550
float getAsFloatLE() const noexcept
Definition: juce_AudioDataConverters.h:181
Definition: juce_AudioDataConverters.h:278
void clearSamples(int numSamples) const noexcept
Definition: juce_AudioDataConverters.h:483
void setAsFloatLE(float newValue) noexcept
Definition: juce_AudioDataConverters.h:108
JSAMPIMAGE data
Definition: jpeglib.h:945
Definition: juce_AudioDataConverters.h:245
void skip(int numSamples) noexcept
Definition: juce_AudioDataConverters.h:251
void setAsInt32LE(int32 newValue) noexcept
Definition: juce_AudioDataConverters.h:212
int numInterleavedChannels
Definition: juce_AudioDataConverters.h:304
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:270
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:160
void findMinAndMax(size_t numSamples, float &minValue, float &maxValue) const noexcept
Definition: juce_AudioDataConverters.h:533
Int24in32(void *d) noexcept
Definition: juce_AudioDataConverters.h:228
ValueType getStart() const noexcept
Definition: juce_Range.h:95
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:191
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:264
int32 getAsInt32BE() const noexcept
Definition: juce_AudioDataConverters.h:111
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:140
Pointer & operator++() noexcept
Definition: juce_AudioDataConverters.h:430
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:239
void clearMultiple(int num) noexcept
Definition: juce_AudioDataConverters.h:190
unsigned char uint8
Definition: juce_MathsFunctions.h:43
void copyFromLE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:238
void advanceData(SampleFormatType &s) noexcept
Definition: juce_AudioDataConverters.h:300
void copyFromBE(SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:167
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:263
uint32 * data
Definition: juce_AudioDataConverters.h:220
int32 getAsInt32LE() const noexcept
Definition: juce_AudioDataConverters.h:210
signed int int32
Definition: juce_MathsFunctions.h:49
char * data
Definition: juce_AudioDataConverters.h:195
Interleaved(const int numInterleavedChans) noexcept
Definition: juce_AudioDataConverters.h:298
static void copyFrom(DestType &dest, SourceType &source) noexcept
Definition: juce_AudioDataConverters.h:77
ValueType getEnd() const noexcept
Definition: juce_Range.h:101