OpenShot Audio Library | OpenShotAudio  0.6.0
juce_SingleThreadedAbstractFifo.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 //==============================================================================
64 {
65 public:
68 
70  explicit SingleThreadedAbstractFifo (int sizeIn)
71  : size (sizeIn)
72  {
73  // This class only works properly when the size is a power of two.
74  // Use nextPowerOfTwo() to find a good size, and ensure that your
75  // backing storage is the same size.
76  jassert (isPowerOfTwo (sizeIn));
77  }
78 
80  int getRemainingSpace() const { return size - numReadable; }
81 
83  int getNumReadable() const { return numReadable; }
84 
86  int getSize() const { return size; }
87 
93  std::array<Range<int>, 2> write (int num)
94  {
95  const auto startPos = (readPos + numReadable) & (size - 1);
96  const auto maxToWrite = jmin (getRemainingSpace(), num);
97  const auto firstBlockSize = jmin (maxToWrite, size - startPos);
98 
99  numReadable += maxToWrite;
100 
101  return { { { startPos, startPos + firstBlockSize }, { 0, maxToWrite - firstBlockSize } } };
102  }
103 
109  std::array<Range<int>, 2> read (int num)
110  {
111  const auto startPos = readPos;
112  const auto maxToRead = jmin (numReadable, num);
113  const auto firstBlockSize = jmin (maxToRead, size - startPos);
114 
115  readPos = (startPos + maxToRead) & (size - 1);
116  numReadable -= maxToRead;
117 
118  return { { { startPos, startPos + firstBlockSize }, { 0, maxToRead - firstBlockSize } } };
119  }
120 
121 private:
122  int size = 0, readPos = 0, numReadable = 0;
123 };
124 
125 
126 } // namespace juce
std::array< Range< int >, 2 > write(int num)
std::array< Range< int >, 2 > read(int num)