OpenShot Library | libopenshot-audio 0.2.0
juce_MemoryMappedAudioFormatReader.h
1
2/** @weakgroup juce_audio_formats-format
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
15 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
16 27th April 2017).
17
18 End User License Agreement: www.juce.com/juce-5-licence
19 Privacy Policy: www.juce.com/juce-5-privacy-policy
20
21 Or: You may also use this code under the terms of the GPL v3 (see
22 www.gnu.org/licenses).
23
24 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
25 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
26 DISCLAIMED.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33
34//==============================================================================
35/**
36 A specialised type of AudioFormatReader that uses a MemoryMappedFile to read
37 directly from an audio file.
38
39 This allows for incredibly fast random-access to sample data in the mapped
40 region of the file, but not all audio formats support it - see
41 AudioFormat::createMemoryMappedReader().
42
43 Note that before reading samples from a MemoryMappedAudioFormatReader, you must first
44 call mapEntireFile() or mapSectionOfFile() to ensure that the region you want to
45 read has been mapped.
46
47 @see AudioFormat::createMemoryMappedReader, AudioFormatReader
48
49 @tags{Audio}
50*/
52{
53protected:
54 //==============================================================================
55 /** Creates an MemoryMappedAudioFormatReader object.
56
57 Note that before attempting to read any data, you must call mapEntireFile()
58 or mapSectionOfFile() to ensure that the region you want to read has
59 been mapped.
60 */
62 int64 dataChunkStart, int64 dataChunkLength, int bytesPerFrame);
63
64public:
65 /** Returns the file that is being mapped */
66 const File& getFile() const noexcept { return file; }
67
68 /** Attempts to map the entire file into memory. */
69 bool mapEntireFile();
70
71 /** Attempts to map a section of the file into memory. */
72 virtual bool mapSectionOfFile (Range<int64> samplesToMap);
73
74 /** Returns the sample range that's currently memory-mapped and available for reading. */
75 Range<int64> getMappedSection() const noexcept { return mappedSection; }
76
77 /** Touches the memory for the given sample, to force it to be loaded into active memory. */
78 void touchSample (int64 sample) const noexcept;
79
80 /** Returns the samples for all channels at a given sample position.
81 The result array must be large enough to hold a value for each channel
82 that this reader contains.
83 */
84 virtual void getSample (int64 sampleIndex, float* result) const noexcept = 0;
85
86 /** Returns the number of bytes currently being mapped */
87 size_t getNumBytesUsed() const { return map != nullptr ? map->getSize() : 0; }
88
89protected:
90 File file;
91 Range<int64> mappedSection;
92 std::unique_ptr<MemoryMappedFile> map;
93 int64 dataChunkStart, dataLength;
94 int bytesPerFrame;
95
96 /** Converts a sample index to a byte position in the file. */
97 inline int64 sampleToFilePos (int64 sample) const noexcept { return dataChunkStart + sample * bytesPerFrame; }
98
99 /** Converts a byte position in the file to a sample index. */
100 inline int64 filePosToSample (int64 filePos) const noexcept { return (filePos - dataChunkStart) / bytesPerFrame; }
101
102 /** Converts a sample index to a pointer to the mapped file memory. */
103 inline const void* sampleToPointer (int64 sample) const noexcept { return addBytesToPointer (map->getData(), sampleToFilePos (sample) - map->getRange().getStart()); }
104
105 /** Used by AudioFormatReader subclasses to scan for min/max ranges in interleaved data. */
106 template <typename SampleType, typename Endianness>
107 Range<float> scanMinAndMaxInterleaved (int channel, int64 startSampleInFile, int64 numSamples) const noexcept
108 {
109 using SourceType = AudioData::Pointer <SampleType, Endianness, AudioData::Interleaved, AudioData::Const>;
110
111 return SourceType (addBytesToPointer (sampleToPointer (startSampleInFile), ((int) bitsPerSample / 8) * channel), (int) numChannels)
112 .findMinAndMax ((size_t) numSamples);
113 }
114
115 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedAudioFormatReader)
116};
117
118} // namespace juce
119
120/** @}*/
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
Reads samples from an audio file stream.
Represents a local file or directory.
Definition juce_File.h:45
A specialised type of AudioFormatReader that uses a MemoryMappedFile to read directly from an audio f...
const File & getFile() const noexcept
Returns the file that is being mapped.
virtual void getSample(int64 sampleIndex, float *result) const noexcept=0
Returns the samples for all channels at a given sample position.
Range< int64 > getMappedSection() const noexcept
Returns the sample range that's currently memory-mapped and available for reading.
const void * sampleToPointer(int64 sample) const noexcept
Converts a sample index to a pointer to the mapped file memory.
size_t getNumBytesUsed() const
Returns the number of bytes currently being mapped.
int64 sampleToFilePos(int64 sample) const noexcept
Converts a sample index to a byte position in the file.
Range< float > scanMinAndMaxInterleaved(int channel, int64 startSampleInFile, int64 numSamples) const noexcept
Used by AudioFormatReader subclasses to scan for min/max ranges in interleaved data.
int64 filePosToSample(int64 filePos) const noexcept
Converts a byte position in the file to a sample index.
#define JUCE_API
This macro is added to all JUCE public class declarations.