OpenShot Audio Library | OpenShotAudio 0.4.0
juce_MemoryMappedAudioFormatReader.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
26namespace juce
27{
28
29//==============================================================================
47{
48protected:
49 //==============================================================================
56 MemoryMappedAudioFormatReader (const File& file, const AudioFormatReader& details,
57 int64 dataChunkStart, int64 dataChunkLength, int bytesPerFrame);
58
59public:
61 const File& getFile() const noexcept { return file; }
62
64 bool mapEntireFile();
65
67 virtual bool mapSectionOfFile (Range<int64> samplesToMap);
68
70 Range<int64> getMappedSection() const noexcept { return mappedSection; }
71
73 void touchSample (int64 sample) const noexcept;
74
79 virtual void getSample (int64 sampleIndex, float* result) const noexcept = 0;
80
82 size_t getNumBytesUsed() const { return map != nullptr ? map->getSize() : 0; }
83
84protected:
85 File file;
86 Range<int64> mappedSection;
87 std::unique_ptr<MemoryMappedFile> map;
88 int64 dataChunkStart, dataLength;
89 int bytesPerFrame;
90
92 inline int64 sampleToFilePos (int64 sample) const noexcept { return dataChunkStart + sample * bytesPerFrame; }
93
95 inline int64 filePosToSample (int64 filePos) const noexcept { return (filePos - dataChunkStart) / bytesPerFrame; }
96
98 inline const void* sampleToPointer (int64 sample) const noexcept { return addBytesToPointer (map->getData(), sampleToFilePos (sample) - map->getRange().getStart()); }
99
101 template <typename SampleType, typename Endianness>
102 Range<float> scanMinAndMaxInterleaved (int channel, int64 startSampleInFile, int64 numSamples) const noexcept
103 {
105
106 return SourceType (addBytesToPointer (sampleToPointer (startSampleInFile), ((int) bitsPerSample / 8) * channel), (int) numChannels)
107 .findMinAndMax ((size_t) numSamples);
108 }
109
110 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedAudioFormatReader)
111};
112
113} // namespace juce
Range< float > findMinAndMax(size_t numSamples) const noexcept
virtual void getSample(int64 sampleIndex, float *result) const noexcept=0
const void * sampleToPointer(int64 sample) const noexcept
int64 sampleToFilePos(int64 sample) const noexcept
Range< float > scanMinAndMaxInterleaved(int channel, int64 startSampleInFile, int64 numSamples) const noexcept
int64 filePosToSample(int64 filePos) const noexcept