Audacity 3.2.0
RingBuffer.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 RingBuffer.h
6
7 Dominic Mazzoni
8
9*******************************************************************/
10
11#ifndef __AUDACITY_RING_BUFFER__
12#define __AUDACITY_RING_BUFFER__
13
14#include "SampleFormat.h"
15#include <atomic>
16
17class RingBuffer final : public NonInterferingBase {
18 public:
21
22 //
23 // For the writer only:
24 //
25
26 size_t AvailForPut() const;
28 size_t WrittenForGet() const;
30 size_t Put(constSamplePtr buffer, sampleFormat format, size_t samples,
31 // optional number of trailing zeroes
32 size_t padding = 0);
34
37 size_t Unput(size_t size);
38 size_t Clear(sampleFormat format, size_t samples);
41 std::pair<samplePtr, size_t> GetUnflushed(unsigned iBlock);
43 void Flush();
44
45 //
46 // For the reader only:
47 //
48
49 size_t AvailForGet() const;
51 size_t Get(samplePtr buffer, sampleFormat format, size_t samples);
52 size_t Discard(size_t samples);
53
54 private:
55 size_t Filled(size_t start, size_t end) const;
56 size_t Free(size_t start, size_t end) const;
57
58 size_t mWritten{0};
59 size_t mLastPadding{0};
60
61 // Align the two atomics to avoid false sharing
63
64 const size_t mBufferSize;
65
68};
69
70#endif /* __AUDACITY_RING_BUFFER__ */
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
char * samplePtr
Definition: SampleFormat.h:57
const char * constSamplePtr
Definition: SampleFormat.h:58
Holds streamed audio samples.
Definition: RingBuffer.h:17
size_t WrittenForGet() const
Reader may concurrently cause a decrease of what this returns.
Definition: RingBuffer.cpp:73
size_t AvailForPut() const
Definition: RingBuffer.cpp:64
size_t Unput(size_t size)
Remove an initial segment of data that has been Put but not Flushed yet.
Definition: RingBuffer.cpp:117
size_t mLastPadding
Definition: RingBuffer.h:59
size_t Put(constSamplePtr buffer, sampleFormat format, size_t samples, size_t padding=0)
Does not apply dithering.
Definition: RingBuffer.cpp:79
size_t Free(size_t start, size_t end) const
Definition: RingBuffer.cpp:51
const size_t mBufferSize
Definition: RingBuffer.h:64
std::pair< samplePtr, size_t > GetUnflushed(unsigned iBlock)
Definition: RingBuffer.cpp:187
NonInterfering< std::atomic< size_t > > mEnd
Definition: RingBuffer.h:62
size_t Filled(size_t start, size_t end) const
Definition: RingBuffer.cpp:46
void Flush()
Flush after a sequence of Put (and/or Clear) calls to let consumer see.
Definition: RingBuffer.cpp:210
size_t Clear(sampleFormat format, size_t samples)
Definition: RingBuffer.cpp:164
size_t Get(samplePtr buffer, sampleFormat format, size_t samples)
Does not apply dithering.
Definition: RingBuffer.cpp:236
const sampleFormat mFormat
Definition: RingBuffer.h:66
size_t mWritten
Definition: RingBuffer.h:58
NonInterfering< std::atomic< size_t > > mStart
Definition: RingBuffer.h:62
const SampleBuffer mBuffer
Definition: RingBuffer.h:67
size_t Discard(size_t samples)
Definition: RingBuffer.cpp:267
RingBuffer(sampleFormat format, size_t size)
Definition: RingBuffer.cpp:32
size_t AvailForGet() const
Definition: RingBuffer.cpp:226
const char * end(const char *str) noexcept
Definition: StringUtils.h:106