OpenShot Audio Library | OpenShotAudio: juce_AbstractFifo.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
juce_AbstractFifo.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 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
78 class JUCE_API AbstractFifo
79{
80public:
81 //==============================================================================
83 AbstractFifo (int capacity) noexcept;
84
86 ~AbstractFifo();
87
88 //==============================================================================
90 int getTotalSize() const noexcept;
91
93 int getFreeSpace() const noexcept;
94
96 int getNumReady() const noexcept;
97
99 void reset() noexcept;
100
105 void setTotalSize (int newSize) noexcept;
106
107 //==============================================================================
147 void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
148
152 void finishedWrite (int numWritten) noexcept;
153
192 void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
193
197 void finishedRead (int numRead) noexcept;
198
199 //==============================================================================
200
201private:
202 enum class ReadOrWrite
203 {
204 read,
205 write
206 };
207
208public:
210 template <ReadOrWrite mode>
211 class ScopedReadWrite final
212 {
213 public:
215 ScopedReadWrite() = default;
216
222 ScopedReadWrite (AbstractFifo& f, int num) noexcept : fifo (&f)
223 {
224 prepare (*fifo, num);
225 }
226
227 ScopedReadWrite (const ScopedReadWrite&) = delete;
228 ScopedReadWrite (ScopedReadWrite&&) noexcept;
229
230 ScopedReadWrite& operator= (const ScopedReadWrite&) = delete;
231 ScopedReadWrite& operator= (ScopedReadWrite&&) noexcept;
232
236 ~ScopedReadWrite() noexcept
237 {
238 if (fifo != nullptr)
239 finish (*fifo, blockSize1 + blockSize2);
240 }
241
245 template <typename FunctionToApply>
246 void forEach (FunctionToApply&& func) const
247 {
248 for (auto i = startIndex1, e = startIndex1 + blockSize1; i != e; ++i) func (i);
249 for (auto i = startIndex2, e = startIndex2 + blockSize2; i != e; ++i) func (i);
250 }
251
252 int startIndex1, blockSize1, startIndex2, blockSize2;
253
254 private:
255 void prepare (AbstractFifo&, int) noexcept;
256 static void finish (AbstractFifo&, int) noexcept;
257 void swap (ScopedReadWrite&) noexcept;
258
259 AbstractFifo* fifo = nullptr;
260 };
261
262 using ScopedRead = ScopedReadWrite<ReadOrWrite::read>;
263 using ScopedWrite = ScopedReadWrite<ReadOrWrite::write>;
264
285 ScopedRead read (int numToRead) noexcept;
286
307 ScopedWrite write (int numToWrite) noexcept;
308
309private:
310 //==============================================================================
311 int bufferSize;
312 Atomic<int> validStart, validEnd;
313
314 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
315};
316
317template<>
318inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::finish (AbstractFifo& f, int num) noexcept
319{
320 f.finishedRead (num);
321}
322
323template<>
324inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::finish (AbstractFifo& f, int num) noexcept
325{
326 f.finishedWrite (num);
327}
328
329template<>
330inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::prepare (AbstractFifo& f, int num) noexcept
331{
332 f.prepareToRead (num, startIndex1, blockSize1, startIndex2, blockSize2);
333}
334
335template<>
336inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::prepare (AbstractFifo& f, int num) noexcept
337{
338 f.prepareToWrite (num, startIndex1, blockSize1, startIndex2, blockSize2);
339}
340
341
342} // namespace juce
juce::AbstractFifo::ScopedReadWrite
Definition: juce_AbstractFifo.h:212
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite(AbstractFifo &f, int num) noexcept
Definition: juce_AbstractFifo.h:222
juce::AbstractFifo::ScopedReadWrite::forEach
void forEach(FunctionToApply &&func) const
Definition: juce_AbstractFifo.h:246
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite()=default
juce::AbstractFifo
Definition: juce_AbstractFifo.h:79
juce::Atomic
Definition: juce_Atomic.h:42