tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_BufferedFileReader.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
14//==============================================================================
24{
25public:
38 juce::TimeSliceThread& timeSliceThread,
39 int samplesToBuffer);
40
42 ~BufferedFileReader() override;
43
50 void setReadTimeout (int timeoutMilliseconds) noexcept;
51
55 bool isFullyBuffered() const;
56
57 //==============================================================================
59 bool readSamples (int* const* destSamples, int numDestChannels, int startOffsetInDestBuffer,
60 juce::int64 startSampleInFile, int numSamples) override;
61
62private:
63 struct BufferedBlock
64 {
65 BufferedBlock (juce::AudioFormatReader&);
66
67 void update (juce::AudioFormatReader&, juce::Range<juce::int64> range, size_t slotIndex);
68
71 bool allSamplesRead = false;
72 std::atomic<juce::uint32> lastUseTime { 0 };
73 std::atomic<int> slotIndex { -1 };
74 };
75
76 struct ScopedSlotAccess
77 {
78 ScopedSlotAccess (BufferedFileReader&, size_t slotIndex);
79 ~ScopedSlotAccess();
80
81 static ScopedSlotAccess fromPosition (BufferedFileReader&, juce::int64 position);
82
83 BufferedBlock* getBlock() const { return block; }
84 void setBlock (BufferedBlock*);
85
86 private:
87 BufferedFileReader& reader;
88 const size_t slotIndex = 0;
89 BufferedBlock* block = nullptr;
90 };
91
92 int useTimeSlice() override;
93
94 enum class PositionStatus
95 {
96 positionChangedByAudioThread,
97 nextChunkScheduled,
98 blocksFull,
99 fullyLoaded
100 };
101
102 PositionStatus readNextBufferChunk();
103
104 static constexpr int samplesPerBlock = 32768;
105
107 juce::TimeSliceThread& thread;
108 std::atomic<juce::int64> nextReadPosition { 0 };
109 int timeoutMs = 0;
110
114
115 size_t numBlocksToBuffer = 0;
116 std::atomic<size_t> numBlocksBuffered { 0 }, nextSlotScheduled { 0 };
117 const bool isFullyBuffering = false;
118
119 size_t getSlotIndexFromSamplePosition (juce::int64 samplePos);
120 juce::Range<juce::int64> getSlotRange (size_t slotIndex);
121 void markSlotUseState (size_t slotIndex, bool isInUse);
122
124};
125
126}} // namespace tracktion { inline namespace engine
bool isFullyBuffered() const
Returns true if this has been initialised to buffer the whole file once that is complete,...
void setReadTimeout(int timeoutMilliseconds) noexcept
Sets a number of milliseconds that the reader can block for in its readSamples() method before giving...
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
long long int64