OpenShot Audio Library | OpenShotAudio  0.3.1
juce_MemoryMappedAudioFormatReader.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
48 {
49 protected:
50  //==============================================================================
57  MemoryMappedAudioFormatReader (const File& file, const AudioFormatReader& details,
58  int64 dataChunkStart, int64 dataChunkLength, int bytesPerFrame);
59 
60 public:
62  const File& getFile() const noexcept { return file; }
63 
65  bool mapEntireFile();
66 
68  virtual bool mapSectionOfFile (Range<int64> samplesToMap);
69 
71  Range<int64> getMappedSection() const noexcept { return mappedSection; }
72 
74  void touchSample (int64 sample) const noexcept;
75 
80  virtual void getSample (int64 sampleIndex, float* result) const noexcept = 0;
81 
83  size_t getNumBytesUsed() const { return map != nullptr ? map->getSize() : 0; }
84 
85 protected:
86  File file;
87  Range<int64> mappedSection;
88  std::unique_ptr<MemoryMappedFile> map;
89  int64 dataChunkStart, dataLength;
90  int bytesPerFrame;
91 
93  inline int64 sampleToFilePos (int64 sample) const noexcept { return dataChunkStart + sample * bytesPerFrame; }
94 
96  inline int64 filePosToSample (int64 filePos) const noexcept { return (filePos - dataChunkStart) / bytesPerFrame; }
97 
99  inline const void* sampleToPointer (int64 sample) const noexcept { return addBytesToPointer (map->getData(), sampleToFilePos (sample) - map->getRange().getStart()); }
100 
102  template <typename SampleType, typename Endianness>
103  Range<float> scanMinAndMaxInterleaved (int channel, int64 startSampleInFile, int64 numSamples) const noexcept
104  {
106 
107  return SourceType (addBytesToPointer (sampleToPointer (startSampleInFile), ((int) bitsPerSample / 8) * channel), (int) numChannels)
108  .findMinAndMax ((size_t) numSamples);
109  }
110 
111  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedAudioFormatReader)
112 };
113 
114 } // namespace juce
Range< float > findMinAndMax(size_t numSamples) const noexcept
virtual void getSample(int64 sampleIndex, float *result) const noexcept=0
Range< float > scanMinAndMaxInterleaved(int channel, int64 startSampleInFile, int64 numSamples) const noexcept
int64 sampleToFilePos(int64 sample) const noexcept
const void * sampleToPointer(int64 sample) const noexcept
int64 filePosToSample(int64 filePos) const noexcept