OpenShot Audio Library | OpenShotAudio  0.6.0
juce_AudioFormatReader.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11  Agreement and JUCE Privacy Policy.
12 
13  End User License Agreement: www.juce.com/juce-7-licence
14  Privacy Policy: www.juce.com/juce-privacy-policy
15 
16  Or: You may also use this code under the terms of the GPL v3 (see
17  www.gnu.org/licenses).
18 
19  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21  DISCLAIMED.
22 
23  ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 class AudioFormat;
30 
31 
32 //==============================================================================
43 class JUCE_API AudioFormatReader
44 {
45 protected:
46  //==============================================================================
56  AudioFormatReader (InputStream* sourceStream,
57  const String& formatName);
58 
59 public:
61  virtual ~AudioFormatReader();
62 
63  //==============================================================================
68  const String& getFormatName() const noexcept { return formatName; }
69 
70  //==============================================================================
89  bool read (float* const* destChannels, int numDestChannels,
90  int64 startSampleInSource, int numSamplesToRead);
91 
130  bool read (int* const* destChannels,
131  int numDestChannels,
132  int64 startSampleInSource,
133  int numSamplesToRead,
134  bool fillLeftoverChannelsWithCopies);
135 
147  bool read (AudioBuffer<float>* buffer,
148  int startSampleInDestBuffer,
149  int numSamples,
150  int64 readerStartSample,
151  bool useReaderLeftChan,
152  bool useReaderRightChan);
153 
170  virtual void readMaxLevels (int64 startSample, int64 numSamples,
171  Range<float>* results, int numChannelsToRead);
172 
190  virtual void readMaxLevels (int64 startSample, int64 numSamples,
191  float& lowestLeft, float& highestLeft,
192  float& lowestRight, float& highestRight);
193 
216  int64 searchForLevel (int64 startSample,
217  int64 numSamplesToSearch,
218  double magnitudeRangeMinimum,
219  double magnitudeRangeMaximum,
220  int minimumConsecutiveSamples);
221 
222 
223  //==============================================================================
225  double sampleRate = 0;
226 
228  unsigned int bitsPerSample = 0;
229 
231  int64 lengthInSamples = 0;
232 
234  unsigned int numChannels = 0;
235 
237  bool usesFloatingPointData = false;
238 
246 
249 
250  //==============================================================================
252  virtual AudioChannelSet getChannelLayout();
253 
254  //==============================================================================
270  virtual bool readSamples (int* const* destChannels,
271  int numDestChannels,
272  int startOffsetInDestBuffer,
273  int64 startSampleInFile,
274  int numSamples) = 0;
275 
276 
277 protected:
278  //==============================================================================
280  template <class DestSampleType, class SourceSampleType, class SourceEndianness>
281  struct ReadHelper
282  {
285 
286  template <typename TargetType>
287  static void read (TargetType* const* destData, int destOffset, int numDestChannels,
288  const void* sourceData, int numSourceChannels, int numSamples) noexcept
289  {
290  for (int i = 0; i < numDestChannels; ++i)
291  {
292  if (void* targetChan = destData[i])
293  {
294  DestType dest (targetChan);
295  dest += destOffset;
296 
297  if (i < numSourceChannels)
298  dest.convertSamples (SourceType (addBytesToPointer (sourceData, i * SourceType::getBytesPerSample()), numSourceChannels), numSamples);
299  else
300  dest.clearSamples (numSamples);
301  }
302  }
303  }
304  };
305 
309  static void clearSamplesBeyondAvailableLength (int* const* destChannels, int numDestChannels,
310  int startOffsetInDestBuffer, int64 startSampleInFile,
311  int& numSamples, int64 fileLengthInSamples)
312  {
313  if (destChannels == nullptr)
314  {
315  jassertfalse;
316  return;
317  }
318 
319  const int64 samplesAvailable = fileLengthInSamples - startSampleInFile;
320 
321  if (samplesAvailable < numSamples)
322  {
323  for (int i = numDestChannels; --i >= 0;)
324  if (destChannels[i] != nullptr)
325  zeromem (destChannels[i] + startOffsetInDestBuffer, (size_t) numSamples * sizeof (int));
326 
327  numSamples = (int) samplesAvailable;
328  }
329  }
330 
331 private:
332  String formatName;
333 
334  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFormatReader)
335 };
336 
337 } // namespace juce
void clearSamples(int numSamples) const noexcept
void convertSamples(Pointer source, int numSamples) const noexcept
const String & getFormatName() const noexcept
static void clearSamplesBeyondAvailableLength(int *const *destChannels, int numDestChannels, int startOffsetInDestBuffer, int64 startSampleInFile, int &numSamples, int64 fileLengthInSamples)
virtual bool readSamples(int *const *destChannels, int numDestChannels, int startOffsetInDestBuffer, int64 startSampleInFile, int numSamples)=0