SDRPlusPlus/core/src/dsp/buffer.h
2021-06-17 20:14:23 +02:00

292 lines
8.7 KiB
C++

#pragma once
#include <dsp/block.h>
#include <string.h>
#define RING_BUF_SZ 1000000
namespace dsp {
template <class T>
class RingBuffer {
public:
RingBuffer() {
}
RingBuffer(int maxLatency) { init(maxLatency); }
~RingBuffer() { delete _buffer; }
void init(int maxLatency) {
size = RING_BUF_SZ;
_buffer = new T[size];
_stopReader = false;
_stopWriter = false;
this->maxLatency = maxLatency;
writec = 0;
readc = 0;
readable = 0;
writable = size;
memset(_buffer, 0, size * sizeof(T));
}
int read(T* data, int len) {
int dataRead = 0;
int toRead = 0;
while (dataRead < len) {
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
}
else {
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
}
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int readAndSkip(T* data, int len, int skip) {
int dataRead = 0;
int toRead = 0;
while (dataRead < len) {
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
}
else {
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
}
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
dataRead = 0;
while (dataRead < skip) {
toRead = std::min<int>(waitUntilReadable(), skip - dataRead);
if (toRead < 0) { return -1; };
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int waitUntilReadable() {
if (_stopReader) { return -1; }
int _r = getReadable();
if (_r != 0) { return _r; }
std::unique_lock<std::mutex> lck(_readable_mtx);
canReadVar.wait(lck, [=](){ return ((this->getReadable(false) > 0) || this->getReadStop()); });
if (_stopReader) { return -1; }
return getReadable(false);
}
int getReadable(bool lock = true) {
if (lock) { _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return _r;
}
int write(T* data, int len) {
int dataWritten = 0;
int toWrite = 0;
while (dataWritten < len) {
toWrite = std::min<int>(waitUntilwritable(), len - dataWritten);
if (toWrite < 0) { return -1; };
if ((toWrite + writec) > size) {
memcpy(&_buffer[writec], &data[dataWritten], (size - writec) * sizeof(T));
memcpy(&_buffer[0], &data[dataWritten + (size - writec)], (toWrite - (size - writec)) * sizeof(T));
}
else {
memcpy(&_buffer[writec], &data[dataWritten], toWrite * sizeof(T));
}
dataWritten += toWrite;
_readable_mtx.lock();
readable += toWrite;
_readable_mtx.unlock();
_writable_mtx.lock();
writable -= toWrite;
_writable_mtx.unlock();
writec = (writec + toWrite) % size;
canReadVar.notify_one();
}
return len;
}
int waitUntilwritable() {
if (_stopWriter) { return -1; }
int _w = getWritable();
if (_w != 0) { return _w; }
std::unique_lock<std::mutex> lck(_writable_mtx);
canWriteVar.wait(lck, [=](){ return ((this->getWritable(false) > 0) || this->getWriteStop()); });
if (_stopWriter) { return -1; }
return getWritable(false);
}
int getWritable(bool lock = true) {
if (lock) { _writable_mtx.lock(); };
int _w = writable;
if (lock) { _writable_mtx.unlock(); _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return std::max<int>(std::min<int>(_w, maxLatency - _r), 0);
}
void stopReader() {
_stopReader = true;
canReadVar.notify_one();
}
void stopWriter() {
_stopWriter = true;
canWriteVar.notify_one();
}
bool getReadStop() {
return _stopReader;
}
bool getWriteStop() {
return _stopWriter;
}
void clearReadStop() {
_stopReader = false;
}
void clearWriteStop() {
_stopWriter = false;
}
void setMaxLatency(int maxLatency) {
this->maxLatency = maxLatency;
}
private:
T* _buffer;
int size;
int readc;
int writec;
int readable;
int writable;
int maxLatency;
bool _stopReader;
bool _stopWriter;
std::mutex _readable_mtx;
std::mutex _writable_mtx;
std::condition_variable canReadVar;
std::condition_variable canWriteVar;
};
template <class T>
class BufferBlock : public generic_block<BufferBlock<T>> {
public:
BufferBlock() {}
BufferBlock(stream<T>* in, int bufferSize) { init(in, bufferSize); }
~BufferBlock(stream<T>* in, int bufferSize) {
generic_block<BufferBlock<T>>::stop();
delete[] buffer;
}
void init(stream<T>* in, int bufferSize) {
_in = in;
_bufferSize = bufferSize;
buffer = new T[_bufferSize];
generic_block<BufferBlock<T>>::registerInput(_in);
}
void setInput(stream<T>* in) {
std::lock_guard<std::mutex> lck(generic_block<BufferBlock<T>>::ctrlMtx);
generic_block<BufferBlock<T>>::tempStop();
generic_block<BufferBlock<T>>::unregisterInput(_in);
_in = in;
generic_block<BufferBlock<T>>::registerInput(_in);
generic_block<BufferBlock<T>>::tempStart();
}
int run() {
int count = _in->read();
if (count < 0) { return -1; }
// If there's enough space in the buffer, write data. Otherwise, discard
{
std::lock_guard<std::mutex> lck(bufferMtx);
if (dataInBuffer + count <= _bufferSize) {
memcpy(&buffer[dataInBuffer], _in->readBuf, count);
dataInBuffer += count;
}
}
// Notify reader that data is available
cnd.notify_all();
_in->flush();
return count;
}
void readWorker() {
}
private:
void doStart() {
}
void doStop() {
}
stream<T>* _in;
int _bufferSize;
T* buffer;
int dataInBuffer = 0;
std::mutex bufferMtx;
std::condition_variable cnd;
bool stopReaderThread = false;
std::thread readerThread;
};
};