OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_AbstractFifo.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 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
23 namespace juce
24{
25
26//==============================================================================
72 class JUCE_API AbstractFifo
73{
74 public:
75 //==============================================================================
77 AbstractFifo (int capacity) noexcept;
78
79 //==============================================================================
81 int getTotalSize() const noexcept;
82
84 int getFreeSpace() const noexcept;
85
87 int getNumReady() const noexcept;
88
90 void reset() noexcept;
91
96 void setTotalSize (int newSize) noexcept;
97
98 //==============================================================================
138 void prepareToWrite (int numToWrite, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
139
143 void finishedWrite (int numWritten) noexcept;
144
183 void prepareToRead (int numWanted, int& startIndex1, int& blockSize1, int& startIndex2, int& blockSize2) const noexcept;
184
188 void finishedRead (int numRead) noexcept;
189
190 //==============================================================================
191
192 private:
193 enum class ReadOrWrite
194 {
195 read,
196 write
197 };
198
199 public:
201 template <ReadOrWrite mode>
202 class ScopedReadWrite final
203 {
204 public:
206 ScopedReadWrite() = default;
207
213 ScopedReadWrite (AbstractFifo& f, int num) noexcept : fifo (&f)
214 {
215 prepare (*fifo, num);
216 }
217
218 ScopedReadWrite (const ScopedReadWrite&) = delete;
219 ScopedReadWrite (ScopedReadWrite&&) noexcept;
220
221 ScopedReadWrite& operator= (const ScopedReadWrite&) = delete;
222 ScopedReadWrite& operator= (ScopedReadWrite&&) noexcept;
223
227 ~ScopedReadWrite() noexcept
228 {
229 if (fifo != nullptr)
230 finish (*fifo, blockSize1 + blockSize2);
231 }
232
236 template <typename FunctionToApply>
237 void forEach (FunctionToApply&& func) const
238 {
239 for (auto i = startIndex1, e = startIndex1 + blockSize1; i != e; ++i) func (i);
240 for (auto i = startIndex2, e = startIndex2 + blockSize2; i != e; ++i) func (i);
241 }
242
243 int startIndex1, blockSize1, startIndex2, blockSize2;
244
245 private:
246 void prepare (AbstractFifo&, int) noexcept;
247 static void finish (AbstractFifo&, int) noexcept;
248 void swap (ScopedReadWrite&) noexcept;
249
250 AbstractFifo* fifo = nullptr;
251 };
252
253 using ScopedRead = ScopedReadWrite<ReadOrWrite::read>;
254 using ScopedWrite = ScopedReadWrite<ReadOrWrite::write>;
255
276 ScopedRead read (int numToRead) noexcept;
277
298 ScopedWrite write (int numToWrite) noexcept;
299
300 private:
301 //==============================================================================
302 int bufferSize;
303 Atomic<int> validStart, validEnd;
304
305 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AbstractFifo)
306};
307
308 template <>
309 inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::finish (AbstractFifo& f, int num) noexcept
310{
311 f.finishedRead (num);
312}
313
314 template <>
315 inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::finish (AbstractFifo& f, int num) noexcept
316{
317 f.finishedWrite (num);
318}
319
320 template <>
321 inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::read>::prepare (AbstractFifo& f, int num) noexcept
322{
323 f.prepareToRead (num, startIndex1, blockSize1, startIndex2, blockSize2);
324}
325
326 template <>
327 inline void AbstractFifo::ScopedReadWrite<AbstractFifo::ReadOrWrite::write>::prepare (AbstractFifo& f, int num) noexcept
328{
329 f.prepareToWrite (num, startIndex1, blockSize1, startIndex2, blockSize2);
330}
331
332
333} // namespace juce
juce::AbstractFifo::ScopedReadWrite
Definition juce_AbstractFifo.h:203
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite(AbstractFifo &f, int num) noexcept
Definition juce_AbstractFifo.h:213
juce::AbstractFifo::ScopedReadWrite::forEach
void forEach(FunctionToApply &&func) const
Definition juce_AbstractFifo.h:237
juce::AbstractFifo::ScopedReadWrite::ScopedReadWrite
ScopedReadWrite()=default
juce::AbstractFifo
Definition juce_AbstractFifo.h:73
juce::Atomic
Definition juce_Atomic.h:42