2010-01-23 19:44:49 +00:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
Audacity: A Digital Audio Editor
|
|
|
|
|
|
|
|
RingBuffer.h
|
|
|
|
|
|
|
|
Dominic Mazzoni
|
|
|
|
|
|
|
|
*******************************************************************/
|
|
|
|
|
|
|
|
#ifndef __AUDACITY_RING_BUFFER__
|
|
|
|
#define __AUDACITY_RING_BUFFER__
|
|
|
|
|
|
|
|
#include "SampleFormat.h"
|
2018-06-19 17:23:54 +00:00
|
|
|
#include <atomic>
|
2010-01-23 19:44:49 +00:00
|
|
|
|
2021-02-02 16:44:00 +00:00
|
|
|
class RingBuffer final : public NonInterferingBase {
|
2010-01-23 19:44:49 +00:00
|
|
|
public:
|
2016-09-04 21:07:53 +00:00
|
|
|
RingBuffer(sampleFormat format, size_t size);
|
2010-01-23 19:44:49 +00:00
|
|
|
~RingBuffer();
|
|
|
|
|
|
|
|
//
|
|
|
|
// For the writer only:
|
|
|
|
//
|
|
|
|
|
2016-09-04 21:07:53 +00:00
|
|
|
size_t AvailForPut();
|
2020-12-01 00:58:00 +00:00
|
|
|
//! Does not apply dithering
|
2018-08-13 22:05:11 +00:00
|
|
|
size_t Put(samplePtr buffer, sampleFormat format, size_t samples,
|
|
|
|
// optional number of trailing zeroes
|
|
|
|
size_t padding = 0);
|
2018-06-13 23:07:39 +00:00
|
|
|
size_t Clear(sampleFormat format, size_t samples);
|
2010-01-23 19:44:49 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// For the reader only:
|
|
|
|
//
|
|
|
|
|
2016-09-04 21:07:53 +00:00
|
|
|
size_t AvailForGet();
|
2020-12-01 00:58:00 +00:00
|
|
|
//! Does not apply dithering
|
2016-09-04 21:07:53 +00:00
|
|
|
size_t Get(samplePtr buffer, sampleFormat format, size_t samples);
|
|
|
|
size_t Discard(size_t samples);
|
2010-01-23 19:44:49 +00:00
|
|
|
|
|
|
|
private:
|
2018-06-19 17:23:54 +00:00
|
|
|
size_t Filled( size_t start, size_t end );
|
|
|
|
size_t Free( size_t start, size_t end );
|
2014-06-03 20:30:19 +00:00
|
|
|
|
2018-06-21 16:09:32 +00:00
|
|
|
// Align the two atomics to avoid false sharing
|
2021-02-02 16:44:00 +00:00
|
|
|
NonInterfering< std::atomic<size_t> > mStart { 0 }, mEnd{ 0 };
|
2018-06-21 16:09:32 +00:00
|
|
|
|
2018-06-19 17:23:54 +00:00
|
|
|
const size_t mBufferSize;
|
2018-06-21 16:09:32 +00:00
|
|
|
|
|
|
|
sampleFormat mFormat;
|
2016-02-01 15:16:00 +00:00
|
|
|
SampleBuffer mBuffer;
|
2010-01-23 19:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* __AUDACITY_RING_BUFFER__ */
|